Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Curry (programming language)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Basic concepts=== A functional program is a set of functions defined by equations or rules. A functional computation consists of replacing subexpressions by equal (with regard to the function definitions) subexpressions until no more replacements (or reductions) are possible and a value or normal form is obtained. For instance, consider the function double defined by double x = x+x The expression β{{Mono|double 1}}β is replaced by {{Mono|1+1}}. The latter can be replaced by {{Mono|2}} if we interpret the operator β{{Mono|+}}β to be defined by an infinite set of equations, e.g., {{Mono|1=1+1 = 2}}, {{Mono|1=1+2 = 3}}, etc. In a similar way, one can evaluate nested expressions (where the subexpressions to be replaced are quoted): 'double (1+2)' β '(1+2)'+(1+2) β 3+'(1+2)' β '3+3' β 6 There is also another order of evaluation if we replace the arguments of operators from right to left: 'double (1+2)' β (1+2)+'(1+2)' β '(1+2)'+3 β '3+3' β 6 In this case, both derivations lead to the same result, a property known as [[Confluence (term rewriting)|confluence]]. This follows from a fundamental property of pure functional languages, termed [[referential transparency]]: the value of a computed result does not depend on the order or time of evaluation, due to the absence of [[Side effect (computer science)|side effects]]. This simplifies reasoning about, and maintaining, pure functional programs. As many functional languages like [[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: <syntaxhighlight lang="haskell"> data Bool = True | False </syntaxhighlight> Functions on Booleans can be defined by pattern matching, i.e., by providing several equations for different argument values: <syntaxhighlight lang="haskell"> not True = False not False = True </syntaxhighlight> The principle of replacing equals by equals is still valid provided that the actual arguments have the required form, e.g.: not '(not False)' β 'not True' β False More complex [[data structure]]s 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}}: <syntaxhighlight lang="haskell"> data List a = [] | a : List a </syntaxhighlight> 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): <syntaxhighlight lang="haskell"> (++) :: [a] -> [a] -> [a] [] ++ ys = ys (x:xs) ++ ys = x : xs++ys </syntaxhighlight> 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: <syntaxhighlight lang="haskell"> last xs | ys++[e] =:= xs = e where ys,e free </syntaxhighlight> 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.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)