week12reg.icl 4.15 KB
Newer Older
Reg Huijben's avatar
Reg Huijben committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
module week12reg

import StdEnv, StdMaybe



::BM a b = {t::a -> b,f::b -> a} //bimap

bm :: BM a a
bm = {f = id, t = id}


:: Action a b
  = Lock (BM a Low) (BM b Low)
  | Unlock (BM a Low) (BM b Low)
  | MoveToShip (BM a High) (BM b High)
  | MoveToKey (BM a High) (BM b High)
  | MoveUp (BM a Low) (BM b High)
  | MoveDown (BM a High) (BM b Low)
  | E.c:(:.) infixl 1 (Action a c) (Action c b)
Reg Huijben's avatar
Reg Huijben committed
21
  | Wait (BM a b)
Reg Huijben's avatar
Reg Huijben committed
22 23 24 25 26 27 28 29 30 31
  | WhileContainerBelow (Action High High)

:: High = High
:: Low = Low

lock = Lock bm bm
unlock = Unlock bm bm
up = MoveUp bm bm
down = MoveDown bm bm
wait :: Action a a
Reg Huijben's avatar
Reg Huijben committed
32
wait = Wait bm
Reg Huijben's avatar
Reg Huijben committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
moveToQuay = MoveToKey bm bm
moveToShip = MoveToShip bm bm
whileContainerBelow :: (Action High High) -> (Action High High)
whileContainerBelow a = WhileContainerBelow a


t1 :: Action High High
t1 = whileContainerBelow (down :. lock :. up :. down :. unlock :. up) //lock :. up
//t1 :: Action High High
//t1 = down :. lock :. up :. down :. unlock :. up
:: Res r e = Error e | Result r

:: State = 
	{ onShip :: [Container]
	, onQuay:: [Container]
	, craneUp :: Bool
	, craneOnQuay :: Bool
	, locked :: Maybe Container
	}
:: Container :== String

initialState =
	{ onShip = []
	, onQuay = ["apples","beer","camera’s"]
	, craneUp = True
	, craneOnQuay = True
	, locked = Nothing 
	}


eval :: (Action a b) State -> (Res State String)
Reg Huijben's avatar
Reg Huijben committed
64
eval (MoveUp {f} {t}) s = Result {s & craneUp = True}
Reg Huijben's avatar
Reg Huijben committed
65 66 67 68 69 70 71 72 73 74 75 76
eval (MoveDown {f} {t}) s = Result {s & craneUp = False}
eval (Lock {f} {t}) s = if s.craneOnQuay 
						case s.onQuay of
							[x:xs] -> case s.locked of  
								Just(c) = Error "Cannot lock container, crane occupied"
								Nothing = Result {s & onQuay = xs, locked = (Just x) }
							[] -> Error "Cannot lock container, none on quay"
						case s.onShip of
							[x:xs] -> case s.locked of  
								Just(c) = Error "Cannot lock container, crane occupied"
								Nothing = Result {s & onShip = xs, locked = (Just x) }
							[] -> Error "Cannot lock container, none on ship"
Reg Huijben's avatar
Reg Huijben committed
77
eval (Unlock {f} {t}) s = if s.craneOnQuay
Reg Huijben's avatar
Reg Huijben committed
78 79 80 81 82 83 84 85 86 87 88 89 90
							case s.locked of
								Just c -> Result {s & onQuay = [c:s.onQuay], locked = Nothing }
								Nothing -> Error "Cannot unlock container, no container on crane"
							case s.locked of
								Just c -> Result {s & onShip = [c:s.onShip], locked = Nothing }
								Nothing -> Error "Cannot unlock container, no container on crane"

eval (MoveToShip {f} {t}) s = if s.craneOnQuay
								(Result {s & craneOnQuay = False})
								(Error "Cannot move to ship, crane is already on ship")
eval (MoveToKey {f} {t}) s = if s.craneOnQuay
								(Error "Cannot move to quay, crane is already on quay")
								(Result {s & craneOnQuay = True})
Reg Huijben's avatar
Reg Huijben committed
91
eval (Wait {f}) s = Result s
Reg Huijben's avatar
Reg Huijben committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133

eval (:. a b) s = case (eval a s) of
					Result s -> (eval b s)
					e -> e

eval (WhileContainerBelow a) s = if (containerBelow s) 

								case (eval a s) of
									(Result s2) -> (eval (WhileContainerBelow a) s2)
									e -> e
								
								(Result s)


containerBelow :: State -> Bool
containerBelow s = case s.locked of
					(Just s) = True
					Nothing = if s.craneOnQuay 
								(length s.onQuay > 0)
								(length s.onShip > 0)

/*  = Lock (BM a Low) (BM b Low)
  | Unlock (BM a Low) (BM b Low)
  | MoveToShip (BM a High) (BM b High)
  | MoveToKey (BM a High) (BM b High)
  | MoveUp (BM a Low) (BM b High)
  | MoveDown (BM a High) (BM b Low)
  | E.c:(:.) infixl 1 (Action a c) (Action c b)
  | Wait (BM a b) (BM a b)
  | WhileContainerBelow (Action High High)
*/
//asd//(Result s)



print :: (Action a b) -> [String]
print (MoveToShip {f} {t}) = ["moveToShip"]
print (MoveToKey {f} {t}) = ["moveToQuay"]
print (MoveUp {f} {t}) = ["moveUp"]
print (MoveDown {f} {t}) = ["moveDown"]
print (Lock {f} {t}) = ["lock"]
print (Unlock {f} {t}) = ["unlock"]
Reg Huijben's avatar
Reg Huijben committed
134
print (Wait {f} ) = ["wait"]
Reg Huijben's avatar
Reg Huijben committed
135 136 137 138 139 140
print (:. a b) = print a ++ [":.":"\n": print b ]
print (WhileContainerBelow a) = ["whileContainerBelow":"(":"\n": (print a ++ [")"])]

t2 :: Action Low Low
t2 = lock :. up :. down :. unlock

Reg Huijben's avatar
Reg Huijben committed
141 142 143
//Start = print loadShip 
Start = eval loadShip initialState

Reg Huijben's avatar
Reg Huijben committed
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178

loadShip = whileContainerBelow (
	down:. 
	lock:. 
	up:. 
	moveToShip:. 
	wait:. 
	down:. 
	wait:. 
	unlock:. 
	up:. 
	moveToQuay 
	)

//Start = loadShip //t1
//Start = g2 (G id)