functional logic programming language currykahl/cas706/2010/pres/curry.pdf · 2010-11-24 ·...
TRANSCRIPT
Functional Logic Programming LanguageOverview of Curry
Main Features of CurryCurry vs Other Languages
Functional Logic Programming LanguageCurry
Xiang Yin
Department of Computer ScienceMcMaster University
November 9, 2010
[email protected] Course Presentation
Functional Logic Programming LanguageOverview of Curry
Main Features of CurryCurry vs Other Languages
Outline
1 Functional Logic Programming Language
2 Overview of Curry
3 Main Features of Curry
4 Curry vs Other Languages
[email protected] Course Presentation
Functional Logic Programming LanguageOverview of Curry
Main Features of CurryCurry vs Other Languages
Functional Programming LanguageLogic Programming LanguageFunctional Logic Programming Language
Functional Programming Language
Functional ProgrammingTreats computation as the evaluation of mathematicalfunctionsAvoids state and mutable dataThe basis is Lambda Calculus
FeaturesNested ExpressionsLazy EvaluationHigh-order Functions
[email protected] Course Presentation
Functional Logic Programming LanguageOverview of Curry
Main Features of CurryCurry vs Other Languages
Functional Programming LanguageLogic Programming LanguageFunctional Logic Programming Language
Logic Programming Language
Logic ProgrammingDefines what rules of the solution should satisfy rather thanhow many procedures should do for obtaining the solutionUse of mathematical logic for computer programmingUse of logic as both a declarative and proceduralrepresentation language
FeaturesLogical VariablesPartial Data StructuresBuilt-in SearchResiduation and Narrowing
[email protected] Course Presentation
Functional Logic Programming LanguageOverview of Curry
Main Features of CurryCurry vs Other Languages
Functional Programming LanguageLogic Programming LanguageFunctional Logic Programming Language
Functional Logic Programming Language
Functional Logic Programming Language - CurryCurry is a general-purpose declarative programming languagethat integrates functional with logic programming.
Benefitsvs pure functional languages: more expressive power dueto the availability of features like function inversion, partialdata structures, existential variables, and non-deterministicsearch.vs pure logic languages: more efficient operationalbehavior since functions provide for more efficientevaluation strategies (lazy evaluation, deterministicreductions) than predicates.
[email protected] Course Presentation
Functional Logic Programming LanguageOverview of Curry
Main Features of CurryCurry vs Other Languages
Predefined TypesPredefined OperationsExpressionsFunctions
Implementation of Curry
PAKCS (Portland Aachen Kiel Curry System): a majorCurry implementation with a WWW interfaceMCC (Münster Curry Complier): is a mature native codecompiler for Curry which conforms to the Curry reportexcept for committed choice which is not supportedKiCS (Kiel Curry System): a compiler that translates Curryinto HaskellSloth: a compiler which translates Curry programs intoProlog programs
[email protected] Course Presentation
Functional Logic Programming LanguageOverview of Curry
Main Features of CurryCurry vs Other Languages
Predefined TypesPredefined OperationsExpressionsFunctions
Predefined Types
Type Declaration ExamplesInteger Int . . . , −2,−1, 0, 1, 2, . . .
Boolean Bool False, TrueCharacter Char ’a’, ’b’, ’c’, . . .
String String “hello”, “world”List of τ [τ ] [], [0, 1, 2], 0 : 1 : 2 : []
Success Success successUnit () ()
[email protected] Course Presentation
Functional Logic Programming LanguageOverview of Curry
Main Features of CurryCurry vs Other Languages
Predefined TypesPredefined OperationsExpressionsFunctions
Predefined Types
Success TypeThe type Success has no visible literal values and is intendedto denote the result of successfully solved constraints.
Unit TypeType Theory: a type that allows only one value (and thuscan hold no information)Curry: useful in situations where the return value of afunction is not importantHaskell: the Nothing in polymorphic type Maybe isisomorphic to the unit typeC, C++, C#, Java : Void Type
[email protected] Course Presentation
Functional Logic Programming LanguageOverview of Curry
Main Features of CurryCurry vs Other Languages
Predefined TypesPredefined OperationsExpressionsFunctions
Predefined Types
Success TypeThe type Success has no visible literal values and is intendedto denote the result of successfully solved constraints.
Unit TypeType Theory: a type that allows only one value (and thuscan hold no information)Curry: useful in situations where the return value of afunction is not importantHaskell: the Nothing in polymorphic type Maybe isisomorphic to the unit typeC, C++, C#, Java : Void Type
[email protected] Course Presentation
Functional Logic Programming LanguageOverview of Curry
Main Features of CurryCurry vs Other Languages
Predefined TypesPredefined OperationsExpressionsFunctions
Predefined Types
Success TypeThe type Success has no visible literal values and is intendedto denote the result of successfully solved constraints.
Unit TypeType Theory: a type that allows only one value (and thuscan hold no information)Curry: useful in situations where the return value of afunction is not importantHaskell: the Nothing in polymorphic type Maybe isisomorphic to the unit typeC, C++, C#, Java : Void Type
[email protected] Course Presentation
Functional Logic Programming LanguageOverview of Curry
Main Features of CurryCurry vs Other Languages
Predefined TypesPredefined OperationsExpressionsFunctions
Predefined Types
Success TypeThe type Success has no visible literal values and is intendedto denote the result of successfully solved constraints.
Unit TypeType Theory: a type that allows only one value (and thuscan hold no information)Curry: useful in situations where the return value of afunction is not importantHaskell: the Nothing in polymorphic type Maybe isisomorphic to the unit typeC, C++, C#, Java : Void Type
[email protected] Course Presentation
Functional Logic Programming LanguageOverview of Curry
Main Features of CurryCurry vs Other Languages
Predefined TypesPredefined OperationsExpressionsFunctions
Predefined Operations
Description Ident. TypeBoolean Equality == a → a → Bool
Constrained Equality =:= a → a → SuccessBoolean Conjunction && Bool → Bool → BoolBoolean Disjunction || Bool → Bool → BoolParallel Conjunction & Success → Success → Success
Constrained Expression &> Success → a → a
[email protected] Course Presentation
Functional Logic Programming LanguageOverview of Curry
Main Features of CurryCurry vs Other Languages
Predefined TypesPredefined OperationsExpressionsFunctions
Predefined Operations
Parallel Conjunction(&)The parallel conjunction applied to expressions u and v , i.e., u& v , evaluates u and v concurrently. If both succeeds, theevaluation succeeds; otherwise it fails.
Constrained Expression(&>)The constrained expression applied to a constraint c and anexpression e, i.e., c &> e, evaluates first c and, if this evaluationsucceeds, then e, otherwise it fails.
[email protected] Course Presentation
Functional Logic Programming LanguageOverview of Curry
Main Features of CurryCurry vs Other Languages
Predefined TypesPredefined OperationsExpressionsFunctions
Expressions
DefinitionAn expression is either a symbol or literal value or is theapplication of an expression to another expression.
Prelude>3 + 5 ∗ 4Result: 23 ?Prelude> 3 + 5 ∗ 4 >= 3 ∗ (4 + 2)Result: True ?Prelude> 4 + 3 == 8Result: False ?
[email protected] Course Presentation
Functional Logic Programming LanguageOverview of Curry
Main Features of CurryCurry vs Other Languages
Predefined TypesPredefined OperationsExpressionsFunctions
Expressions
DefinitionAn expression is either a symbol or literal value or is theapplication of an expression to another expression.
Prelude>3 + 5 ∗ 4Result: 23 ?Prelude> 3 + 5 ∗ 4 >= 3 ∗ (4 + 2)Result: True ?Prelude> 4 + 3 == 8Result: False ?
[email protected] Course Presentation
Functional Logic Programming LanguageOverview of Curry
Main Features of CurryCurry vs Other Languages
Predefined TypesPredefined OperationsExpressionsFunctions
Expressions
DefinitionAn expression is either a symbol or literal value or is theapplication of an expression to another expression.
Prelude>3 + 5 ∗ 4Result: 23 ?Prelude> 3 + 5 ∗ 4 >= 3 ∗ (4 + 2)Result: True ?Prelude> 4 + 3 == 8Result: False ?
[email protected] Course Presentation
Functional Logic Programming LanguageOverview of Curry
Main Features of CurryCurry vs Other Languages
Predefined TypesPredefined OperationsExpressionsFunctions
Functions
DefinitionA function is a device that takes arguments and returns aresult. The result is obtained by evaluating an expression whichgenerally involves the function’s arguments.
constant function: nine = 3 ∗ 3function with argument: square x = x ∗ xRemarks: The syntax of Curry is actually very similar toHaskell.
[email protected] Course Presentation
Functional Logic Programming LanguageOverview of Curry
Main Features of CurryCurry vs Other Languages
Predefined TypesPredefined OperationsExpressionsFunctions
Functions
DefinitionA function is a device that takes arguments and returns aresult. The result is obtained by evaluating an expression whichgenerally involves the function’s arguments.
constant function: nine = 3 ∗ 3function with argument: square x = x ∗ xRemarks: The syntax of Curry is actually very similar toHaskell.
[email protected] Course Presentation
Functional Logic Programming LanguageOverview of Curry
Main Features of CurryCurry vs Other Languages
Predefined TypesPredefined OperationsExpressionsFunctions
Functions
DefinitionA function is a device that takes arguments and returns aresult. The result is obtained by evaluating an expression whichgenerally involves the function’s arguments.
constant function: nine = 3 ∗ 3function with argument: square x = x ∗ xRemarks: The syntax of Curry is actually very similar toHaskell.
[email protected] Course Presentation
Functional Logic Programming LanguageOverview of Curry
Main Features of CurryCurry vs Other Languages
Predefined TypesPredefined OperationsExpressionsFunctions
Functions
Load and ReloadIf we write the definitions of nine and square with a standardtext editor into a file named “firstprog.curry”:Prelude> :l firstprogfirstprog>firstprog> square nineResult: 81 ?If we add the definition “two = 2” to the file and we want toreload it:firstprog> :rfirstprog> square (square two)Result: 16 ?
[email protected] Course Presentation
Functional Logic Programming LanguageOverview of Curry
Main Features of CurryCurry vs Other Languages
Pattern MatchingHigher-Order ComputationLazy EvaluationLogic Variables
Pattern Matching
Pattern Matching
The definition of a function can be broken into several rules.This feature allows a function to dispatch the expression to bereturned depending on the values of its arguments.
not x = if x == True then Falseelse True
not :: Bool → Boolnot False = Truenot True = FalseRemarks: It is quite useful to simplify the complex datastructures.
[email protected] Course Presentation
Functional Logic Programming LanguageOverview of Curry
Main Features of CurryCurry vs Other Languages
Pattern MatchingHigher-Order ComputationLazy EvaluationLogic Variables
Pattern Matching
Pattern Matching
The definition of a function can be broken into several rules.This feature allows a function to dispatch the expression to bereturned depending on the values of its arguments.
not x = if x == True then Falseelse True
not :: Bool → Boolnot False = Truenot True = FalseRemarks: It is quite useful to simplify the complex datastructures.
[email protected] Course Presentation
Functional Logic Programming LanguageOverview of Curry
Main Features of CurryCurry vs Other Languages
Pattern MatchingHigher-Order ComputationLazy EvaluationLogic Variables
Pattern Matching
Pattern Matching
The definition of a function can be broken into several rules.This feature allows a function to dispatch the expression to bereturned depending on the values of its arguments.
not x = if x == True then Falseelse True
not :: Bool → Boolnot False = Truenot True = FalseRemarks: It is quite useful to simplify the complex datastructures.
[email protected] Course Presentation
Functional Logic Programming LanguageOverview of Curry
Main Features of CurryCurry vs Other Languages
Pattern MatchingHigher-Order ComputationLazy EvaluationLogic Variables
Higher-Order Computation
DefinitionA function that takes an argument of function type is referred toas a higher-order function. Loosely speaking, a higher-orderfunction is computation parameterized by another computation.
Examplesort _ [] = []sort f (x:xs) = insert f x (sort f xs)insert _ x [] = [x]insert f x (y:ys) | f x y = x : y : ys
| otherwise = y : insert f x yssort (<=) [3,5,1,2,6,8,9,7]Result: [1,2,3,5,6,7,8,9] ?
[email protected] Course Presentation
Functional Logic Programming LanguageOverview of Curry
Main Features of CurryCurry vs Other Languages
Pattern MatchingHigher-Order ComputationLazy EvaluationLogic Variables
Lazy Evaluation
DefinitionLazy Evaluation is the technique of delaying a computation untilthe result is required. More details:
The evaluation of an expression t is the process ofobtaining a value v from t.The value v is obtained from t by replacing an instance ofthe left-hand side of a rule with the corresponding instanceof the right-hand side.The evaluation of an expression t proceeds replacementafter replacement until an expression v in which no morereplacements are possible is obtained.
[email protected] Course Presentation
Functional Logic Programming LanguageOverview of Curry
Main Features of CurryCurry vs Other Languages
Pattern MatchingHigher-Order ComputationLazy EvaluationLogic Variables
Lazy Evaluation
Example1square x = x ∗ xan instance of square x is replaced with the correspondinginstance of x ∗ x . For example, 4 + square(2 + 3) is replaced by4 + (2 + 3) ∗ (2 + 3).
Example2If v is not a value: fails; otherwise v is the result.For example, the following function head computes the firstelement of a (non-null) list: head (x:_) = xAn attempt to evaluate “head []” fails, since no replacement ispossible and the expression is not a value since it contains afunction.
[email protected] Course Presentation
Functional Logic Programming LanguageOverview of Curry
Main Features of CurryCurry vs Other Languages
Pattern MatchingHigher-Order ComputationLazy EvaluationLogic Variables
Lazy Evaluation
BenefitsPerformance increases due to avoiding unnecessarycalculationsAvoiding error conditions in the evaluation of compoundexpressionsThe capability of constructing potentially infinite datastructuresThe capability of defining control structures as abstractionsinstead of as primitives
[email protected] Course Presentation
Functional Logic Programming LanguageOverview of Curry
Main Features of CurryCurry vs Other Languages
Pattern MatchingHigher-Order ComputationLazy EvaluationLogic Variables
Logic Variables
DefinitionA logic variable is either a variable occurring in an expressiontyped by the user at the interpreter prompt or it is a variable inthe condition and/or right-hand side of a rewrite rule which doesnot occur in the left-hand side.
Case OnePrelude> z == 2 + 2 where z free
Case Twopath a z = edge a b && path b z where b free
[email protected] Course Presentation
Functional Logic Programming LanguageOverview of Curry
Main Features of CurryCurry vs Other Languages
Curry vs HaskellCurry vs Prolog
Curry vs Haskell
It is nearly a superset of Haskell, lacking support mostly foroverloading using type classes, which someimplementations provide anyway as a language extension,such as the MCC.It contains some special features, such as Search ForSolutions, and Residuation and Narrowing.
[email protected] Course Presentation
Functional Logic Programming LanguageOverview of Curry
Main Features of CurryCurry vs Other Languages
Curry vs HaskellCurry vs Prolog
Curry vs Haskell
Search For SolutionsOne of the distinguishing features of Curry in comparison toHaskell is its ability to search for solutions, i.e., to computevalues for the arguments of functions so that the functions canbe evaluated.
[email protected] Course Presentation
Functional Logic Programming LanguageOverview of Curry
Main Features of CurryCurry vs Other Languages
Curry vs HaskellCurry vs Prolog
Curry vs Haskell
Prelude> x &&(y || (not x)) where x,y
free Free variables in goal: x, yResult: TrueBindings:x=Truey=True ? ;Result: FalseBindings: x=Truey=False ? ;Result: FalseBindings: x=Falsey=y ? ;No more solutions.
[email protected] Course Presentation
Functional Logic Programming LanguageOverview of Curry
Main Features of CurryCurry vs Other Languages
Curry vs HaskellCurry vs Prolog
Curry vs Haskell
Prelude> x &&(y || (not x)) where x,y
free Free variables in goal: x, yResult: TrueBindings:x=Truey=True ? ;Result: FalseBindings: x=Truey=False ? ;Result: FalseBindings: x=Falsey=y ? ;No more solutions.
[email protected] Course Presentation
Functional Logic Programming LanguageOverview of Curry
Main Features of CurryCurry vs Other Languages
Curry vs HaskellCurry vs Prolog
Curry vs Haskell
Prelude> x &&(y || (not x)) where x,y
free Free variables in goal: x, yResult: TrueBindings:x=Truey=True ? ;Result: FalseBindings: x=Truey=False ? ;Result: FalseBindings: x=Falsey=y ? ;No more solutions.
[email protected] Course Presentation
Functional Logic Programming LanguageOverview of Curry
Main Features of CurryCurry vs Other Languages
Curry vs HaskellCurry vs Prolog
Curry vs Haskell
Prelude> x &&(y || (not x)) where x,y
free Free variables in goal: x, yResult: TrueBindings:x=Truey=True ? ;Result: FalseBindings: x=Truey=False ? ;Result: FalseBindings: x=Falsey=y ? ;No more solutions.
[email protected] Course Presentation
Functional Logic Programming LanguageOverview of Curry
Main Features of CurryCurry vs Other Languages
Curry vs HaskellCurry vs Prolog
Residuation and Narrowing
ResiduationLet e be an expression to evaluate and v a variable occurring ine. Suppose that e cannot be evaluated because the value of vis not known. Residuation suspends the evaluation of e. If it ispossible, we will address this possibility shortly, some otherexpression f is evaluated in hopes that the evaluation of f willbind a value to v. If and when this happens, the evaluation of eresumes. If the expression f does not exists, e is said toflounder and the evaluation of e fails.
ExamplePrelude> z == 2+2 where z freeFree variables in goal: z*** Goal suspended!
[email protected] Course Presentation
Functional Logic Programming LanguageOverview of Curry
Main Features of CurryCurry vs Other Languages
Curry vs HaskellCurry vs Prolog
Residuation and Narrowing
NarrowingNarrowing is a mechanism whereby a variable is bound to avalue selected from among alternatives imposed by constraints.Each possible value is tried in some order, with the remainderof the program invoked in each case to determine the validity ofthe binding.
RemarksBy contrast to residuation, if e cannot be evaluated because thevalue of v is not known, narrowing guesses a value for v. Theguessed value is uninformed except that only values that makeit possible to continue the computation are chosen.
[email protected] Course Presentation
Functional Logic Programming LanguageOverview of Curry
Main Features of CurryCurry vs Other Languages
Curry vs HaskellCurry vs Prolog
Narrowing
ExamplePrelude> z =:= 2+2 where z freeFree variables in goal: zResult: successBindings:z=4 ?
RemarksThe difference between Boolean Equality (==) and ConstrainedEquality (=:=) are in two aspects:
The type returned by the operation.The operation =:= does narrowing instead of == doesresiduation
[email protected] Course Presentation
Functional Logic Programming LanguageOverview of Curry
Main Features of CurryCurry vs Other Languages
Curry vs HaskellCurry vs Prolog
Curry vs Prolog
By the availability of several new features in comparison to purelogic programming, Curry avoids the following impure constructof Prolog:
The call predicate is replaced by the higher-order featuresof Curry.Many applications of assert and retract can be eliminatedusing implications in conditionsThe I/O operations of Prolog are replaced by thedeclarative monadic I/O concept of functionalprogramming.
[email protected] Course Presentation
Functional Logic Programming LanguageOverview of Curry
Main Features of CurryCurry vs Other Languages
Curry vs HaskellCurry vs Prolog
Summary
1 Features of Functional Logic Programming Language:Functional Programming LanguageLogic Programming Language
2 Features of Curry:Predefined Types and OperationsExpressions and FunctionsFeatures From Functional Language: Pattern Matching,Higher-Order Computation and Lazy EvaluationFeatures From Logic Language: Logic Variables, SearchFor Solutions, and Residuation and Narrowing
3 Curry vs Haskell && Curry vs Prolog
[email protected] Course Presentation
Appendix For Further Reading
References I
Sergio Antoy, Michael Hanus.Curry A Tutorial Introduction.Draft of December 5, 2007.
Michael Hanus, Herbert Kuchen, Juan JoséMoreno-Navarro.Curry: A Truly Functional Logic Language.1995.
WikiCurry (programming language).Available:http://en.wikipedia.org/wiki/Curry_(programming_language)
[email protected] Course Presentation
Appendix For Further Reading
References II
Curry (Home Page)A Truly Integrated Functional Logic Language.Available:http://www-ps.informatik.uni-kiel.de/currywiki/
[email protected] Course Presentation