Made module object so the files stay small
[mc1516pa.git] / code / SokobanObjectwise.icl
1 module SokobanObjectwise
2
3 import StdString
4 import StdFile
5 import StdTuple
6 import StdFunc
7 import StdOrdList
8 import StdList
9
10 from Data.Func import $
11 from Data.List import intercalate, maximum, permutations
12 from Text import class Text(concat,join), instance Text String
13
14 import Sokoban
15
16 :: AnnotatedSokoban :== [(Int, Int, SokobanTile)]
17
18 Start :: *World -> *World
19 Start w
20 # (io, w) = stdio w
21 # (puzzle, io) = parseFromFile io
22 # io = io <<< encode puzzle <<< "\n"
23 = snd (fclose io w)
24
25 encode :: SokobanPuzzle -> String
26 encode p = join "\n" [
27 "MODULE ob(ix, iy)",
28 "VAR",
29 "\tx: 0.." <+ maxX <+ ";",
30 "\ty: 0.." <+ maxY <+ ";",
31 "INIT x=ix & y=iy",
32 "INVAR " <+ encodeObPosition legalPos,
33 "",
34 "MODULE main",
35 "VAR",
36 "\tagent: " <+ declareObject agent,
37 join "\n" ["\tb" <+ i <+ ": " <+ declareObject b\\i<-boxnums & b<-boxes],
38 "\tmovement: {left, up, right, down, finished};",
39 "--Handy variable for deltas",
40 deltaDefine,
41 "--Agent not on the box",
42 "INVAR " <+ encodeAgentNotOnBox boxnums <+ ";",
43 "--Box not on box",
44 encodeBoxOnBoxes (indexList boxes),
45 "--Goal state",
46 "INVAR " <+ encodeGoalState boxnums targetPos <+ ";",
47 "",
48 encodeObMovement "agent",
49 encodeBoxMovement boxnums,
50 "CTLSPEC ! EF (movement = finished);",
51
52 ""]
53 where
54 targetPos = getTargets annot
55 boxes = getBoxes annot
56 boxnums = indexList boxes
57 agent = hd $ getAgents annot
58 legalPos = legalPositions annot
59 annot = annotate p
60 (maxX, maxY) = getMetrics annot
61
62 declareObject :: (Int, Int) -> String
63 declareObject (x, y) = "ob(" <+ x <+ ", " <+ y <+ ");"
64
65 deltaDefine :: String
66 deltaDefine = join "\n" [
67 "DEFINE dx := case",
68 "\t\tmovement = left : -1;",
69 "\t\tmovement = right: +1;",
70 "\t\tTRUE: 0;",
71 "\tesac;",
72 "\tdy := case",
73 "\t\tmovement = up : -1;",
74 "\t\tmovement = down: +1;",
75 "\t\tTRUE: 0;",
76 "\tesac;"]
77
78 //THIS IS NAIVE AND SHOULD BE IMPROVED
79 legalPositions :: AnnotatedSokoban -> [(Int, Int)]
80 legalPositions p = [(x, y)\\(x, y, t)<-p | case t of Wall=False; _=True]
81
82 annotate :: SokobanPuzzle -> AnnotatedSokoban
83 annotate (Sokoban p) = flatten [[(x, y, t)\\t<-r & x<-[0..]]\\r<-p & y<-[0..]]
84
85 getTargets :: AnnotatedSokoban -> [(Int, Int)]
86 getTargets p = [(x, y)\\(x, y, t)<-p |
87 case t of Target=True;TargetBox=True;TargetAgent=True;_=False]
88
89 getMetrics :: AnnotatedSokoban -> (Int, Int)
90 getMetrics p = (maximum (map fst3 p),maximum (map snd3 p))
91
92 getAgents :: AnnotatedSokoban -> [(Int, Int)]
93 getAgents p = [(x, y)\\(x, y, Agent)<-p] ++ [(x, y)\\(x, y, TargetAgent)<-p]
94
95 getBoxes :: AnnotatedSokoban -> [(Int, Int)]
96 getBoxes p = [(x, y)\\(x, y, Box)<-p] ++ [(x, y)\\(x, y, TargetBox)<-p]
97
98 (<+) infixr 5 :: a b -> String | toString a & toString b
99 (<+) a b = toString a +++ toString b
100
101 encodeGoalState :: [Int] [(Int, Int)] -> String
102 encodeGoalState boxes targets = "(" <+
103 join " |\n\t" (map encodeOneGoal boxtargets) <+ ")<->movement=finished"
104 where
105 boxtargets = map (zip2 targets) (permutations boxes)
106
107 encodeOneGoal :: [((Int, Int), Int)] -> String
108 encodeOneGoal p = "(" <+ join " & " (encodeOneGoal` p) <+ ")"
109
110 encodeOneGoal` :: [((Int, Int), Int)] -> [String]
111 encodeOneGoal` [] = []
112 encodeOneGoal` [((x, y), b):xs] = [
113 "b" <+ b <+ ".x = " <+ x <+ " & " <+
114 "b" <+ b <+ ".y = " <+ y:encodeOneGoal` xs]
115
116 encodeBoxOnBoxes :: [Int] -> String
117 encodeBoxOnBoxes [] = ""
118 encodeBoxOnBoxes [b] = ""
119 encodeBoxOnBoxes [b:bs] = "INVAR " <+ encodeBoxOnBox b bs <+ ";\n" <+
120 encodeBoxOnBoxes bs
121 where
122 encodeBoxOnBox :: Int [Int] -> String
123 encodeBoxOnBox x bs = join " & " ["!(b" <+ x <+ ".x=b" <+ b <+ ".x &" <+
124 "b" <+ x <+ ".y=b" <+ b <+ ".y)"\\b<-bs]
125
126 encodeAgentNotOnBox :: [Int] -> String
127 encodeAgentNotOnBox bs = join " &\n\t" [
128 "!(b" <+ i <+ ".x = agent.x & b" <+ i <+ ".y = agent.y)"\\i<-bs]
129
130 encodeObMovement :: String -> String
131 encodeObMovement s = "TRANS next(" <+ s <+ ".x) = " <+ s <+ ".x + dx;\n" <+
132 "TRANS next(" <+ s <+ ".y) = " <+ s <+ ".y + dy;"
133
134 encodeObPosition :: [(Int, Int)] -> String
135 encodeObPosition p = join " |\n\t" [forThisX x p\\x<-removeDup $ map fst p]
136 where
137 forThisX :: Int [(Int, Int)] -> String
138 forThisX cx p = "(x=" <+ cx <+ " & (" <+
139 join " | " ["y=" <+ y \\(x, y)<-p | x == cx] <+ "))"
140
141 encodeBoxMovement :: [Int] -> String
142 encodeBoxMovement bs = join "\n" $ map ebm bs
143 where
144 ebm :: Int -> String
145 ebm i = "TRANS next(b" <+ i <+ ".x) = case\n" <+
146 "\t\tnext(agent.x) = b" <+ i <+ ".x & " <+
147 "next(agent.y) = b" <+ i <+ ".y: b" <+ i <+ ".x + dx;\n" <+
148 "\t\tTRUE: b" <+ i <+ ".x;\n" <+
149 "\tesac;\n" <+
150 "TRANS next(b" <+ i <+ ".y) = case\n" <+
151 "\t\tnext(agent.x) = b" <+ i <+ ".x & " <+
152 "next(agent.y) = b" <+ i <+ ".y: b" <+ i <+ ".y + dy;\n" <+
153 "\t\tTRUE: b" <+ i <+ ".y;\n" <+
154 "\tesac;"