Skip to content

Commit 2b97f0d

Browse files
committed
Add tests for resymbolization
1 parent 7b0201a commit 2b97f0d

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

src/stdlib/mexpr/resymbolize.mc

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
include "mexpr/ast.mc"
88
include "mexpr/eq.mc"
99
include "mexpr/pprint.mc"
10+
include "mexpr/symbolize.mc"
1011

1112
lang Resymbolize = Ast
1213
sem resymbolizeBindings : Expr -> Expr
@@ -221,3 +222,55 @@ lang MExprResymbolize =
221222
sem resymbolizeType nameMap =
222223
| ty -> smap_Type_Type (resymbolizeType nameMap) ty
223224
end
225+
226+
lang TestLang = MExprResymbolize + MExprEq + MExprPrettyPrint + MExprSym
227+
sem collectSymVars : Expr -> Map String Name
228+
sem collectSymVars =
229+
| e -> collectSymVarsH (mapEmpty cmpString) e
230+
231+
sem collectSymVarsH : Map String Name -> Expr -> Map String Name
232+
sem collectSymVarsH acc =
233+
| TmVar t ->
234+
if nameHasSym t.ident then mapInsert (nameGetStr t.ident) t.ident acc
235+
else acc
236+
| t -> sfold_Expr_Expr collectSymVarsH acc t
237+
end
238+
239+
mexpr
240+
241+
use TestLang in
242+
243+
let optionGet = lam o. optionGetOrElse (lam. never) o in
244+
let nameNotEq = lam l. lam r. not (nameEq l r) in
245+
246+
-- Unsymbolized variables in the AST are symbolized
247+
let e = resymbolizeBindings (var_ "x") in
248+
let syms = collectSymVars e in
249+
utest mapMem "x" syms with false in
250+
251+
-- Variables bound in the AST are re-symbolized
252+
let x = nameSym "x" in
253+
let e = resymbolizeBindings (bind_ (nulet_ x (int_ 2)) (nvar_ x)) in
254+
let syms = collectSymVars e in
255+
utest mapMem "x" syms with true in
256+
utest optionGet (mapLookup "x" syms) with x using nameNotEq in
257+
258+
-- Unsymbolized variables that are bound in the AST are symbolized
259+
let e = resymbolizeBindings (bind_ (ulet_ "x" (int_ 2)) (var_ "x")) in
260+
let syms = collectSymVars e in
261+
utest mapMem "x" syms with true in
262+
263+
-- Symbolized free variables are not re-symbolized
264+
let y = nameSym "y" in
265+
let e = resymbolizeBindings
266+
(bind_
267+
(nulet_ x (int_ 2))
268+
(addi_ (nvar_ x) (nvar_ y)))
269+
in
270+
let syms = collectSymVars e in
271+
utest mapMem "x" syms with true in
272+
utest mapMem "y" syms with true in
273+
utest optionGet (mapLookup "x" syms) with x using nameNotEq in
274+
utest optionGet (mapLookup "y" syms) with y using nameEq in
275+
276+
()

0 commit comments

Comments
 (0)