14.11.2012 Views

Curry: An Integrated Functional Logic Language

Curry: An Integrated Functional Logic Language

Curry: An Integrated Functional Logic Language

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Intuitively, a function f declared as a choice function behaves as follows. A call to f is evaluated<br />

as usual (with a fair evaluation of disjunctive alternatives, which is important here!) but with the<br />

following differences:<br />

1. No free variable of this function call is bound (i.e., this function call and all its subcalls are<br />

considered as rigid).<br />

2. If one rule for f matches and its guard is entailed (i.e., satisfiable without binding goal<br />

variables), all other alternatives for evaluating this call are ignored.<br />

This means that the first applicable rule for this function call (after pattern matching and guard<br />

checking) is taken and all other alternatives are discarded.<br />

As an example, consider the non-deterministic function merge for the fair merge of two lists:<br />

merge :: [a] -> [a] -> [a]<br />

merge eval choice<br />

merge [] l2 = l2<br />

merge l1 [] = l1<br />

merge (e:r) l2 = e : merge r l2<br />

merge l1 (e:r) = e : merge l1 r<br />

Due to the choice annotation, a call to merge is reducible if one of the input lists is known to be<br />

empty or non-empty (but it is not reducible of both inputs are unknown). Thus, merge can also<br />

merge partially unknown lists which are incrementally instantiated during computation.<br />

<strong>An</strong>other application of the choice annotation is a fair evaluation of disjunctive search goals.<br />

For instance, the following function takes a search goal and computes one result (if it exists) for<br />

the search variable in a fair manner, i.e., possible infinite computation branches in a disjunction do<br />

not inhibit the computation of a solution in another branch:<br />

tryone :: (a->Success) -> a<br />

tryone eval choice<br />

tryone g | g x = x where x free<br />

Note that functions containing choice expressions may be indeterministic in contrast to other userdefined<br />

functions, i.e., identical calls may lead to different answers at different times. This is similar<br />

to non-deterministic functions which can be given a declarative semantics [18]. The difference to<br />

non-deterministic functions is that only one result is computed for functions declared with choice<br />

instead of all results. As a consequence, the completeness result for <strong>Curry</strong>’s operational semantics<br />

[23] does not hold in the presence of the evaluation annotation choice. Since indeterministic functions<br />

are mainly used in the coordination level of concurrent applications (as in parallel functional<br />

computation models [11, 12]), this is a only minor problem. A programming environment for <strong>Curry</strong><br />

could classify the indeterministic functions in a program.<br />

9 Interface to External Functions and Constraint Solvers<br />

Since <strong>Curry</strong> has only a small number of builtins, it provides a simple interface to connect external<br />

functions (functions programmed in another language) and external constraint solvers. External<br />

34

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!