File: hanoi.dats

package info (click to toggle)
ats2-lang 0.1.3-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 28,136 kB
  • ctags: 20,441
  • sloc: ansic: 354,696; makefile: 2,679; sh: 638
file content (203 lines) | stat: -rw-r--r-- 3,344 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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
134
135
136
137
138
139
140
141
142
143
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
(*
//
// Implementing the Hanoi Tower problem
//
// The code was first written by Hongwei Xi in the summer of 2004
// The code is ported to ATS2 by Hongwei Xi on the last day of May, 2012
//
*)

(* ****** ****** *)
//
#include
"share/atspre_staload.hats"
//
(* ****** ****** *)
//
abstype
post_type (n:int) = ptr // for posts
typedef post (n:int) = post_type (n)
//
(* ****** ****** *)

extern
fun post_make {sz:pos} (sz: int sz): post (sz)
extern
fun post_initize {sz:nat} (p: post (sz), sz: int sz): void

(* ****** ****** *)

extern
fun post_get_at
  {sz:int} (p: post (sz), i: natLt sz): natLte (sz)
overload [] with post_get_at
extern
fun post_set_at
  {sz:int} (p: post (sz), i: natLt sz, x: natLte (sz)): void
overload [] with post_set_at

(* ****** ****** *)

fun
showpiece
  {sz:int}
(
  sz: int sz, n: natLte sz
) : void = let
//
fun loop
{
  i:nat | i <= 2*sz
} .<2*sz-i>.
(
  i: int (i)
) :<cloref1> void = let
in
//
if i < (sz-n) then
(
  print ' '; loop (i + 1)
) else if i < (sz+n-1) then
(
  print 'O'; loop (i + 1)
) else if i < (sz + sz) then
(
  print ' '; loop (i + 1)
) // end of [if]
//
end // end of [loop]
//
in
  loop (0)
end // end of [showpiece]

(* ****** ****** *)

fun play
  {sz:pos} .<>.
  (sz: int sz): void = let
//
//
val lp = post_make (sz)
val mp = post_make (sz)
val rp = post_make (sz)
//
fun showposts
  .<>. (
  (*argumentless*)
) :<cloref1> void = let
//
fun loop
  {i:nat | i <= sz} (
  i: int i
) :<cloref1> void = let
in
  if sz > i then begin
    showpiece (sz, lp[i]);
    showpiece (sz, mp[i]);
    showpiece (sz, rp[i]);
    print_newline (); 
    loop (i + 1)
  end else begin
    print_newline ()
  end // end of [if]
end // end of [loop]
//
in
  loop (0)
end // end of [showposts]
//
val () = post_initize (lp, sz)
//
viewtypedef post = post (sz)
//
fun move
{
  n,s,p,p':nat
| p <= sz &&
  p' <= sz &&
  s + p + p' == sz + sz &&
  n > 0 &&
  s + n <= sz &&
  n <= p &&
  n <= p'
} .<n>. (
  n: int n
, src: post, s: int s,
  post: post, p: int p
, post': post, p': int p'
) :<cloref1> void = (
  if n = 1 then (
    post[p-1] := src[s]; src[s] := 0; showposts ()
  ) else (
    move (
      n-1, src, s, post', p', post, p
    ) ; // end of [move]
    post[p-1] := src[s+n-1]; src[s+n-1] := 0; showposts ();
    move (
      n-1, post', p' - n + 1, post, p - 1, src, s + n
    ) ; // end of [move]
  ) (* end of [if] *)
) (* end of [move] *)
//
in
//
  showposts ();
  move (sz, lp, 0, rp, sz, mp, sz);
  print ("This round of play has finished.");
  print_newline ();
//
end // end of [play]

(* ****** ****** *)

implement
main (
  argc, argv
) = let
  val () = play (4) in 0(*normalexit*)
end // end of [main]

(* ****** ****** *)

local

typedef
T (n:int) = natLte n
assume
post_type (n:int) = arrayref (T(n), n)

in // in of [local]

implement
post_make {sz} (sz) =
  arrayref_make_elt (g1int2uint(sz), 0)
// end of [post_make]

implement
post_initize
  {sz} (p, sz) = let
//
fun loop {
  i:nat | i <= sz
} .<sz-i>. (
  i: int i
) :<cloref1> void =
  if i < sz then let
    val i1 = succ(i) in arrayref_set_at (p, i, i1) ; loop (i1)
  end // end of [if]
//
in
  loop (0)
end // end of [post_initize]

implement
post_get_at (p, i) = arrayref_get_at (p, i)
implement
post_set_at (p, i, x) = arrayref_set_at (p, i, x)

end // end of [local]

(* ****** ****** *)

(* end of [hanoi.dats] *)