File: word-chain.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 (370 lines) | stat: -rw-r--r-- 5,935 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
//
// For chaining words based on
// their occurrences in a given file
//
(* ****** ****** *)
//
// HX-2014-06
//
(* ****** ****** *)
//
#include
"share/atspre_staload.hats"
//
#include
"share/HATS/atspre_staload_libats_ML.hats"
//
(* ****** ****** *)
//
staload UN = $UNSAFE
//
(* ****** ****** *)
//
staload "libats/SATS/qlist.sats"
staload _ = "libats/DATS/qlist.dats"
//
(* ****** ****** *)
//
staload "libats/SATS/hashfun.sats"
//
(* ****** ****** *)
//
typedef word = string
//
typedef wordlst = List0 (word)
typedef wordlst(n:int) = list(word, n)
vtypedef wordlst_vt(n:int) = list_vt(word, n)
//
vtypedef wordque(n:int) = qlist(word, n)
//
(* ****** ****** *)

extern
fun
wordque_insert_list
  {n0:int}{n:int}
(
  wq: !wordque(n0) >> wordque(n0+n), ws: wordlst(n)
) : void // end of [wordque_insert_list]

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

implement
wordque_insert_list
  (wq, ws) =
(
  case+ ws of
  | nil () => ()
  | cons (w, ws) => let
      val () = qlist_insert (wq, w) in wordque_insert_list (wq, ws)
    end // end of [cons]
)

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

extern
fun{}
word_get (): stropt
implement
word_get<> () = let
//
implement
fileref_get_word$isalpha<>
  (letter) = not(isspace(letter))
//
in
//
strptr2stropt (fileref_get_word (stdin_ref))
//
end // end of [word_get]

(* ****** ****** *)
//
extern
fun
stringlst_hash
  (xs: List0(string)): ulint
//
(* ****** ****** *)

implement
stringlst_hash (xs) = let
//
fun loop
(
  K: ulint, H: ulint, xs: List0(string)
) : ulint =
(
case+ xs of
| list_nil () => H
| list_cons (x, xs) => let
    val H = string_hash_multiplier (K, H, x)
  in
    loop (K, H, xs)
  end // end of [list_cons]
)
//
in
  loop (33ul(*K*), 31415926536ul(*H0*), xs)
end // end of [stringlst_hash]

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

abstype wordmap_type = ptr
typedef wordmap = wordmap_type

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

extern
fun wordmap_make_nil (): wordmap

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

extern
fun
wordmap_find
  {n:int | n > 0}
(
  map: wordmap, ws: wordlst(n)
) : wordlst // end-of-fun

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

extern
fun
wordmap_insert
  {n:int | n > 0}
(
  map: wordmap, ws: wordlst(n), w: word
) : wordmap // end of [wordmap_insert]

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

extern
fun{}
fprint_wordmap
(
  out: FILEref, map: wordmap
) : void // end of [fprint_wordmap]
//
overload fprint with fprint_wordmap
//
(* ****** ****** *)

local
//
typedef key = list0(word)
typedef itm = list0(word)
//
assume
wordmap_type = hashtbl (key, itm)
//
implement
hash_key<key> (k) =
  $effmask_all (stringlst_hash (g1ofg0(k)))
//
implement
equal_key_key<key>
  (k1, k2) = list_equal<string> (g1ofg0(k1), g1ofg0(k2))
//
in (* in-of-local *)

implement
wordmap_make_nil
(
// argumentless
) = hashtbl_make_nil (i2sz(4096))

implement
wordmap_find
  (map, ws) = let
//
val ws = g0ofg1_list (ws)
val res = hashtbl_search_ref (map, ws)
val isnot = cptr_isnot_null (res)
//
in
//
if isnot
  then g1ofg0($UN.cptr_get(res)) else list_nil((*void*))
//
end // end of [wordmap_find]

implement
wordmap_insert
  (map, ws, w) = let
//
val ws = g0ofg1_list (ws)
val res = hashtbl_search_ref (map, ws)
val isnot = cptr_isnot_null (res)
//
in
//
if
isnot
then let
  val ws = $UN.cptr_get (res)
  val () = $UN.cptr_set (res, cons0 (w, ws))
in
  map
end // end of [then]
else let
  val () = hashtbl_insert_any (map, ws, list0_sing (w))
in
  map
end // end of [else]
//
end // end of [wordmap_insert]

implement
{}(*tmp*)
fprint_wordmap (out, map) = fprint_hashtbl (out, map)

end // end of [local]

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

extern
fun{}
wordmap_build{n:pos}(ws: wordlst(n)): wordmap

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

implement
{}(*tmp*)
wordmap_build
  {n}(ws0) = let
//
fun loop
(
  map: wordmap, wq: qlist(word, n)
) : wordmap = let
//
val opt = word_get ()
val issome = stropt_is_some (opt)
//
in
//
if
issome
then let
  val w = stropt_unsome (opt)
  val ws = qlist_takeout_list (wq)
  val ws = list_vt2t (ws)
  val map = wordmap_insert (map, ws, w)
  val+list_cons (_, ws) = ws
  val () = wordque_insert_list (wq, ws)
  val () = qlist_insert<word> (wq, w)
in
  loop (map, wq)
end // end of [then]
else let
  val () =
    free (qlist_takeout_list (wq))
  val () = qlist_free_nil{word} (wq) in (map)
end // end of [else]
//
end // end of [loop]
//
val map = wordmap_make_nil ()
val wq0 = qlist_make_nil{word}()
val ((*void*)) = wordque_insert_list (wq0, ws0)
//
in
  loop (map, wq0)
end // end of [wordmap_build]

(* ****** ****** *)
//
staload
STDLIB = "libc/SATS/stdlib.sats"
//
extern
fun
wordlst_choose
  {n:pos}(ws: wordlst(n), n: int(n)): word
//
implement
wordlst_choose
  {n} (ws, n) = let
  val x = $STDLIB.random()
  val i = $UN.cast{natLt(n)}(x mod $UN.cast2lint(n))
in
  list_get_at (ws, i)
end // end of [wordlst_choose]
//
(* ****** ****** *)
//
extern
fun
wordmap_nchoose{n:pos}
  (map: wordmap, ws0: wordlst(n), N: int): void
//
(* ****** ****** *)

implement
wordmap_nchoose
  {n}(map, ws0, N) = let
//
fun loop
(
  ws: wordlst_vt(n), i: int
) : void =
(
if
(i > 0)
then let
  typedef key = list0(word)
  typedef itm = list0(word)
  val ws_itm =
    wordmap_find (map, $UN.list_vt2t(ws))
  val n = length (ws_itm)
//
  val () = assertloc (n > 0)
  val w_chosen = wordlst_choose (ws_itm, n)
  val () = fprint! (stdout_ref, ' ', w_chosen)
//
  val+~list_vt_cons (_, ws) = ws
  val ws = list_vt_extend (ws, w_chosen)
in
  loop (ws, pred(i))
end // end of [then]
else list_vt_free (ws)
)
//
in
  loop (list_copy (ws0), N)
end // end of [wordmap_nchoose]

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

implement
main0 () = () where
{
//
val ws0 =
  $list{word}("", "")
//
val map = wordmap_build (ws0)
//
(*
local
implement
fprint_hashtbl$sep<> = fprint_newline
implement
fprint_hashtbl$mapto<> (out) = fprint (out, " => ")
in (*in-of-local*)
val () = fprintln! (stdout_ref, "map = ", map)
end // end of [local]
*)
//
val ws1 =
  $list{word}("", "")
val () = fprint_list_sep (stdout_ref, ws1, " ")
val () = wordmap_nchoose (map, ws1, 250)
//
} (* end of [main0] *)

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

(* end of [word-chain.dats] *)