Curry (programming language): Difference between revisions

Content deleted Content added
Glatk (talk | contribs)
m External links: Added missing author (Kiselyov), and the publication date (2009) to the referenced paper "Purely Functional Lazy Non-deterministic Programming"
m Task 70: Update syntaxhighlight tags - remove use of deprecated <source> tags
Line 58:
 
As many functional languages like [[haskell (programming language)|Haskell]] do, Curry supports the definition of [[algebraic data type]]s by enumerating their constructors. For instance, the type of Boolean values consists of the constructors {{Mono|True}} and {{Mono|False}} that are declared as follows:
<sourcesyntaxhighlight lang="haskell">
data Bool = True | False
</syntaxhighlight>
</source>
Functions on Booleans can be defined by pattern matching, i.e., by providing several equations for different argument values:
<sourcesyntaxhighlight lang="haskell">
not True = False
not False = True
</syntaxhighlight>
</source>
The principle of replacing equals by equals is still valid provided that the actual arguments have the required form, e.g.:
Line 72:
More complex data structures can be obtained by [[recursive data type]]s. For instance, a list of elements, where the type of elements is arbitrary (denoted by
the type variable {{Mono|a}}), is either the empty list “{{Mono|[]}}” or the non-empty list “{{Mono|x:xs}}” consisting of a first element {{Mono|x}} and a list {{Mono|xs}}:
<sourcesyntaxhighlight lang="haskell">
data List a = [] | a : List a
</syntaxhighlight>
</source>
The type “{{Mono|List a}}” is usually written as {{Mono|[a]}} and finite lists x1{{Mono|:}}x2{{Mono|:}}...{{Mono|:}}xn{{Mono|:[]}} are written as {{Mono|[}}x1{{Mono|,}}x2{{Mono|,}}...{{Mono|,}}xn{{Mono|]}}. We can define operations on recursive types by inductive definitions where pattern matching supports the convenient separation of the different cases. For instance, the concatenation operation “{{Mono|++}}” on polymorphic lists can be defined as follows (the optional type declaration in the first line specifies that “{{Mono|++}}” takes two lists as input and produces an output list, where all list elements are of the same unspecified type):
<sourcesyntaxhighlight lang="haskell">
(++) :: [a] -> [a] -> [a]
[] ++ ys = ys
(x:xs) ++ ys = x : xs++ys
</syntaxhighlight>
</source>
Beyond its application for various programming tasks, the operation “{{Mono|++}}” is also useful to specify the behavior of other functions on lists. For instance, the behavior of a function last that yields the last element of a list can be specified as follows: for all lists xs and elements e, {{Mono|last}} xs = e if ∃ys{{Mono|:}}ys{{Mono|++[}}e{{Mono|]}} = xs.
Based on this specification, one can define a function that satisfies this specification by employing logic programming features. Similarly to logic languages, functional logic languages provide search for solutions for existentially quantified variables. In contrast to pure logic languages, they support equation solving over nested functional expressions so that an equation like ys{{Mono|++[}}e{{Mono|]}} = {{Mono|[1,2,3]}} is solved by instantiating ys to the list {{Mono|[1,2]}} and e to the value {{Mono|3}}. In Curry one can define the operation last as follows:
<sourcesyntaxhighlight lang="haskell">
last xs | ys++[e] =:= xs = e where ys,e free
</syntaxhighlight>
</source>
Here, the symbol “{{Mono|1==:=}}” is used for equational constraints in order to provide a syntactic distinction from defining equations. Similarly, extra variables (i.e., variables not occurring in the left-hand side of the defining equation) are explicitly declared by “{{Mono|where...free}}” in order to provide some opportunities to detect bugs caused by typos. A conditional equation of the form l {{Mono|{{!}}}} c {{Mono|1==}} r is applicable for reduction if its condition c has been solved. In contrast to purely functional languages where conditions are only evaluated to a Boolean value, functional logic languages support the solving of conditions by guessing values for the unknowns in the condition. Narrowing as discussed in the next section is used to solve this kind of conditions.
 
Line 111:
 
The rule defining {{Mono|last}} shown above expresses the fact that the actual argument must match the result of narrowing the expression {{Mono|ys++[e]}}. Curry can express this property also in the following more concise way:
<sourcesyntaxhighlight lang="haskell">
last (ys++[e]) = e
</syntaxhighlight>
</source>
Haskell does not allow such a declaration since the pattern in the left-hand side contains a defined function ({{Mono|++}}). Such a pattern is also called ''functional pattern''.<ref>{{cite journal|doi=10.1007/11680093_2 | title=Declarative Programming with Function Patterns | year=2006 | journal=Lecture Notes in Computer Science | volume=3901 | pages=6–22 | author=Antoy Sergio, Hanus Michael| isbn=978-3-540-32654-0 }}</ref> Functional patterns are enabled by the combined functional and logic features of Curry and support concise definitions of tasks requiring deep pattern matching in hierarchical data structures.
 
Line 126:
 
The rules defining {{Mono|?}} show an important feature of Curry: all rules are tried in order to evaluate some operation. Hence, one can define by
<sourcesyntaxhighlight lang="haskell">
insert x ys = x : ys
insert x (y:ys) = y : insert x ys
</syntaxhighlight>
</source>
an operation to insert an element into a list at an indeterminate position so that the operation {{Mono|perm}} defined by
<sourcesyntaxhighlight lang="haskell">
perm [] = []
perm (x:xs) = insert x (perm xs)
</syntaxhighlight>
</source>
returns any permutation of a given input list.