programming and using eclipse. 2 overview what is eclipse? crash course logic programming the...

38
Programming and Using ECLiPSe

Upload: rosalyn-dorsey

Post on 18-Dec-2015

219 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Programming and Using ECLiPSe. 2 Overview  What is ECLiPSe?  Crash course Logic Programming  The Programming Environment

Programming and Using ECLiPSe

Page 2: Programming and Using ECLiPSe. 2 Overview  What is ECLiPSe?  Crash course Logic Programming  The Programming Environment

3

Execution Basics

Execution can be regarded as a search through a tree-shaped search-space.

Attempts to find solution(s) to query, given the program (rules, facts, constraints)

Execution can be regarded as a search through a tree-shaped search-space.

Attempts to find solution(s) to query, given the program (rules, facts, constraints)

Page 3: Programming and Using ECLiPSe. 2 Overview  What is ECLiPSe?  Crash course Logic Programming  The Programming Environment

4

Basic Syntax

Terms are basic “building blocks”. Can be: atomic variables structures list (specialised structure)

Terms are used to build programs (facts, rules, constraints are all terms syntactically)

Page 4: Programming and Using ECLiPSe. 2 Overview  What is ECLiPSe?  Crash course Logic Programming  The Programming Environment

5

Terms: Atomic Types

Atomic terms: “constants”

atomsstart with lower-case letter, or wrapped in single quotes

e.g. atom a3 ’Atom’ ’an atom’ -*- ???

stringswrapped in double quotes

e.g. "purple" "a string" ""

numbers: integers e.g. 1 9999 -23 643463461293469floats e.g. 3.1416 -1.0e5 1.0Infrationals e.g. 1_3 22_7bounded reals e.g. 3.14159__3.14160 -1.01__-0.99

Page 5: Programming and Using ECLiPSe. 2 Overview  What is ECLiPSe?  Crash course Logic Programming  The Programming Environment

6

Terms: Structures

Characterised by name and arity (number of arguments) name/arity is called functor Arguments are again arbitrary terms (untyped) Canonical syntax

colour(purple)

neighbour(france, germany)

s(s(s(0)))

Operator syntaxSome structures can be written in alternative syntax (if declared)

e.g. infix 1 + 2 is exactly the same as +(1, 2)

Characterised by name and arity (number of arguments) name/arity is called functor Arguments are again arbitrary terms (untyped) Canonical syntax

colour(purple)

neighbour(france, germany)

s(s(s(0)))

Operator syntaxSome structures can be written in alternative syntax (if declared)

e.g. infix 1 + 2 is exactly the same as +(1, 2)

Page 6: Programming and Using ECLiPSe. 2 Overview  What is ECLiPSe?  Crash course Logic Programming  The Programming Environment

7

Terms: Lists

Specialised structures, with special syntaxe.g. [1, 2, 3] [] (empty list)

List consists of a head and a tailHead is the first element of a list

Tail is the rest of the list (also a list)

Can be written [Head | Tail]

These are equivalent:[1, 2, 3] [1 | [2, 3]] [1 | [2 | [3 | []]]]

Specialised structures, with special syntaxe.g. [1, 2, 3] [] (empty list)

List consists of a head and a tailHead is the first element of a list

Tail is the rest of the list (also a list)

Can be written [Head | Tail]

These are equivalent:[1, 2, 3] [1 | [2, 3]] [1 | [2 | [3 | []]]]

Page 7: Programming and Using ECLiPSe. 2 Overview  What is ECLiPSe?  Crash course Logic Programming  The Programming Environment

8

Logical Variables

Placeholders for valuesFree/instantiated

Not memory locationsNot mutable, single assignment

Names start with a capital lettere.g. Colour Countries

Scope is the term: p(X,X). q(X).

Anonymous variables written as _Each _ is a different variable!

Page 8: Programming and Using ECLiPSe. 2 Overview  What is ECLiPSe?  Crash course Logic Programming  The Programming Environment

9

Predicates, Goals and Queries

Built-in/library predicates have predefined meaninge.g. integer(X) is true iff X is an integer

A goal is a formula whose truth we want to knowinteger(99)

integer(hello)

A query is the top-level goal?- integer(99).

Yes.

?- integer(hello).

No.

Page 9: Programming and Using ECLiPSe. 2 Overview  What is ECLiPSe?  Crash course Logic Programming  The Programming Environment

10

Matching

?- 3 = 3.

Yes.

?- 3 = 4.

No.

?- hello = 3.

No.

?- foo(a,2) = foo(a,2).

Yes.

?- foo(a,2) = foo(b,2).

No.

?- foo(a,2) = foo(a,2,c).

No.

?- foo(3,4) = 7.

13

No.

?- +(3,4) = 7.

No.

?- 3 + 4 = 7.

No.

?- foo(a,2) = foo(a,2,c).

No.

?- foo(3,4) = 7.

13

No.

?- +(3,4) = 7.

No.

?- 3 + 4 = 7.

No.

Page 10: Programming and Using ECLiPSe. 2 Overview  What is ECLiPSe?  Crash course Logic Programming  The Programming Environment

11

Unification

Unification = matching after variable substitution

?- X = 3.

X = 3

Yes.

?- 3 = X.

X = 3

Yes.

?- X = 3, Y = 4.

X = 3

Y = 4

Yes.

?- X = 3, X = 4.

No.

?- X = 3.

X = 3

Yes.

?- 3 = X.

X = 3

Yes.

?- X = 3, Y = 4.

X = 3

Y = 4

Yes.

?- X = 3, X = 4.

No.

?- X = foo(a,2).

X = foo(a,2)

Yes.

?- foo(X,Y) = foo(a,2).

X = a

Y = 2

Yes.

?- foo(X,2) = foo(a,Y).

X = a

Y = 2

Yes.

?- foo(X,X) = foo(a,2).

No.

?- X = foo(a,2).

X = foo(a,2)

Yes.

?- foo(X,Y) = foo(a,2).

X = a

Y = 2

Yes.

?- foo(X,2) = foo(a,Y).

X = a

Y = 2

Yes.

?- foo(X,X) = foo(a,2).

No.

Page 11: Programming and Using ECLiPSe. 2 Overview  What is ECLiPSe?  Crash course Logic Programming  The Programming Environment

12

Unification examples

Will these succeed or fail??- point(X, Y) = point(A, 1).Yes: X = A, Y = 1

?- point(X, Y, Z) = point(1, 2).No

?- 1 + 3 = 4.No

?- Apples = Oranges.Yes: Apples = Oranges

?- 'Atom' = "Atom".No

Will these succeed or fail??- point(X, Y) = point(A, 1).Yes: X = A, Y = 1

?- point(X, Y, Z) = point(1, 2).No

?- 1 + 3 = 4.No

?- Apples = Oranges.Yes: Apples = Oranges

?- 'Atom' = "Atom".No

Page 12: Programming and Using ECLiPSe. 2 Overview  What is ECLiPSe?  Crash course Logic Programming  The Programming Environment

13

Unification with Domain Variables

Requires solving simple equality constraints:

?- X::1..5, Y::3..7, X = Y.

X = X{3..5}

Y = X{3..5}

Yes.

?- X::1..5, foo(X) = foo(9).

No.

?- X::1..5, Y::3..7, foo(X,Y) = foo(Z,Z).

X = X{3..5}

Y = X{3..5}

Yes.

Page 13: Programming and Using ECLiPSe. 2 Overview  What is ECLiPSe?  Crash course Logic Programming  The Programming Environment

14

Program = Predicate definitions

Predicate definitions consist of clauses that define what’s true

Facts:

brother(fred, jane).

male(fred).

Rules:

uncle(Uncle, Nephew) :- brother(Uncle, Parent), parent(Parent, Nephew).

means:

Uncle Nephew Parent brother(Uncle, Parent) parent(Parent, Nephew)

Directives:

:- use_module(library(ic)).

Predicate definitions consist of clauses that define what’s true

Facts:

brother(fred, jane).

male(fred).

Rules:

uncle(Uncle, Nephew) :- brother(Uncle, Parent), parent(Parent, Nephew).

means:

Uncle Nephew Parent brother(Uncle, Parent) parent(Parent, Nephew)

Directives:

:- use_module(library(ic)).

‘fullstop’

Page 14: Programming and Using ECLiPSe. 2 Overview  What is ECLiPSe?  Crash course Logic Programming  The Programming Environment

15

Conjunction and Disjunction

Conjunction is written as comma:

uncle(U, N) :-

brother(U, P), parent(P, N).

Operational semantics: left-to-right execution

Disjunction either as alternative clauses:atomic_particle(proton).atomic_particle(neutron).atomic_particle(electron).

or explicitly as semicolon:atomic_particle(X) :-

( X = proton ; X = neutron ; X = electron ).

Operational semantics: left-to-right try and backtrack

Page 15: Programming and Using ECLiPSe. 2 Overview  What is ECLiPSe?  Crash course Logic Programming  The Programming Environment

16

Recursion

Defining rule with recursion ancestor(X, Y) :- parent(X, Y).

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

ancestor(X, Z).

Tip for programming recursion: assume predicate already defined when writing the recursive case

Page 16: Programming and Using ECLiPSe. 2 Overview  What is ECLiPSe?  Crash course Logic Programming  The Programming Environment

17

Execution model

Keep a “resolvent” of unsolved subgoalsStart with initial query

Stop when resolvent empty

Process subgoals from left-to-rightSimplify subgoals by applying predicate definitions

Accumulate variable substitutions in the process

If a contradiction occurs, backtrackUndo everything since the most recent alternative

Try the next alternative left-to-right

Page 17: Programming and Using ECLiPSe. 2 Overview  What is ECLiPSe?  Crash course Logic Programming  The Programming Environment

18

Basic Execution

parent(abe, herbert).

ancestor(Ancestor, Descendant) :-

parent(Ancestor, Descendant).

?- ancestor(abe, bart).

ancestor(Ancestor, Descendant) :-

parent(Ancestor, Child),

ancestor(Child, Descendant).

parent(abe, homer).

parent(homer, bart).

parent(marge, bart).

ancestor(Ancestor, Descendant) :-

parent(Ancestor, Descendant).

Try clause 1

ancestor(Ancestor, Descendant) :-

parent(Ancestor, Descendant). parent(abe, herbert).

parent(abe, homer).

parent(homer, bart).

parent(marge, bart).

Ancestor1 = abe, Descendant1 = bart

?- parent(abe, bart).

parent(abe, herbert).

parent(abe, homer).

parent(homer, bart).

parent(marge, bart).

Try clause 2 Ancestor1 = abe, Descendant1 = bart

?- parent(abe, Child1), ancestor(Child1, bart).

ancestor(Ancestor, Descendant) :-

parent(Ancestor, Child),

ancestor(Child, Descendant).

ancestor(Ancestor, Descendant) :-

parent(Ancestor, Child),

ancestor(Child, Descendant).

Try clause 1 Child1 = homer

?- ancestor(homer, bart).

parent(abe, homer).parent(abe, homer).

Try clause 1

ancestor(Ancestor, Descendant) :-

parent(Ancestor, Descendant).

ancestor(Ancestor, Descendant) :-

parent(Ancestor, Descendant).

Ancestor2 = homer, Descendant2 = bart

?- parent(homer, bart).Try clause 3

parent(homer, bart).

Success!

Page 18: Programming and Using ECLiPSe. 2 Overview  What is ECLiPSe?  Crash course Logic Programming  The Programming Environment

19

If-then-else construct

( Condition -> Then ; Else ) If Condition succeeds, any choices made in it are committed to and

Then is executed If Condition failed, Else is executed

Examplemax(X, Y, Max) :-

( X > Y ->

Max = X

;

Max = Y

).

Use with simple tests only!

Page 19: Programming and Using ECLiPSe. 2 Overview  What is ECLiPSe?  Crash course Logic Programming  The Programming Environment

20

Some built-ins for list processing

member(Term, List)

length(List, N)

append(List1, List2, AppendedList)

reverse(List, Reversed)

sort(List, Sorted)

flatten(NestedList, FlatList)?- flatten([[1, 2, 3], 2, [], [[3], 4], 5], L).L = [1, 2, 3, 2, 3, 4, 5]Yes

member(Term, List)

length(List, N)

append(List1, List2, AppendedList)

reverse(List, Reversed)

sort(List, Sorted)

flatten(NestedList, FlatList)?- flatten([[1, 2, 3], 2, [], [[3], 4], 5], L).L = [1, 2, 3, 2, 3, 4, 5]Yes

Page 20: Programming and Using ECLiPSe. 2 Overview  What is ECLiPSe?  Crash course Logic Programming  The Programming Environment

22

All solutions predicates: findall/3

findall(?Term, +Goal, ?List)Finds all solutions of Goal, and creates a list List with an instance of Term for each solution

Example:Given:

neighbour(1,2).

neighbour(2,3).

neighbour(3,1).

?- findall(X-Y, neighbour(X,Y), Neighbours).

Neighbours = [1 - 2, 2 - 3, 3 - 1]

Yes

Page 21: Programming and Using ECLiPSe. 2 Overview  What is ECLiPSe?  Crash course Logic Programming  The Programming Environment

25

Libraries

libraries extends functionality of ECLiPSe (utilities, solvers….)

need to be loaded in to ECLiPSe: :- lib(<Name>) at the start of a program, or

lib(<Name>) at top-level prompt

Can sometimes avoid coding by using libraries - check documentations!

Interval Constraints library: important solverlib(ic)

Page 22: Programming and Using ECLiPSe. 2 Overview  What is ECLiPSe?  Crash course Logic Programming  The Programming Environment

26

Programming Issues (I)

Programming = Logic + ControlStill programming. Need to think about programming issues

A good specification may not be a good program, e.g.sort(Ls, Ss) :- /* BAD */

permute(Ls, Ss), ordered(Ss).

Even the constraints version is extremely inefficient - make use of property

of problem if available - many sorting algorithms available! In fact, for sorting, should use built-in sorts (efficient as implemented in low

level). Often easiest method to find max/min of a list in ECLiPSe

Page 23: Programming and Using ECLiPSe. 2 Overview  What is ECLiPSe?  Crash course Logic Programming  The Programming Environment

28

Programming Issues (II)

What are the problems with this code? split(Ls, Pivot, Bigs, Smalls) :- /* BAD */ findall(L1, (member(L1, Ls), L1 >= Pivot), Bigs),

findall(L2, (member(L2, Ls), L2 < Pivot), Smalls).

Misuse of findall/3 - should not use all solutions predicate to

traverse list

List traversed twice – redundant computation

Better code:split([], _, [], []).split([L|Ls], Pivot, Bigs, Smalls) :- (L >= Pivot -> Bigs = [L|Bigs0], Smalls = Smalls0 ; Bigs = Bigs0, Smalls = [L|Smalls0] ),

split(Ls, Pivot, Bigs0, Smalls0).

Page 24: Programming and Using ECLiPSe. 2 Overview  What is ECLiPSe?  Crash course Logic Programming  The Programming Environment

29

Generate-and-test

coloured([A,B,C,D]) :-

colour(A),

colour(B),

colour(C),

colour(D),

A \= B,

A \= C,

A \= D,

B \= C,

B \= D,

C \= D.

coloured([A,B,C,D]) :-

colour(A),

colour(B),

colour(C),

colour(D),

A \= B,

A \= C,

A \= D,

B \= C,

B \= D,

C \= D.

?- coloured([A,B,C,D]).

colour(red).

colour(green).

colour(blue).

colour(yellow).

?- coloured([A,B,C,D]).

colour(red).

colour(green).

colour(blue).

colour(yellow).

A B

C

D

A B

C

D

coloured([A,B,C,D]) :-

colour(A),

colour(B),

colour(C),

colour(D),

A \= B,

A \= C,

A \= D,

B \= C,

B \= D,

C \= D.

coloured([A,B,C,D]) :-

colour(A),

colour(B),

colour(C),

colour(D),

A \= B,

A \= C,

A \= D,

B \= C,

B \= D,

C \= D.

coloured([A,B,C,D]) :-

colour(A),

colour(B),

colour(C),

colour(D),

A \= B,

A \= C,

A \= D,

B \= C,

B \= D,

C \= D.

coloured([A,B,C,D]) :-

colour(A),

colour(B),

colour(C),

colour(D),

A \= B,

A \= C,

A \= D,

B \= C,

B \= D,

C \= D.

coloured([A,B,C,D]) :-

colour(A),

colour(B),

colour(C),

colour(D),

A \= B,

A \= C,

A \= D,

B \= C,

B \= D,

C \= D.

coloured([A,B,C,D]) :-

colour(A),

colour(B),

colour(C),

colour(D),

A \= B,

A \= C,

A \= D,

B \= C,

B \= D,

C \= D.

Page 25: Programming and Using ECLiPSe. 2 Overview  What is ECLiPSe?  Crash course Logic Programming  The Programming Environment

30

Extension for Constraint Solving

Some subgoals are viewed as “constraints”Not handled by the general Prolog-mechanism

Either data-driven propagation behaviourDemons delay/resume and propagate information

Or passing to a specialised solverResults get reintegrated

Constrains effectively stay in the resolventSatisfiability can usually only be decided later

Backtracking mechanism is unaffectedStill acts as a “time machine” to reset to an earlier state

Page 26: Programming and Using ECLiPSe. 2 Overview  What is ECLiPSe?  Crash course Logic Programming  The Programming Environment

31

Constrain-and-search

coloured([A,B,C,D]) :-

A ~= B,

A ~= C,

A ~= D,

B ~= C,

B ~= D,

C ~= D,

colour(A),

colour(B),

colour(C),

colour(D).

coloured([A,B,C,D]) :-

A ~= B,

A ~= C,

A ~= D,

B ~= C,

B ~= D,

C ~= D,

colour(A),

colour(B),

colour(C),

colour(D).

?- coloured([A,B,C,D]).

colour(red).

colour(green).

colour(blue).

colour(yellow).

?- coloured([A,B,C,D]).

colour(red).

colour(green).

colour(blue).

colour(yellow).

A B

C

D

A B

C

D

coloured([A,B,C,D]) :-

A ~= B,

A ~= C,

A ~= D,

B ~= C,

B ~= D,

C ~= D,

colour(A),

colour(B),

colour(C),

colour(D).

coloured([A,B,C,D]) :-

A ~= B,

A ~= C,

A ~= D,

B ~= C,

B ~= D,

C ~= D,

colour(A),

colour(B),

colour(C),

colour(D).

Page 27: Programming and Using ECLiPSe. 2 Overview  What is ECLiPSe?  Crash course Logic Programming  The Programming Environment

32

Resolvent in ECLiPSe

q1, …, qm,

schedule(wake)

p1 , … , pn . Delayed Goals

suspend (delay)

s1

s4sl

s2 s3

r1, …, rk,

Prio 1 Prio 2 … Prio 12

Page 28: Programming and Using ECLiPSe. 2 Overview  What is ECLiPSe?  Crash course Logic Programming  The Programming Environment

34

Overview

What is ECLiPSe? Crash course Logic Programming The Programming Environment

Page 29: Programming and Using ECLiPSe. 2 Overview  What is ECLiPSe?  Crash course Logic Programming  The Programming Environment

35

Warm-up Exercise

How to access help How to run a query How to write and compile a program How to use the tracer and inspector

Page 30: Programming and Using ECLiPSe. 2 Overview  What is ECLiPSe?  Crash course Logic Programming  The Programming Environment

36

ECLiPSe Documentation

Tutorial Introduction

Application Development Manual

User Manual

Constraint Library Manual

Embedding and Interfacing Manual

Visualisation Tools ManualHtml and/or Pdf

Reference ManualHtml and plain text (help), details every predicate

ExamplesWeb site

Page 31: Programming and Using ECLiPSe. 2 Overview  What is ECLiPSe?  Crash course Logic Programming  The Programming Environment

37

TkECLiPSe

Development GUI for ECLiPSe Replaces traditional ‘command line’ interface A toplevel plus a suite of development tools Help available:

Popup balloons Help menu for tkeclipse HTML/PDF documentation for all of ECLiPSe

Page 32: Programming and Using ECLiPSe. 2 Overview  What is ECLiPSe?  Crash course Logic Programming  The Programming Environment

38

Toplevel

Query entry (with history mechanisms) Results window Output window (normal, error, warning, old) Buttons: run, more, make, interrupt Menus:

File: compilation Run: run query in different modes Tools: development tools Help

Page 33: Programming and Using ECLiPSe. 2 Overview  What is ECLiPSe?  Crash course Logic Programming  The Programming Environment

39

Compiling

Short bits of code: Tools -> Compile Scratch Pad

Larger programs: Use separate editor to create xxx.ecl file File -> Compile … Use make-button to recompile after changes

Page 34: Programming and Using ECLiPSe. 2 Overview  What is ECLiPSe?  Crash course Logic Programming  The Programming Environment

40

Tracer (debugger)

Call stack: current subgoal + ancestorsRight button for popup menu

Double click for inspector

Trace log window Debug command buttons

Where to stop after continuing execution

Tools in MenuPrint options: change the display of goals/terms

Filter: flexible conditional breakpoints

Analyze failure: find reason for failure or abort

Page 35: Programming and Using ECLiPSe. 2 Overview  What is ECLiPSe?  Crash course Logic Programming  The Programming Environment

41

Reminder: Box Model

CALL EXIT

FAIL

LEAVE

NEXT

ELSE

REDO

*EXITRESUMEDELAY

p(X,Y) :- ...

p(X,Y) :- …

p(X,Y) :- …

Page 36: Programming and Using ECLiPSe. 2 Overview  What is ECLiPSe?  Crash course Logic Programming  The Programming Environment

43

Inspector

Tree display for structured terms Double click to expand/collapse term Right click for popup menu Useful for debugging

Page 37: Programming and Using ECLiPSe. 2 Overview  What is ECLiPSe?  Crash course Logic Programming  The Programming Environment

44

Delayed goals viewer

Show current suspended goals (unsolved constraints) Woken goals are green Double click to inspect goal with inspector Right click for popup menu on goal Different filter conditions

Page 38: Programming and Using ECLiPSe. 2 Overview  What is ECLiPSe?  Crash course Logic Programming  The Programming Environment

45

Warm-up Exercise

Write and test a predicate that detects palindromes:?- palin([k,a,y,a,k]).Yes.?- palin([a,b,b,a]).Yes.?- palin([f,o,o]).No.?- palin(P).P = []MoreP = [_203]MoreP = [_203, _203]More…