Skip to content

Commit dd50c7a

Browse files
committed
Improved tests
1 parent 0e3a875 commit dd50c7a

File tree

1 file changed

+155
-88
lines changed

1 file changed

+155
-88
lines changed

compiler/shared/test/diff-ir/IRTailRec.mls

Lines changed: 155 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -6,173 +6,240 @@
66
:interpIR
77
class True
88
class False
9-
fun f(n) = g(n)
10-
fun g(m) = f(m)
11-
fun h() = h()
12-
2
13-
//│ |#class| |True|↵|#class| |False|↵|#fun| |f|(|n|)| |#=| |g|(|n|)|↵|#fun| |g|(|m|)| |#=| |f|(|m|)|↵|#fun| |h|(||)| |#=| |h|(||)|↵|2|
14-
//│ Parsed: {class True {}; class False {}; fun f = (n,) => g(n,); fun g = (m,) => f(m,); fun h = () => h(); 2}
9+
fun f(n) = if n == 0 then 0 else g(n - 1)
10+
fun g(m) = if m == 0 then 1 else f(m - 1)
11+
f(4)
12+
//│ |#class| |True|↵|#class| |False|↵|#fun| |f|(|n|)| |#=| |#if| |n| |==| |0| |#then| |0| |#else| |g|(|n| |-| |1|)|↵|#fun| |g|(|m|)| |#=| |#if| |m| |==| |0| |#then| |1| |#else| |f|(|m| |-| |1|)|↵|f|(|4|)|
13+
//│ Parsed: {class True {}; class False {}; fun f = (n,) => if (==(n,)(0,)) then 0 else g(-(n,)(1,),); fun g = (m,) => if (==(m,)(0,)) then 1 else f(-(m,)(1,),); f(4,)}
1514
//│
1615
//│
1716
//│ IR:
1817
//│ Program({ClassInfo(0, True, []),ClassInfo(1, False, [])}, {
1918
//│ Def(0, f, [n$0],
2019
//│ 1,
21-
//│ let* (x$0) = g(n$0) in -- #5
22-
//│ x$0 -- #4
20+
//│ let x$0 = ==(n$0,0) in -- #17
21+
//│ if x$0 -- #16
22+
//│ true =>
23+
//│ jump j$0(0) -- #5
24+
//│ false =>
25+
//│ let x$2 = -(n$0,1) in -- #15
26+
//│ let* (x$3) = g(x$2) in -- #14
27+
//│ jump j$0(x$3) -- #13
28+
//│ )
29+
//│ Def(1, j$0, [x$1],
30+
//│ 1,
31+
//│ x$1 -- #3
2332
//│ )
24-
//│ Def(1, g, [m$0],
33+
//│ Def(2, g, [m$0],
2534
//│ 1,
26-
//│ let* (x$1) = f(m$0) in -- #11
27-
//│ x$1 -- #10
35+
//│ let x$4 = ==(m$0,0) in -- #35
36+
//│ if x$4 -- #34
37+
//│ true =>
38+
//│ jump j$1(1) -- #23
39+
//│ false =>
40+
//│ let x$6 = -(m$0,1) in -- #33
41+
//│ let* (x$7) = f(x$6) in -- #32
42+
//│ jump j$1(x$7) -- #31
2843
//│ )
29-
//│ Def(2, h, [],
44+
//│ Def(3, j$1, [x$5],
3045
//│ 1,
31-
//│ let* (x$2) = h() in -- #15
32-
//│ x$2 -- #14
46+
//│ x$5 -- #21
3347
//│ )
3448
//│ },
35-
//│ 2 -- #16)
49+
//│ let* (x$8) = f(4) in -- #41
50+
//│ x$8 -- #40)
3651
//│ Program({ClassInfo(0, True, []),ClassInfo(1, False, [])}, {
3752
//│ Def(0, f, [n$0],
3853
//│ 1,
39-
//│ let* (x$0) = g(n$0) in -- #5
40-
//│ x$0 -- #4
54+
//│ let x$0 = ==(n$0,0) in -- #17
55+
//│ if x$0 -- #16
56+
//│ true =>
57+
//│ jump j$0(0) -- #5
58+
//│ false =>
59+
//│ let x$2 = -(n$0,1) in -- #15
60+
//│ let* (x$3) = g(x$2) in -- #14
61+
//│ jump j$0(x$3) -- #13
62+
//│ )
63+
//│ Def(1, j$0, [x$1],
64+
//│ 1,
65+
//│ x$1 -- #3
4166
//│ )
42-
//│ Def(1, g, [m$0],
67+
//│ Def(2, g, [m$0],
4368
//│ 1,
44-
//│ let* (x$1) = f(m$0) in -- #11
45-
//│ x$1 -- #10
69+
//│ let x$4 = ==(m$0,0) in -- #35
70+
//│ if x$4 -- #34
71+
//│ true =>
72+
//│ jump j$1(1) -- #23
73+
//│ false =>
74+
//│ let x$6 = -(m$0,1) in -- #33
75+
//│ let* (x$7) = f(x$6) in -- #32
76+
//│ jump j$1(x$7) -- #31
4677
//│ )
47-
//│ Def(2, h, [],
78+
//│ Def(3, j$1, [x$5],
4879
//│ 1,
49-
//│ let* (x$2) = h() in -- #15
50-
//│ x$2 -- #14
80+
//│ x$5 -- #21
5181
//│ )
5282
//│ },
53-
//│ 2 -- #16)
83+
//│ let* (x$8) = f(4) in -- #41
84+
//│ x$8 -- #40)
5485
//│
5586
//│ Promoted:
5687
//│ Program({ClassInfo(0, True, []),ClassInfo(1, False, [])}, {
5788
//│ Def(0, f, [n$0],
5889
//│ 1,
59-
//│ let* (x$0) = g(n$0) in -- #5
60-
//│ x$0 -- #4
90+
//│ let x$0 = ==(n$0,0) in -- #17
91+
//│ if x$0 -- #16
92+
//│ true =>
93+
//│ jump j$0(0) -- #5
94+
//│ false =>
95+
//│ let x$2 = -(n$0,1) in -- #15
96+
//│ let* (x$3) = g(x$2) in -- #14
97+
//│ jump j$0(x$3) -- #13
98+
//│ )
99+
//│ Def(1, j$0, [x$1],
100+
//│ 1,
101+
//│ x$1 -- #3
61102
//│ )
62-
//│ Def(1, g, [m$0],
103+
//│ Def(2, g, [m$0],
63104
//│ 1,
64-
//│ let* (x$1) = f(m$0) in -- #11
65-
//│ x$1 -- #10
105+
//│ let x$4 = ==(m$0,0) in -- #35
106+
//│ if x$4 -- #34
107+
//│ true =>
108+
//│ jump j$1(1) -- #23
109+
//│ false =>
110+
//│ let x$6 = -(m$0,1) in -- #33
111+
//│ let* (x$7) = f(x$6) in -- #32
112+
//│ jump j$1(x$7) -- #31
66113
//│ )
67-
//│ Def(2, h, [],
114+
//│ Def(3, j$1, [x$5],
68115
//│ 1,
69-
//│ let* (x$2) = h() in -- #15
70-
//│ x$2 -- #14
116+
//│ x$5 -- #21
71117
//│ )
72118
//│ },
73-
//│ 2 -- #16)
119+
//│ let* (x$8) = f(4) in -- #41
120+
//│ x$8 -- #40)
74121
//│
75122
//│ Interpreted:
76-
//│ 2
123+
//│ 0
77124

78-
// remark: this really should be changed so we don't need to add `class True` every time we want to use conditionals
79125
:interpIR
80126
class True
81127
class False
82-
fun f(n) = g(n)
83-
fun g(m) = f(m)
84-
fun h() = h()
85-
2
86-
//│ |#class| |True|↵|#class| |False|↵|#fun| |f|(|n|)| |#=| |g|(|n|)|↵|#fun| |g|(|m|)| |#=| |f|(|m|)|↵|#fun| |h|(||)| |#=| |h|(||)|↵|2|
87-
//│ Parsed: {class True {}; class False {}; fun f = (n,) => g(n,); fun g = (m,) => f(m,); fun h = () => h(); 2}
128+
fun f(n) = if n == 0 then 0 else g(n - 1)
129+
fun g(m) = if m == 0 then 1 else f(m - 1)
130+
f(4)
131+
//│ |#class| |True|↵|#class| |False|↵|#fun| |f|(|n|)| |#=| |#if| |n| |==| |0| |#then| |0| |#else| |g|(|n| |-| |1|)|↵|#fun| |g|(|m|)| |#=| |#if| |m| |==| |0| |#then| |1| |#else| |f|(|m| |-| |1|)|↵|f|(|4|)|
132+
//│ Parsed: {class True {}; class False {}; fun f = (n,) => if (==(n,)(0,)) then 0 else g(-(n,)(1,),); fun g = (m,) => if (==(m,)(0,)) then 1 else f(-(m,)(1,),); f(4,)}
88133
//│
89134
//│
90135
//│ IR:
91136
//│ Program({ClassInfo(0, True, []),ClassInfo(1, False, [])}, {
92137
//│ Def(0, f, [n$0],
93138
//│ 1,
94-
//│ let* (x$0) = g(n$0) in -- #5
95-
//│ x$0 -- #4
139+
//│ let x$0 = ==(n$0,0) in -- #17
140+
//│ if x$0 -- #16
141+
//│ true =>
142+
//│ jump j$0(0) -- #5
143+
//│ false =>
144+
//│ let x$2 = -(n$0,1) in -- #15
145+
//│ let* (x$3) = g(x$2) in -- #14
146+
//│ jump j$0(x$3) -- #13
147+
//│ )
148+
//│ Def(1, j$0, [x$1],
149+
//│ 1,
150+
//│ x$1 -- #3
96151
//│ )
97-
//│ Def(1, g, [m$0],
152+
//│ Def(2, g, [m$0],
98153
//│ 1,
99-
//│ let* (x$1) = f(m$0) in -- #11
100-
//│ x$1 -- #10
154+
//│ let x$4 = ==(m$0,0) in -- #35
155+
//│ if x$4 -- #34
156+
//│ true =>
157+
//│ jump j$1(1) -- #23
158+
//│ false =>
159+
//│ let x$6 = -(m$0,1) in -- #33
160+
//│ let* (x$7) = f(x$6) in -- #32
161+
//│ jump j$1(x$7) -- #31
101162
//│ )
102-
//│ Def(2, h, [],
163+
//│ Def(3, j$1, [x$5],
103164
//│ 1,
104-
//│ let* (x$2) = h() in -- #15
105-
//│ x$2 -- #14
165+
//│ x$5 -- #21
106166
//│ )
107167
//│ },
108-
//│ 2 -- #16)
168+
//│ let* (x$8) = f(4) in -- #41
169+
//│ x$8 -- #40)
109170
//│
110171
//│ Strongly Connected Tail Calls:
111-
//│ List(Set(h), Set(g, f))
172+
//│ List(Set(g), Set(j$1), Set(f), Set(j$0))
112173
//│ Program({ClassInfo(0, True, []),ClassInfo(1, False, [])}, {
113174
//│ Def(0, f, [n$0],
114175
//│ 1,
115-
//│ let* (r0) = _g_f_opt(0,n$0,0) in -- #26
116-
//│ r0 -- #25
117-
//│ )
118-
//│ Def(1, g, [m$0],
119-
//│ 1,
120-
//│ let* (r0) = _g_f_opt(1,0,m$0) in -- #24
121-
//│ r0 -- #23
176+
//│ let x$0 = ==(n$0,0) in -- #17
177+
//│ if x$0 -- #16
178+
//│ true =>
179+
//│ jump j$0(0) -- #5
180+
//│ false =>
181+
//│ let x$2 = -(n$0,1) in -- #15
182+
//│ let* (x$3) = g(x$2) in -- #14
183+
//│ jump j$0(x$3) -- #13
122184
//│ )
123-
//│ Def(2, h, [],
185+
//│ Def(1, j$0, [x$1],
124186
//│ 1,
125-
//│ let* (x$2) = h() in -- #15
126-
//│ x$2 -- #14
187+
//│ x$1 -- #3
127188
//│ )
128-
//│ Def(3, _g_f_opt_jp, [tailrecBranch,g_m$0,f_n$0],
189+
//│ Def(2, g, [m$0],
129190
//│ 1,
130-
//│ let scrut = ==(0,tailrecBranch) in -- #21
131-
//│ if scrut -- #19
191+
//│ let x$4 = ==(m$0,0) in -- #35
192+
//│ if x$4 -- #34
132193
//│ true =>
133-
//│ jump _g_f_opt_jp(0,f_n$0,f_n$0) -- #18
194+
//│ jump j$1(1) -- #23
134195
//│ false =>
135-
//│ jump _g_f_opt_jp(1,g_m$0,g_m$0) -- #17
196+
//│ let x$6 = -(m$0,1) in -- #33
197+
//│ let* (x$7) = f(x$6) in -- #32
198+
//│ jump j$1(x$7) -- #31
136199
//│ )
137-
//│ Def(4, _g_f_opt, [tailrecBranch,g_m$0,f_n$0],
200+
//│ Def(3, j$1, [x$5],
138201
//│ 1,
139-
//│ jump _g_f_opt_jp(tailrecBranch,g_m$0,f_n$0) -- #22
202+
//│ x$5 -- #21
140203
//│ )
141204
//│ },
142-
//│ 2 -- #16)
205+
//│ let* (x$8) = f(4) in -- #41
206+
//│ x$8 -- #40)
143207
//│
144208
//│ Promoted:
145209
//│ Program({ClassInfo(0, True, []),ClassInfo(1, False, [])}, {
146210
//│ Def(0, f, [n$0],
147211
//│ 1,
148-
//│ let* (r0) = _g_f_opt(0,n$0,0) in -- #26
149-
//│ r0 -- #25
150-
//│ )
151-
//│ Def(1, g, [m$0],
152-
//│ 1,
153-
//│ let* (r0) = _g_f_opt(1,0,m$0) in -- #24
154-
//│ r0 -- #23
212+
//│ let x$0 = ==(n$0,0) in -- #17
213+
//│ if x$0 -- #16
214+
//│ true =>
215+
//│ jump j$0(0) -- #5
216+
//│ false =>
217+
//│ let x$2 = -(n$0,1) in -- #15
218+
//│ let* (x$3) = g(x$2) in -- #14
219+
//│ jump j$0(x$3) -- #13
155220
//│ )
156-
//│ Def(2, h, [],
221+
//│ Def(1, j$0, [x$1],
157222
//│ 1,
158-
//│ let* (x$2) = h() in -- #15
159-
//│ x$2 -- #14
223+
//│ x$1 -- #3
160224
//│ )
161-
//│ Def(3, _g_f_opt_jp, [tailrecBranch,g_m$0,f_n$0],
225+
//│ Def(2, g, [m$0],
162226
//│ 1,
163-
//│ let scrut = ==(0,tailrecBranch) in -- #21
164-
//│ if scrut -- #19
227+
//│ let x$4 = ==(m$0,0) in -- #35
228+
//│ if x$4 -- #34
165229
//│ true =>
166-
//│ jump _g_f_opt_jp(0,f_n$0,f_n$0) -- #18
230+
//│ jump j$1(1) -- #23
167231
//│ false =>
168-
//│ jump _g_f_opt_jp(1,g_m$0,g_m$0) -- #17
232+
//│ let x$6 = -(m$0,1) in -- #33
233+
//│ let* (x$7) = f(x$6) in -- #32
234+
//│ jump j$1(x$7) -- #31
169235
//│ )
170-
//│ Def(4, _g_f_opt, [tailrecBranch,g_m$0,f_n$0],
236+
//│ Def(3, j$1, [x$5],
171237
//│ 1,
172-
//│ jump _g_f_opt_jp(tailrecBranch,g_m$0,f_n$0) -- #22
238+
//│ x$5 -- #21
173239
//│ )
174240
//│ },
175-
//│ 2 -- #16)
241+
//│ let* (x$8) = f(4) in -- #41
242+
//│ x$8 -- #40)
176243
//│
177244
//│ Interpreted:
178-
//│ 2
245+
//│ 0

0 commit comments

Comments
 (0)