hobbes
a language, embedded compiler, and runtime for efficient dynamic expression evaluation, data storage and analysis
typeinf.H
Go to the documentation of this file.
1 
2 #ifndef HOBBES_LANG_TYPEINF_HPP_INCLUDED
3 #define HOBBES_LANG_TYPEINF_HPP_INCLUDED
4 
5 #include <hobbes/lang/type.H>
6 #include <hobbes/lang/expr.H>
8 #include <map>
9 #include <set>
10 
11 namespace hobbes {
12 
13 // type equivalence data
14 struct UTypeRec {
16  bool visited;
17  size_t donebc;
18 };
19 
20 // represent a type substitution abstractly as disjoint sets of equivalent types ordered by 'definedness'
21 struct LiftUType {
22  static UTypeRec apply(const MonoTypePtr&);
23 };
24 
26  static const UTypeRec& apply(const UTypeRec&, const UTypeRec&);
27 };
28 
30 public:
31  MonoTypeUnifier(const TEnvPtr&);
33  MonoTypeUnifier& operator=(const MonoTypeUnifier&);
34 
35  // what type environment are we unifying in?
36  const TEnvPtr& typeEnv() const;
37 
38  // how many unifications have we made?
39  size_t size() const;
40 
41  // suppress/unsuppress bindings to a particularly-named variable
42  void suppress(const std::string&);
43  void suppress(const str::seq&);
44  void unsuppress(const std::string&);
45  void unsuppress(const str::seq&);
46 
47  // read/write a var-name/type association
48  void bind(const std::string&, const MonoTypePtr&);
49  MonoTypePtr binding(const std::string&);
50 
51  // specify that two types should be equal
52  void unify(const MonoTypePtr&, const MonoTypePtr&);
53 
54  // resolve the input type against the local mapping
57 
58  // merge another unifier's equivalence set with this one
59  size_t merge(const MonoTypeUnifier&);
60 
61  // represent this unification set as a type substitution
63 private:
65  size_t bcount;
66 
67  // avoid binding to certain type variables
68  typedef std::map<std::string, size_t> SuppressVarCounts;
69  SuppressVarCounts suppressVarCounts;
70 
71  bool suppressed(const std::string&) const;
72  bool suppressed(const MonoTypePtr&) const;
73 
74  // equivalences between types
76  M m;
77 };
78 
79 // simplify safe suppression of type variables
81 public:
82  scoped_unification_suppression(MonoTypeUnifier*, const std::string&);
84 private:
86  std::string sv;
87 };
88 
89 // recursively apply unification substitution in-place
96 
97 // a simple test to determine if two types _can_ be unified
98 bool unifiable(const TEnvPtr&, const MonoTypePtr&, const MonoTypePtr&);
99 bool unifiable(const TEnvPtr&, const MonoTypes&, const MonoTypes&);
100 bool unifiable(const TEnvPtr&, const ConstraintPtr&, const ConstraintPtr&);
101 
102 // allow constraint refinement according to implied type membership
103 bool refine(const TEnvPtr& tenv, const ConstraintPtr& c, MonoTypeUnifier* s, Definitions*);
104 bool refine(const TEnvPtr& tenv, const Constraints& cs, MonoTypeUnifier* s, Definitions*);
105 
106 // when we infer/validate types, this can produce a modified expression (for explicit resolution of implicit qualifiers, and explicit type annotations)
107 ExprPtr validateType(const TEnvPtr& tenv, const ExprPtr& e, Definitions*);
108 
109 // extend type inference to definitions, which can be generally recursive
110 ExprPtr validateType(const TEnvPtr& tenv, const std::string& vname, const ExprPtr& e, Definitions*);
111 
112 // type unification
113 void mgu(const ExprPtr&, const ExprPtr&, MonoTypeUnifier*);
114 void mgu(const ExprPtr&, const QualTypePtr&, MonoTypeUnifier*);
115 void mgu(const ExprPtr&, const MonoTypePtr&, MonoTypeUnifier*);
116 
117 void mgu(const QualTypePtr& t0, const QualTypePtr& t1, MonoTypeUnifier* u);
118 void mgu(const MonoTypePtr& t0, const MonoTypePtr& t1, MonoTypeUnifier* u);
119 void mgu(const MonoTypes& ts0, const MonoTypes& ts1, MonoTypeUnifier* u);
120 
121 void mgu(const ConstraintPtr&, const ConstraintPtr&, MonoTypeUnifier*);
122 
123 // some utilities for dealing with qualified types
124 typedef std::pair<Constraints, MonoTypes> QualLiftedMonoTypes;
125 QualLiftedMonoTypes liftQualifiers(const QualTypes& qts);
126 
127 typedef std::pair<Constraints, MonoTypePtr> QualLiftedMonoType;
128 
129 }
130 
131 #endif
132 
TEnvPtr tenv
Definition: typeinf.H:64
std::string sv
Definition: typeinf.H:86
SuppressVarCounts suppressVarCounts
Definition: typeinf.H:69
std::pair< Constraints, MonoTypes > QualLiftedMonoTypes
Definition: typeinf.H:124
bool visited
Definition: typeinf.H:16
std::map< std::string, size_t > SuppressVarCounts
Definition: typeinf.H:68
void apply(const transition_lookahead &tl, parserdef *p)
Definition: lalr.C:564
size_t donebc
Definition: typeinf.H:17
QualLiftedMonoTypes liftQualifiers(const QualTypes &qts)
Definition: typeinf.C:1008
Definition: boot.H:7
std::vector< QualTypePtr > QualTypes
Definition: type.H:30
MonoType::ptr MonoTypePtr
Definition: type.H:71
Definition: typeinf.H:21
ExprPtr substitute(const VarMapping &vm, const ExprPtr &e, bool *mapped=0)
Definition: expr.C:968
bool unifiable(const TEnvPtr &, const MonoTypePtr &, const MonoTypePtr &)
Definition: typeinf.C:437
std::pair< Constraints, MonoTypePtr > QualLiftedMonoType
Definition: typeinf.H:127
Definition: typeinf.H:29
Definition: typeinf.H:14
MonoTypeUnifier * u
Definition: typeinf.H:85
std::vector< ConstraintPtr > Constraints
Definition: type.H:35
M m
Definition: typeinf.H:76
std::shared_ptr< Expr > ExprPtr
Definition: expr.H:58
MonoTypeSubst substitution(const str::seq &ns, const MonoTypes &ts)
Definition: type.H:145
std::shared_ptr< TEnv > TEnvPtr
Definition: type.H:80
std::vector< std::string > seq
Definition: str.H:19
equivalence_mapping< MonoTypePtr, UTypeRec, LiftUType, MoreDefinedType > M
Definition: typeinf.H:75
MonoTypePtr ty
Definition: typeinf.H:15
std::vector< Definition > Definitions
Definition: expr.H:62
ExprPtr validateType(const TEnvPtr &tenv, const ExprPtr &e, Definitions *)
Definition: typeinf.C:925
Definition: typeinf.H:25
Definition: file.C:71
std::shared_ptr< Constraint > ConstraintPtr
Definition: type.H:33
bool refine(const TEnvPtr &tenv, const ConstraintPtr &c, MonoTypeUnifier *s, Definitions *)
Definition: typeinf.C:485
std::vector< MonoTypePtr > MonoTypes
Definition: type.H:72
std::shared_ptr< QualType > QualTypePtr
Definition: type.H:28
size_t bcount
Definition: typeinf.H:65
std::map< TVName, MonoTypePtr > MonoTypeSubst
Definition: type.H:143
void mgu(const ExprPtr &, const ExprPtr &, MonoTypeUnifier *)
Definition: typeinf.C:959