annotate ppc/test/queens.p @ 0:bfdcc3820b32

Basis
author Mike Spivey <mike@cs.ox.ac.uk>
date Thu, 05 Oct 2017 08:04:15 +0100
parents
children
rev   line source
mike@0 1 (* N queens in functional form *)
mike@0 2
mike@0 3 const N = 8;
mike@0 4
mike@0 5 proc queens(k: integer; proc choice(x: integer): integer);
mike@0 6 var y, j, q: integer; ok: boolean;
mike@0 7
mike@0 8 proc choice1(x: integer): integer;
mike@0 9 begin
mike@0 10 if x = k then
mike@0 11 return y
mike@0 12 else
mike@0 13 return choice(x)
mike@0 14 end;
mike@0 15 end;
mike@0 16
mike@0 17 begin
mike@0 18 if k > N then
mike@0 19 print(choice)
mike@0 20 else
mike@0 21 y := 1;
mike@0 22 while y <= N do
mike@0 23 j := 1; ok := true;
mike@0 24 while ok and (j < k) do
mike@0 25 q := choice(j);
mike@0 26 ok := (q <> y) and (q+j <> y+k) and (q-j <> y-k);
mike@0 27 j := j+1
mike@0 28 end;
mike@0 29 if ok then queens(k+1, choice1) end;
mike@0 30 y := y+1
mike@0 31 end
mike@0 32 end
mike@0 33 end;
mike@0 34
mike@0 35 proc print(proc choice(x: integer): integer);
mike@0 36 var x: integer;
mike@0 37 begin
mike@0 38 x := 1;
mike@0 39 while x <= N do
mike@0 40 print_num(choice(x));
mike@0 41 x := x+1
mike@0 42 end;
mike@0 43 newline()
mike@0 44 end;
mike@0 45
mike@0 46 proc choice0(x: integer): integer;
mike@0 47 begin
mike@0 48 return 0
mike@0 49 end;
mike@0 50
mike@0 51 begin
mike@0 52 queens(1, choice0)
mike@0 53 end.
mike@0 54
mike@0 55 (*<<
mike@0 56 15863724
mike@0 57 16837425
mike@0 58 17468253
mike@0 59 17582463
mike@0 60 24683175
mike@0 61 25713864
mike@0 62 25741863
mike@0 63 26174835
mike@0 64 26831475
mike@0 65 27368514
mike@0 66 27581463
mike@0 67 28613574
mike@0 68 31758246
mike@0 69 35281746
mike@0 70 35286471
mike@0 71 35714286
mike@0 72 35841726
mike@0 73 36258174
mike@0 74 36271485
mike@0 75 36275184
mike@0 76 36418572
mike@0 77 36428571
mike@0 78 36814752
mike@0 79 36815724
mike@0 80 36824175
mike@0 81 37285146
mike@0 82 37286415
mike@0 83 38471625
mike@0 84 41582736
mike@0 85 41586372
mike@0 86 42586137
mike@0 87 42736815
mike@0 88 42736851
mike@0 89 42751863
mike@0 90 42857136
mike@0 91 42861357
mike@0 92 46152837
mike@0 93 46827135
mike@0 94 46831752
mike@0 95 47185263
mike@0 96 47382516
mike@0 97 47526138
mike@0 98 47531682
mike@0 99 48136275
mike@0 100 48157263
mike@0 101 48531726
mike@0 102 51468273
mike@0 103 51842736
mike@0 104 51863724
mike@0 105 52468317
mike@0 106 52473861
mike@0 107 52617483
mike@0 108 52814736
mike@0 109 53168247
mike@0 110 53172864
mike@0 111 53847162
mike@0 112 57138642
mike@0 113 57142863
mike@0 114 57248136
mike@0 115 57263148
mike@0 116 57263184
mike@0 117 57413862
mike@0 118 58413627
mike@0 119 58417263
mike@0 120 61528374
mike@0 121 62713584
mike@0 122 62714853
mike@0 123 63175824
mike@0 124 63184275
mike@0 125 63185247
mike@0 126 63571428
mike@0 127 63581427
mike@0 128 63724815
mike@0 129 63728514
mike@0 130 63741825
mike@0 131 64158273
mike@0 132 64285713
mike@0 133 64713528
mike@0 134 64718253
mike@0 135 68241753
mike@0 136 71386425
mike@0 137 72418536
mike@0 138 72631485
mike@0 139 73168524
mike@0 140 73825164
mike@0 141 74258136
mike@0 142 74286135
mike@0 143 75316824
mike@0 144 82417536
mike@0 145 82531746
mike@0 146 83162574
mike@0 147 84136275
mike@0 148 >>*)
mike@0 149
mike@0 150 (*[[
mike@0 151 MODULE Main 0 0
mike@0 152 IMPORT Lib 0
mike@0 153 ENDHDR
mike@0 154
mike@0 155 PROC _queens 16 0 0
mike@0 156 ! if k > N then
mike@0 157 LDLW 16
mike@0 158 CONST 8
mike@0 159 JLEQ L2
mike@0 160 ! print(choice)
mike@0 161 LDLW 24
mike@0 162 LDLW 20
mike@0 163 CONST 0
mike@0 164 GLOBAL _print
mike@0 165 PCALL 2
mike@0 166 JUMP L3
mike@0 167 LABEL L2
mike@0 168 ! y := 1;
mike@0 169 CONST 1
mike@0 170 STLW -4
mike@0 171 ! while y <= N do
mike@0 172 JUMP L5
mike@0 173 LABEL L4
mike@0 174 ! j := 1; ok := true;
mike@0 175 CONST 1
mike@0 176 STLW -8
mike@0 177 CONST 1
mike@0 178 STLC -13
mike@0 179 ! while ok and (j < k) do
mike@0 180 JUMP L8
mike@0 181 LABEL L7
mike@0 182 ! q := choice(j);
mike@0 183 LDLW -8
mike@0 184 LDLW 24
mike@0 185 LDLW 20
mike@0 186 PCALLW 1
mike@0 187 STLW -12
mike@0 188 ! ok := (q <> y) and (q+j <> y+k) and (q-j <> y-k);
mike@0 189 LDLW -12
mike@0 190 LDLW -4
mike@0 191 JEQ L12
mike@0 192 LDLW -12
mike@0 193 LDLW -8
mike@0 194 PLUS
mike@0 195 LDLW -4
mike@0 196 LDLW 16
mike@0 197 PLUS
mike@0 198 JEQ L12
mike@0 199 LDLW -12
mike@0 200 LDLW -8
mike@0 201 MINUS
mike@0 202 LDLW -4
mike@0 203 LDLW 16
mike@0 204 MINUS
mike@0 205 JEQ L12
mike@0 206 CONST 1
mike@0 207 JUMP L13
mike@0 208 LABEL L12
mike@0 209 CONST 0
mike@0 210 LABEL L13
mike@0 211 STLC -13
mike@0 212 ! j := j+1
mike@0 213 LDLW -8
mike@0 214 CONST 1
mike@0 215 PLUS
mike@0 216 STLW -8
mike@0 217 LABEL L8
mike@0 218 LDLC -13
mike@0 219 JNEQZ L10
mike@0 220 JUMP L9
mike@0 221 LABEL L10
mike@0 222 LDLW -8
mike@0 223 LDLW 16
mike@0 224 JLT L7
mike@0 225 LABEL L9
mike@0 226 ! if ok then queens(k+1, choice1) end;
mike@0 227 LDLC -13
mike@0 228 JNEQZ L16
mike@0 229 JUMP L18
mike@0 230 LABEL L16
mike@0 231 LOCAL 0
mike@0 232 GLOBAL _choice1
mike@0 233 LDLW 16
mike@0 234 CONST 1
mike@0 235 PLUS
mike@0 236 CONST 0
mike@0 237 GLOBAL _queens
mike@0 238 PCALL 3
mike@0 239 LABEL L18
mike@0 240 ! y := y+1
mike@0 241 LDLW -4
mike@0 242 CONST 1
mike@0 243 PLUS
mike@0 244 STLW -4
mike@0 245 LABEL L5
mike@0 246 LDLW -4
mike@0 247 CONST 8
mike@0 248 JLEQ L4
mike@0 249 LABEL L3
mike@0 250 RETURN
mike@0 251 END
mike@0 252
mike@0 253 PROC _choice1 0 0 0
mike@0 254 ! if x = k then
mike@0 255 LDLW 16
mike@0 256 LDLW 12
mike@0 257 LDNW 16
mike@0 258 JNEQ L20
mike@0 259 ! return y
mike@0 260 LDLW 12
mike@0 261 LDNW -4
mike@0 262 RETURNW
mike@0 263 LABEL L20
mike@0 264 ! return choice(x)
mike@0 265 LDLW 16
mike@0 266 LDLW 12
mike@0 267 LDNW 24
mike@0 268 LDLW 12
mike@0 269 LDNW 20
mike@0 270 PCALLW 1
mike@0 271 RETURNW
mike@0 272 END
mike@0 273
mike@0 274 PROC _print 4 0 0
mike@0 275 ! x := 1;
mike@0 276 CONST 1
mike@0 277 STLW -4
mike@0 278 ! while x <= N do
mike@0 279 JUMP L23
mike@0 280 LABEL L22
mike@0 281 ! print_num(choice(x));
mike@0 282 LDLW -4
mike@0 283 LDLW 20
mike@0 284 LDLW 16
mike@0 285 PCALLW 1
mike@0 286 CONST 0
mike@0 287 GLOBAL lib.print_num
mike@0 288 PCALL 1
mike@0 289 ! x := x+1
mike@0 290 LDLW -4
mike@0 291 CONST 1
mike@0 292 PLUS
mike@0 293 STLW -4
mike@0 294 LABEL L23
mike@0 295 LDLW -4
mike@0 296 CONST 8
mike@0 297 JLEQ L22
mike@0 298 ! newline()
mike@0 299 CONST 0
mike@0 300 GLOBAL lib.newline
mike@0 301 PCALL 0
mike@0 302 RETURN
mike@0 303 END
mike@0 304
mike@0 305 PROC _choice0 0 0 0
mike@0 306 ! return 0
mike@0 307 CONST 0
mike@0 308 RETURNW
mike@0 309 END
mike@0 310
mike@0 311 PROC MAIN 0 0 0
mike@0 312 ! queens(1, choice0)
mike@0 313 CONST 0
mike@0 314 GLOBAL _choice0
mike@0 315 CONST 1
mike@0 316 CONST 0
mike@0 317 GLOBAL _queens
mike@0 318 PCALL 3
mike@0 319 RETURN
mike@0 320 END
mike@0 321
mike@0 322 ! End
mike@0 323 ]]*)