Logic programming Logic program A set of logical formulas Logic - - PowerPoint PPT Presentation

logic programming
SMART_READER_LITE
LIVE PREVIEW

Logic programming Logic program A set of logical formulas Logic - - PowerPoint PPT Presentation

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


slide-1
SLIDE 1

1

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

TUT Software Systems

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

slide-2
SLIDE 2

2

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

TUT Software Systems

Features of Logic programming

  • 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 ) ”The challenge of Imperative programming is that all details

  • f computation

must be expressed.”

slide-3
SLIDE 3

3

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

TUT Software Systems

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)

slide-4
SLIDE 4

4

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

TUT Software Systems

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

slide-5
SLIDE 5

5

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

TUT Software Systems

New in Curry (vs Haskell)

Partial functions

  • empty [] = []

empty [3]

– Haskell: Run-time error – Curry: "No solution" (continue search for other solutions)

slide-6
SLIDE 6

6

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

TUT Software Systems

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

slide-7
SLIDE 7

7

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

TUT Software Systems

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]

slide-8
SLIDE 8

8

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

TUT Software Systems

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

slide-9
SLIDE 9

9

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

TUT Software Systems

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

slide-10
SLIDE 10

10

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

TUT Software Systems

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