Standard ML - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

Standard ML

Description:

triangle(point(-1,0),P2,P3) = triangle(P1,point(1, ... the variables in both terms can be instantiated to objects in ... to represent simple geometric shapes. ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 26
Provided by: davidh129
Category:

less

Transcript and Presenter's Notes

Title: Standard ML


1
Prolog
2
Matching
  • point(A,B) point(1,2)
  • A1 , B2
  • point(A,B) point(X,Y,Z)
  • No!
  • 22 4
  • No!
  • triangle(point(-1,0),P2,P3) triangle(P1,point(1,
    0),point(0,Y))
  • P1point(-1,0) , P2point(1,0), P3point(0,Y)

3
Matching
  • An operation on terms. Two terms match if
  • they are identical, or
  • the variables in both terms can be instantiated
    to objects in such a way that after the
    substitution of variables by these objects the
    terms become identical.
  • date(D,M,1995) matches date(D1,may,Y1)
  • date(D,M,1995) doesnt match date(D1,M1,1996)
  • date(D) doesnt match day(D)
  • If matching succeeds it always results in the
    most general instantiation possible.
  • date(D,M,1995) date(D1,may,Y1).D D1
    MmayY11995

4
General rules for matching two terms S and T
  • (1) If S and T are constants then S and T match
    only if they are the same object.
  • (2) If S is a variable and T is anything, then
    they match, and S is instantiated to T. (or the
    other way around...)
  • (3) If S and T are structures then they match
    only if
  • (a) S and T have the same principal functor
    and the same number of components, and
  • (b) all their corresponding components
    match.
  • The resulting instantiation is determined
    by the matching of the components.

5
An Illustration
  • Use structures to represent simple geometric
    shapes.
  • point - two numbers representing X and Y
    coordinates.
  • seg - a line defined by two points
  • triangle - defined by three points.
  • point(1,1)
  • seg( point(1,1), point(2,3) )
  • triangle( point(4,2), point(6,4), point(7,1) )
  • In the same program we can also use three
    dimensional points
  • point(1,3,5)
  • This will result in a different relation with the
    same name.
  • Match triangle(point(1,1), A,
    point(2,3)) triangle(X,
    point(4,Y),point(2,Z)).

6
  • match by
  • triangle triangle
  • point(1,1) X
  • A point(4,Y)
  • point(2,3) point(2,Z)
  • The resulting instantiation is
  • X point(1,1)
  • A point(4,Y)
  • Z 3

7
Matching as means of Computation
  • A program with two facts
  • vertical( seg( point(X,Y), point(X, Y1) )
    ).horizontal( seg( point(X,Y), point(X1,Y) ) ).
  • Conversation
  • ?- vertical( seg( point(1,1), point(1,2) )).yes
  • ?- vertical( seg( point(1,1), point(2,Y) )).no
  • ?- vertical( seg( point(2,3), P)).P point(2,Y)
  • When prolog has to invent a variable name (like
    the Y above) it will be in the form _n where n is
    an arbitrary number.

8
Regular Rectangle
  • Both vertical and Horizontal, i.e.,
  • regular(rectangle(point(X1,Y1),point(X1,Y2),
    point(X2,Y2),point(X2,Y1)).
  • regular(rectangle(point(X1,Y2),point(X2,Y2),
    point(X2,Y1),point(X1,Y1)).

(x1,y2)
(x2,y2)
(x2,y1)
(x1,y1)
9
List 1
  • Write a goal, using append, to delete the last
    three elements from a list L producing another
    list L1.
  • Hint L is the concatenation of L1 and a
    three-element list.
  • del3(L,L1) - append(L1,_,_,_,L).

10
List 2
  • Write a goal to delete the first three elements
    and the last three elements from a list L
    producing list L2.
  • del_3_3(L,L2) - del3(L,L1), _,_,_L2L1.
  • del_3_3(L,L2) - del3(L,_,_,_L2).
  • del_3_3(_,_,_L4,L2) - del3(L4,L2).

11
List 3
  • Using append, define the relation last(Item,List)
    to test if Item is the last element of a list
    List.
  • last(Item,List)- append(_,Item,List).
  • The same without using append.
  • last(Item,Item).
  • last(Item,_L)- last(Item,L).

12
List 4
  • Define two predicates evenlength(L) and
    oddlength(L) so that they are true if their
    argument is a list of even or odd length
    respectively.
  • evenlength().
  • oddlength(_L)-evenlength(L).
  • evenlength(_L)- oddlength(L).

13
(No Transcript)
14
Arithmetics
  • Predefined operators for basic arithmetic
  • , -, , /, mod
  • This is a special case in which an operator may
    invoke an operation. It will be done only if it
    is explicitly indicated.
  • Example
  • X 1 2.X12
  • The predefined operator is forces evaluation.
  • X is 1 2.X3
  • The mod operator associates to the right.All the
    others associates to the left.
  • The comparison operators also force evaluation.
  • 145 34 gt 100.Yes

15
  • The comparison operators
  • X gt Y X is greater than Y.
  • X lt Y X is less than Y.
  • X gt Y X is greater than or equal to Y.
  • X lt Y X is less than or equal to Y.
  • X Y the values of X and Y are equal.
  • X \ Y the values of X and Y are not equal.

16
and
  • X Y causes the matching of X and Y and possibly
    instantiation of variables.
  • X Y causes an arithmetic evaluation of X and
    Y, and cannot cause any instantiation of
    variables.
  • 1 2 2 1.
  • gt yes
  • 1 2 2 1.
  • gt no
  • 1 A B 2.
  • gt A 2
  • gt B 1
  • 1 A B 2.
  • gt WARNING Unbound variable in arithmetic
    expression
  • gt Fail ( 6) 1 _G149 _G151 2 ?
  • gt No

17
Example The Greatest Common Devisor
  • given X and Y, the gcd D can be found by
  • (1) If X and Y are equal then D is equal to
    X.(2) If X lt Y then D is equal to the gcd of X
    and (Y-X).(3) If Y lt X then do the same as in
    (2) with X and Y interchanged.
  • gcd(X,X,X).
  • gcd(X,Y,D) -
  • XltY,
  • Y1 is Y - X,
  • gcd(X,Y1,D).
  • gcd(X,Y,D) -
  • Y lt X,
  • gcd(Y,X,D).

18
(No Transcript)
19
Lists
  • A sequence of any number of items.
  • Structure of lists .( Head, Tail ) .(a,
    .(b, )) eq.
  • Shorthand
  • tom, jerry is the same as .(tom, .(jerry,
    ))
  • a tail is the same as .(a, tail)
  • a,b,c a b,c a,b c a,b,c
  • Elements can be lists and structures
  • a, 1, 2, 3, tom, 1995, date(1,may,1995)

20
Operations on Lists
  • Membership
  • member( X, L) if X is a member of the list L.
  • member(X, X Tail).
  • member(X, Head Tail) -
  • member(X, Tail).
  • Concatenation
  • conc(L1, L2, L3) if L3 is the concatenation of L1
    and L2.
  • conc(, L, L).
  • conc(XL1, L2, XL3) -
  • conc(L1, L2, L3).

21
  • conc( a,b,c, 1,2,3, L).
  • gt L a,b,c,1,2,3
  • conc( L1, L2, a,b,c ).
  • gt L1
  • L2 a,b,c
  • gt L1 a
  • L2 b,c
  • gt L1 a,b
  • L2 c
  • gt L1 a,b,c
  • L2
  • gt no
  • conc( Before, 4After, 1,2,3,4,5,6,7).
  • gt Before 1,2,3
  • After 5,6,7
  • conc(_, Pred, 4, Succ _, 1,2,3,4,5,6,7).
  • gt Pred 3
  • Succ 5

22
  • Redefining member using conc
  • member1(X, L) - conc(_, X_, L).
  • Adding an Item in the front
  • add(X, L, XL).
  • Deleting an item
  • del(X, XTail, Tail).del(X, YTail,
    YTail1) - del(X, Tail, Tail1).
  • If there are several occurrences of X in the list
    then del will be able to delete only one of them.
  • To insert an item at any place in the
    listdel(a, L, 1,2,3).gt L a,1,2,3gt L
    1,a,2,3gt L 1,2,a,3gt L 1,2,3,agt no

23
  • We can define insert using del
  • insert(X,List,BiggerList) - del(X,
    BiggerList, List).
  • The sublist relation
  • sublist(S, L) - conc(L1, L2, L), conc(S,
    L3, L2).
  • sublist(S, a,b,c).gt S gt S a...gt S
    b,c ...

24
  • Permutations
  • permutation(, ).permutation(XL, P) -
    permutation(L, L1), insert(X, L1, P).
  • permutation( a,b,c, P).gt P a,b,cgt P
    a,c,bgt P b,a,c...
  • permutation2(, ).permutation2(L, XP) -
    del(X, L, L1), permutation2(L1, P).

25
Length
  • The length of a list can be calculated in the
    following way
  • if the list is empty then its length is 0.
  • if the list is not empty then List Head
    Tail. In this case the length is equal to 1 plus
    the length of the tail Tail.
  • length is built in. If you want to try defining
    it, change the name...
  • length(, 0).length(_Tail,N) -
    length(Tail, N1), N is 1 N1.
  • length(a,b,c,d,e, N).gt N 4length(L,4).gt
    L _5, _10, _15, _20 ..... ?

what happens if the order of these clauses is
changed?
Write a Comment
User Comments (0)
About PowerShow.com