Logic programming • Logic program – A set of logical formulas • Logic programming – Writing formulas, "describing rules of the game" • Execution of a program – Searching for a result that fulfills the rules Principles of programming languages TUT Software Systems 1 Maarit Harsu / Matti Rintala / Henri Hansen
Features of Logic ”The challenge of Imperative programming programming is that all details of computation must be expressed.” • Declarative semantics – Simpler than for imperative languages – Semantics of a proposition can be deduced from the expression • Programmer describes the result, not how it is achieved – e.g., sorting – c.f. Definition of a system sort ( old_list, new_list ) ← permutation ( old_list, new_list ) ∧ sorted ( new_list ) sorted ( list ) ← ∀ j: 1 ≤ j < n, list ( j ) ≤ list ( j + 1 ) Principles of programming languages TUT Software Systems 2 Maarit Harsu / Matti Rintala / Henri Hansen
Curry - functional logic language • "Modern" research language • Combines functional and logic paradigms • Several implementations exist • Syntax (almost) from Haskell • Adds features for logic programming: – Free variables – Non-deterministic functions – Logical constraints, built-in search (with several search strategies) Principles of programming languages TUT Software Systems 3 Maarit Harsu / Matti Rintala / Henri Hansen
New in Curry (vs Haskell) Non-deterministic functions • f x = x f x = x+1 f 3 – Haskell: first match is chosen, 3 returned – Curry: both matches chosen, two execution branches, both 3 and 4 returned • Builtin choice-operator ?: 0 ? 1 returns both 0 and 1 Principles of programming languages TUT Software Systems 4 Maarit Harsu / Matti Rintala / Henri Hansen
New in Curry (vs Haskell) Partial functions • empty [] = [] empty [3] – Haskell: Run-time error – Curry: "No solution" (continue search for other solutions) Principles of programming languages TUT Software Systems 5 Maarit Harsu / Matti Rintala / Henri Hansen
New in Curry (vs Haskell) Constraints • equality =:=, constraint combinator & (and &>), anything returning success • Unlike booleans (==), requires constraint to hold • Can be used to limit function definitions (other major uses too) f x y | x =:= reverse y = x++y function defined only for some lists Principles of programming languages TUT Software Systems 6 Maarit Harsu / Matti Rintala / Henri Hansen
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] Principles of programming languages TUT Software Systems 7 Maarit Harsu / Matti Rintala / Henri Hansen
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 Principles of programming languages TUT Software Systems 8 Maarit Harsu / Matti Rintala / Henri Hansen
Shortcomings of logic paradigm • Controlling the order of resolution – efficiency – Infinite loops possible • Closed-world assumption – A goal can be proven but not disproven • Natural limits – No need to describe computation – However, different solution strategies differ in efficiency Principles of programming languages TUT Software Systems 9 Maarit Harsu / Matti Rintala / Henri Hansen
Applications for logic programming • Relational databases – Input (facts) – Database relations (rules) – Queries • AI • Expert systems – Fact-based deduction • Language processing – top-down –resolution resembles natural language • Teaching Principles of programming languages TUT Software Systems 10 Maarit Harsu / Matti Rintala / Henri Hansen
Recommend
More recommend