Curry functional logic language Modern research language - - PowerPoint PPT Presentation

curry functional logic language
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen

TUT Pervasive Computing

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)

1

slide-2
SLIDE 2

Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen

TUT Pervasive Computing

New in Curry (vs Haskell)

  • Non-deterministic functions

– 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

2

f x = x f x = x + 1 f 3

slide-3
SLIDE 3

Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen

TUT Pervasive Computing

New in Curry (vs Haskell)

  • Partial functions

– Haskell: run-time error – Curry: ”No solution” (continues search for

  • ther solutions)

3

empty [ ] = [ ] empty [ 3 ]

slide-4
SLIDE 4

Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen

TUT Pervasive Computing

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)

– function defined only for some lists

4

=:= & &> f x y | x =:= reverse y = x ++ y

slide-5
SLIDE 5

Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen

TUT Pervasive Computing

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 ]

5

slide-6
SLIDE 6

Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen

TUT Pervasive Computing

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

slide-7
SLIDE 7

Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen

TUT Pervasive Computing

Curry examples

7

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

slide-8
SLIDE 8

Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen

TUT Pervasive Computing

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 programs = algorithms + data structures algorithms = logic + control => programs = logic Wirth: Kowalski:

slide-9
SLIDE 9

Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen

TUT Pervasive Computing

9

Properties of logic programming

  • 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 ) sorted ( list ) ← ∀j: 1 ≤ j < n, list ( j ) ≤ list ( j + 1 ) ”The challenge of imperative programming is that all details of computation must (remember to) be expressed.”

slide-10
SLIDE 10

Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen

TUT Pervasive Computing

Propositions

  • A claim that is true or false
  • Symbolic logic is used for

– expressing propositions – expressing relations between propositions – deriving new propositions from existing ones

  • A close relation to mathematical logic

10

2 + 3 = 5 2 + 3 = 4

slide-11
SLIDE 11

Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen

TUT Pervasive Computing

11

More about propositions

  • Propositional symbols

– e.g. Rains, Blows – often shorthand, e.g. P, Q, R

  • Logical connectors

∧, ∨, ¬, →, ↔

  • Inference operations

⇒, ⇔ – inference rules

  • Logical rules:

– e.g. associativity, distributivity, commutativity, idempotence, de Morgan’s laws, etc. Rains → GetsWet Rains ⇒ GetsWet modus ponens:

slide-12
SLIDE 12

Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen

TUT Pervasive Computing

12

Logical inference

  • Deductions are derived from other statements

(premises) with formal inference rules

  • Quantifiers

– quantifier elimination ( X ∈ { x1, x2, ..., xn } )

  • ( ∀X: P ( X ) ) ↔ P ( x1 ) ∧ P ( x2 ) ∧ ... ∧ P ( xn )
  • ( ∃X: P ( X ) ) ↔ P ( x1 ) ∨ P ( x2 ) ∨ ... ∨ P ( xn )

– quantifiers can be expressed as one another – in mathematics, equivalence and implication usually

  • mit universal quantifier

Universal quantifier implicit in logic programming

∀X: ( X > 0 → X + 1 > 0 ) ¬ ( ∀X: P ( X ) ) ↔ ∃X: ( ¬ P ( X ) ) ¬ ( ∃X: P ( X ) ) ↔ ∀X: ( ¬ P ( X ) )

slide-13
SLIDE 13

Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen

TUT Pervasive Computing

Logic programming examples

  • Propositions

– variables allowed – implicit quantifiers

  • Much like mathematical functions:

– functor (function name) – list of arguments (parameters) in parenthesis

  • Propositions have no inherent semantics

13

man ( leo ) likes ( paul, steak ) female ( X ) → human ( X ) mother ( maria, X ) ∧ male ( X )

slide-14
SLIDE 14

Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen

TUT Pervasive Computing

14

Clausal forms

  • 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

B1 ∨ B2 ∨ ... ∨ Bm ← A1 ∧ A2 ∧ ... ∧ An

likes ( paul, trout ) ← likes ( paul, fish ) ∧ fish ( trout ) father ( leo, paul ) ∨ father ( leo, maria ) ← father ( paul, peter ) ∧ mother ( maria, peter ) ∧ grandfather ( leo, peter )

A → B ¬A ∨ B

slide-15
SLIDE 15

Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen

TUT Pervasive Computing

15

Horn clauses

  • Limited clausal form

– at most one proposition on the left

  • Three forms:

1. fact (n = 0): – assumed to be true 2. rule (n > 0) – true, if all propositions on the right are true 3. goal (n > 0, H is missing) – asks if the propositions on the right are true (query)

  • Most formulas can be represented as Horn clauses

H ← A1 ∧ ... ∧ An ( n ≥ 0 ) H A1 ∧ ... ∧ An ( n > 0 ) H ← A1 ∧ ... ∧ An ( n > 0 )

slide-16
SLIDE 16

Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen

TUT Pervasive Computing

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 ). male ( leo ). female ( maria ). male ( peter ). father ( leo, peter ). father ( leo, sofia ). mother ( maria, peter ). mother ( maria, sofia ).

slide-17
SLIDE 17

Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen

TUT Pervasive Computing

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 ).

slide-18
SLIDE 18

Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen

TUT Pervasive Computing

18

Goals (queries)

  • The goal is to find out whether the query is true

(or false)

  • 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

male ( leo ). father ( X, sofia ).

slide-19
SLIDE 19

Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen

TUT Pervasive Computing

19

Resolution

  • 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 P1 ← P2 P2 ← P3 P1 ← P3

slide-20
SLIDE 20

Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen

TUT Pervasive Computing

20

Rules for unification

  • A constant can only be unified to itself
  • A variable can be unified to anything

– unification to a value

  • variable is instantiated with the value

– unification to an uninstantiated variable

  • if either of the variables gets later a value, the same value is

given to both variables

  • Two structures can be unified if

– they have the same functor, – same number of arguments, and – the arguments in corresponding positions can be unified functor ( parameter_list )

slide-21
SLIDE 21

Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen

TUT Pervasive Computing

21

Prolog

  • Several dialects

– vary greatly in syntax – examples follow Edinburg syntax

  • Small number of statements that can be

complicated

  • Statements formed from the following terms:

– constant

  • numeric value or identifier beginning with a lower-case letter

– variable

  • identifier beginning with an upper-case letter

– functor (structure) functor ( parameter_list )

slide-22
SLIDE 22

Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen

TUT Pervasive Computing

22

Arithmetic in Prolog (instantiation)

  • is-operator

– right side variables must have been instantiated – left side variable must not have been instantiated – left side variable becomes instantiated – in this case the statement is satisfied – otherwise the statement is not satisfied and no instantiation takes place A is B / 17 + C. Sum is Sum + Number.

does not satisfy

slide-23
SLIDE 23

Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen

TUT Pervasive Computing

23

Arithmetic in Prolog (unification)

?- X = 2 + 3. X = 2 + 3 ?- X is 2 + 3. X = 5 ?- X is 2 + 3, X = 5. X = 5 ?- X is 2 + 3, X = 2 + 3. no Variable X is unified to term 2 + 3 Expression is evaluated Goal is satisfied Goal is failed: term 2 + 3 cannot be unified to term 5 = unification is expression evaluation

slide-24
SLIDE 24

Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen

TUT Pervasive Computing

24

More about unification

  • Unification of two terms (statements)

– terms are assigned to variables with aiming to get the statements identical

  • can be unified:
  • cannot be unified:
  • Unifaction is either unsatisfied or produces

the most general unification

X + cat = 5 + Y 6 + X = cat + Y 2 + X = 2 + Y

slide-25
SLIDE 25

Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen

TUT Pervasive Computing

25

Resolution in logic programming

  • Two approaches:

– bottom-up resolution (forward chaining)

  • starts with the facts and rules in the database and tries to find a

sequence of matches that lead to the goal

– top-down resolution (backward chaining)

  • starts with the goal and tries to find a sequence of

matching propositions that lead to some set of original facts P1. P2 :- P1. P3 :- P2. P4 :- P3. Q :- P4.

male ( paul ). father ( paul ). male ( X ) :- father ( X ).

What if after these facts and rules, Q is queried? Goal:

Prolog way

Facts and rules:

slide-26
SLIDE 26

Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen

TUT Pervasive Computing

26

Subgoal processing

  • Goal may have several subgoals

– depth-first search

  • processing the first subgoal completely before working
  • n the others

– breadth-first search

  • working on all subgoals in parallel
  • Backtracking

– previous subgoal is re-examined and tried with other values

  • Generate and test –principle

– the first subgoals generate solutions, and the later subgoals choose satisfiable ones – unsatisfiable alternatives cause backtracking and generation of new solution proposals

generate and test guess and verify

Prolog way

slide-27
SLIDE 27

Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen

TUT Pervasive Computing

27

Example on subgoals and backtracking

hobby ( peter, squash ). hobby ( peter, tennis ). hobby ( emil, football ). hobby ( emma, volleyball ). equipment ( squash, ball ). equipment ( squash, racket ). equipment ( tennis, ball ). equipment ( tennis, racket ). equipment ( football, ball ). equipment ( volleyball, ball ). hobby ( peter, X ), equipment ( X, Y ) X = squash, Y = ball X = squash, Y = racket X = tennis, Y = ball X = tennis, Y = racket

Facts: Goal: Results:

slide-28
SLIDE 28

Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen

TUT Pervasive Computing

28

Finding a solution

Goal Solutions hobby ( peter, X ), equipment ( X, Y ) X Y hobby ( peter, squash ) equipment ( squash, ball ) equipment ( squash, racket ) equipment squash? hobby ( peter, tennis ) equipment ( tennis, ball ) equipment ( tennis, racket ) equipment tennis? hobby peter? squash squash tennis tennis ball racket ball racket backtracking backtracking

slide-29
SLIDE 29

Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen

TUT Pervasive Computing

29

Solution as a search tree

hobby ( peter, X ), equipment ( X, Y ) X = squash equipment ( squash, Y ) Y = ball Y = racket X = tennis X = ? equipment ( tennis, Y ) Y = ball Y = racket Y = ? X = squash Y = ball X = squash Y = racket X = tennis Y = ball X = tennis Y = racket Y = ?

slide-30
SLIDE 30

Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen

TUT Pervasive Computing

30

Example on computation

speed ( ford, 100 ). speed ( chevy, 105 ). speed ( dodge, 95 ). speed ( volvo, 80 ). time ( ford, 20 ). time ( chevy, 21 ). time ( dodge, 24 ). time ( volvo, 24 ). distance ( X, Y ) :- speed ( X, Speed ), time ( X, Time ), Y is Speed * Time. ?- distance ( chevy, Chevy_Distance ). Chevy_Distance = 2205 Goal: Facts and rules

slide-31
SLIDE 31

Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen

TUT Pervasive Computing

31

Lists

  • List syntax
  • Empty list
  • List head and tail
  • Creating a list
  • Dismantled parts
  • Adding elements

[ apple, orange, pear ] [ ] [ X | Y ] new_list ( [ apple, orange, pear ] ) new_list ( [ Head | Tail ] ) [ Element_1 | List_2 ]

slide-32
SLIDE 32

Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen

TUT Pervasive Computing

32

List operations

  • Finding a list element (member)
  • Constructing a new list by appending two existing

lists one after the other (append)

– no need to care how the appending is done – it is sufficient to describe the properties of the new list append ( [ ], Y, Y ). append ( [ H | X ], Y, [ H | Z ] ) :- append ( X, Y , Z ). member ( M, [ M | _ ] ). member ( M, [ _ | T ] ) :- member ( M, T ).

slide-33
SLIDE 33

Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen

TUT Pervasive Computing

33

Tracing append

(1) Call: append ( [ paul, julia ], [ albert, laura ], _1 )? (2) Call: append ( [ julia ], [ albert, laura ], _2 )? (3) Call: append ( [ ], [ albert, laura ], _3 )? (3) Exit: append ( [ ], [ albert, laura ], [ albert, laura ] ) (2) Exit: append ( [ julia ], [ albert, laura ], [ julia, albert, laura ] ) (1) Exit: append ( [ paul, julia ], [ albert, laura ], [paul, julia, albert, laura ] ) Family = [ paul, julia, albert, laura ] trace. append ( [ paul, julia ], [ albert, laura ], Family ).

append ( [ ], Y, Y ). append ( [ H | X ], Y, [ H | Z ] ) :- append ( X, Y , Z ).

slide-34
SLIDE 34

Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen

TUT Pervasive Computing

34

Example uses of append

?- append ( [ a, b ], [ c, d ], Z ). Z = [ a, b, c, d ] ?- append ( [ a, b ], Y, [ a, b, c, d ] ). Y = [ c, d ] ?- append ( X, [ c, d ], [ a, b, c, d ] ). X = [ a, b ] ?- append ( X, [ d, c ], [ a, b, c, d ] ). no Relations vs. functions

slide-35
SLIDE 35

Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen

TUT Pervasive Computing

35

Order of subgoals (efficiency of backtracking)

  • Goal

– searches for all males in the database and tests if one of them is Sofia’s father – in the worst case, tests every male in the database – backtracking is needed for all unsatisfied goals

  • More efficient goal

– searches for Sofia’s parents in the database and tests if they are male (i.e. father) – most probably there are not more than two parents, and thus, only one backtracking is needed male ( X ), parent ( X, sofia ). parent ( X, sofia ), male ( X ).

slide-36
SLIDE 36

Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen

TUT Pervasive Computing

36

Order of subgoals (infinite computation)

?- X = [ 1, 2, 3 ], member ( a, X ). no ?- member ( a, X ), X = [ 1, 2, 3 ]. [ infinite computation ] X = [ a | _ ] ; can ’a’ be the first element of X? X = [ _, a | _ ] ; can ’a’ be the second element of X? X = [ _, _, a | _ ] ; can ’a’ be the third element of X? ...

Goal: Progress of computation

member ( M, [ M | _ ] ). member ( M, [ _ | T ] ) :- member ( M, T ).

slide-37
SLIDE 37

Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen

TUT Pervasive Computing

37

Shortcomings of logic paradigm

  • Controlling the order of resolution

– order affects efficiency – infinite loops are possible

  • Closed-world assumption

– a goal can be proved to true, but not to false

  • Intrinsic limitatations

– no need to specify in which way to accomplish a task – however, the different ways may be different in efficiency (e.g. sorting)

  • Negation problem (in Prolog)

– Prolog’s not is not a logical negation

ancestor ( X, X ). ancestor ( X, Y ) :- ancestor ( Z, Y ), parent ( X, Z ).

Infinite loop: not ( not ( member ( X, [ a, b, c ] ) ) ).

slide-38
SLIDE 38

Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen

TUT Pervasive Computing

38

Applications of logic programming

  • Relational database management systems

– data put into the database (facts) – relations between database elements (rules) – database queries (goals)

  • Artificial intelligence
  • Expert systems

– inference based on facts and rules

  • Natural language processing

– top-down resolution is similar to parsing based on context-free grammars

  • Teaching