6 import Control.Applicative
10 import Text => qualified join
12 :: BM a b = {t :: a->b, f :: b->a, t2 :: A.c t:(t c a)->t c b, f2 :: A.c t:(t b c)->t a c}
13 bm = {t = id, f = id, t2 = id, f2 = id}
16 = MoveToShip (BM b High) (BM a High)
17 | MoveToQuay (BM b High) (BM a High)
18 | MoveUp (BM b Low) (BM a High)
19 | MoveDown (BM b High) (BM a Low)
20 | Lock (BM b Low) (BM a Low)
21 | Unlock (BM b Low) (BM a Low)
23 | E.i: (:.) infixl 1 (Action b i) (Action i a)
24 | WhileContainerBelow (BM b a) (Action a a)
28 moveToShip = MoveToShip bm bm
29 moveToQuay = MoveToQuay bm bm
31 moveDown = MoveDown bm bm
35 whileContainerBelow = WhileContainerBelow bm
42 , locked :: Maybe String
47 , onQuay = ["apples","beer","camera's"]
54 eval :: (Action i f) State -> MaybeErrorString State
55 eval (MoveToShip _ _) s = pure {s & craneOnQuay = False}
56 eval (MoveToQuay _ _) s = pure {s & craneOnQuay = True}
57 eval (MoveUp _ _) s = pure {s & craneUp = True}
58 eval (MoveDown _ _) s = pure {s & craneUp = False}
59 eval (Lock _ _) s=:{locked=Nothing}
62 [c:r] = pure {s & onQuay = r, locked = Just c}
63 [] = Error "Cannot lock container on empty quay"
65 [c:r] = pure {s & onShip = r, locked = Just c}
66 [] = Error "Cannot lock container on empty ship"
67 eval (Lock _ _) s=:{locked=(Just c)}
68 = Error ("Cannot lock new container, craner contains " +++ c)
69 eval (Unlock _ _) s=:{locked=(Just c)}
70 | s.craneOnQuay = pure {s & onQuay = [c:s.onQuay], locked = Nothing}
71 = pure {s & onShip = [c:s.onShip], locked = Nothing}
72 eval (Unlock b1 b2) s=:{locked=Nothing}
73 = Error "Cannot unlock container, crane contains nothing"
74 eval (Wait _) s = Ok s
75 eval (a :. b) s = eval a s >>= eval b
76 eval (WhileContainerBelow _ action) s
77 | s.craneOnQuay && not (isEmpty s.onQuay) || not s.craneOnQuay && not (isEmpty s.onShip)
78 = eval (action :. whileContainerBelow action) s
81 opt :: (Action a b) -> Action a b
82 opt (WhileContainerBelow b a) = WhileContainerBelow b (opt a)
83 opt (Wait bm :. b) = opt (bm.f2 b)
84 opt (a :. Wait bm) = opt (bm.t2 a)
85 opt (a :. b) = opt a :. opt b
88 Start = opt (moveToShip :. wait :. moveDown)
89 //print :: (Action i f) [String] -> [String]
90 //print (MoveToShip _ _) s = ["Move to ship":s]
91 //print (MoveToQuay _ _) s = ["Move to quay":s]
92 //print (MoveUp _ _) s = ["Move up":s]
93 //print (MoveDown _ _) s = ["Move down":s]
94 //print (Lock _ _) s = ["Lock":s]
95 //print (Unlock _ _) s = ["Unlock":s]
96 //print (Wait _) s = ["Wait":s]
97 //print (a :. b) s = print a ["\n":print b s]
98 //print (WhileContainerBelow _ a) s
99 // = ["While container below (":indent ["\n":print a [")":s]]]
101 // indent s = map (\s->if (s.[0] == '\n') (s +++ "\t") s)
104 print :: (Action i f) String [String] -> [String]
105 print (MoveToShip _ _) i s = [i,"Move to ship":s]
106 print (MoveToQuay _ _) i s = [i,"Move to quay":s]
107 print (MoveUp _ _) i s = [i,"Move up":s]
108 print (MoveDown _ _) i s = [i,"Move down":s]
109 print (Lock _ _) i s = [i,"Lock":s]
110 print (Unlock _ _) i s = [i,"Unlock":s]
111 print (Wait _) i s = [i,"Wait":s]
112 print (a :. b) i s = print a i ["\n":print b i s]
113 print (WhileContainerBelow _ a) i s
114 = [i,"While container below (\n":print a ("\t"+++i) ["\n",i,")":s]]
116 Start = print loadShip "" []
118 p1 = moveToShip :. moveDown
120 //p2 = moveToShip :. wait :. lock // the required type error
123 whileContainerBelow (