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.

Eval [try(g)] ⇒<br />

⎧<br />

[] if Eval [c] ⇒ ∅<br />

⎪⎨<br />

⎪⎩<br />

[g ′ ] if Eval [c] ⇒ {σ success} (i.e., σ is a mgu for all equations in c) with<br />

Dom(σ) ⊆ {x, x1, . . . , xn} (or c = success and σ = id) and<br />

g ′ = \x->let x1, . . . , xn free in σ ◦ ϕ success<br />

D if Eval [c] ⇒ {σ c ′ } with Dom(σ) ⊆ {x, x1, . . . , xn},<br />

g ′ = \x->let x1, . . . , xn, y1, . . . , ym free in σ ◦ ϕ c ′<br />

where {y1, . . . , ym} = VRan(σ) \ ({x, x1, . . . , xn} ∪ free(g)),<br />

and Eval [try(g ′ )] ⇒ D<br />

[g1,...,gk] if Eval [c] ⇒ {σ1 c1, . . . , σk ck}, k > 1, and, for i = 1, . . . , k,<br />

Dom(σi) ⊆ {x, x1, . . . , xn} and<br />

gi = \x->let x1, . . . , xn, y1, . . . , ymi free in σi ◦ ϕ ci<br />

where {y1, . . . , ymi } = VRan(σi) \ ({x, x1, . . . , xn} ∪ free(g))<br />

Figure 5: Operational semantics of the try operator for g = \x->let x1, . . . , xn free in ϕ c<br />

It is easy to see that this algorithm computes a definitional tree for each function since the<br />

number of rules is reduced in each recursive call and it keeps the invariant that the left-hand sides<br />

of the current set of rules are always instances of the current call pattern.<br />

Note that the algorithm gt is not strictly conform with the pattern matching strategy of functional<br />

languages like Haskell or Miranda, since it generates for the rules<br />

f 0 0 = 0<br />

f x 1 = 0<br />

the definitional tree<br />

branch(f(x1,x2), 2, flex, branch(f(x1,0), 1, flex, rule(f(0,0) = 0)),<br />

rule(f(x1,1) = 0))<br />

(although both arguments are demanded, only the second argument is at an inductive position)<br />

whereas Haskell or Miranda have a strict left-to-right pattern matching strategy which could be<br />

expressed by the definitionl tree<br />

or(branch(f(x1,x2), 1, flex, branch(f(0,x2), 2, flex, rule(f(0,0) = 0))),<br />

branch(f(x1,x2), 2, flex, rule(f(x1,1) = 0)))<br />

The latter tree is not optimal since it has a non-deterministic or node and always requires the<br />

evaluation of the first argument (in the first alternative). If the function definitions are uniform in<br />

the sense of [48], the strategy described by gt is identical to traditional functional languages.<br />

D.7 Encapsulated Search<br />

The exact behavior of the try operator is specified in Figure 5. Note that a substitution ϕ of the<br />

form {x1 ↦→ t1, . . . , xn ↦→ tn}, computed by evaluating the body of a search goal, must be encoded as<br />

a constraint in the new search goal. Therefore, ϕ is transformed into its equational representation<br />

ϕ, where ϕ is a solved constraint of the form x1=:=t1 &...& xn=:=tn. Hence, a search goal can<br />

72

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

Saved successfully!

Ooh no, something went wrong!