/src/hobbes/lib/hobbes/lang/preds/packsto.C
Line | Count | Source |
1 | | |
2 | | #include <hobbes/lang/preds/packsto.H> |
3 | | #include <hobbes/lang/preds/class.H> |
4 | | #include <hobbes/lang/typeinf.H> |
5 | | |
6 | | namespace hobbes { |
7 | | |
8 | | // temporarily restrict supported existential types to the immediate use-case for closures |
9 | | // this lets us avoid awkward problems (for now) like how to skip past an existentially-quantified field in a record |
10 | 0 | bool isSafeExistentialType(const Exists* e) { |
11 | 0 | if (const Record* r = is<Record>(e->absType())) { |
12 | 0 | const Record::Members& ms = r->members(); |
13 | 0 | if (ms.size() != 2) return false; |
14 | | |
15 | 0 | if (const TVar* ev = is<TVar>(ms[1].type)) { |
16 | 0 | if (ev->name() != e->absTypeName()) { |
17 | 0 | return false; |
18 | 0 | } |
19 | 0 | } else { |
20 | 0 | return false; |
21 | 0 | } |
22 | | |
23 | 0 | if (const Func* f = is<Func>(ms[0].type)) { |
24 | 0 | if (const TVar* rt = is<TVar>(f->result())) { |
25 | 0 | if (rt->name() == e->absTypeName()) { |
26 | 0 | return false; |
27 | 0 | } |
28 | 0 | } |
29 | 0 | } else { |
30 | 0 | return false; |
31 | 0 | } |
32 | | |
33 | 0 | return true; |
34 | 0 | } else { |
35 | 0 | return false; |
36 | 0 | } |
37 | 0 | } |
38 | | |
39 | | struct PacksTo { |
40 | | MonoTypePtr underlyingType; |
41 | | MonoTypePtr abstractType; |
42 | | }; |
43 | | |
44 | 0 | static bool dec(const ConstraintPtr& c, PacksTo* pt) { |
45 | 0 | if (c->name() == Existentials::constraintName() && c->arguments().size() == 2) { |
46 | 0 | pt->underlyingType = c->arguments()[0]; |
47 | 0 | pt->abstractType = c->arguments()[1]; |
48 | 0 | return true; |
49 | 0 | } |
50 | | |
51 | 0 | return false; |
52 | 0 | } |
53 | 0 | static void upd(const ConstraintPtr& c, const PacksTo& pt) { |
54 | 0 | auto& args = const_cast<MonoTypes&>(c->arguments()); |
55 | 0 | if (args.size() == 2) { |
56 | 0 | args[0] = pt.underlyingType; |
57 | 0 | args[1] = pt.abstractType; |
58 | 0 | } |
59 | 0 | } |
60 | | |
61 | | /**** |
62 | | * enforce/translate type judgements and computations with existential types |
63 | | ****/ |
64 | 0 | std::string Existentials::constraintName() { |
65 | 0 | return "packsto"; |
66 | 0 | } |
67 | | |
68 | 0 | bool Existentials::refine(const TEnvPtr& tenv, const ConstraintPtr& cst, MonoTypeUnifier* u, Definitions*) { |
69 | 0 | PacksTo pt; |
70 | 0 | if (dec(cst, &pt)) { |
71 | 0 | if (const Exists* e = is<Exists>(pt.abstractType)) { |
72 | 0 | MonoTypeUnifier lu(tenv); |
73 | 0 | scoped_unification_suppression slus(&lu, e->absTypeName()); |
74 | 0 | mgu(u->substitute(pt.underlyingType), e->absType(), &lu); |
75 | |
|
76 | 0 | MonoTypeSubst lus = lu.substitution(); |
77 | 0 | for (MonoTypeSubst::const_iterator si = lus.begin(); si != lus.end(); ++si) { |
78 | 0 | u->bind(si->first, si->second); |
79 | 0 | } |
80 | 0 | pt.abstractType = substitute(lus, pt.abstractType); |
81 | 0 | upd(cst, pt); |
82 | 0 | return !lus.empty(); |
83 | 0 | } |
84 | 0 | } |
85 | 0 | return false; |
86 | 0 | } |
87 | | |
88 | 0 | bool Existentials::satisfied(const TEnvPtr& tenv, const ConstraintPtr& cst, Definitions*) const { |
89 | 0 | PacksTo pt; |
90 | 0 | if (dec(cst, &pt)) { |
91 | 0 | if (const Exists* e = is<Exists>(pt.abstractType)) { |
92 | 0 | if (!isSafeExistentialType(e)) { |
93 | 0 | return false; |
94 | 0 | } |
95 | | |
96 | 0 | if (!unifiable(tenv, pt.underlyingType, e->absType())) { |
97 | 0 | return false; |
98 | 0 | } |
99 | | |
100 | 0 | NameSet utns = tvarNames(pt.underlyingType); |
101 | 0 | NameSet atns = tvarNames(e->absType()); |
102 | |
|
103 | 0 | return utns.empty() && atns.size() == 1 && (e->absTypeName() == *atns.begin()); |
104 | 0 | } else { |
105 | 0 | return *pt.underlyingType == *pt.abstractType; |
106 | 0 | } |
107 | 0 | } else { |
108 | 0 | return false; |
109 | 0 | } |
110 | 0 | } |
111 | | |
112 | 0 | bool Existentials::satisfiable(const TEnvPtr&, const ConstraintPtr& cst, Definitions*) const { |
113 | 0 | PacksTo pt; |
114 | 0 | if (dec(cst, &pt)) { |
115 | 0 | if (is<TVar>(pt.abstractType) != nullptr) { |
116 | 0 | return true; |
117 | 0 | } else if (const Exists* e = is<Exists>(pt.abstractType)) { |
118 | 0 | if (!isSafeExistentialType(e)) { |
119 | 0 | return false; |
120 | 0 | } else { |
121 | 0 | return true; // too liberal? changed from 'unifiable' to work with streams |
122 | 0 | } |
123 | 0 | } else { |
124 | 0 | return true; |
125 | 0 | } |
126 | 0 | } else { |
127 | 0 | return false; |
128 | 0 | } |
129 | 0 | } |
130 | | |
131 | 0 | void Existentials::explain(const TEnvPtr&, const ConstraintPtr&, const ExprPtr&, Definitions*, annmsgs*) { |
132 | 0 | } |
133 | | |
134 | | struct UnqualifySafePackF : public switchExprTyFn { |
135 | | const ConstraintPtr& constraint; |
136 | 0 | UnqualifySafePackF(const ConstraintPtr& cst) : constraint(cst) { |
137 | 0 | } |
138 | | |
139 | 0 | ExprPtr wrapWithTy(const QualTypePtr& qty, Expr* e) const override { |
140 | 0 | ExprPtr result(e); |
141 | 0 | result->type(removeConstraint(this->constraint, qty)); |
142 | 0 | return result; |
143 | 0 | } |
144 | | }; |
145 | | |
146 | | struct StripTransparentPackF : public switchExprTyFn { |
147 | | const ConstraintPtr& constraint; |
148 | 0 | StripTransparentPackF(const ConstraintPtr& cst) : constraint(cst) { |
149 | 0 | } |
150 | | |
151 | 0 | ExprPtr wrapWithTy(const QualTypePtr& qty, Expr* e) const override { |
152 | 0 | ExprPtr result(e); |
153 | 0 | result->type(removeConstraint(this->constraint, qty)); |
154 | 0 | return result; |
155 | 0 | } |
156 | | |
157 | 0 | ExprPtr with(const Pack* p) const override { |
158 | 0 | if (hasConstraint(this->constraint, p->type())) { |
159 | 0 | return switchOf(p->expr(), *this); |
160 | 0 | } else { |
161 | 0 | return wrapWithTy(p->type(), new Pack(switchOf(p->expr(), *this), p->la())); |
162 | 0 | } |
163 | 0 | } |
164 | | }; |
165 | | |
166 | 0 | ExprPtr Existentials::unqualify(const TEnvPtr&, const ConstraintPtr& cst, const ExprPtr& e, Definitions*) const { |
167 | | // if we're eliminating a constraint that indicates no information is hidden, then get rid of the superfluous packs |
168 | | // else, just remove the constraint from terms and let final compilation determine how to do the packing |
169 | 0 | PacksTo pt; |
170 | 0 | if (dec(cst, &pt)) { |
171 | 0 | if (is<Exists>(pt.abstractType) != nullptr) { |
172 | 0 | return switchOf(e, UnqualifySafePackF(cst)); |
173 | 0 | } else { |
174 | 0 | return switchOf(e, StripTransparentPackF(cst)); |
175 | 0 | } |
176 | 0 | } else { |
177 | | // ??? |
178 | 0 | throw annotated_error(*e, "Internal error in existential unqualification, unexpected constraint: " + show(cst)); |
179 | 0 | } |
180 | 0 | } |
181 | | |
182 | 0 | PolyTypePtr Existentials::lookup(const std::string&) const { |
183 | 0 | return PolyTypePtr(); // this won't be necessary |
184 | 0 | } |
185 | | |
186 | 0 | SymSet Existentials::bindings() const { |
187 | 0 | return SymSet(); // no overloaded symbols are defined here |
188 | 0 | } |
189 | | |
190 | 0 | FunDeps Existentials::dependencies(const ConstraintPtr&) const { |
191 | 0 | return FunDeps(); |
192 | 0 | } |
193 | | |
194 | | } |
195 | | |