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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Axiom:<br />

A ⊢ x :: τ<br />

if τ is a generic instance of A(x)<br />

Application: A ⊢ e1 :: τ1 → τ2 A ⊢ e2 :: τ1<br />

A ⊢ e1 e2 :: τ2<br />

Abstraction:<br />

Existential:<br />

A[x/τ] ⊢ e :: τ ′<br />

A ⊢ \x->e :: τ → τ ′ if τ is a type expression<br />

A[x/τ] ⊢ e :: Success<br />

A ⊢ let x free in e :: Success<br />

Conditional: A ⊢ e1 :: Bool A ⊢ e2 :: τ A ⊢ e3 :: τ<br />

A ⊢ if e1 then e2 else e3 :: τ<br />

Figure 1: Typing rules for <strong>Curry</strong> programs<br />

if τ is a type expression<br />

where T is a type constructor of arity n, as defined by a datatype declaration (cf. Section 2.1), 9 and<br />

τ1, . . . , τn are type expressions (note that list, tuple and function types have the special syntax [·],<br />

(·,· · ·,·), and ·->· as described above). For instance, [(Int,a)]->a is a type expression containing<br />

the type variable a. A type scheme is a type expression with a universal quantification for some<br />

type variables, i.e., it has the form ∀α1, . . . , αn.τ (n ≥ 0; in case of n = 0, the type scheme is<br />

equivalent to a type expression). A function type declaration f::τ is considered as an assignment<br />

of the type scheme ∀α1, . . . , αn.τ to f, where α1, . . . , αn are all type variables occurring in τ. The<br />

type τ is called a generic instance of the type scheme ∀α1, . . . , αn.τ ′ if there is a substitution<br />

σ = {α1 ↦→ τ1, . . . , αn ↦→ τn} on the types with σ(τ ′ ) = τ.<br />

The types of all defined functions are collected in a type environment A which is a mapping<br />

from identifiers to type schemes. It contains at least the type schemes of the defined functions and<br />

an assignment of types for some local variables. <strong>An</strong> expression e is well-typed and has type τ w.r.t.<br />

a type environment A if A ⊢ e :: τ is derivable according to the inference rules shown in Figure 1.<br />

A defining equation f t1 . . . tn = e [where x free] is well-typed w.r.t. a type environment<br />

A if A(f) = ∀α1, . . . , αm.τ [and A(x) is a type expression] and A ⊢ \t1-> · · · \tn->e :: τ is derivable<br />

according to the above inference rules. A conditional equation l | c = r is considered (for the<br />

purpose of typing) as syntactic sugar for l = cond c r where cond is a new function with type<br />

scheme A(cond) = ∀α.Success → α → α.<br />

A program is well-typed if all its rules are well-typed with a unique assignment of type schemes<br />

to defined functions. 10<br />

Note that the following recursive definition is a well-typed <strong>Curry</strong> program according to the<br />

definition above (and the type definitions given in the prelude, cf. Appendix B):<br />

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

f x = if length x == 0 then fst (g x x) else x<br />

9<br />

We assume here that all type constructors introduced by type synonyms (cf. Section 2.2) are replaced by their<br />

definitions.<br />

10<br />

Here we assume that all local declarations are eliminated by the transformations described in Appendix D.8.<br />

19

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

Saved successfully!

Ooh no, something went wrong!