Skip to content

Commit da579fe

Browse files
committed
Generating simple types and examples
1 parent 825c45e commit da579fe

File tree

1 file changed

+139
-0
lines changed

1 file changed

+139
-0
lines changed

interpreter.sml

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
structure Interpreter =
2+
struct
3+
4+
datatype basetp = TBool | TRange of int * int | TString of string list
5+
6+
datatype gentp =
7+
TSum of (string * gentp) list
8+
| TProd of (string * gentp) list
9+
| TBase of basetp
10+
| TOne
11+
(* | Rectp of ident * gentp | Var of ident *)
12+
13+
type ident = int
14+
15+
datatype exp =
16+
Inj of string * exp
17+
| Tuple of (string * exp) list
18+
| Case of exp * ((string * (ident list) * exp) list)
19+
| Var of ident
20+
| EUnit
21+
| ETrue
22+
| EFalse
23+
| Num of int
24+
| Str of string
25+
| EBool of bool
26+
27+
val rando = Random.rand (19627, 9212987)
28+
29+
fun randInRange min max = Random.randRange (min, max) rando
30+
31+
fun randMember (l : 'a list) : 'a =
32+
let
33+
val idx = randInRange 0 (List.length l - 1)
34+
in
35+
List.nth (l, idx)
36+
end
37+
38+
fun genbase (b : basetp) : exp =
39+
case b of
40+
TBool => randMember [EBool true, EBool false]
41+
| TRange (min, max) => Num (randInRange min max)
42+
| TString options => Str (randMember options)
43+
44+
45+
fun gen (tau : gentp) : exp =
46+
case tau of
47+
TOne => EUnit
48+
| TBase b => genbase b
49+
| TProd tps => Tuple (map (fn (s, t) => (s, gen t)) tps)
50+
| TSum tps =>
51+
let
52+
val (s, t) = randMember tps
53+
in
54+
Inj (s, gen t)
55+
end
56+
57+
fun lookup kvs k =
58+
case kvs of
59+
[] => raise Match
60+
| ((k',v)::kvs) => if k=k' then v else lookup kvs k
61+
62+
fun proj (elems : (string * exp) list) (field : string) =
63+
lookup elems field
64+
65+
fun projStr elems field =
66+
let
67+
val Str s = lookup elems field
68+
in s end
69+
70+
(* Tests *)
71+
72+
val abcs = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
73+
"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
74+
75+
val monsterTypes = ["Zombie", "Vampire", "Ogre", "Dragon", "Orc", "Goblin",
76+
"Witch", "Grue", "Warlock", "Werewolf"]
77+
78+
val TAlphabet = TBase (TString abcs)
79+
val TDigits = TBase (TRange (0, 9))
80+
val TMonsterType = TBase (TString monsterTypes)
81+
82+
val StrongRange = TBase (TRange (5,9))
83+
val WeakRange = TBase (TRange (1,4))
84+
val TMonster = TProd [("Type", TMonsterType),
85+
("Stats", TSum [("Strong", StrongRange), ("Weak", WeakRange)])]
86+
87+
88+
(* Generate monster *)
89+
fun genMonster () = gen TMonster
90+
91+
(* Generate a list of monsters *)
92+
fun dungeon n = List.tabulate (n, fn i => genMonster ())
93+
94+
(* Example ideas:
95+
* - hearthstone cards
96+
* - Tracery stories
97+
*
98+
* *)
99+
100+
(* Tracery Stories *)
101+
val they = TBase (TString ["they"])
102+
val them = TBase (TString ["them"])
103+
val their = TBase (TString ["their"])
104+
val theirs = TBase (TString ["theirs"])
105+
val themself = TBase (TString ["themself"])
106+
107+
val she = TBase (TString ["she"])
108+
val her = TBase (TString ["her"])
109+
110+
val he = TBase (TString ["he"])
111+
val him = TBase (TString ["him"])
112+
val his = TBase (TString ["his"])
113+
114+
val they_pronouns = TProd [("they", they), ("them", them), ("their", their)]
115+
val she_pronouns = TProd [("they", she), ("them", her), ("their", her)]
116+
val he_pronouns = TProd [("they", he), ("them", him), ("their", his)]
117+
118+
val TPronouns = TSum [("they", they_pronouns), ("he", he_pronouns),
119+
("she", she_pronouns)]
120+
121+
122+
fun genPronouns () =
123+
let
124+
val (Inj (_, Tuple heroPronouns)) = gen TPronouns
125+
in
126+
heroPronouns
127+
end
128+
129+
fun genStory () =
130+
let
131+
val heroPronouns = genPronouns ()
132+
in
133+
"Our hero went into the dungeons to find the treasure. "
134+
^ (projStr heroPronouns "they") ^ " descended into the final gave, drew "
135+
^ (projStr heroPronouns "their") ^ " sword, and fought the beast who faced "
136+
^ (projStr heroPronouns "them") ^ "."
137+
end
138+
139+
end

0 commit comments

Comments
 (0)