Functional Logic Programming Language Overview of Curry Main Features of Curry Curry vs Other Languages Functional Logic Programming Language Curry Xiang Yin Department of Computer Science McMaster University November 9, 2010 [email protected]Course Presentation
42
Embed
Functional Logic Programming Language Currykahl/CAS706/2010/Pres/Curry.pdf · 2010-11-24 · Functional Logic Programming Language Overview of Curry Main Features of Curry Curry vs
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Functional Logic Programming LanguageOverview of Curry
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
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.
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
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
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
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
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
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.
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.
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.
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.
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 ?
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.
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.
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.
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] ?
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.
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.
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
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
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.
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.
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!
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.
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.
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