logic and knowledge representation

Logic and Knowledge Representation P r o g r a m m i n g - PowerPoint PPT Presentation

Logic and Knowledge Representation P r o g r a m m i n g a s p r o b l e m - s o l v i n g , F i r s t s t e p s i n P r o l o g 2 0 A p r i l 2 0 1 8 g s i l e n o @e n s t . f


  1. An ancient puzzle ~ 9th century ● O n c e u p o n a t i m e a f a r m e r w e n t t o m a r k e t a n d p u r c h a s e d a , a , a n d a . O n h i s f o x g o o s e b a g o f b e a n s w a y h o m e , t h e f a r m e r c a m e t o t h e b a n k o f a r i v e r . I n c r o s s i n g t h e r i v e r b y b o a t , t h e f a r m e r c o u l d c a r r y o n l y h i m s e l f a n d a s i n g l e o n e o f h i s p u r c h a s e s - t h e f o x , t h e g o o s e , o r t h e b a g o f t h e b e a n s .

  2. An ancient puzzle ~ 9th century ● O n c e u p o n a t i m e a f a r m e r w e n t t o m a r k e t a n d p u r c h a s e d a , a , a n d a . O n h i s f o x g o o s e b a g o f b e a n s w a y h o m e , t h e f a r m e r c a m e t o t h e b a n k o f a r i v e r . I n c r o s s i n g t h e r i v e r b y b o a t , t h e f a r m e r c o u l d c a r r y o n l y h i m s e l f a n d a s i n g l e o n e o f h i s p u r c h a s e s - t h e f o x , t h e g o o s e , o r t h e b a g o f t h e b e a n s .

  3. An ancient puzzle ~ 9th century ● O n c e u p o n a t i m e a f a r m e r w e n t t o m a r k e t a n d p u r c h a s e d a , a , a n d a . O n h i s f o x g o o s e b a g o f b e a n s w a y h o m e , t h e f a r m e r c a m e t o t h e b a n k o f a r i v e r . I n c r o s s i n g t h e r i v e r b y b o a t , t h e f a r m e r c o u l d c a r r y o n l y h i m s e l f a n d a s i n g l e o n e o f h i s p u r c h a s e s - t h e f o x , t h e g o o s e , o r t h e b a g o f t h e b e a n s .

  4. An ancient puzzle ~ 9th century ● I f l e f t t o g e t h e r , t h e f o x w o u l d e a t t h e g o o s e , o r t h e g o o s e w o u l d e a t t h e b e a n s . ● T h e f a r m e r ' s c h a l l e n g e w a s t o c a r r y h i m s e l f a n d h i s p u r c h a s e s t o t h e f a r b a n k o f t h e r i v e r , l e a v i n g e a c h p u r c h a s e i n t a c t . H o w d i d h e d o i t ?

  5. An ancient puzzle ~ 9th century ● Wh a t i s t h e g o a l ? ● Wh a t i s t h e i n i t i a l s i t u a t i o n ? ● Wh i c h a r e t h e r e s o u r c e s / c o n s t r a i n t s ?

  6. From problems to solvers P r o b l e m P r o b l e m A n a l y s i s A n a l y s i s P r o b l e m s o l v i n g m e t h o d A l g o r i t h m P l a n n i n g P r o g r a m m i n g P r o b l e m s o l v i n g t a s k P r o g r a m E x e c u t i o n E x e c u t i o n S o l u t i o n O u t c o m e

  7. From problems to solvers P r o b l e m P r o b l e m A n a l y s i s A n a l y s i s P r o b l e m s o l v i n g m e t h o d A l g o r i t h m P l a n n i n g P r o g r a m m i n g P r o b l e m s o l v i n g t a s k P r o g r a m E x e c u t i o n E x e c u t i o n S o l u t i o n O u t c o m e T h e “ r e a l ” p r o b l e m i s n o t p r o g r a m m i n g b u t fj n d i n g t h e p a t h t o w a r d t h e s o l u t i o n .

  8. Imperative vs Declarative

  9. Imperative vs Declarative ● I m p e r a t i v e : – p r o g r a m m i n g f o c u s i n g o n t h e s e q u e n c e o f n e c e s s a r y t o s o l v e t h e p r o b l e m ( w h i c h i n o p e r a t i o n s t u r n u s u a l l y s t a y s i m p l i c i t ) ● D e c l a r a t i v e – p r o g r a m m i n g f o c u s i n g o n d e s c r i b i n g t h e p r o b l e m ( w h i l e t h e s e q u e n c e o f o p e r a t i o n s t o b e p e r f o r m e d i s l e f t i m p l i c i t )

  10. Imperative programming F o c u s : h o w t o c o m p u t e B a s e d o n i n s t r u c t i o n s , c o r r e s p o n d e n t t o a c t i o n s t o t h e m a c h i n e . c o m m a n d e d – I t a s s u m e s t h a t t h e c o m p u t e r c a n m a i n t a i n t h e c h a n g e s ( t h e s i d e - e fg e c t s ) c a u s e d b y t h e c o m p u t a t i o n p r o c e s s .

  11. Imperative programming F o c u s : h o w t o c o m p u t e ● M o s t p o p u l a r p r o g r a m m i n g l a n g u a g e s i m p l e m e n t t h e i m p e r a t i v e p a r a d i g m : – i t m o s t c l o s e l y r e s e m b l e s t h e a c t u a l m a c h i n e i t s e l f , s o t h e p r o g r a m m e r t h i n k s i n a m u c h c l o s e r w a y t o t h e m a c h i n e ; – b e c a u s e o f s u c h c l o s e n e s s , i t w a s u n t i l r e c e n t l y t h e o n l y o n e e ffj c i e n t e n o u g h f o r w i d e s p r e a d u s e .

  12. Imperative programming ● A d v a n t a g e s – e ffj c i e n t a s c l o s e t o t h e m a c h i n e – p o p u l a r – f a m i l i a r ● D i s a d v a n t a g e s – a p r o g r a m c a n b e c o m p l e x t o u n d e r s t a n d , b e c a u s e t h e r e f e r e n t i a l t r a n s p a r e n c y d o e s n o t h o l d ( d u e t o s i d e e fge c t s ) – a b s t r a c t i o n i s m o r e l i m i t e d – o i s c r u c i a l , w h i c h i s n o t s u i t e d i n c e r t a i n r d e r p r o b l e m s

  13. Declarative programming F o c u s : w h a t t o c o m p u t e ( a s d e s i r e d o u t c o m e ) ● I t i s n o t c o n c e r n e d a b o u t h o w t o d o t h i n g s , b u t w h a t s h o u l d b e o b t a i n e d . – L a n g u a g e s : d o m a i n s p e c i fj c ( e . g . H T M L ) , q u e r y ( S Q L ) , l o g i c ( P r o l o g ) .

  14. Declarative/Logic programming F o c u s : w h a t t o c o m p u t e ( a s d e s i r e d o u t c o m e ) ● V a r i o u s l o g i c a l a s s e r t i o n s a b o u t a s i t u a t i o n a r e m a d e , d e s c r i b i n g a l l k n o w n a n d a b o u t t h e f a c t s r u l e s m o d e l e d w o r l d . T h e n q u e r i e s a r e m a d e . ● T h e r o l e o f t h e c o m p u t e r i s t o m a i n t a i n d a t a a n d t o p e r f o r m i n f e r e n c e s .

  15. Algorithm = Logic + Control “ A n a l g o r i t h m c a n b e r e g a r d e d a s c o n s i s t i n g o f – a , w h i c h s p e c i fj e s t h e k n o w l e d g e l o g i c c o m p o n e n t t o b e u s e d i n s o l v i n g p r o b l e m s , a n d – a , w h i c h d e t e r m i n e s t h e c o n t r o l c o m p o n e n t p r o b l e m - s o l v i n g s t r a t e g i e s b y m e a n s o f w h i c h t h a t k n o w l e d g e i s u s e d . T h e l o g i c c o m p o n e n t d e t e r m i n e s t h e m e a n i n g o f t h e a l g o r i t h m w h e r e a s t h e c o n t r o l c o m p o n e n t o n l y a fg e c t s i t s e ffj c e n c y . ” R o b e r t K o w a l s k y , A l g o r i t h m = L o g i c + C o n t r o l ( 1 9 7 9 )

  16. Imperative vs Declarative ● I m p e r a t i v e : – i a p p r o a c h : a l l e x e c u t i o n n s i d e - t o - o u t s i d e a l t e r n a t i v e s a r e e x p l i c i t l y s p e c i fj e d a n d n e w a l t e r n a t i v e s m u s t b e e x p l i c i t l y a d d e d ● D e c l a r a t i v e – o u t s i d e - t o - i n s i d e a p p r o a c h : c o n s t r a i n t s i m p l i c i t l y s p e c i f y e x e c u t i o n a l t e r n a t i v e s a s a l l a l t e r n a t i v e s t h a t s a t i s f y t h e c o n s t r a i n t s ; a d d i n g n e w c o n s t r a i n t s u s u a l l y m e a n s d i s c a r d i n g s o m e e x e c u t i o n a l t e r n a t i v e s

  17. : I m p e r a t i v e y o u c o m m a n d t h e d i r e c t i o n s

  18. : I m p e r a t i v e y o u c o m m a n d t h e d i r e c t i o n s

  19. : I m p e r a t i v e y o u c o m m a n d t h e d i r e c t i o n s ● Wh a t i f t h e l a b y i n t h c h a n g e s ?

  20. : y o u D e c l a r a t i v e g i v e j u s t t h e l a b y r i n t h . T h e c o m p u t e r fj n d s t h e w a y .

  21. : y o u D e c l a r a t i v e g i v e j u s t t h e l a b y r i n t h . T h e c o m p u t e r fj n d s t h e w a y . ● F o r i n s t a n c e , v i a t r i a l , e r r o r a n d b a c k t r a c k i n g

  22. : y o u D e c l a r a t i v e g i v e j u s t t h e l a b y r i n t h . T h e c o m p u t e r fj n d s t h e w a y . ● F o r i n s t a n c e , v i a t r i a l , e r r o r a n d b a c k t r a c k i n g

  23. : y o u D e c l a r a t i v e g i v e j u s t t h e l a b y r i n t h . T h e c o m p u t e r fj n d s t h e w a y . ● F o r i n s t a n c e , v i a t r i a l , e r r o r a n d b a c k t r a c k i n g

  24. : y o u D e c l a r a t i v e g i v e j u s t t h e l a b y r i n t h . T h e c o m p u t e r fj n d s t h e w a y . ● F o r i n s t a n c e , v i a t r i a l , e r r o r a n d b a c k t r a c k i n g

  25. First steps in Prolog

  26. Histrory of Prolog 1 9 6 5 R e s o l u t i o n a l g o r i t h m b y J . A . R o b i n s o n f o l l o w e d b y S L D r e s o l u t i o n b y R . K o w a l s k i 1 9 7 2 P R O g r a m m a t i o n e n L O G i q u e c r e a t e d b y A . C o l m e r a u e r a n d P . R o u s s e l i n L u m i n y , M a r s e i l l e 1 9 8 0 P r o l o g a c k n o w l e d g e d a s a m a j o r A . I . l a n g u a g e … n o w v a r i o u s v e r s i o n s , u s e d i n C o n s t r a i n t P r o g r a m m i n g , o r b a s i s / r e f e r e n c e f o r a l t e r n a t i v et e c h n i q u e s , a s D a t a L o g , A n w s e r S e t P r o g r a m m i n g ( A S P ) , e t c .

  27. First program parent(marge, lisa). parent(marge, bart). parent(marge, maggie). parent(homer, lisa). parent(homer, bart). parent(homer, maggie). parent(abraham, homer). parent(abraham, herb). parent(mona, homer). parent(jackie, marge). parent(clancy, marge). parent(jackie, patty). parent(clancy, patty). parent(jackie, selma). parent(clancy, selma). parent(selma, ling).

  28. First program parent(marge, lisa). parent(marge, bart). parent(marge, maggie). parent(homer, lisa). parent(homer, bart). parent(homer, maggie). parent(abraham, homer). parent(abraham, herb). parent(mona, homer). parent(jackie, marge). parent(clancy, marge). parent(jackie, patty). parent(clancy, patty). parent(jackie, selma). parent(clancy, selma). parent(selma, ling). child(X,Y) :- parent(Y,X).

  29. Prolog clauses ● F a c t female(marge).

  30. Prolog clauses ● F a c t female(marge). ● R u l e child(X,Y) :- parent(Y,X).

  31. Prolog clauses ● F a c t female(marge). ● R u l e child(X,Y) :- parent(Y,X). E x e r c i s e : w r i t e mother(X,Y) grandparent(X,Y) ancestor(X,Y) cousin(X,Y)

  32. Prolog syntax and bonuses

  33. Prolog syntax: Terms ● C o n s t a n t s I d e n t i fj e r s s t r i n g s o f l e t t e r s , d i g i t s , o r u n d e r s c o r e “ _ ” t h a t s t a r t w i t h l o w e r c a s e l e t t e r s . marge lisa x25 x_25 alpha_beta

  34. Prolog syntax: Terms ● C o n s t a n t s I d e n t i fj e r s s t r i n g s o f l e t t e r s , d i g i t s , o r u n d e r s c o r e “ _ ” t h a t s t a r t w i t h l o w e r c a s e l e t t e r s . marge lisa x25 x_25 alpha_beta N u m b e r s 1.001 2 3.03

  35. Prolog syntax: Terms ● C o n s t a n t s I d e n t i fj e r s s t r i n g s o f l e t t e r s , d i g i t s , o r u n d e r s c o r e “ _ ” t h a t s t a r t w i t h l o w e r c a s e l e t t e r s . marge lisa x25 x_25 alpha_beta N u m b e r s 1.001 2 3.03 S t r i n g s e n c l o s e d i n s i n g l e q u o t e s 'Mary' '.01' 'string' N o t e t h a t , b e c a u s e o f t h e e n c l o s i n g , s t r i n g s c a n s t a r t w i t h u p p e r c a s e l e t t e r , o r c a n b e a n u m b e r n o w t r e a t e d a s a s t r i n g .

  36. Prolog syntax: Terms ● C o n s t a n t s I d e n t i fj e r s s t r i n g s o f l e t t e r s , d i g i t s , o r u n d e r s c o r e “ _ ” t h a t s t a r t w i t h l o w e r c a s e l e t t e r s . marge lisa x25 x_25 alpha_beta N u m b e r s 1.001 2 3.03 ● V a r i a b l e s s t r i n g s o f l e t t e r s , d i g i t s o r u n d e r s c o r e t h a t s t a r t w i t h a n u p p e r c a s e l e t t e r o r t h e u n d e r s c o r e _x, Anna, Successor_State , N o t e : u n d e s c o r e b y i t s e l f ( “ _ ” ) i s a s p e c i a l v a r i a b l e . a n o n y m o u s

  37. Prolog syntax: Terms ● S t r u c t u r e s <identifier>(Term1, ..., Termk) r e c u r s i v e d e fj n i t i o n : e a c h t e r m c a n i t s e l f b e a s t r u c t u r e date(1, may, 1983) point(X, Y, Z) date(+(0,1), may, +(1900,-(183,100)))

  38. Prolog syntax: Terms S t r u c t u r e s c a n b e t h o u g h t a s t r e e s ! date(+(0,1), may, +(1900,-(183,100)))

  39. Prolog syntax: Predicates ● P a r e s y n t a x i c a l l y t h e s a m e a s s t r u c t u r e s . r e d i c a t e s <identifier>(Term1, ..., Termk) parent(marge, bart) female(marge)

  40. Prolog syntax: Predicates ● P a r e s y n t a x i c a l l y t h e s a m e a s s t r u c t u r e s . r e d i c a t e s <identifier>(Term1, ..., Termk) parent(marge, bart) female(marge) . . b u t t h e y a r e n o t t e r m s ! → p ≠ r e d i c a t e i d e n t i fj e r s s t r u c t u r e i d e n t i fj e r s

  41. Prolog syntax: Predicates t h i n g s s a i d a b o u t o b j e c t s o b j e c t s → p ≠ r e d i c a t e i d e n t i fj e r s s t r u c t u r e i d e n t i fj e r s

  42. Prolog Syntax ● A p r o l o g i s a s e q u e n c e o f f a c t s a n d r u l e s . p r o g r a m ● F a c t s a r e p r e d i c a t e s t e r m i n a t e d b y a p e r i o d “ . ” <identifier>(Term1, ..., Termk).

  43. Prolog Syntax ● A p r o l o g i s a s e q u e n c e o f f a c t s a n d r u l e s . p r o g r a m ● F a c t s a r e p r e d i c a t e s t e r m i n a t e d b y a p e r i o d “ . ” <identifier>(Term1, ..., Termk). F a c t s e n c o d e a s s e r t i o n s , t h i n g s t h a t a r e d e c l a r e d c e r t a i n ! female(marge). parent(homer, bart).

  44. Prolog Syntax ● A p r o l o g i s a s e q u e n c e o f f a c t s a n d r u l e s . p r o g r a m ● R u l e s a r e i n t h e f o r m : predicateH :- predicate1, .., predicatek. t h e l e f t o f “ :- ” i s n a m e d , t h e r i g h t . h e a d b o d y

  45. Prolog Syntax ● A p r o l o g i s a s e q u e n c e o f f a c t s a n d r u l e s . p r o g r a m ● R u l e s a r e i n t h e f o r m : predicateH :- predicate1, .., predicatek. ● R u l e s e n c o d e w a y s o f d e r i v i n g o r c o m p u t i n g a n e w f a c t . animal(X) :- elephant(X). We c a n s h o w t h a t X i s a n a n i m a l i f w e c a n s h o w t h a t i t i s a n e l e p h a n t .

  46. Prolog Syntax ● A p r o l o g i s a s e q u e n c e o f f a c t s a n d r u l e s . p r o g r a m ● R u l e s a r e i n t h e f o r m : predicateH :- predicate1, .., predicatek. ● R u l e s e n c o d e w a y s o f d e r i v i n g o r c o m p u t i n g a n e w f a c t . animal(X) :- elephant(X). We c a n s h o w t h a t X i s a n a n i m a l i f w e c a n s h o w t h a t i t i s a n e l e p h a n t . father(X,Y) :- parent(X,Y), male(X). We c a n s h o w t h a t X i s a f a t h e r o f Y i f w e c a n s h o w t h a t X i s a p a r e n t o f Y a n d t h a t X i s m a l e .

  47. Prolog's main operation ● A i s a s e q u e n c e o f p r e d i c a t e s q u e r y predicate1, predicate2, ..., predicatek – u s i n g t h e f a c t s a n d r u l e s i n t h e P r o l o g p r o g r a m , t h e s o l v e r t r i e s t o p r o v e t h a t t h i s s e q u e n c e o f p r e d i c a t e s i s t r u e . – i n p r o v i n g t h e s e q u e n c e i t ( s h o u l d ) p e r f o r m t h e c o m p u t a t i o n y o u w a n t .

  48. Prolog's main operation ● A i s a s e q u e n c e o f p r e d i c a t e s q u e r y predicate1, predicate2, ..., predicatek – u s i n g t h e f a c t s a n d r u l e s i n t h e P r o l o g p r o g r a m , t h e s o l v e r t r i e s t o p r o v e t h a t t h i s s e q u e n c e o f p r e d i c a t e s i s t r u e . – i n p r o v i n g t h e s e q u e n c e i t ( s h o u l d ) p e r f o r m t h e c o m p u t a t i o n y o u w a n t . d e l i c a t e p o i n t m i g h t b e : p r o c e d u r a l c o m p u t a t i o n m i x e d w i t h d e c l a r a t i v e c o m p u t a t i o n

  49. Prolog's resolution strategy

  50. Horn clauses ● F a c t s a n d r u l e s f a l l s u n d e r t h e d e fj n i t i o n o f , H o r n c l a u s e s t h e s t r u c t u r e s o n w h i c h S L D r e s o l u t i o n w a s p r o v e n : G e n e r a l f o r m : F :- F1, F2,…, Fn. ● T o p r o v e F , o n e m u s t s u c c e s s i v e l y p r o v e F1 , … , Fn .

  51. Horn clauses ● F a c t s a n d r u l e s f a l l s u n d e r t h e d e fj n i t i o n o f H o r n c l a u s e s , t h e s t r u c t u r e s o n w h i c h S L D r e s o l u t i o n w a s p r o v e n : G e n e r a l f o r m : F :- F1, F2,…, Fn. ● T o p r o v e F , o n e m u s t s u c c e s s i v e l y p r o v e F1 , … , Fn . ● F i s t h e c l a u s e ’ s h e a d . ● F1 , F2 , … , Fn c o n s t i t u t e t h e c l a u s e ’ s t a i l o r b o d y . ● A f a c t i s a c l a u s e w i t h a n e m p t y t a i l .

  52. Prolog's strategy ● T o a n s w e r t h e q u e s t i o n , P r o l o g b u i l d s a : s e a r c h t r e e – s e t o f p o s s i b i l i t i e s ( c l a u s e s m a t c h i n g t h e q u e s t i o n ) r e p r e s e n t e d a s a t r e e – e a c h c h o i c e i s a n o d e o f t h e s e a r c h t r e e – t r i a l a n d e r r o r s e q u e n t i a l s e a r c h , f o l l o w i n g t h e o r d e r o f d e c l a r a t i o n

  53. Prolog's strategy, example p(1) :- a(1). p(1) :- b(1). a(1) :- c(1). c(1) :- d(1). c(1) :- d(2). b(1) :- e(1). e(1). d(3). ● Q u e r y : ?- p(1).

  54. Prolog's strategy, example p(1) :- a(1). p(1) :- b(1). a(1) :- c(1). c(1) :- d(1). c(1) :- d(2). b(1) :- e(1). e(1). d(3). ● Q u e r y : ?- p(1).

  55. Prolog's strategy, example p(1) :- a(1). p(1) :- b(1). a(1) :- c(1). c(1) :- d(1). c(1) :- d(2). b(1) :- e(1). e(1). d(3). ● Q u e r y : ?- p(1).

  56. Prolog's strategy, example p(1) :- a(1). p(1) :- b(1). a(1) :- c(1). c(1) :- d(1). c(1) :- d(2). b(1) :- e(1). e(1). d(3). ● Q u e r y : ?- p(1).

  57. Prolog's strategy, example p(1) :- a(1). p(1) :- b(1). a(1) :- c(1). c(1) :- d(1). c(1) :- d(2). b(1) :- e(1). e(1). d(3). ● Q u e r y : ?- p(1).

  58. Prolog's strategy, example p(1) :- a(1). p(1) :- b(1). a(1) :- c(1). c(1) :- d(1). c(1) :- d(2). b(1) :- e(1). e(1). d(3). ● Q u e r y : ?- p(1).

  59. Prolog's strategy, example p(1) :- a(1). p(1) :- b(1). a(1) :- c(1). c(1) :- d(1). c(1) :- d(2). b(1) :- e(1). e(1). d(3). ● Q u e r y : ?- p(1).

  60. Prolog's strategy, example p(1) :- a(1). p(1) :- b(1). a(1) :- c(1). c(1) :- d(1). c(1) :- d(2). b(1) :- e(1). e(1). d(3). ● Q u e r y : ?- p(1).

  61. Prolog's strategy, example p(1) :- a(1). p(1) :- b(1). a(1) :- c(1). c(1) :- d(1). c(1) :- d(2). b(1) :- e(1). e(1). d(3). ● Q u e r y : ?- p(1).

  62. Prolog's strategy, example p(1) :- a(1). p(1) :- b(1). a(1) :- c(1). c(1) :- d(1). c(1) :- d(2). b(1) :- e(1). b(1) :- d(3). e(1). d(3). ● Q u e r y : ?- p(1). w i t h b a c k t r a c k i n g w e c a n g e t m o r e a n s w e r s b y u s i n g “ ; ”

  63. Prolog's strategy ● T h e s o l v e r f o l l o w s a d e p t h - fj r s t s t r a t e g y – s u c c e s s n o d e : s o l u t i o n f o u n d ! t h e s o l v e r d i s p l a y s i t a n d s t o p s . – f : t h e s o l v e r b a c k t r a c k s u p i n a i l u r e n o d e t h e t r e e , u n t i l i t fj n d s a c h o i c e p o i n t w i t h u n e x p l o r e d b r a n c h e s .

  64. Prolog's strategy ● I f b a c k t r a c k i n g e n c o u n t e r s n o c h o i c e p o i n t l e f t , P r o l o g s t o p s . N o f u r t h e r s o l u t i o n . ● N O T E : S o m e b r a n c h e s a r e i n fj n i t e ! S o s e a r c h m a y n o t s t o p … T a k e c a r e o f : – T h e o r d e r o f g o a l s i n c l a u s e t a i l s – T h e o r d e r o f c l a u s e s

  65. Resolution with variables

  66. Using variables... ● V a r i a b l e s a l l o w u s t o : – c o m p u t e m o r e t h a n y e s / n o a n s w e r s ?- parent(marge, X).

  67. Using variables... ● V a r i a b l e s a l l o w u s t o : – c o m p u t e m o r e t h a n y e s / n o a n s w e r s ?- parent(marge, X). – c o m p r e s s t h e p r o g r a m . parent(marge, lisa) :- child(lisa, marge). parent(marge, bart) :- child(bart, marge). … parent(X, Y) :- child(Y, X).

  68. Using variables... ● V a r i a b l e s a l l o w u s t o : – c o m p u t e m o r e t h a n y e s / n o a n s w e r s ?- parent(marge, X). – c o m p r e s s t h e p r o g r a m . parent(marge, lisa) :- child(lisa, marge). parent(marge, bart) :- child(bart, marge). … parent(X, Y) :- child(Y, X). – r ( w h e n d e c l a r a t i v i t y i s m a i n t a n e d ) e v e r s i b i l i t y ?- parent(Y, lisa).

  69. Variables matching via Unification ?- brother(bart, Who). w i t h U n i fj c a t i o n brother(X,Y) :- brother(X,Y) male(X), X=bart, Y=Who parent(X,Z), parent(Y,Z), X \== Y. male(bart) parent(bart,Z) parent(bart,Z) bart \== Who

  70. Unification examples ● U n i fj c a t i o n p r e d i c a t e : “ = ” ?- a(B,C) = a(2,3). YES {B=2, C=3} ?- a(X,Y,L) = a(Y,2,carole). YES {X=2, Y=2, L=carole} ?- a(X,X,Y) = a(Y,u,v). NO

  71. Unification examples ● U n i fj c a t i o n p r e d i c a t e : “ = ” ?- a(B,C) = a(2,3). YES {B=2, C=3} ?- a(X,Y,L) = a(Y,2,carole). YES {X=Y, Y=2, L=carole} ?- a(X,X,Y) = a(Y,u,v). NO E x e r c i s e U n i f y p(X,b(Z,a),X) w i t h p(Y,Y,b(V,a))

  72. Unification algorithm A f r e e v a r i a b l e c a n b e s e e n a s a p o i n t e r t o N I L . Wh e n n o t f r e e , i t i s s a i d . a v a r i a b l e m e a n s r e a c h i n g t h e b o u n d v a r i a b l e D e r e f e r e n c i n g v a l u e t o w h i c h i t i s b o u n d . t 1 , t 2 a r e t w o t e r m s p r o c e d u r e u n i f y ( t 1 , t 2 ) d e r e f e r e n c e i s a f u n c t i o n t h a t d e r e f e r e n c e s b o u n d v a r i a b l e s a n d t 3 : = d e r e f e r e n c e ( t 1 ) ; t 4 : = d e r e f e r e n c e ( t 2 ) i f t 3 i s a v a r i a b l e t h e n r e t u r n s t h e i n p u t o t h e r w i s e t 3 p o i n t s t o t 4 ; r e t u r n s u c c e s s e l s e i f t 4 i s a v a r i a b l e t h e n ● X a n d m a r g e w h e r e X i s t 4 p o i n t s t o t 3 ; r e t u r n s u c c e s s b o u n d t o t h e v a l u e m a r g e e l s e i f t 3 i s a n a t o m a n d t 4 i s a n a t o m t h e n w i l l m a t c h . i f t 3 = t 4 t h e n r e t u r n s u c c e s s e l s e r e t u r n f a i l ● X a n d Y w h e r e X i s b o u n d e l s e l e t t 3 = f ( t 3 1 , . . , t 3 n ) a n d t 4 = g ( t 4 1 , . . , t 4 m ) t o m a r g e a n d Y i s b o u n d t o i f f = g a n d n = m t h e n m a r g e w i l l m a t c h , f o r i : = 1 t o n d o ● X i f u n i f y ( t 3 i , t 4 i ) f a i l s t h e n r e t u r n f a i l a n d m a r g e w h e r e X i s r e t u r n s u c c e s s b o u n d t o l i s a w i l l n o t e l s e m a t c h . r e t u r n f a i l

  73. Lists

  74. Representing lists ● L a r e a c r u c i a l d a t a s t r u c t u r e i n P r o l o g . T h e y a r e i s t s u s u a l l y w r i t t e n a s : [a, b, c, d] T h i s c o r r e s p o n d s t o t h e s t r u c t u r e d t e r m : [a|[b|[c|[d|[]]]]] w h e r e [] i s a s p e c i a l c o n s t a n t t h e e m p t y l i s t .

  75. Representing lists ● E a c h l i s t i s o f t h e f o r m [ < h e a d > | < r e s t _ o f _ l i s t > ] < h e a d > a n e l e m e n t o f t h e l i s t ( n o t n e c e s s a r i l y a l i s t ) . < r e s t _ o f _ l i s t > i s a l i s t ( a s u b - l i s t ) . [a, b, c, d] = [a | [b, c, d] ] a : h e a d ; [ b , c , d ] : t a i l [a, b, c, d] = [a, b | [c, d]] [_|_] h a s a t l e a s t o n e e l e m e n t .

  76. Cut

  77. Backtracking control using CUT ● S o m e t i m e s i t i s u s e f u l t o c o n t r o l t h e b a c k t r a c k i n g , a n d t h i s c a n b e d o n e u s i n g t h e “ ! ” , t h e c u t o p e r a t o r . → o n c e i t i s e x e c u t e d , i t b a c k t r a c k i n g d i s a l l o w s

  78. Backtracking control using CUT ● S o m e t i m e s i t i s u s e f u l t o c o n t r o l t h e b a c k t r a c k i n g , a n d t h i s c a n b e d o n e u s i n g t h e “ ! ” , t h e c u t o p e r a t o r . → o n c e i t i s e x e c u t e d , i t b a c k t r a c k i n g d i s a l l o w s p :- b1, b2, !, a1, a2, a3. p :- r1, r2 . p :- r3 . B e f o r e r e a c h i n g c u t , t h e r e m i g h t b e b a c k t r a c k i n g o n b 1 a n d b 2 o r t r y i n g o t h e r r u l e s f o r p i f o n e o f b 1 o r b 2 c a n n o t b e s a t i s fj e d . A f t e r r e a c h i n g ! , n o m o r e b a c k t r a c k i n g . T h e s e c o n d a n d t h i r d r u l e w i l l n o t b e s e a r c h e d .

Recommend


More recommend