-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamic.dats
73 lines (65 loc) · 1.74 KB
/
dynamic.dats
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
// hello from 2012!
//
// compile with atscc, compiler for ATS, an ML dialect
// see <www.ats-lang.org>
staload _ = "prelude/DATS/list.dats"
staload _ = "prelude/DATS/string.dats"
// the type of dynamic terms
// NOTE: using an abstract type here would be true
// to the spirit of dynamically typed languages
datatype DT =
| DTint of int | DTstr of string | DTlst of List DT
| DTfun of DT -<1> DT
// printing
fun put (x: DT): void = case+ x of
| DTint x => print x
| DTstr x => print x
| DTlst xs => let
fun loop (xs: List DT): void = case+ xs of
| list_cons (x, xs) => begin
put x;
if list_is_cons xs then print ", ";
loop xs
end // end of [begin]
| list_nil () => () // nothing
in
print "["; loop xs; print "]"
end // end of [let]
| DTfun f => print "<fun>"
fun len (x: DT): int = case+ x of
| DTlst xs => list_length<DT> (xs)
| DTstr s => int_of_size (string_length s)
| _ => begin
prerr "[len]: type error\n";
exit {int} (1)
end
fun hd (x: DT): DT = case+ x of
| DTlst xs => begin
case+ xs of
| list_cons (x, xss) => x
| list_nil () => begin
prerr ("[hd]: list is empty\n");
exit {DT} (1)
end
end
| _ => begin
prerr "[hd]: type error\n";
exit {DT} (1)
end
fun sort (x: &DT): void = () // TODO: implement; only valid on lists but not strings
fun test (l: &DT): void = begin
if len l > 0 then begin
l := hd l;
put l
end;
if len l <= 0 then begin
sort l
end
end
implement main () = let
// we may as well introduce more operations on [DT]
// to ease constructing "dynamically-typed" terms
var l = DTlst (list_cons (DTstr "hello", list_cons (DTint 5, list_nil ())))
in
test l
end // end of [main]