hobbes
a language, embedded compiler, and runtime for efficient dynamic expression evaluation, data storage and analysis
type.H
Go to the documentation of this file.
1 
2 #ifndef HOBBES_LANG_TYPE_HPP_INCLUDED
3 #define HOBBES_LANG_TYPE_HPP_INCLUDED
4 
5 #include <hobbes/util/hash.H>
6 #include <hobbes/util/ptr.H>
7 #include <hobbes/util/array.H>
8 #include <hobbes/util/str.H>
10 #include <string>
11 #include <vector>
12 #include <set>
13 #include <map>
14 #include <memory>
15 #include <stdexcept>
16 
17 namespace hobbes {
18 
19 // there is a type for holding type equality constraints
20 class MonoTypeUnifier;
21 
22 // forall a0..aN : T
23 class PolyType;
24 typedef std::shared_ptr<PolyType> PolyTypePtr;
25 typedef std::vector<PolyTypePtr> PolyTypes;
26 
27 // (C0, ..., Cn) => T
28 class QualType;
29 typedef std::shared_ptr<QualType> QualTypePtr;
30 typedef std::vector<QualTypePtr> QualTypes;
31 
32 // P(T)
33 class Constraint;
34 typedef std::shared_ptr<Constraint> ConstraintPtr;
35 typedef std::vector<ConstraintPtr> Constraints;
36 
37 // T
38 class MonoType {
39 public:
40  virtual ~MonoType();
41  typedef std::shared_ptr<MonoType> ptr;
42 
43  virtual void show(std::ostream&) const = 0;
44  bool operator==(const MonoType& rhs) const;
45 
46  // improves performance of case-analysis over instances (to avoid 'dynamic_cast')
47 public:
48  int case_id() const;
49 protected:
50  MonoType(int cid);
51 
52  // memoize type construction
53  template <typename Class, typename T, typename ... Args>
54  static ptr makeType(const Args&... args);
55 private:
56  int cid;
57 
58 public:
59  // improves performance of identifying type variables and tgens
60  typedef std::set<std::string> TypeVarNames;
61  TypeVarNames freeTVars;
62  int tgenCount;
63 
64  // improves performance of computing the memory size of a type
65  mutable unsigned int memorySize;
66 
67  // improves performance of unhiding opaque type aliases
68  mutable ptr unaliasedType;
69 };
70 
72 typedef std::vector<MonoTypePtr> MonoTypes;
73 
74 inline std::ostream& operator<<(std::ostream& out, const MonoTypePtr& t) {
75  t->show(out);
76  return out;
77 }
78 
79 // nested environments for types and type predicate resolvers
80 class TEnv;
81 typedef std::shared_ptr<TEnv> TEnvPtr;
82 
84 typedef std::shared_ptr<Unqualifier> UnqualifierPtr;
85 
87 typedef std::shared_ptr<UnqualifierSet> UnqualifierSetPtr;
88 
89 class TEnv {
90 public:
91  TEnv(const TEnvPtr& parent);
92  TEnv();
93 
94  bool hasBinding(const std::string& vname) const;
95  bool hasImmediateBinding(const std::string& vname) const;
96 
97  // in/out type bindings
98  void bind(const std::string& vname, const PolyTypePtr&);
99  void bind(const std::string& vname, const QualTypePtr&);
100  void bind(const std::string& vname, const MonoTypePtr&);
101  void unbind(const std::string& vname);
102  PolyTypePtr lookup(const std::string& vname) const;
103 
104  // overloading / subtyping
105  void bind(const std::string& predName, const UnqualifierPtr& uq);
106  UnqualifierPtr lookupUnqualifier(const std::string& predName) const;
107  UnqualifierPtr lookupUnqualifier(const ConstraintPtr& cst) const;
108 
109  // get access to the parent and root type environment from here
110  const TEnvPtr& parentTypeEnv() const;
111  TEnv* root();
112 public:
113  // get access to the internal type environment map (should only be used for debugging)
114  typedef std::map<std::string, PolyTypePtr> PolyTypeEnv;
115 
116  PolyTypeEnv typeEnvTable() const;
117  str::set boundVariables() const;
118 
119  typedef std::map<std::string, UnqualifierPtr> Unqualifiers;
120  const Unqualifiers& unqualifiers() const;
121 private:
122  TEnvPtr parent;
123  UnqualifierSetPtr unquals; // non-empty iff parent==0
124  PolyTypeEnv ptenv;
125 
126 public:
127  // pack/unpack opaque type aliases
128  void alias(const std::string&, const MonoTypePtr&);
129  MonoTypePtr unalias(const std::string&) const;
130  bool isOpaqueTypeAlias(const std::string&) const;
131 private:
132  typedef std::map<std::string, MonoTypePtr> TypeAliases;
133  TypeAliases typeAliases;
134 };
135 
136 TEnvPtr fnFrame(const TEnvPtr&, const str::seq&, const MonoTypes&);
137 TEnvPtr bindFrame(const TEnvPtr&, const std::string&, const MonoTypePtr&);
138 TEnvPtr bindFrame(const TEnvPtr&, const std::string&, const QualTypePtr&);
139 
140 typedef std::string TVName;
141 typedef std::vector<TVName> Names;
142 typedef std::set<TVName> NameSet;
143 typedef std::map<TVName, MonoTypePtr> MonoTypeSubst;
144 
145 inline MonoTypeSubst substitution(const str::seq& ns, const MonoTypes& ts) {
146  MonoTypeSubst s;
147  size_t n = std::min<size_t>(ns.size(), ts.size());
148  for (size_t i = 0; i < n; ++i) {
149  s[ns[i]] = ts[i];
150  }
151  return s;
152 }
153 
154 // determine if a constraint is satisfied/satisfiable in this type environment (or if a sequence of constraints are all satisfied/satisfiable)
155 // (forward declare the 'definitions' type from expr -- this is a little awkward)
156 class Expr;
157 bool satisfied(const UnqualifierPtr&, const TEnvPtr&, const ConstraintPtr&, std::vector<std::pair<std::string, std::shared_ptr<Expr> > >*);
158 bool satisfied(const TEnvPtr& tenv, const ConstraintPtr& c, std::vector<std::pair<std::string, std::shared_ptr<Expr> > >*);
159 bool satisfied(const TEnvPtr& tenv, const Constraints& cs, std::vector<std::pair<std::string, std::shared_ptr<Expr> > >*);
160 bool satisfiable(const UnqualifierPtr&, const TEnvPtr&, const ConstraintPtr&, std::vector<std::pair<std::string, std::shared_ptr<Expr> > >*);
161 bool satisfiable(const TEnvPtr& tenv, const ConstraintPtr& c, std::vector<std::pair<std::string, std::shared_ptr<Expr> > >*);
162 bool satisfiable(const TEnvPtr& tenv, const Constraints& cs, std::vector<std::pair<std::string, std::shared_ptr<Expr> > >*);
163 
164 // a polytype (or type scheme) can describe many types
165 class PolyType {
166 public:
167  PolyType(int vs, const QualTypePtr& qt);
168  PolyType(const QualTypePtr& qt);
169 
170  int typeVariables() const;
171  QualTypePtr instantiate() const;
172 
173  void show(std::ostream&) const;
174  bool operator==(const PolyType& rhs) const;
175 
176  // this should only be used if you don't mind capturing bound type variables
177  const QualTypePtr& qualtype() const;
178 private:
179  int vs;
180  QualTypePtr qt;
181 };
182 
183 inline std::ostream& operator<<(std::ostream& out, const PolyTypePtr& t) {
184  t->show(out);
185  return out;
186 }
187 
188 // a qualified type is a monotype with a set of constraints
189 class QualType {
190 public:
191  QualType(const Constraints& cs, const MonoTypePtr& mt);
192  QualType(const MonoTypePtr& mt);
193 
194  const Constraints& constraints() const;
195  const MonoTypePtr& monoType() const;
196 
197  Constraints& constraints();
198  void monoType(const MonoTypePtr&);
199 
200  void show(std::ostream&) const;
201  bool operator==(const QualType& rhs) const;
202 private:
203  Constraints cs;
204  MonoTypePtr mt;
205 };
206 
207 inline std::ostream& operator<<(std::ostream& out, const QualTypePtr& t) {
208  t->show(out);
209  return out;
210 }
211 
212 // a constraint qualifies types
213 class Constraint {
214 public:
215  Constraint(const std::string& cat, const MonoTypes& mts);
216 
217  // the name of the predicate for this constraint
218  std::string name() const;
219 
220  // the predicate's arguments
221  const MonoTypes& arguments() const;
222 
223  // type variable names in/out
224  ConstraintPtr instantiate(const MonoTypes& ts) const;
225  NameSet tvarNames() const;
226  ConstraintPtr substitute(const MonoTypeSubst& s) const;
227 
228  // update internally-stored types in-place according to a unification set
229  void update(MonoTypeUnifier*);
230 
231  // compare/show
232  void show(std::ostream&) const;
233  bool operator==(const Constraint& rhs) const;
234 
235  // are there free variables referenced in this constraint?
236  bool hasFreeVariables() const;
237 private:
238  std::string cat;
239  MonoTypes mts;
240 public:
241  // avoid unnecessary satisfied/satisfiable checks
242  enum { Unresolved, Satisfied, Unsatisfiable } state;
243 };
244 
245 inline std::ostream& operator<<(std::ostream& out, const ConstraintPtr& t) {
246  t->show(out);
247  return out;
248 }
249 
250 // improves performance of case-analysis over MonoType instances (to avoid 'dynamic_cast')
251 template <typename Case>
252  class MonoTypeCase : public MonoType {
253  public:
254  MonoTypeCase();
255  };
256 
257 // a type treated as primitive (by name)
258 // may be an "actual primitive" (void, unit, bool, byte, char, int, long, float, double, ...)
259 // or may be an alias for a composite type
260 class Prim : public MonoTypeCase<Prim> {
261 public:
262  void show(std::ostream& out) const;
263 
264  const std::string& name() const;
265  const MonoTypePtr& representation() const;
266 
267  static const int type_case_id = 0;
268 
269  static MonoTypePtr make(const std::string&, const MonoTypePtr& t = MonoTypePtr());
270 private:
271  std::string nm;
272  MonoTypePtr t;
273 
274  friend class MonoType;
275  Prim(const std::string&, const MonoTypePtr&);
276 };
277 
278 // an opaque pointer to some C++ type
279 class OpaquePtr : public MonoTypeCase<OpaquePtr> {
280 public:
281  void show(std::ostream& out) const;
282 
283  const std::string& name() const;
284  unsigned int size() const;
285  bool storedContiguously() const;
286 
287  static const int type_case_id = 1;
288 
289  static MonoTypePtr make(const std::string& nm, unsigned int sz, bool scontig = false);
290 private:
291  std::string nm;
292  unsigned int sz;
293  bool scontig;
294 
295  friend class MonoType;
296  OpaquePtr(const std::string&, unsigned int, bool);
297 };
298 
299 // remove the contiguity flag on opaque pointers
300 MonoTypePtr normIfOpaquePtr(const MonoTypePtr& ty);
301 
302 // a type variable (may be substituted for some type)
303 class TVar : public MonoTypeCase<TVar> {
304 public:
305  const std::string& name() const;
306  void show(std::ostream& out) const;
307 
308  static const int type_case_id = 2;
309 
310  static MonoTypePtr make(const std::string&);
311 private:
312  std::string nm;
313 
314  friend class MonoType;
315  TVar(const std::string&);
316 };
317 
318 // this constructor is ONLY used for polytype instantiation
319 class TGen : public MonoTypeCase<TGen> {
320 public:
321  int id() const;
322  void show(std::ostream& out) const; // should never be shown
323 
324  static const int type_case_id = 3;
325 
326  static MonoTypePtr make(int);
327 private:
328  int x;
329 
330  friend class MonoType;
331  TGen(int);
332 };
333 
334 // an array whose length is determined
335 class FixedArray : public MonoTypeCase<FixedArray> {
336 public:
337  void show(std::ostream& out) const;
338 
339  const MonoTypePtr& type() const;
340  const MonoTypePtr& length() const;
341 
342  long requireLength() const;
343 
344  static const int type_case_id = 4;
345 
346  static MonoTypePtr make(const MonoTypePtr& ty, const MonoTypePtr& len);
347 private:
348  MonoTypePtr ty;
349  MonoTypePtr len;
350 
351  friend class MonoType;
352  FixedArray(const MonoTypePtr&, const MonoTypePtr&);
353 };
354 
355 // an array whose length is not known at compile-time
356 class Array : public MonoTypeCase<Array> {
357 public:
358  void show(std::ostream& out) const;
359 
360  const MonoTypePtr& type() const;
361 
362  static const int type_case_id = 5;
363 
364  static MonoTypePtr make(const MonoTypePtr&);
365 private:
366  MonoTypePtr ty;
367 
368  friend class MonoType;
369  Array(const MonoTypePtr&);
370 };
371 
372 // the 'coproduct' construction (a variant is _one of_ a set of types)
373 class Variant : public MonoTypeCase<Variant> {
374 public:
375  struct Member {
376  Member(const std::string& selector, const MonoTypePtr& type, unsigned int id);
377  Member();
378  bool operator==(const Member&) const;
379  bool operator<(const Member&) const;
380 
381  std::string selector;
382  MonoTypePtr type;
383  unsigned int id;
384  };
385  typedef std::vector<Member> Members;
386 
387  void show(std::ostream& out) const;
388 
389  // get the first visible member and the type of the 'remainder' of the variant after ignoring the first field
390  // (these are used for structural decomposition in generic functions)
391  Member headMember() const;
392  MonoTypePtr tailType() const;
393 
394  const Members& members() const;
395  const MonoTypePtr& payload(const std::string& selector) const;
396  unsigned int index(const std::string& selector) const;
397  unsigned int id(const std::string& selector) const;
398  const Member* mmember(const std::string& selector) const;
399 
400  // does this variant actually represent a 'sum'?
401  bool isSum() const;
402 
403  // handle layout logic for variants
404  mutable unsigned int payloadSizeM; // cache for 'payloadSize' query
405 
406  unsigned int payloadOffset() const;
407  unsigned int payloadSize() const;
408  unsigned int size() const;
409 
410  static const int type_case_id = 6;
411 
412  static MonoTypePtr make(const Members&);
413  static MonoTypePtr make(const MonoTypePtr& hty, const Members& tty);
414  static MonoTypePtr make(const std::string& lbl, const MonoTypePtr& hty, const Members& tty);
415 private:
416  Members ms;
417 
418  friend class MonoType;
419  Variant(const Members&);
420 };
421 
422 // the 'product' construction (a record is _all of_ a set of types)
423 class Record : public MonoTypeCase<Record> {
424 public:
425  struct Member {
426  Member(const std::string& field, const MonoTypePtr& type, int offset = -1);
427  Member();
428  bool operator==(const Member&) const;
429  bool operator<(const Member&) const;
430 
431  std::string field;
432  MonoTypePtr type;
433  int offset;
434  };
435  typedef std::vector<Member> Members;
436 
437  void show(std::ostream& out) const;
438 
439  const Members& members() const;
440  const MonoTypePtr& member(const std::string& mn) const;
441  unsigned int index(const std::string& mn) const;
442  const Member* mmember(const std::string& mn) const;
443 
444  // get the first visible member and the type of the 'remainder' of the struct after ignoring the first field
445  // (these are used for structural decomposition in generic functions)
446  Member headMember() const;
447  MonoTypePtr tailType() const;
448 
449  // handle alignment determination (with explicit padding added)
450  const Members& alignedMembers() const;
451  unsigned int alignedIndex(const std::string& mn) const;
452 
453  // the amount of contiguous memory used to represent this record
454  unsigned int size() const;
455 
456  // does this record actually represent a 'tuple'?
457  bool isTuple() const;
458 
459  // this function must be idempotent
460  static Members withExplicitPadding(const Members&, const std::string& pfx = ".p");
461 
462  static const int type_case_id = 7;
463 private:
464  Members ms;
465  Members ams;
466 
467  mutable unsigned int maxFieldAlignmentM;
468  unsigned int maxFieldAlignment() const;
469 
470  static void showRecord(std::ostream&, const Members&);
471  static std::string showRecord(const Members&);
472 
473  unsigned int index(const Members& ms, const std::string& mn) const;
474 public:
475  // this function must be idempotent
476  static Members withResolvedMemoryLayout(const Members&);
477 
478  // constructors
479  static MonoTypePtr make(const Members& ms);
480  static MonoTypePtr make(const MonoTypePtr& hty, const Members& tty);
481  static MonoTypePtr make(const std::string& lbl, const MonoTypePtr& hty, const Members& tty);
482 private:
483  friend class MonoType;
484  Record(const Members& ms);
485 };
486 
487 // determine type alignment (for efficient/correct access in memory)
488 unsigned int alignment(const MonoTypePtr&);
489 
490 // a flat (contextless) function type
491 class Func : public MonoTypeCase<Func> {
492 public:
493  void show(std::ostream& out) const;
494 
495  const MonoTypePtr& argument() const;
496  const MonoTypePtr& result() const;
497 
498  // untuples the 'argument' type, for convenience
499  MonoTypes parameters() const;
500 
501  static const int type_case_id = 8;
502 
503  static MonoTypePtr make(const MonoTypePtr&, const MonoTypePtr&);
504 private:
505  MonoTypePtr aty;
506  MonoTypePtr rty;
507 
508  friend class MonoType;
509  Func(const MonoTypePtr&, const MonoTypePtr&);
510 };
511 
512 // an abstraction of type structure suitable for unifying otherwise distinct types
513 class Exists : public MonoTypeCase<Exists> {
514 public:
515  void show(std::ostream& out) const;
516 
517  const std::string& absTypeName() const;
518  const MonoTypePtr& absType() const;
519 
520  static const int type_case_id = 9;
521 
522  static MonoTypePtr make(const std::string& tname, const MonoTypePtr& bty);
523 private:
524  std::string tname;
525  MonoTypePtr bty;
526 
527  friend class MonoType;
528  Exists(const std::string& tname, const MonoTypePtr& bty);
529 };
530 
531 // project out of existential types
532 MonoTypePtr unpackedType(const Exists* e);
533 MonoTypePtr unpackedType(const MonoTypePtr& mty);
534 QualTypePtr unpackedType(const QualTypePtr& qty);
535 
536 // type-level values (the writing is on the wall here -- this is a hack and we are going to need full dependent types)
537 class TString : public MonoTypeCase<TString> {
538 public:
539  void show(std::ostream& out) const;
540 
541  const std::string& value() const;
542 
543  static const int type_case_id = 10;
544 
545  static MonoTypePtr make(const std::string&);
546 private:
547  std::string val;
548 
549  friend class MonoType;
550  TString(const std::string&);
551 };
552 
553 class TLong : public MonoTypeCase<TLong> {
554 public:
555  void show(std::ostream& out) const;
556 
557  long value() const;
558 
559  static const int type_case_id = 11;
560 
561  static MonoTypePtr make(long);
562 private:
563  long x;
564 
565  friend class MonoType;
566  TLong(long x);
567 };
568 
569 // a type-level abstraction
570 class TAbs : public MonoTypeCase<TAbs> {
571 public:
572  void show(std::ostream&) const;
573 
574  const str::seq& args() const;
575  const MonoTypePtr& body() const;
576 
577  static const int type_case_id = 15;
578 
579  static MonoTypePtr make(const str::seq&, const MonoTypePtr&);
580 private:
582  MonoTypePtr b;
583 
584  friend class MonoType;
585  TAbs(const str::seq&, const MonoTypePtr&);
586 };
587 
588 // a type-level application
589 class TApp : public MonoTypeCase<TApp> {
590 public:
591  void show(std::ostream&) const;
592 
593  const MonoTypePtr& fn() const;
594  const MonoTypes& args() const;
595 
596  static const int type_case_id = 12;
597 
598  static MonoTypePtr make(const MonoTypePtr&, const MonoTypes&);
599 private:
600  MonoTypePtr f;
601  MonoTypes targs;
602 
603  friend class MonoType;
604  TApp(const MonoTypePtr&, const MonoTypes&);
605 };
606 
607 // a recursive type
608 class Recursive : public MonoTypeCase<Recursive> {
609 public:
610  void show(std::ostream&) const;
611 
612  const std::string& recTypeName() const;
613  const MonoTypePtr& recType() const;
614 
615  static const int type_case_id = 13;
616 
617  static MonoTypePtr make(const std::string&, const MonoTypePtr&);
618 private:
619  std::string tname;
620  MonoTypePtr bty;
621 
622  friend class MonoType;
623  Recursive(const std::string& tname, const MonoTypePtr& bty);
624 };
625 
626 // expressions in types
627 // (eventually we will need to eliminate the distinction between types and expressions)
628 class Expr;
629 typedef std::shared_ptr<Expr> ExprPtr;
630 
631 class TExpr : public MonoTypeCase<TExpr> {
632 public:
633  void show(std::ostream&) const;
634 
635  const ExprPtr& expr() const;
636 
637  static const int type_case_id = 14;
638 
639  static MonoTypePtr make(const ExprPtr&);
640 private:
641  ExprPtr e;
642 
643  friend class MonoType;
644  TExpr(const ExprPtr&);
645 };
646 
647 template <typename Case>
649  }
650 
651 // simplify showing types (with or without simplifying type variable names)
652 std::string show(const PolyType& e);
653 std::string show(const PolyType* e);
654 std::string show(const PolyTypePtr& e);
655 std::string show(const QualType& e);
656 std::string show(const QualType* e);
657 std::string show(const QualTypePtr& e);
658 std::string show(const Constraint& e);
659 std::string show(const Constraint* e);
660 std::string show(const ConstraintPtr& e);
661 std::string show(const MonoType& e);
662 std::string show(const MonoType* e);
663 std::string show(const MonoTypePtr& e);
664 
665 str::seq showNoSimpl(const MonoTypes&);
666 str::seq showNoSimpl(const Constraints&);
667 std::string showNoSimpl(const PolyType& e);
668 std::string showNoSimpl(const PolyType* e);
669 std::string showNoSimpl(const PolyTypePtr& e);
670 std::string showNoSimpl(const QualType& e);
671 std::string showNoSimpl(const QualType* e);
672 std::string showNoSimpl(const QualTypePtr& e);
673 std::string showNoSimpl(const Constraint& e);
674 std::string showNoSimpl(const Constraint* e);
675 std::string showNoSimpl(const ConstraintPtr& e);
676 std::string showNoSimpl(const MonoType& e);
677 std::string showNoSimpl(const MonoType* e);
678 std::string showNoSimpl(const MonoTypePtr& e);
679 
680 // 'safely' consume types
681 // (if the set of mono types is extended but functions on types aren't extended, it will be a compile-error)
682 template <typename T>
683  struct switchType {
684  virtual T with(const Prim* v) const = 0;
685  virtual T with(const OpaquePtr* v) const = 0;
686  virtual T with(const TVar* v) const = 0;
687  virtual T with(const TGen* v) const = 0;
688  virtual T with(const TAbs* v) const = 0;
689  virtual T with(const TApp* v) const = 0;
690  virtual T with(const FixedArray* v) const = 0;
691  virtual T with(const Array* v) const = 0;
692  virtual T with(const Variant* v) const = 0;
693  virtual T with(const Record* v) const = 0;
694  virtual T with(const Func* v) const = 0;
695  virtual T with(const Exists* v) const = 0;
696  virtual T with(const Recursive* v) const = 0;
697 
698  virtual T with(const TString* v) const = 0;
699  virtual T with(const TLong* v) const = 0;
700 
701  virtual T with(const TExpr*) const = 0;
702  };
703 
704 struct CPtr {
705  template <typename T>
706  struct as {
707  typedef const T* ty;
708  };
709 };
710 
711 struct MPtr {
712  template <typename T>
713  struct as {
714  typedef T* ty;
715  };
716 };
717 
718 template <typename T, typename Ptr, typename PtrC, typename F>
719  T switchOfF(Ptr ty, F f) {
720  typedef typename PtrC::template as<Prim>::ty PrimT;
721  typedef typename PtrC::template as<OpaquePtr>::ty OpaquePtrT;
722  typedef typename PtrC::template as<TVar>::ty TVarT;
723  typedef typename PtrC::template as<TGen>::ty TGenT;
724  typedef typename PtrC::template as<TAbs>::ty TAbsT;
725  typedef typename PtrC::template as<TApp>::ty TAppT;
726  typedef typename PtrC::template as<FixedArray>::ty FixedArrayT;
727  typedef typename PtrC::template as<Array>::ty ArrayT;
728  typedef typename PtrC::template as<Variant>::ty VariantT;
729  typedef typename PtrC::template as<Record>::ty RecordT;
730  typedef typename PtrC::template as<Func>::ty FuncT;
731  typedef typename PtrC::template as<Exists>::ty ExistsT;
732  typedef typename PtrC::template as<Recursive>::ty RecursiveT;
733  typedef typename PtrC::template as<TString>::ty TStringT;
734  typedef typename PtrC::template as<TLong>::ty TLongT;
735  typedef typename PtrC::template as<TExpr>::ty TExprT;
736 
737  switch (ty->case_id()) {
738  case Prim::type_case_id:
739  return f.with((PrimT)ty);
740  case OpaquePtr::type_case_id:
741  return f.with((OpaquePtrT)ty);
742  case TVar::type_case_id:
743  return f.with((TVarT)ty);
744  case TGen::type_case_id:
745  return f.with((TGenT)ty);
746  case TAbs::type_case_id:
747  return f.with((TAbsT)ty);
748  case TApp::type_case_id:
749  return f.with((TAppT)ty);
750  case FixedArray::type_case_id:
751  return f.with((FixedArrayT)ty);
752  case Array::type_case_id:
753  return f.with((ArrayT)ty);
754  case Variant::type_case_id:
755  return f.with((VariantT)ty);
756  case Record::type_case_id:
757  return f.with((RecordT)ty);
758  case Func::type_case_id:
759  return f.with((FuncT)ty);
760  case Exists::type_case_id:
761  return f.with((ExistsT)ty);
762  case Recursive::type_case_id:
763  return f.with((RecursiveT)ty);
764  case TString::type_case_id:
765  return f.with((TStringT)ty);
766  case TLong::type_case_id:
767  return f.with((TLongT)ty);
768  case TExpr::type_case_id:
769  return f.with((TExprT)ty);
770  default:
771  throw std::runtime_error("Internal error, cannot switch on unknown type: " + show(ty));
772  }
773  }
774 
775 template <typename T>
776  T switchOf(const MonoType& ty, const switchType<T>& f) {
777  return switchOfF< T, const MonoType*, CPtr, const switchType<T>& >(&ty, f);
778  }
779 
780 template <typename T>
781  T switchOf(const MonoTypePtr& ty, const switchType<T>& f) {
782  return switchOfF< T, const MonoType*, CPtr, const switchType<T>& >(ty.get(), f);
783  }
784 
785 template <typename T>
786  std::vector<T> switchOf(const MonoTypes& ts, const switchType<T>& f) {
787  std::vector<T> result;
788  for (MonoTypes::const_iterator t = ts.begin(); t != ts.end(); ++t) {
789  result.push_back(switchOf(*t, f));
790  }
791  return result;
792  }
793 
794 template <typename T>
795  T switchOf(const MonoTypes& ts, T s, T (*appendF)(T,T), const switchType<T>& f) {
796  for (MonoTypes::const_iterator t = ts.begin(); t != ts.end(); ++t) {
797  s = appendF(s, switchOf(*t, f));
798  }
799  return s;
800  }
801 
802 template <typename K, typename T>
803  std::vector< std::pair<K, T> > switchOf(const std::vector< std::pair<K, MonoTypePtr> >& kts, const switchType<T>& f) {
804  typedef std::pair<K, T> KT;
805  typedef std::vector<KT> KTS;
806  KTS r;
807  for (typename std::vector< std::pair<K, MonoTypePtr> >::const_iterator kt = kts.begin(); kt != kts.end(); ++kt) {
808  r.push_back(KT(kt->first, switchOf(kt->second, f)));
809  }
810  return r;
811  }
812 
813 template <typename T>
816  for (Variant::Members::const_iterator m = ms.begin(); m != ms.end(); ++m) {
817  r.push_back(Variant::Member(m->selector, switchOf(m->type, f), m->id));
818  }
819  return r;
820  }
821 
822 template <typename T>
825  for (Record::Members::const_iterator m = ms.begin(); m != ms.end(); ++m) {
826  r.push_back(Record::Member(m->field, switchOf(m->type, f), m->offset));
827  }
828  return r;
829  }
830 
831 inline MonoTypes selectTypes(const Variant::Members& ms) {
832  MonoTypes r;
833  for (Variant::Members::const_iterator m = ms.begin(); m != ms.end(); ++m) {
834  r.push_back(m->type);
835  }
836  return r;
837 }
838 
840  str::seq r;
841  for (const auto& m : ms) {
842  r.push_back(m.selector);
843  }
844  return r;
845 }
846 
847 inline MonoTypes selectTypes(const Record::Members& ms) {
848  MonoTypes r;
849  for (Record::Members::const_iterator m = ms.begin(); m != ms.end(); ++m) {
850  r.push_back(m->type);
851  }
852  return r;
853 }
854 
855 inline str::seq selectNames(const Record::Members& ms) {
856  str::seq r;
857  for (const auto& m : ms) {
858  r.push_back(m.field);
859  }
860  return r;
861 }
862 
863 // walk a type structure for a side-effect (override any part to do per-constructor walks)
864 struct walkTy : public switchType<UnitV> {
865  UnitV with(const Prim* v) const;
866  UnitV with(const OpaquePtr* v) const;
867  UnitV with(const TVar* v) const;
868  UnitV with(const TGen* v) const;
869  UnitV with(const TAbs* v) const;
870  UnitV with(const TApp* v) const;
871  UnitV with(const FixedArray* v) const;
872  UnitV with(const Array* v) const;
873  UnitV with(const Variant* v) const;
874  UnitV with(const Record* v) const;
875  UnitV with(const Func* v) const;
876  UnitV with(const Exists* v) const;
877  UnitV with(const Recursive* v) const;
878  UnitV with(const TString* v) const;
879  UnitV with(const TLong* v) const;
880  UnitV with(const TExpr* v) const;
881 };
882 
883 // copy a type structure entirely (override any part to do a partial 'Type -> Type' transform)
884 struct switchTyFn : public switchType<MonoTypePtr> {
885  MonoTypePtr with(const Prim* v) const;
886  MonoTypePtr with(const OpaquePtr* v) const;
887  MonoTypePtr with(const TVar* v) const;
888  MonoTypePtr with(const TGen* v) const;
889  MonoTypePtr with(const TAbs* v) const;
890  MonoTypePtr with(const TApp* v) const;
891  MonoTypePtr with(const FixedArray* v) const;
892  MonoTypePtr with(const Array* v) const;
893  MonoTypePtr with(const Variant* v) const;
894  MonoTypePtr with(const Record* v) const;
895  MonoTypePtr with(const Func* v) const;
896  MonoTypePtr with(const Exists* v) const;
897  MonoTypePtr with(const Recursive* v) const;
898 
899  // bleh
900  MonoTypePtr with(const TString* v) const;
901  MonoTypePtr with(const TLong* v) const;
902 
903  // not quite as bleh
904  MonoTypePtr with(const TExpr*) const;
905 };
906 
907 MonoTypePtr clone(const MonoTypePtr&);
908 MonoTypePtr clone(const MonoType*);
909 MonoTypePtr clone(const MonoType&);
910 
911 QualTypePtr cloneP(const QualTypePtr& p);
912 MonoTypePtr cloneP(const MonoTypePtr& p);
913 
914 // simplify working with record types
915 QualTypePtr lookupFieldType(const QualTypePtr& qt, const std::string& fieldName);
916 MonoTypePtr lookupFieldType(const MonoTypePtr& mt, const std::string& fieldName);
917 
918 // join two sets of constraints together (eliminating redundant constraints)
919 Constraints mergeConstraints(const Constraints& lhs, const Constraints& rhs);
920 void mergeConstraints(const Constraints& fcs, Constraints* tcs);
921 
922 // polytype / gen utilities
923 MonoTypes tgens(int vs);
924 int tgenSize(const MonoTypePtr& mt);
925 int tgenSize(const MonoTypes& mts);
926 int tgenSize(const Constraints& cs);
927 int tgenSize(const ConstraintPtr& c);
928 int tgenSize(const QualTypePtr& qt);
929 
930 typedef std::set<int> TGenVarSet;
931 TGenVarSet tgenVars(const MonoTypePtr&);
932 
933 TVName canonicalName(int v);
934 MonoTypeSubst canonicalNameSubst(const NameSet& ns);
935 TVName freshName();
936 Names freshNames(int vs);
937 MonoTypePtr freshTypeVar();
938 MonoTypes freshTypeVars(int vs);
939 MonoTypes typeVars(const Names& ns);
940 
941 MonoTypes freshen(const MonoTypes&);
942 ConstraintPtr freshen(const ConstraintPtr&);
943 Constraints freshen(const Constraints& cs);
944 
945 QualTypePtr instantiate(int vs, const QualTypePtr& scheme);
946 Constraints instantiate(int vs, const Constraints& cs);
947 ConstraintPtr instantiate(int vs, const ConstraintPtr& c);
948 MonoTypePtr instantiate(int vs, const MonoTypePtr& mt);
949 MonoTypes instantiate(int vs, const MonoTypes& ts);
950 QualTypePtr instantiate(const Names& ns, const QualTypePtr& scheme);
951 Constraints instantiate(const Names& ns, const Constraints& cs);
952 ConstraintPtr instantiate(const Names& ns, const ConstraintPtr& c);
953 MonoTypePtr instantiate(const Names& ns, const MonoTypePtr& mt);
954 MonoTypes instantiate(const Names& ns, const MonoTypes& ts);
955 QualTypePtr instantiate(const MonoTypes& ts, const QualTypePtr& scheme);
956 Constraints instantiate(const MonoTypes& ts, const Constraints& cs);
957 ConstraintPtr instantiate(const MonoTypes& ts, const ConstraintPtr& c);
958 MonoTypePtr instantiate(const MonoTypes& ts, const MonoTypePtr& mt);
959 MonoTypes instantiate(const MonoTypes& ts, const MonoTypes& sts);
960 
961 NameSet tvarNames(const QualTypePtr& qt);
962 NameSet tvarNames(const Constraints& cs);
963 NameSet tvarNames(const ConstraintPtr& c);
964 NameSet tvarNames(const MonoTypePtr& mt);
965 NameSet tvarNames(const MonoType& mt);
966 NameSet tvarNames(const MonoTypes& mts);
967 void tvarNames(const QualTypePtr& qt, NameSet* out);
968 void tvarNames(const Constraints& cs, NameSet* out);
969 void tvarNames(const ConstraintPtr& c, NameSet* out);
970 void tvarNames(const MonoTypePtr& mt, NameSet* out);
971 void tvarNames(const MonoType& mt, NameSet* out);
972 void tvarNames(const MonoTypes& mts, NameSet* out);
973 
974 bool isFreeVarNameIn(const TVName&, const MonoTypePtr&);
975 bool isFreeVarNameIn(const TVName&, const MonoTypes&);
976 
977 bool hasFreeVariables(const QualTypePtr&);
978 bool hasFreeVariables(const Constraints&);
979 bool hasFreeVariables(const ConstraintPtr&);
980 bool hasFreeVariables(const MonoTypePtr&);
981 bool hasFreeVariables(const MonoTypes&);
982 
983 void show(const MonoTypeSubst& s, std::ostream& out);
984 std::string show(const MonoTypeSubst& s);
985 
986 QualTypePtr substitute(const MonoTypeSubst& s, const QualTypePtr& qt);
987 Constraints substitute(const MonoTypeSubst& s, const Constraints& cs);
988 ConstraintPtr substitute(const MonoTypeSubst& s, const ConstraintPtr& p);
989 MonoTypePtr substitute(const MonoTypeSubst& s, const MonoType& mt);
990 MonoTypePtr substitute(const MonoTypeSubst& s, const MonoTypePtr& mt);
991 MonoTypes substitute(const MonoTypeSubst& s, const MonoTypes& ts);
992 
993 MonoTypePtr substituteStep(const MonoTypeSubst& s, const MonoTypePtr& mt);
994 
995 PolyTypePtr generalize(const QualTypePtr& qt);
996 
997 QualTypePtr simplifyVarNames(const PolyType&);
998 QualTypePtr simplifyVarNames(const PolyTypePtr&);
999 ConstraintPtr simplifyVarNames(const Constraint&);
1000 ConstraintPtr simplifyVarNames(const ConstraintPtr&);
1001 QualTypePtr simplifyVarNames(const QualType&);
1002 QualTypePtr simplifyVarNames(const QualTypePtr&);
1003 MonoTypePtr simplifyVarNames(const MonoType&);
1004 MonoTypePtr simplifyVarNames(const MonoTypePtr&);
1005 MonoTypes simplifyVarNames(const MonoTypes& mts);
1006 
1007 // simplify working with type structures lifting of C++ types
1008 inline MonoTypePtr primty(const char* x) {
1009  return Prim::make(x);
1010 }
1011 
1012 inline MonoTypePtr primty(const char* x, const MonoTypePtr& aty) {
1013  return Prim::make(x, aty);
1014 }
1015 
1016 inline QualTypePtr qualtype(const Constraints& cs, const MonoTypePtr& p) {
1017  return QualTypePtr(new QualType(cs, p));
1018 }
1019 
1020 inline QualTypePtr qualtype(const Constraints& cs, MonoType* mt) {
1021  return qualtype(cs, MonoTypePtr(mt));
1022 }
1023 
1024 inline QualTypePtr qualtype(const MonoTypePtr& p) {
1025  return QualTypePtr(new QualType(p));
1026 }
1027 
1028 inline QualTypePtr qualtype(MonoType* mt) {
1029  return QualTypePtr(new QualType(MonoTypePtr(mt)));
1030 }
1031 
1032 inline QualTypePtr qualtype(const char* x) {
1033  return qualtype(primty(x));
1034 }
1035 
1036 inline PolyTypePtr polytype(int tvs, const QualTypePtr& qt) {
1037  return PolyTypePtr(new PolyType(tvs, qt));
1038 }
1039 
1040 inline PolyTypePtr polytype(const QualTypePtr& qt) {
1041  return PolyTypePtr(new PolyType(qt));
1042 }
1043 
1044 inline PolyTypePtr polytype(const MonoTypePtr& p) {
1045  return polytype(qualtype(p));
1046 }
1047 
1048 inline PolyTypePtr polytype(MonoType* mt) {
1049  return polytype(MonoTypePtr(mt));
1050 }
1051 
1052 inline PolyTypePtr polytype(const char* x) {
1053  return polytype(primty(x));
1054 }
1055 
1056 inline MonoTypePtr tgen(int i) {
1057  return TGen::make(i);
1058 }
1059 
1060 inline MonoTypePtr tvar(const std::string& vn) {
1061  return TVar::make(vn);
1062 }
1063 
1064 inline MonoTypePtr tstring(const std::string& x) {
1065  return TString::make(x);
1066 }
1067 
1068 inline MonoTypePtr tuple(const MonoTypes& mtys = MonoTypes()) {
1069  if (mtys.size() == 0) {
1070  return primty("unit");
1071  } else {
1072  Record::Members ms;
1073  for (unsigned int i = 0; i < mtys.size(); ++i) {
1074  ms.push_back(Record::Member(".f" + str::from(i), mtys[i]));
1075  }
1076  return Record::make(ms);
1077  }
1078 }
1079 
1080 inline MonoTypePtr sumtype(const MonoTypePtr& t0, const MonoTypePtr& t1) {
1081  Variant::Members ms;
1082  ms.push_back(Variant::Member(".f0", t0, 0));
1083  ms.push_back(Variant::Member(".f1", t1, 1));
1084  return Variant::make(ms);
1085 }
1086 
1087 inline MonoTypePtr maybety(const MonoTypePtr& t) {
1088  return sumtype(primty("unit"), t);
1089 }
1090 
1091 inline MonoTypePtr isMaybe(const MonoTypePtr& t) {
1092  if (const Variant* vt = is<Variant>(t)) {
1093  const Variant::Members& ms = vt->members();
1094  if (ms.size() == 2 && ms[0].selector == ".f0" && ms[1].selector == ".f1" && ms[0].type == primty("unit")) {
1095  return ms[1].type;
1096  }
1097  }
1098  return MonoTypePtr();
1099 }
1100 
1101 inline MonoTypePtr tstrings(const std::vector<std::string>& xs) {
1102  MonoTypes ts;
1103  for (const auto& x : xs) {
1104  ts.push_back(tstring(x));
1105  }
1106  return tuple(ts);
1107 }
1108 
1109 inline MonoTypePtr functy(const MonoTypePtr& aty, const MonoTypePtr& rty) {
1110  return Func::make(aty, rty);
1111 }
1112 
1113 inline MonoTypePtr functy(const MonoTypes& atys, const MonoTypePtr& rty) {
1114  if (atys.size() == 0) {
1115  return Func::make(tuple(list(tuple())), rty);
1116  } else {
1117  return Func::make(tuple(atys), rty);
1118  }
1119 }
1120 
1121 inline MonoTypePtr closty(const MonoTypePtr& aty, const MonoTypePtr& rty) {
1122  return Exists::make("E", tuple(list(functy(tuple(list(tvar("E"), aty)), rty), tvar("E"))));
1123 }
1124 
1125 inline MonoTypePtr closty(const MonoTypes& atys, const MonoTypePtr& rty) {
1126  return Exists::make("E", tuple(list(functy(tuple(cons(tvar("E"), atys)), rty), tvar("E"))));
1127 }
1128 
1129 inline MonoTypePtr tabs(const str::seq& tns, const MonoTypePtr& body) {
1130  return TAbs::make(tns, body);
1131 }
1132 
1133 inline MonoTypePtr tapp(const MonoTypePtr& f, const MonoTypes& args) {
1134  return TApp::make(f, args);
1135 }
1136 
1137 inline MonoTypePtr tlong(long x) {
1138  return TLong::make(x);
1139 }
1140 
1141 inline MonoTypePtr texpr(const ExprPtr& x) {
1142  return TExpr::make(x);
1143 }
1144 
1145 bool isMonoSingular(const MonoType&);
1146 bool isMonoSingular(const MonoType*);
1147 bool isMonoSingular(const MonoTypePtr&);
1148 bool isMonoSingular(const QualTypePtr&);
1149 bool isMonotype(const QualTypePtr& qt);
1150 bool isMonotype(const PolyTypePtr& pt);
1151 MonoTypePtr requireMonotype(const QualTypePtr& qt);
1152 MonoTypePtr requireMonotype(const PolyTypePtr& pt);
1153 MonoTypes requireMonotype(const PolyTypes& pts);
1154 
1155 inline MonoTypePtr arrayty(const MonoTypePtr& ty, size_t n) {
1156  return FixedArray::make(ty, tlong(n));
1157 }
1158 
1159 inline MonoTypePtr arrayty(const MonoTypePtr& ty) {
1160  return Array::make(ty);
1161 }
1162 
1163 template <typename T>
1164  MonoTypePtr opaqueptr(bool insertcontig) {
1165  return OpaquePtr::make(str::demangle<T>(), insertcontig ? sizeof(T) : 0, insertcontig);
1166  }
1167 
1168 inline bool isVoid(const MonoTypePtr& t) {
1169  if (Prim* pt = is<Prim>(t)) {
1170  return pt->name() == "void";
1171  } else {
1172  return false;
1173  }
1174 }
1175 
1176 // determine the size of monotypes
1177 unsigned int sizeOf(const MonoTypePtr& mt);
1178 
1179 // a type is equivalent to unit if its size is 0
1180 inline bool isUnit(const MonoTypePtr& t) {
1181  try {
1182  return sizeOf(t) == 0;
1183  } catch (std::exception&) {
1184  return false; // well if we can't get a size, it must not be unit (yet?)
1185  }
1186 }
1187 
1188 // does this string refer to a named primitive type?
1189 bool isPrimName(const std::string& tn);
1190 
1191 // unroll a recursive type
1192 MonoTypePtr unroll(const MonoTypePtr&);
1193 
1194 // support an efficient binary codec for type descriptions
1195 void encode(const QualTypePtr&, std::ostream&);
1196 void encode(const MonoTypePtr&, std::ostream&);
1197 
1198 void decode(QualTypePtr*, std::istream&);
1199 void decode(MonoTypePtr*, std::istream&);
1200 
1201 void encode(const QualTypePtr&, std::vector<unsigned char>*);
1202 void encode(const MonoTypePtr&, std::vector<unsigned char>*);
1203 MonoTypePtr decode(const std::vector<unsigned char>& in);
1204 MonoTypePtr decode(const unsigned char* b, const unsigned char* e);
1205 
1206 // open all opaque type aliases
1207 MonoTypePtr unalias(const MonoTypePtr&);
1208 
1209 // record type constructors
1210 template <typename ... Xs>
1211  struct accumRecTy {
1212  static void accum(Record::Members*) {
1213  }
1214  };
1215 
1216 template <typename ... Xs>
1217  struct accumRecTy<const char*, MonoTypePtr, Xs...> {
1218  static void accum(Record::Members* r, const char* n, const MonoTypePtr& t, Xs ... xs) {
1219  r->push_back(Record::Member(n, t));
1220  accumRecTy<Xs...>::accum(r, xs...);
1221  }
1222  };
1223 
1224 template <typename ... Xs>
1225  inline MonoTypePtr makeRecordType(Xs ... xs) {
1226  Record::Members ms;
1227  accumRecTy<Xs...>::accum(&ms, xs...);
1228  return Record::make(ms);
1229  }
1230 
1231 
1232 // some temporary abstraction/hacking on the way to fully general type constructor form
1233 inline MonoTypePtr fnresult(const MonoTypePtr& fty) {
1234  if (const Func* f = is<Func>(fty)) {
1235  return f->result();
1236  } else if (const TApp* fa = is<TApp>(fty)) {
1237  if (const Prim* fn = is<Prim>(fa->fn())) {
1238  if (fn->name() == "->" && fa->args().size() == 2) {
1239  return fa->args()[1];
1240  }
1241  }
1242  }
1243 
1244  throw std::runtime_error("Expected function type: " + show(fty));
1245 }
1246 
1247 void compactMTypeMemory();
1248 
1249 // hash record and variant members
1250 template <>
1251  struct genHash<Record::Member> {
1252  inline size_t operator()(const Record::Member& x) const {
1253  typedef std::tuple<std::string, MonoTypePtr, unsigned int> RMTup;
1254  static genHash<RMTup> h;
1255  return h(RMTup(x.field, x.type, x.offset));
1256  }
1257  };
1258 template <>
1259  struct genHash<Variant::Member> {
1260  inline size_t operator()(const Variant::Member& x) const {
1261  typedef std::tuple<std::string, MonoTypePtr, unsigned int> VMTup;
1262  static genHash<VMTup> h;
1263  return h(VMTup(x.selector, x.type, x.id));
1264  }
1265  };
1266 template <>
1267  struct genHash<MonoTypePtr> {
1268  inline size_t operator()(const MonoTypePtr& x) const {
1269  static std::hash<void*> h;
1270  return h((void*)x.get());
1271  }
1272  };
1273 
1274 }
1275 
1276 #endif
1277 
std::string nm
Definition: type.H:312
std::shared_ptr< PolyType > PolyTypePtr
Definition: type.H:23
Definition: tyunqualify.H:20
MonoTypePtr rty
Definition: type.H:506
std::vector< TVName > Names
Definition: type.H:141
MonoTypePtr substituteStep(const MonoTypeSubst &s, const MonoTypePtr &mt)
Definition: type.C:1983
virtual void show(std::ostream &) const =0
MonoTypePtr clone(const MonoType &)
Definition: type.C:1552
Definition: type.H:356
bool hasFreeVariables(const MonoTypes &)
Definition: type.C:1879
TEnvPtr bindFrame(const TEnvPtr &, const std::string &, const MonoTypePtr &)
Definition: type.C:273
MonoTypePtr ty
Definition: type.H:366
int tgenSize(const QualTypePtr &qt)
Definition: type.C:1695
int vs
Definition: type.H:179
std::vector< Member > Members
Definition: type.H:385
PolyTypePtr generalize(const QualTypePtr &qt)
Definition: type.C:2016
Definition: type.H:279
std::string showNoSimpl(const MonoTypePtr &e)
Definition: type.C:69
Definition: type.H:589
MonoTypePtr functy(const MonoTypes &atys, const MonoTypePtr &rty)
Definition: type.H:1113
MonoTypePtr primty(const char *x, const MonoTypePtr &aty)
Definition: type.H:1012
Definition: hash.H:17
ExprPtr field(MDFA *dfa, const std::string &vn, const std::string &fn)
Definition: dfa.C:183
MonoTypes substitute(const MonoTypeSubst &s, const MonoTypes &ts)
Definition: type.C:2007
std::set< std::string > TypeVarNames
Definition: type.H:60
MonoTypePtr arrayty(const MonoTypePtr &ty)
Definition: type.H:1159
size_t operator()(const Variant::Member &x) const
Definition: type.H:1260
std::set< TVName > NameSet
Definition: type.H:142
MonoTypePtr tgen(int i)
Definition: type.H:1056
MonoTypes tgens(int vs)
Definition: type.C:1661
void mergeConstraints(const Constraints &fcs, Constraints *tcs)
Definition: type.C:1588
MonoTypes typeVars(const Names &ns)
Definition: type.C:1653
Names freshNames(int vs)
Definition: type.C:1604
MonoTypePtr type
Definition: type.H:432
MonoTypePtr opaqueptr(bool insertcontig)
Definition: type.H:1164
Definition: type.H:89
Definition: tyunqualify.H:48
const T * ty
Definition: type.H:707
std::string field
Definition: type.H:431
unsigned int sizeOf(const MonoTypePtr &mt)
Definition: type.C:2190
Definition: type.H:165
MonoTypes freshTypeVars(int vs)
Definition: type.C:1616
bool satisfied(const UnqualifierPtr &, const TEnvPtr &, const ConstraintPtr &, std::vector< std::pair< std::string, std::shared_ptr< Expr > > > *)
MonoTypePtr aty
Definition: type.H:505
bool isPrimName(const std::string &tn)
Definition: type.C:2197
std::map< std::string, MonoTypePtr > TypeAliases
Definition: type.H:132
MonoTypePtr bty
Definition: type.H:525
bool satisfiable(const UnqualifierPtr &, const TEnvPtr &, const ConstraintPtr &, std::vector< std::pair< std::string, std::shared_ptr< Expr > > > *)
std::pair< std::string, std::string > pair
Definition: str.H:220
T * make(const Args &... args)
Definition: hobbes.H:60
void compactMTypeMemory()
Definition: type.C:595
Definition: type.H:373
str::seq targns
Definition: type.H:581
std::map< std::string, UnqualifierPtr > Unqualifiers
Definition: type.H:119
Definition: expr.H:507
str::seq selectNames(const Variant::Members &ms)
Definition: type.H:839
MonoTypePtr f
Definition: type.H:600
void encode(const MonoTypePtr &, std::vector< unsigned char > *)
Definition: type.C:2516
Definition: type.H:711
bool isVoid(const MonoTypePtr &t)
Definition: type.H:1168
std::string from(const T &x)
Definition: str.H:101
QualTypePtr qualtype(const Constraints &cs, const MonoTypePtr &p)
Definition: type.H:1016
bool isMonoSingular(const QualTypePtr &)
Definition: type.C:1674
Definition: type.H:252
Definition: boot.H:7
Definition: type.H:425
NameSet tvarNames(const ExprPtr &)
Definition: expr.C:1383
Definition: type.H:423
Definition: type.H:713
MonoTypePtr unroll(const MonoTypePtr &)
Definition: type.C:2217
MonoTypes mts
Definition: type.H:239
TVName canonicalName(int v)
Definition: type.C:2032
MonoTypePtr len
Definition: type.H:349
llvm::Value * offset(llvm::IRBuilder<> *b, llvm::Value *p, llvm::Value *o0)
Definition: llvm.H:419
Definition: type.H:319
MonoTypePtr bty
Definition: type.H:620
TEnvPtr fnFrame(const TEnvPtr &, const str::seq &, const MonoTypes &)
Definition: type.C:264
std::vector< QualTypePtr > QualTypes
Definition: type.H:30
size_t operator()(const Record::Member &x) const
Definition: type.H:1252
bool isFreeVarNameIn(const TVName &, const MonoTypes &)
Definition: type.C:1848
MonoTypePtr texpr(const ExprPtr &x)
Definition: type.H:1141
MonoTypePtr tstring(const std::string &x)
Definition: type.H:1064
Definition: type.H:570
Definition: type.H:683
int index(const std::vector< T > &xs, T x)
Definition: array.H:63
MonoType::ptr MonoTypePtr
Definition: type.H:71
Definition: type.H:608
std::ostream & operator<<(std::ostream &out, const array< char > *x)
Definition: hobbes.H:38
MonoTypePtr maybety(const MonoTypePtr &t)
Definition: type.H:1087
ExprPtr substitute(const VarMapping &vm, const ExprPtr &e, bool *mapped=0)
Definition: expr.C:968
static void accum(Record::Members *)
Definition: type.H:1212
virtual ~MonoType()
Definition: type.C:491
Definition: type.H:864
Definition: type.H:704
int x
Definition: type.H:328
MonoTypePtr type
Definition: type.H:382
std::string nm
Definition: type.H:271
static ptr makeType(const Args &... args)
Definition: type.H:335
MonoTypePtr lookupFieldType(const MonoTypePtr &mt, const std::string &fieldName)
Definition: type.C:1570
MonoTypePtr closty(const MonoTypes &atys, const MonoTypePtr &rty)
Definition: type.H:1125
long x
Definition: type.H:563
bool isMonotype(const PolyTypePtr &pt)
Definition: type.C:2245
PolyTypePtr polytype(const char *x)
Definition: type.H:1052
Members ms
Definition: type.H:464
std::string tname
Definition: type.H:619
std::set< int > TGenVarSet
Definition: type.H:930
MonoTypePtr fnresult(const MonoTypePtr &fty)
Definition: type.H:1233
int case_id() const
Definition: type.C:490
UnqualifierSetPtr unquals
Definition: type.H:123
bool operator==(const MonoType &rhs) const
Definition: type.C:493
MonoTypePtr tabs(const str::seq &tns, const MonoTypePtr &body)
Definition: type.H:1129
MonoTypes simplifyVarNames(const MonoTypes &mts)
Definition: type.C:2064
bool scontig
Definition: type.H:293
std::vector< T > cons(T h, std::vector< T > t)
Definition: array.H:286
Members ms
Definition: type.H:416
std::shared_ptr< Unqualifier > UnqualifierPtr
Definition: type.H:83
MonoTypePtr t
Definition: type.H:272
Definition: typeinf.H:29
MonoTypes instantiate(const MonoTypes &ts, const MonoTypes &sts)
Definition: type.C:1763
std::vector< ConstraintPtr > Constraints
Definition: type.H:35
int tgenCount
Definition: type.H:62
Definition: type.H:553
MonoTypePtr tlong(long x)
Definition: type.H:1137
MonoType(int cid)
Definition: type.C:489
Definition: type.H:706
void tvarNames(const MonoTypes &mts, NameSet *out)
Definition: type.C:1838
ptr unaliasedType
Definition: type.H:68
std::string selector
Definition: type.H:381
std::map< std::string, PolyTypePtr > PolyTypeEnv
Definition: type.H:114
unsigned int sz
Definition: type.H:292
Definition: type.H:303
std::vector< PolyTypePtr > PolyTypes
Definition: type.H:25
std::shared_ptr< Expr > ExprPtr
Definition: expr.H:58
TypeVarNames freeTVars
Definition: type.H:61
unsigned int payloadSizeM
Definition: type.H:404
Definition: type.H:884
std::vector< T > list()
Definition: array.H:25
MonoTypePtr ty
Definition: type.H:348
MonoTypeCase()
Definition: type.H:648
MonoTypePtr tvar(const std::string &vn)
Definition: type.H:1060
PolyTypeEnv ptenv
Definition: type.H:124
size_t r(const reader::MetaData &md, size_t o, T *t)
Definition: storage.H:1730
TypeAliases typeAliases
Definition: type.H:133
QualTypePtr instantiate(int vs, const QualTypePtr &scheme)
Definition: type.C:1722
int cid
Definition: type.H:56
MonoTypeSubst substitution(const str::seq &ns, const MonoTypes &ts)
Definition: type.H:145
std::shared_ptr< TEnv > TEnvPtr
Definition: type.H:80
Definition: type.H:38
std::vector< std::string > seq
Definition: str.H:19
uint32_t state
Definition: regex.C:372
MonoTypePtr freshTypeVar()
Definition: type.C:1612
T * ty
Definition: type.H:714
Definition: type.H:375
ExprPtr e
Definition: type.H:641
Definition: type.H:1211
ExprPtr fn(const str::seq &vns, const ExprPtr &b, const LexicalAnnotation &la)
Definition: expr.H:837
std::string show(const MonoTypeSubst &s)
Definition: type.C:1889
#define out
Definition: netio.H:19
MonoTypes requireMonotype(const PolyTypes &pts)
Definition: type.C:2265
TGenVarSet tgenVars(const MonoTypePtr &)
Definition: type.C:1710
MonoTypePtr normIfOpaquePtr(const MonoTypePtr &ty)
Definition: type.C:640
MonoTypes targs
Definition: type.H:601
Definition: type.H:513
std::string cat
Definition: type.H:238
MonoTypePtr tstrings(const std::vector< std::string > &xs)
Definition: type.H:1101
Members ams
Definition: type.H:465
std::string TVName
Definition: type.H:140
std::set< std::string > set
Definition: str.H:241
unsigned int alignment(const MonoTypePtr &)
Definition: type.C:508
std::shared_ptr< UnqualifierSet > UnqualifierSetPtr
Definition: type.H:86
MonoTypePtr isMaybe(const MonoTypePtr &t)
Definition: type.H:1091
uint32_t result
Definition: regex.C:376
Definition: type.H:537
MonoTypePtr unalias(const MonoTypePtr &)
Definition: type.C:2738
std::shared_ptr< MonoType > ptr
Definition: type.H:41
unsigned int maxFieldAlignmentM
Definition: type.H:467
std::vector< Member > Members
Definition: type.H:435
MonoTypePtr unpackedType(const Exists *e)
Definition: type.C:2274
QualTypePtr qualtype(const char *x)
Definition: type.H:1032
std::shared_ptr< Constraint > ConstraintPtr
Definition: type.H:33
T switchOfF(Ptr ty, F f)
Definition: type.H:719
bool hasFreeVariables(const QualTypePtr &)
Definition: type.C:1858
MonoTypePtr sumtype(const MonoTypePtr &t0, const MonoTypePtr &t1)
Definition: type.H:1080
MonoTypes selectTypes(const Variant::Members &ms)
Definition: type.H:831
MonoTypePtr mt
Definition: type.H:204
unsigned int memorySize
Definition: type.H:65
Definition: expr.H:15
std::map< std::string, llvm::Value * > Args
Definition: dfa.C:1276
Constraints freshen(const Constraints &cs)
Definition: type.C:1644
std::vector< MonoTypePtr > MonoTypes
Definition: type.H:72
MonoTypePtr cloneP(const MonoTypePtr &p)
Definition: type.C:1558
Constraints cs
Definition: type.H:203
QualTypePtr qt
Definition: type.H:180
std::string nm
Definition: type.H:291
static void accum(Record::Members *r, const char *n, const MonoTypePtr &t, Xs ... xs)
Definition: type.H:1218
Definition: type.H:631
Definition: type.H:491
MonoTypeSubst canonicalNameSubst(const NameSet &ns)
Definition: type.C:2041
std::shared_ptr< QualType > QualTypePtr
Definition: type.H:28
MonoTypePtr tuple(const MonoTypes &mtys=MonoTypes())
Definition: type.H:1068
std::string tname
Definition: type.H:524
int offset
Definition: type.H:433
size_t operator()(const MonoTypePtr &x) const
Definition: type.H:1268
LexicalAnnotation m(const YYLTYPE &p)
Definition: hexpr.parse.C:127
Record::Members switchOf(const Record::Members &ms, const switchType< T > &f)
Definition: type.H:823
bool in(T x, const std::set< T > &xs)
Definition: array.H:47
std::map< TVName, MonoTypePtr > MonoTypeSubst
Definition: type.H:143
bool isUnit(const MonoTypePtr &t)
Definition: type.H:1180
MonoTypePtr decode(const unsigned char *b, const unsigned char *e)
Definition: type.C:2685
TEnvPtr parent
Definition: type.H:122
MonoTypePtr b
Definition: type.H:582
MonoTypePtr makeRecordType(Xs ... xs)
Definition: type.H:1225
Definition: type.H:260
Definition: type.H:189
std::string val
Definition: type.H:547
Definition: type.H:213
T lookup(const std::map< K, T > &tenv, const K &n)
Definition: cc.C:518
MonoTypePtr tapp(const MonoTypePtr &f, const MonoTypes &args)
Definition: type.H:1133
unsigned int id
Definition: type.H:383
unsigned char UnitV
Definition: array.H:17
TVName freshName()
Definition: type.C:1600