hobbes
a language, embedded compiler, and runtime for efficient dynamic expression evaluation, data storage and analysis
hasfield.H
Go to the documentation of this file.
1 
2 #ifndef HOBBES_LANG_TYPEPREDS_HASFIELD_HPP_INCLUDED
3 #define HOBBES_LANG_TYPEPREDS_HASFIELD_HPP_INCLUDED
4 
6 
7 namespace hobbes {
8 
9 // a 'hasfield' constraint ensures that a given type has a field with a certain name, with a certain type
10 // this constraint is intended to be generic, to subsume record projection, variant construction, and "object methods"
11 // e.g.:
12 // yes: HasField {x:int,y:bool} "y" bool
13 // no: HasField {x:int,y:bool} "chicken" int
14 // no: HasField {x:int,y:bool} "y" int
15 struct HasField {
16  enum Direction { Read, Write };
17 
23 
24  static Constraint* newConstraint(Direction,const MonoTypePtr&, const MonoTypePtr&, const MonoTypePtr&, const ExprPtr&);
25  static Constraint* newConstraint(Direction,const MonoTypePtr&, const MonoTypePtr&, const MonoTypePtr&);
26  static ConstraintPtr constraint(Direction,const MonoTypePtr&, const MonoTypePtr&, const MonoTypePtr&, const ExprPtr&);
28 };
29 bool dec(const ConstraintPtr&, HasField*);
30 void upd(const ConstraintPtr&, const HasField&);
31 
32 // a "has-field eliminator" knows how to resolve a "HasField" constraint at a particular (category of) type
33 struct HFEliminator {
34  // is this HF instance eliminable?
35  virtual bool satisfied(const TEnvPtr& tenv, const HasField&, Definitions*) const = 0;
36 
37  // is it possible for this HF instance to eventually be eliminated?
38  virtual bool satisfiable(const TEnvPtr& tenv, const HasField&, Definitions*) const = 0;
39 
40  // refine the substitution set associated with this constraint
41  virtual bool refine(const TEnvPtr& tenv, const HasField&, MonoTypeUnifier* s, Definitions*) = 0;
42 
43  // unqualify a constraint (satisfied() must have returned true)
44  virtual ExprPtr unqualify(const TEnvPtr&, const ConstraintPtr&, const ExprPtr&, Definitions*) const = 0;
45 
46  // what would you call this eliminator?
47  virtual std::string name() const = 0;
48 };
49 
50 // a 'field verifier' is a scheme for validating types as containing a named field somehow
51 class FieldVerifier : public Unqualifier {
52 public:
53  FieldVerifier();
54  static std::string constraintName();
55 
56  // extend the set of 'hasfield' eliminators dynamically (dangerous?)
57  void addEliminator(HFEliminator*);
58 
59  // unqualifier interface
61  bool satisfied(const TEnvPtr&,const ConstraintPtr&,Definitions*) const;
62  bool satisfiable(const TEnvPtr&,const ConstraintPtr&,Definitions*) const;
63  void explain(const TEnvPtr& tenv, const ConstraintPtr& cst, const ExprPtr& e, Definitions* ds, annmsgs* msgs);
64  ExprPtr unqualify(const TEnvPtr&,const ConstraintPtr&, const ExprPtr&, Definitions*) const;
65  PolyTypePtr lookup (const std::string& vn) const;
66  SymSet bindings () const;
67  FunDeps dependencies(const ConstraintPtr&) const;
68 private:
69  typedef std::vector<HFEliminator*> HFEliminators;
70  HFEliminators eliminators;
71 };
72 
73 }
74 
75 #endif
76 
std::shared_ptr< PolyType > PolyTypePtr
Definition: type.H:23
Definition: tyunqualify.H:20
static ConstraintPtr constraint(Direction, const MonoTypePtr &, const MonoTypePtr &, const MonoTypePtr &, const ExprPtr &)
Definition: hasfield.C:21
HFEliminators eliminators
Definition: hasfield.H:70
Definition: hasfield.H:16
void upd(const ConstraintPtr &, const HasField &)
Definition: hasfield.C:40
Definition: hasfield.H:51
bool satisfied(const UnqualifierPtr &, const TEnvPtr &, const ConstraintPtr &, std::vector< std::pair< std::string, std::shared_ptr< Expr > > > *)
bool satisfiable(const UnqualifierPtr &, const TEnvPtr &, const ConstraintPtr &, std::vector< std::pair< std::string, std::shared_ptr< Expr > > > *)
std::set< std::string > SymSet
Definition: tyunqualify.H:12
Definition: hasfield.H:33
Definition: boot.H:7
std::vector< FunDep > FunDeps
Definition: tyunqualify.H:17
ExprPtr recordExpr
Definition: hasfield.H:20
MonoType::ptr MonoTypePtr
Definition: type.H:71
Direction
Definition: hasfield.H:16
MonoTypePtr fieldName
Definition: hasfield.H:21
Definition: hasfield.H:16
Definition: typeinf.H:29
std::shared_ptr< Expr > ExprPtr
Definition: expr.H:58
bool dec(const ConstraintPtr &, AppendsTo *)
Definition: appendsto.C:15
std::shared_ptr< TEnv > TEnvPtr
Definition: type.H:80
std::vector< Definition > Definitions
Definition: expr.H:62
Definition: hasfield.H:15
std::shared_ptr< Constraint > ConstraintPtr
Definition: type.H:33
bool refine(const TEnvPtr &tenv, const ConstraintPtr &c, MonoTypeUnifier *s, Definitions *)
Definition: typeinf.C:485
MonoTypePtr fieldType
Definition: hasfield.H:22
std::vector< HFEliminator * > HFEliminators
Definition: hasfield.H:69
static Constraint * newConstraint(Direction, const MonoTypePtr &, const MonoTypePtr &, const MonoTypePtr &, const ExprPtr &)
Definition: hasfield.C:19
Definition: type.H:213
T lookup(const std::map< K, T > &tenv, const K &n)
Definition: cc.C:518
Direction direction
Definition: hasfield.H:18
std::vector< annmsg > annmsgs
Definition: lannotation.H:58
MonoTypePtr recordType
Definition: hasfield.H:19