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.

type RecF a = a -> RecF a -- recursive definitions not allowed<br />

type Place = [Addr] -- mutually recursive definitions not allowed<br />

type Addr = [Place]<br />

However, recursive definitions with an intervening datatype are allowed, since recursive datatype<br />

definitions are also allowed. For instance, the following definitions are valid:<br />

type Place = [Addr]<br />

data Addr = Tag [Place]<br />

2.3 Function Declarations<br />

A function is defined by a type declaration (which can be omitted) followed by a list of defining<br />

equations. A type declaration for an n-ary function has the form<br />

f :: τ1 -> τ2 -> · · · -> τn -> τ<br />

where τ1, . . . , τn, τ are type expressions and τ is not a functional type. The simplest form of a<br />

defining equation (or rule) for an n-ary function f (n ≥ 0) is<br />

f t1 . . . tn = e<br />

where t1, . . . , tn are data terms and the right-hand side e is an expression. The left-hand side<br />

f t1 . . . tn must not contain multiple occurrences of a variable. Functions can also be defined by<br />

conditional equations which have the form<br />

f t1 . . . tn | c = e<br />

where the condition c is a constraint (cf. Section 2.6). In order to apply a conditional equation, its<br />

condition must be solved.<br />

Note that one can define functions with a non-determinate behavior by providing several rules<br />

with overlapping left-hand sides or free variables (i.e., variables which do not occur in the left-hand<br />

side) in the conditions or right-hand sides of rules. For instance, the following non-deterministic<br />

function inserts an element at an arbitrary position in a list:<br />

insert x [] = [x]<br />

insert x (y:ys) = x : y : ys<br />

insert x (y:ys) = y : insert x ys<br />

Such non-deterministic functions can be given a perfect declarative semantics [18] and their implementation<br />

causes no overhead in <strong>Curry</strong> since techniques for handling non-determinism are already<br />

contained in the logic programming part (see also [4]). However, deterministic functions are advantageous<br />

since they provide for more efficient implementations (like the dynamic cut [34]). If one is<br />

interested only in defining deterministic functions, this can be ensured by the following restrictions:<br />

1. Each variable occurring in the right-hand side of a rule must also occur in the corresponding<br />

left-hand side.<br />

2. If f t1 . . . tn | c = e and f t ′ 1 . . . t′ n | c ′ = e ′ are rules defining f and σ is a substitu-<br />

5

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

Saved successfully!

Ooh no, something went wrong!