Escolar Documentos
Profissional Documentos
Cultura Documentos
Entry Point
The water_jugs solution is derived by a simple, breadth-first, state-space search; and translated into a readable format by a DCG. w a t e r _ j u g s: S m a l l C a p a c i t y=3 , L a r g e C a p a c i t y=4 , R e s e r v o i ri sS m a l l C a p a c i t y+L a r g e C a p a c i t y+1 , v o l u m e (s m a l l ,C a p a c i t i e s ,S m a l l C a p a c i t y) , v o l u m e (l a r g e ,C a p a c i t i e s ,L a r g e C a p a c i t y) , v o l u m e (r e s e r v o i r ,C a p a c i t i e s ,R e s e r v o i r) , v o l u m e (s m a l l ,S t a r t ,0) , v o l u m e (l a r g e ,S t a r t ,0) , v o l u m e (r e s e r v o i r ,S t a r t ,R e s e r v o i r) , v o l u m e (l a r g e ,E n d ,2) , w a t e r _ j u g s _ s o l u t i o n (S t a r t ,C a p a c i t i e s ,E n d ,S o l u t i o n) , p h r a s e (n a r r a t i v e ( S o l u t i o n ,C a p a c i t i e s ,E n d ) ,C h a r s) , p u t _ c h a r s (C h a r s) . water_jugs_solution( +Start, +Capacities, +End, ?Solution ) holds when Solution is the terminal 'node' in a state-space search - beginning with a 'start state' in which the water-jugs have Capacities and contain the Start volumes. The terminal node is reached when the water-jugs contain the End volumes. w a t e r _ j u g s _ s o l u t i o n (S t a r t ,C a p a c i t i e s ,E n d ,S o l u t i o n): -
www.binding-time.co.uk/water_jugs.html
1/5
3/13/13
s o l v e _ j u g s ([ s t a r t ( S t a r t ) ] ,C a p a c i t i e s ,[ ] ,E n d ,S o l u t i o n) . solve_jugs( +Nodes, +Capacities, +Visited, +End, ?Solution ) holds when Solution is the terminal 'node' in a state-space search, beginning with a first 'open' node in Nodes, and terminating when the waterjugs contain the End volumes. Capacities define the capacities of the water-jugs, while Visited is a list of expanded ('closed') node states. The 'breadth-first' operation of solve_jugs is due to the 'existing' Nodes being appended to the 'new' nodes. (If the 'new' nodes were appended to the 'existing' nodes, the operation would be 'depth-first'.) s o l v e _ j u g s ([ N o d e | N o d e s ] ,C a p a c i t i e s ,V i s i t e d ,E n d ,S o l u t i o n): n o d e _ s t a t e (N o d e ,S t a t e) , (S t a t e=E n d> S o l u t i o n=N o d e ;o t h e r w i s e> f i n d a l l ( S u c c e s s o r , s u c c e s s o r ( N o d e ,C a p a c i t i e s ,V i s i t e d ,S u c c e s s o r ) , S u c c e s s o r s ) , a p p e n d (N o d e s ,S u c c e s s o r s ,N e w N o d e s) , s o l v e _ j u g s (N e w N o d e s ,C a p a c i t i e s ,[ S t a t e | V i s i t e d ] ,E n d ,S o l u t i o n) ) . successor( +Node, +Capacities, +Visited, ?Successor ) Successor is a successor of Node, for water-jugs with Capacities, if there is a legal 'transition' from Node's state to Successor's state, and Successor's state is not a member of the Visited states. s u c c e s s o r (N o d e ,C a p a c i t i e s ,V i s i t e d ,S u c c e s s o r): n o d e _ s t a t e (N o d e ,S t a t e) , S u c c e s s o r=n o d e ( A c t i o n , S t a t e 1 , N o d e ) , j u g _ t r a n s i t i o n (S t a t e ,C a p a c i t i e s ,A c t i o n ,S t a t e 1) , \ +m e m b e r (S t a t e 1 ,V i s i t e d) . jug_transition( +State, +Capacities, ?Action, ?SuccessorState ) holds when Action describes a valid transition, from State to SuccessorState, for water-jugs with Capacities. There are 2 sorts of Action: e m p t y _ i n t o ( S o u r c e , T a r g e t ) : valid if Source is not already empty and the combined contents from Source and Target, (in State), are not greater than the capacity of the Target jug. In SuccessorState: Source becomes empty, while the Target jug acquires the combined contents of Source and Target in State. f i l l _ f r o m ( S o u r c e , T a r g e t ) : valid if Source is not already empty and the combined contents from Source and Target, (in State), are greater than the capacity of the Target jug. In SuccessorState: the Target jug becomes full, while Source retains the difference between the combined contents of Source and Target, in State, and the capacity of the Target jug.
www.binding-time.co.uk/water_jugs.html
2/5
3/13/13
In either case, the contents of the unused jug are unchanged. j u g _ t r a n s i t i o n (S t a t e 0 ,C a p a c i t i e s ,e m p t y _ i n t o ( S o u r c e , T a r g e t ) ,S t a t e 1): v o l u m e (S o u r c e ,S t a t e 0 ,C o n t e n t 0) , C o n t e n t 0>0 , j u g _ p e r m u t a t i o n (S o u r c e ,T a r g e t ,U n u s e d) , v o l u m e (T a r g e t ,S t a t e 0 ,C o n t e n t 1) , v o l u m e (T a r g e t ,C a p a c i t i e s ,C a p a c i t y) , C o n t e n t 0+C o n t e n t 1= <C a p a c i t y , v o l u m e (S o u r c e ,S t a t e 1 ,0) , v o l u m e (T a r g e t ,S t a t e 1 ,C o n t e n t 2) , C o n t e n t 2i sC o n t e n t 0+C o n t e n t 1 , v o l u m e (U n u s e d ,S t a t e 0 ,U n c h a n g e d) , v o l u m e (U n u s e d ,S t a t e 1 ,U n c h a n g e d) . j u g _ t r a n s i t i o n (S t a t e 0 ,C a p a c i t i e s ,f i l l _ f r o m ( S o u r c e , T a r g e t ) ,S t a t e 1): v o l u m e (S o u r c e ,S t a t e 0 ,C o n t e n t 0) , C o n t e n t 0>0 , j u g _ p e r m u t a t i o n (S o u r c e ,T a r g e t ,U n u s e d) , v o l u m e (T a r g e t ,S t a t e 0 ,C o n t e n t 1) , v o l u m e (T a r g e t ,C a p a c i t i e s ,C a p a c i t y) , C o n t e n t 1<C a p a c i t y , C o n t e n t 0+C o n t e n t 1>C a p a c i t y , v o l u m e (S o u r c e ,S t a t e 1 ,C o n t e n t 2) , v o l u m e (T a r g e t ,S t a t e 1 ,C a p a c i t y) , C o n t e n t 2i sC o n t e n t 0+C o n t e n t 1-C a p a c i t y , v o l u m e (U n u s e d ,S t a t e 0 ,U n c h a n g e d) , v o l u m e (U n u s e d ,S t a t e 1 ,U n c h a n g e d) .
Data Abstraction
volume( ?Jug, ?State, ?Volume ) holds when Jug ('large', 'small' or 'reservoir') has Volume in State. v o l u m e (s m a l l ,j u g s ( S m a l l ,_ L a r g e ,_ R e s e r v o i r ) ,S m a l l) . v o l u m e (l a r g e ,j u g s ( _ S m a l l ,L a r g e ,_ R e s e r v o i r ) ,L a r g e) . v o l u m e (r e s e r v o i r ,j u g s ( _ S m a l l ,_ L a r g e ,R e s e r v o i r ) ,R e s e r v o i r) . jug_permutation( ?Source, ?Target, ?Unused ) holds when Source, Target and Unused are a permutation of 'small', 'large' and 'reservoir'. j u g _ p e r m u t a t i o n (S o u r c e ,T a r g e t ,U n u s e d): s e l e c t (S o u r c e ,[ s m a l l ,l a r g e ,r e s e r v o i r ] ,R e s i d u e) , s e l e c t (T a r g e t ,R e s i d u e ,[ U n u s e d ]) . node_state( ?Node, ?State ) holds when the contents of the water-jugs at Node are described by State. n o d e _ s t a t e (s t a r t ( S t a t e ) ,S t a t e) . n o d e _ s t a t e (n o d e ( _ T r a n s i t i o n ,S t a t e ,_ P r e d e c e s s o r ) ,S t a t e) .
www.binding-time.co.uk/water_jugs.html
3/5
3/13/13
narrative/5 is a DCG presenting water-jugs solutions in a readable format. The grammar is 'headrecursive', because the 'nodes list', describing the solution, has the last node outermost. n a r r a t i v e (s t a r t ( S t a r t ) ,C a p a c i t i e s ,E n d)> " G i v e nt h r e ej u g sw i t hc a p a c i t i e so f : " ,n e w l i n e , l i t e r a l _ v o l u m e s (C a p a c i t i e s) , " T oo b t a i nt h er e s u l t : " ,n e w l i n e , l i t e r a l _ v o l u m e s (E n d) , " S t a r t i n gw i t h : " ,n e w l i n e , l i t e r a l _ v o l u m e s (S t a r t) , " D ot h ef o l l o w i n g : " ,n e w l i n e . n a r r a t i v e (n o d e ( T r a n s i t i o n ,R e s u l t ,P r e d e c e s s o r ) ,C a p a c i t i e s ,E n d)> n a r r a t i v e (P r e d e c e s s o r ,C a p a c i t i e s ,E n d) , l i t e r a l _ a c t i o n (T r a n s i t i o n ,R e s u l t) . l i t e r a l _ v o l u m e s (V o l u m e s)> i n d e n t ,l i t e r a l (V o l u m e s) ," ; " ,n e w l i n e . l i t e r a l _ a c t i o n (T r a n s i t i o n ,R e s u l t)> i n d e n t ," -" ,l i t e r a l (T r a n s i t i o n) ,"g i v i n g : " ,n e w l i n e , i n d e n t ,i n d e n t ,l i t e r a l (R e s u l t) ,n e w l i n e . l i t e r a l (e m p t y _ i n t o ( F r o m , T o ))> " E m p t yt h e" ,l i t e r a l (F r o m) ,"i n t ot h e" , l i t e r a l (T o) . l i t e r a l (f i l l _ f r o m ( F r o m , T o ))> " F i l lt h e" ,l i t e r a l (T o) ,"f r o mt h e" , l i t e r a l (F r o m) . l i t e r a l (j u g s ( S m a l l , L a r g e , R e s e r v o i r ))> l i t e r a l _ n u m b e r (S m a l l) ,"g a l l o n si nt h es m a l lj u g ," , l i t e r a l _ n u m b e r (L a r g e) ,"g a l l o n si nt h el a r g ej u ga n d" , l i t e r a l _ n u m b e r (R e s e r v o i r) ,"g a l l o n si nt h er e s e r v o i r " . l i t e r a l (s m a l l)>" s m a l lj u g " . l i t e r a l (l a r g e)>" l a r g ej u g " . l i t e r a l (r e s e r v o i r)>" r e s e r v o i r " . l i t e r a l _ n u m b e r (N u m b e r ,P l u s ,M i n u s): n u m b e r (N u m b e r) , n u m b e r _ c h a r s (N u m b e r ,C h a r s) , a p p e n d (C h a r s ,M i n u s ,P l u s) . i n d e n t>" " . n e w l i n e>" " .
Utility Predicates
www.binding-time.co.uk/water_jugs.html
4/5
3/13/13
Output
The output of the program is:
? -w a t e r _ j u g s . G i v e nt h r e ej u g sw i t hc a p a c i t i e so f : 3g a l l o n si nt h es m a l lj u g ,4g a l l o n si nt h el a r g ej u ga n d8g a l l o n si nt h er e s e r v o i r ; T oo b t a i nt h er e s u l t : 0g a l l o n si nt h es m a l lj u g ,2g a l l o n si nt h el a r g ej u ga n d6g a l l o n si nt h er e s e r v o i r ; S t a r t i n gw i t h : 0g a l l o n si nt h es m a l lj u g ,0g a l l o n si nt h el a r g ej u ga n d8g a l l o n si nt h er e s e r v o i r ; D ot h ef o l l o w i n g : -F i l lt h es m a l lj u gf r o mt h er e s e r v o i rg i v i n g : 3g a l l o n si nt h es m a l lj u g ,0g a l l o n si nt h el a r g ej u ga n d5g a l l o n si nt h er e s e r v o i r -E m p t yt h es m a l lj u gi n t ot h el a r g ej u gg i v i n g : 0g a l l o n si nt h es m a l lj u g ,3g a l l o n si nt h el a r g ej u ga n d5g a l l o n si nt h er e s e r v o i r -F i l lt h es m a l lj u gf r o mt h er e s e r v o i rg i v i n g : 3g a l l o n si nt h es m a l lj u g ,3g a l l o n si nt h el a r g ej u ga n d2g a l l o n si nt h er e s e r v o i r -F i l lt h el a r g ej u gf r o mt h es m a l lj u gg i v i n g : 2g a l l o n si nt h es m a l lj u g ,4g a l l o n si nt h el a r g ej u ga n d2g a l l o n si nt h er e s e r v o i r -E m p t yt h el a r g ej u gi n t ot h er e s e r v o i rg i v i n g : 2g a l l o n si nt h es m a l lj u g ,0g a l l o n si nt h el a r g ej u ga n d6g a l l o n si nt h er e s e r v o i r -E m p t yt h es m a l lj u gi n t ot h el a r g ej u gg i v i n g : 0g a l l o n si nt h es m a l lj u g ,2g a l l o n si nt h el a r g ej u ga n d6g a l l o n si nt h er e s e r v o i r y e s
www.binding-time.co.uk/water_jugs.html
5/5