full logic programming. data structures pure lp allows only to represent relations (=predicates) to...

18
Full Logic Programming

Upload: daisy-gaines

Post on 18-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Full Logic Programming. Data structures Pure LP allows only to represent relations (=predicates) To obtain full LP we will add functors (=function symbols)

Full Logic Programming

Page 2: Full Logic Programming. Data structures Pure LP allows only to represent relations (=predicates) To obtain full LP we will add functors (=function symbols)

Data structures

• Pure LP allows only to represent relations (=predicates)

• To obtain full LP we will add functors (=function symbols)

• This will allow us to represent data structures

Page 3: Full Logic Programming. Data structures Pure LP allows only to represent relations (=predicates) To obtain full LP we will add functors (=function symbols)

Data structures

• Pure LP allows only to represent relations (=predicates)

• To obtain full LP we will add functors (=function symbols)

• This will allow us to represent data structures

• In particular, functors can be nested

Page 4: Full Logic Programming. Data structures Pure LP allows only to represent relations (=predicates) To obtain full LP we will add functors (=function symbols)

Data structures

• Pure LP allows only to represent relations (=predicates)

• To obtain full LP we will add functors (=function symbols)

• This will allow us to represent data structures

• In particular, functors can be nested

Page 5: Full Logic Programming. Data structures Pure LP allows only to represent relations (=predicates) To obtain full LP we will add functors (=function symbols)

Functor examplescons(a,[ ]) describes the list [a]. [ ] is an individual constant, standing for the empty list . The cons functor has a syntactic sugar notation as an inx operator |:•cons(a,[ ]) is written: [a|[ ]].•cons(b,cons(a,[ ])) the list [b,a], or [b|[a|[ ]]]. The syntax [b,a] uses the•printed form of lists in Prolog.•tree(Element,Left,Right) a binary tree, with Element as the root, and Left and Right as its sub-trees.•tree(5,tree(8,void,void),tree(9,void,tree(3,void,void)))

Page 6: Full Logic Programming. Data structures Pure LP allows only to represent relations (=predicates) To obtain full LP we will add functors (=function symbols)

Capturing natural number arithmetics

• 1. Definition of natural numbers:• % Signature: natural_number(N)/1• % Purpose: N is a natural number.• natural_number(0).• natural_number(s(X)) :- natural_number(X).

Page 7: Full Logic Programming. Data structures Pure LP allows only to represent relations (=predicates) To obtain full LP we will add functors (=function symbols)

Addition and substraction

• % Signature: Plus(X,Y,Z)/3• % Purpose: Z is the sum of X and Y.• plus(X, 0, X) :- natural_number(X).• plus(X, s(Y), s(Z)) :- plus(X, Y, Z).• ?- plus(s(0), 0, s(0)). /* checks 1+0=1• Yes.• ?- plus(X, s(0), s(s(0)). /* checks X+1=2, e.g., minus• X=s(0).• ?- plus(X, Y, s(s(0))). /* checks X+Y=2, e.g., all pairs of natural• numbers, whose sum equals 2• X=0, Y=s(s(0));• X=s(0), Y=s(0);• X=s(s(0)), Y=0.

Page 8: Full Logic Programming. Data structures Pure LP allows only to represent relations (=predicates) To obtain full LP we will add functors (=function symbols)

Less or Equal

• % Signature: le(X,Y)/2• % Purpose: X is less or equal Y.• le(0, X) :- natural_number(X).• le(s(X), s(Z)) :- le(X, Z).

Page 9: Full Logic Programming. Data structures Pure LP allows only to represent relations (=predicates) To obtain full LP we will add functors (=function symbols)

Multiplication

• % Signature: Times(X,Y,Z)/2• % Purpose: Z = X*Y• times(0, X, 0) :- natural_number(X).• times(s(X), Y, Z) :- times(X, Y, XY), plus(XY, Y,

Z).

Page 10: Full Logic Programming. Data structures Pure LP allows only to represent relations (=predicates) To obtain full LP we will add functors (=function symbols)

Implications• Pure LP can’t capture unbounded arithmetics.

• It follows that full LP is strictly more expressible than pure LP

– In facts full LP is as expressible as Turing Machines

• Full LP is undecidable– In RE: if there is a proof we will find it, but if not we may fail to

terminate

• Proofs may be of unbounded size since unification may generate new symbols through repeated substitutions of variables x with f(y).

Page 11: Full Logic Programming. Data structures Pure LP allows only to represent relations (=predicates) To obtain full LP we will add functors (=function symbols)

Data Structures% Signature: binary_tree(T)/1% Purpose: T is a binary tree.binary_tree(void).binary_tree(tree(Element,Left,Right)) :-binary_tree(Left),binary_tree(Right).

% Signature: tree_member(X, T)/2% Purpose: X is a member of T.tree_member(X, tree(X, _, _)).tree_member(X, tree(Y,Left, _)):- tree_member(X,Left).tree_member(X, tree(Y, _, Right)):- tree_member(X,Right).

Page 12: Full Logic Programming. Data structures Pure LP allows only to represent relations (=predicates) To obtain full LP we will add functors (=function symbols)

Lists• Syntax:• [ ] is the empty list .• [Head|Tail] is a syntactic sugar for cons(Head, Tail), where Tail is a

list term.• Simple syntax for bounded length lists:• [a|[ ]] = [a]• [a|[ b|[ ]]] = [a,b]• [rina]• [sister_of(rina),moshe|[yossi,reuven]] =

[sister_of(rina),moshe,yossi,reuven]• Defining a list:• list([]). /* defines the basis• list([X|Xs]) :- list(Xs). /* defines the recursion

Page 13: Full Logic Programming. Data structures Pure LP allows only to represent relations (=predicates) To obtain full LP we will add functors (=function symbols)

Lists• Syntax:• [ ] is the empty list .• [Head|Tail] is a syntactic sugar for cons(Head, Tail), where Tail is a

list term.• Simple syntax for bounded length lists:• [a|[ ]] = [a]• [a|[ b|[ ]]] = [a,b]• [rina]• [sister_of(rina),moshe|[yossi,reuven]] =

[sister_of(rina),moshe,yossi,reuven]• Defining a list:• list([]). /* defines the basis• list([X|Xs]) :- list(Xs). /* defines the recursion

Page 14: Full Logic Programming. Data structures Pure LP allows only to represent relations (=predicates) To obtain full LP we will add functors (=function symbols)

List membership:

• % Signature: member(X, List)/2• % Purpose: X is a member of List.• member(X, [X|Xs]).• member(X, [Y|Ys]) :- member(X, Ys)

Page 15: Full Logic Programming. Data structures Pure LP allows only to represent relations (=predicates) To obtain full LP we will add functors (=function symbols)

List membership:

• % Signature: member(X, List)/2• % Purpose: X is a member of List.• member(X, [X|Xs]).• member(X, [Y|Ys]) :- member(X, Ys)

Page 16: Full Logic Programming. Data structures Pure LP allows only to represent relations (=predicates) To obtain full LP we will add functors (=function symbols)

Append

• append([], Xs, Xs).• append([X|Xs], Ys, [X|Zs] ) :- • append(Xs, Ys, Zs).

• ?- append([a,b], [c], X). • ?- append(Xs, [a,d], [b,c,a,d]).• ?- append(Xs, Ys, [a,b,c,d]).

Page 17: Full Logic Programming. Data structures Pure LP allows only to represent relations (=predicates) To obtain full LP we will add functors (=function symbols)

Append

• append([], Xs, Xs).• append([X|Xs], Y, [X|Zs] ) :- append(Xs, Y, Zs).

• ?- append([a,b], [c], X). • ?- append(Xs, [a,d], [b,c,a,d]).• ?- append(Xs, Ys, [a,b,c,d]).

Page 18: Full Logic Programming. Data structures Pure LP allows only to represent relations (=predicates) To obtain full LP we will add functors (=function symbols)

Reverse

reverse([], []).reverse([H|T], R) :- reverse(T, S), append(S, [H], R).

OR

reverse(Xs, Ys):- reverse_help(Xs,[],Ys).reverse_help([X|Xs], Acc, Ys ) :- reverse_help(Xs,[X|Acc],Ys). reverse_help([ ],Ys,Ys ).