curry functional logic language
play

Curry functional logic language Modern research language - PowerPoint PPT Presentation

Curry functional logic language Modern research language Combines functional and logic paradigms Several implementations exist Syntax (almost from Haskell) Added features for logic programming free variables


  1. Curry – functional logic language • ”Modern” research language • Combines functional and logic paradigms • Several implementations exist • Syntax (almost from Haskell) • Added features for logic programming – free variables – non-deterministic functions – logical constraints, built-in search (with several search strategies) TUT Pervasive Computing Principles of programming languages 1 Maarit Harsu / Matti Rintala / Henri Hansen

  2. New in Curry (vs Haskell) f x = x f x = x + 1 • Non-deterministic functions f 3 – Haskell: first match is chosen, 3 returned – Curry: both matches chosen, two execution branches, both 3 and 4 returned • Built-in choice operator ? : – 0 ? 1 returns 0 as well as 1 TUT Pervasive Computing Principles of programming languages 2 Maarit Harsu / Matti Rintala / Henri Hansen

  3. New in Curry (vs Haskell) empty [ ] = [ ] • Partial functions empty [ 3 ] – Haskell: run-time error – Curry: ”No solution” (continues search for other solutions) TUT Pervasive Computing Principles of programming languages 3 Maarit Harsu / Matti Rintala / Henri Hansen

  4. =:= New in Curry (vs Haskell) & &> • Constraints – constrained equation ( =:= ) returning success (or fail ) – unlike boolean equation ( == ) requires constraint to hold – can be used to limit function definitions (other major uses too) f x y – function defined only for some lists | x =:= reverse y = x ++ y TUT Pervasive Computing Principles of programming languages 4 Maarit Harsu / Matti Rintala / Henri Hansen

  5. New in Curry (vs Haskell) • Free variables – value not known beforehand – Curry tries to deduce (search for) the value based on constraints (unification) – [ 1 ] ++ x =:= [ 1, 2, 3 ] where x free returns binding x = [ 2, 3 ] TUT Pervasive Computing Principles of programming languages 5 Maarit Harsu / Matti Rintala / Henri Hansen

  6. Close to specification • Specification of a function that returns the last element of a list: last xs = e ⇔ ∃ ys: ys ++ [ e ] = xs , where xs is a list and e an element • In Curry: last xs | ys ++ [ e ] =:= xs = e where ys, e free TUT Pervasive Computing Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen

  7. Curry examples take 3 x =:= [ 1, 2, 3 ] & reverse x =:= x where x free insert x ys = x:ys insert x ( y:ys ) = y : insert x ys permutate [ ] = [ ] permutate ( x:xs ) = insert x (permutate xs) permutate [ 1, 2, 3, 1 ] =:= a ++ [ 1, 2 ] ++ b where a, b free TUT Pervasive Computing Principles of programming languages 7 Maarit Harsu / Matti Rintala / Henri Hansen

  8. Logic programming (in general) • Logic program – set of logical formulas • Logic programming – writing formulas • Program execution – inference of the logical consequencies of the program Wirth: programs = algorithms + data structures algorithms = logic + control Kowalski: => programs = logic TUT Pervasive Computing Principles of programming languages 8 Maarit Harsu / Matti Rintala / Henri Hansen

  9. ”The challenge of imperative Properties of logic programming is that all details of computation programming must (remember to) be expressed.” • Declarative semantics – considerably simpler than the semantics of imperative languages – semantics of a given statement (proposition) can be deducted from the statement itself • Programmer describes the result of the computation, not the steps leading to the result – c.f. requirements specification – e.g. sorting: sort ( old_list, new_list ) ← permutation ( old_list, new_list ) ∧ sorted ( new_list ) ← ∀ j: 1 ≤ j < n, list ( j ) ≤ list ( j + 1 ) sorted ( list ) TUT Pervasive Computing Principles of programming languages 9 Maarit Harsu / Matti Rintala / Henri Hansen

  10. Propositions 2 + 3 = 5 • A claim that is true or false 2 + 3 = 4 • Symbolic logic is used for – expressing propositions – expressing relations between propositions – deriving new propositions from existing ones • A close relation to mathematical logic TUT Pervasive Computing Principles of programming languages 10 Maarit Harsu / Matti Rintala / Henri Hansen

  11. More about propositions • Propositional symbols – e.g. Rains, Blows – often shorthand, e.g. P, Q, R modus ponens: • Logical connectors ∧ , ∨ , ¬ , → , ↔ Rains → GetsWet • Inference operations Rains ⇒ , ⇔ ⇒ GetsWet – inference rules • Logical rules: – e.g. associativity, distributivity, commutativity, idempotence, de Morgan’s laws, etc. TUT Pervasive Computing Principles of programming languages 11 Maarit Harsu / Matti Rintala / Henri Hansen

  12. Logical inference • Deductions are derived from other statements ( premises ) with formal inference rules • Quantifiers – quantifier elimination ( X ∈ { x 1 , x 2 , ..., x n } ) • ( ∀ X: P ( X ) ) ↔ P ( x 1 ) ∧ P ( x 2 ) ∧ ... ∧ P ( x n ) • ( ∃ X: P ( X ) ) ↔ P ( x 1 ) ∨ P ( x 2 ) ∨ ... ∨ P ( x n ) – quantifiers can be expressed as one another ¬ ( ∀ X: P ( X ) ) ↔ ∃ X: ( ¬ P ( X ) ) ¬ ( ∃ X: P ( X ) ) ↔ ∀ X: ( ¬ P ( X ) ) – in mathematics, equivalence and implication usually omit universal quantifier ∀ X: ( X > 0 → X + 1 > 0 ) � Universal quantifier implicit in logic programming TUT Pervasive Computing Principles of programming languages 12 Maarit Harsu / Matti Rintala / Henri Hansen

  13. Logic programming examples female ( X ) → human ( X ) • Propositions mother ( maria, X ) ∧ male ( X ) – variables allowed man ( leo ) – implicit quantifiers likes ( paul, steak ) • Much like mathematical functions: – functor (function name) – list of arguments (parameters) in parenthesis • Propositions have no inherent semantics TUT Pervasive Computing Principles of programming languages 13 Maarit Harsu / Matti Rintala / Henri Hansen

  14. B 1 ∨ B 2 ∨ ... ∨ B m ← A 1 ∧ A 2 ∧ ... ∧ A n Clausal forms A → B ¬ A ∨ B • Standardized expression of equivalent formulas • Expressing propositions in clausal form: – left side: head, consequent – right side: body, antecedent • The head is true, if the body can be proved to be true likes ( paul, trout ) ← likes ( paul, fish ) ∧ fish ( trout ) father ( leo, paul ) ∨ father ( leo, maria ) ← father ( paul, peter ) ∧ mother ( maria, peter ) ∧ grandfather ( leo, peter ) TUT Pervasive Computing Principles of programming languages 14 Maarit Harsu / Matti Rintala / Henri Hansen

  15. Horn clauses H ← A 1 ∧ ... ∧ A n ( n ≥ 0 ) • Limited clausal form – at most one proposition on the left • Three forms: H fact ( n = 0 ): 1. – assumed to be true H ← A 1 ∧ ... ∧ A n ( n > 0 ) rule ( n > 0 ) 2. – true, if all propositions on the right are true A 1 ∧ ... ∧ A n ( n > 0 ) goal ( n > 0 , H is missing) 3. – asks if the propositions on the right are true (query) • Most formulas can be represented as Horn clauses TUT Pervasive Computing Principles of programming languages 15 Maarit Harsu / Matti Rintala / Henri Hansen

  16. Facts • Horn clauses with an empty body (only the head) • Clauses that are assumed to be true (axioms) • Stored to a database • Meaning given by the programmer female ( sofia ). father ( leo, peter ). male ( leo ). father ( leo, sofia ). female ( maria ). mother ( maria, peter ). male ( peter ). mother ( maria, sofia ). TUT Pervasive Computing Principles of programming languages 16 Maarit Harsu / Matti Rintala / Henri Hansen

  17. Rules • Correspond to implications in mathematics • Horn clauses with both head and body – right side (condition): if-part – left side (consequence): then-part ancestor ( maria, sofia ) :- mother ( maria, sofia ). parent ( X, Y ) :- mother ( X, Y ). parent ( X, Y ) :- father ( X, Y ). grandparent ( X, Z ) :- parent ( X, Y ), parent ( Y, Z ). sibling ( X, Y ) :- mother ( M, X ), mother ( M, Y ), father ( F, X ), father ( F, Y ). TUT Pervasive Computing Principles of programming languages 17 Maarit Harsu / Matti Rintala / Henri Hansen

  18. Goals (queries) • The goal is to find out whether the query is true (or false) male ( leo ). • The system answers either – ”yes”: goal is true based on the given facts and rules – ”no”: goal is false or the system cannot conclude the truth value • If the goal includes variables, the system instantiates the variables so as to make the goal true father ( X, sofia ). TUT Pervasive Computing Principles of programming languages 18 Maarit Harsu / Matti Rintala / Henri Hansen

  19. P1 ← P2 Resolution P2 ← P3 P1 ← P3 • Inference process – a new Horn clause is inferred from existing ones • Variables in Horn clauses – instantiation • assigning temporary values to variables during resolution – unification • finding values for which the resolution is succesful • recursive, may require several instantiations • pattern matching – backtracking • undoing instantiations • Contradictions between clauses – proof by contradictions TUT Pervasive Computing Principles of programming languages 19 Maarit Harsu / Matti Rintala / Henri Hansen

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend