/src/hobbes/lib/hobbes/lang/macroexpand.C
Line | Count | Source |
1 | | |
2 | | #include <hobbes/lang/macroexpand.H> |
3 | | #include <hobbes/lang/tylift.H> |
4 | | #include <set> |
5 | | |
6 | | namespace hobbes { |
7 | | |
8 | | // walk the expression tree, find all macro references, and expand them |
9 | | // (because this should be called after type inference, we need to preserve explicit type annotations) |
10 | | struct macroExpandF : public switchExprC<ExprPtr> { |
11 | | std::set<std::string> boundNames; |
12 | | |
13 | 11.9k | macroExpandF() = default; |
14 | 248k | explicit macroExpandF(const std::set<std::string>& boundNames) : boundNames(boundNames) { } |
15 | | |
16 | 238k | macroExpandF under(const std::string& name) const { |
17 | 238k | auto names = this->boundNames; |
18 | 238k | names.insert(name); |
19 | 238k | return macroExpandF(names); |
20 | 238k | } |
21 | | |
22 | 9.92k | macroExpandF under(const str::seq& names) const { |
23 | 9.92k | auto result = this->boundNames; |
24 | 9.92k | result.insert(names.begin(), names.end()); |
25 | 9.92k | return macroExpandF(result); |
26 | 9.92k | } |
27 | | |
28 | 17.7M | static ExprPtr mk(const QualTypePtr& qty, Expr* out) { |
29 | 17.7M | out->type(qty); |
30 | 17.7M | return ExprPtr(out); |
31 | 17.7M | } |
32 | 470k | static ExprPtr mk(const MonoTypePtr& mty, Expr* out) { |
33 | 470k | return mk(qualtype(mty), out); |
34 | 470k | } |
35 | 17.0M | static ExprPtr mk(const Expr* src, Expr* out) { |
36 | 17.0M | return mk(src->type(), out); |
37 | 17.0M | } |
38 | | |
39 | 2.53M | ExprPtr withConst(const Expr* v) const override { return mk(v, v->clone()); } |
40 | 9.88k | ExprPtr with(const Fn* v) const override { auto body = under(v->varNames()); return mk(v, new Fn(v->varNames(), switchOf(v->body(), body), v->la())); } |
41 | 668 | ExprPtr with(const MkArray* v) const override { return mk(v, new MkArray(switchOf(v->values(), *this), v->la())); } |
42 | 830 | ExprPtr with(const MkVariant* v) const override { return mk(v, new MkVariant(v->label(), switchOf(v->value(), *this), v->la())); } |
43 | 988 | ExprPtr with(const MkRecord* v) const override { return mk(v, new MkRecord(switchOf(v->fields(), *this), v->la())); } |
44 | 2.42k | ExprPtr with(const AIndex* v) const override { return mk(v, new AIndex(switchOf(v->array(), *this), switchOf(v->index(), *this), v->la())); } |
45 | 134k | ExprPtr with(const Proj* v) const override { return mk(v, new Proj(switchOf(v->record(), *this), v->field(), v->la())); } |
46 | 151k | ExprPtr with(const Assump* v) const override { return mk(v, new Assump(switchOf(v->expr(), *this), v->ty(), v->la())); } |
47 | | |
48 | 458 | ExprPtr with(const Case* v) const override { |
49 | 458 | ExprPtr de = v->defaultExpr(); |
50 | 458 | if (de.get() != nullptr) { de = switchOf(de, *this); } |
51 | 458 | Case::Bindings bs; |
52 | 668 | for (const auto& b : v->bindings()) { |
53 | 668 | auto branch = under(b.vname); |
54 | 668 | bs.push_back(Case::Binding(b.selector, b.vname, switchOf(b.exp, branch))); |
55 | 668 | } |
56 | 458 | return mk(v, new Case(switchOf(v->variant(), *this), bs, de, v->la())); |
57 | 458 | } |
58 | | |
59 | 218k | ExprPtr with(const Switch* v) const override { |
60 | 218k | ExprPtr de = v->defaultExpr(); |
61 | 218k | if (de) { de = switchOf(de, *this); } |
62 | 218k | return mk(v, new Switch(switchOf(v->expr(), *this), switchOf(v->bindings(), *this), de, v->la())); |
63 | 218k | } |
64 | | |
65 | 10.0M | ExprPtr with(const Var* v) const override { |
66 | 10.0M | if (this->boundNames.find(v->value()) != this->boundNames.end()) { |
67 | 6.76M | return mk(v, v->clone()); |
68 | 6.76M | } else if (v->value() == "and") { |
69 | 0 | return macroEtaLiftAnd(v->la()); |
70 | 3.29M | } else if (v->value() == "or") { |
71 | 0 | return macroEtaLiftOr(v->la()); |
72 | 3.29M | } else { |
73 | 3.29M | return mk(v, v->clone()); |
74 | 3.29M | } |
75 | 10.0M | } |
76 | | |
77 | 237k | ExprPtr with(const Let* v) const override { |
78 | 237k | auto body = under(v->var()); |
79 | 237k | return mk(v, new Let(v->var(), switchOf(v->varExpr(), *this), switchOf(v->bodyExpr(), body), v->la())); |
80 | 237k | } |
81 | | |
82 | 40 | ExprPtr with(const LetRec* v) const override { |
83 | 40 | str::seq names; |
84 | 40 | for (const auto& b : v->bindings()) { names.push_back(b.first); } |
85 | 40 | auto scope = under(names); |
86 | 40 | LetRec::Bindings bs; |
87 | 40 | for (const auto& b : v->bindings()) { |
88 | 40 | bs.push_back(LetRec::Binding(b.first, switchOf(b.second, scope))); |
89 | 40 | } |
90 | 40 | return mk(v, new LetRec(bs, switchOf(v->bodyExpr(), scope), v->la())); |
91 | 40 | } |
92 | | |
93 | 3.82M | ExprPtr with(const App* v) const override { |
94 | 3.82M | Var* fn = is<Var>(v->fn()); |
95 | 3.82M | if ((fn != nullptr) && this->boundNames.find(fn->value()) == this->boundNames.end() && fn->value() == "and") { |
96 | 235k | return macroExpandAnd(switchOf(v->args(), *this), v->la()); |
97 | 3.59M | } else if ((fn != nullptr) && this->boundNames.find(fn->value()) == this->boundNames.end() && fn->value() == "or") { |
98 | 90 | return macroExpandOr(switchOf(v->args(), *this), v->la()); |
99 | 3.59M | } else { |
100 | 3.59M | return mk(v, new App(switchOf(v->fn(), *this), switchOf(v->args(), *this), v->la())); |
101 | 3.59M | } |
102 | 3.82M | } |
103 | | |
104 | 133k | ExprPtr with(const Assign* v) const override { |
105 | 133k | return mk(v, new Assign(switchOf(v->left(), *this), switchOf(v->right(), *this), v->la())); |
106 | 133k | } |
107 | | |
108 | 230 | ExprPtr with(const Pack* v) const override { |
109 | 230 | return mk(v, new Pack(switchOf(v->expr(), *this), v->la())); |
110 | 230 | } |
111 | | |
112 | 60 | ExprPtr with(const Unpack* v) const override { |
113 | 60 | auto body = under(v->varName()); |
114 | 60 | return mk(v, new Unpack(v->varName(), switchOf(v->package(), *this), switchOf(v->expr(), body), v->la())); |
115 | 60 | } |
116 | | |
117 | | // x and y = if x then y else false |
118 | 235k | static ExprPtr macroExpandAnd(const Exprs& es, const LexicalAnnotation& la) { |
119 | 235k | if (es.size() != 2) { |
120 | 0 | throw annotated_error(la, "and expects exactly two arguments"); |
121 | 0 | } |
122 | 235k | MonoTypePtr tbool = MonoTypePtr(Prim::make("bool")); |
123 | 235k | QualTypePtr tbfn = qualtype(Func::make(tuplety(list(tbool, tbool, tbool)), tbool)); |
124 | 235k | return mk(tbool, new App(mk(tbfn, new Var("if", la)), list(es[0], es[1], mk(tbool, new Bool(false, la))), la)); |
125 | 235k | } |
126 | | |
127 | | // and => \(x,y).x and y |
128 | 0 | static ExprPtr macroEtaLiftAnd(const LexicalAnnotation& la) { |
129 | 0 | MonoTypePtr tbool = MonoTypePtr(Prim::make("bool")); |
130 | 0 | QualTypePtr tafn = qualtype(Func::make(tuplety(list(tbool, tbool)), tbool)); |
131 | 0 | return mk(tafn, new Fn(list<std::string>("x", "y"), macroExpandAnd(list(mk(tbool, new Var("x", la)), mk(tbool, new Var("y", la))), la), la)); |
132 | 0 | } |
133 | | |
134 | | // x or y = if x then true else y |
135 | 90 | static ExprPtr macroExpandOr(const Exprs& es, const LexicalAnnotation& la) { |
136 | 90 | if (es.size() != 2) { |
137 | 0 | throw annotated_error(la, "or expects exactly two arguments"); |
138 | 0 | } |
139 | 90 | MonoTypePtr tbool = MonoTypePtr(Prim::make("bool")); |
140 | 90 | QualTypePtr tbfn = qualtype(Func::make(tuplety(list(tbool, tbool, tbool)), tbool)); |
141 | 90 | return mk(tbool, new App(mk(tbfn, new Var("if", la)), list(es[0], mk(tbool, new Bool(true, la)), es[1]), la)); |
142 | 90 | } |
143 | | |
144 | | // or => \(x,y).x or y |
145 | 0 | static ExprPtr macroEtaLiftOr(const LexicalAnnotation& la) { |
146 | 0 | MonoTypePtr tbool = MonoTypePtr(Prim::make("bool")); |
147 | 0 | QualTypePtr tafn = qualtype(Func::make(tuplety(list(tbool, tbool)), tbool)); |
148 | 0 | return mk(tafn, new Fn(list<std::string>("x", "y"), macroExpandOr(list(mk(tbool, new Var("x", la)), mk(tbool, new Var("y", la))), la), la)); |
149 | 0 | } |
150 | | }; |
151 | | |
152 | 11.9k | ExprPtr macroExpand(const ExprPtr& e) { |
153 | 11.9k | return switchOf(e, macroExpandF()); |
154 | 11.9k | } |
155 | | |
156 | 10 | void initMacroEnvironment(const TEnvPtr& tenv) { |
157 | 10 | tenv->bind("and", prim<bool(bool,bool)>()); |
158 | 10 | tenv->bind("or", prim<bool(bool,bool)>()); |
159 | 10 | } |
160 | | |
161 | | } |
162 | | |