Module: Jade::Frontend::TypeChecking::Unification
- Extended by:
- Unification
- Included in:
- Unification
- Defined in:
- lib/jade/frontend/type_checking/unification.rb
Defined Under Namespace
Classes: Context, UnificationError
Instance Method Summary collapse
Instance Method Details
#unify(type1, type2, env, ctx = Context.empty) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/jade/frontend/type_checking/unification.rb', line 18 def unify(type1, type2, env, ctx = Context.empty) return Ok[Substitution::EMPTY] if never?(type1) || never?(type2) case [type1, type2] in [Type::Application, Type::Application] case [type1.constructor, type2.constructor] in [Type::Var, _] if type1.args.length < type2.args.length unify_partial(type1, type2, env, ctx) .map_error { UnificationError.new(type1, type2) } in [_, Type::Var] if type2.args.length < type1.args.length unify_partial(type2, type1, env, ctx) .map_error { UnificationError.new(type1, type2) } else unify(type1.constructor, type2.constructor, env, ctx) .map_error { UnificationError.new(type1, type2) } .and_then do |cons| unify_many(type1.args.map { cons.apply(it) }, type2.args.map { cons.apply(it) }, env, ctx) .map_both { cons.compose(it) } .map_error do |final_sub| UnificationError.new( final_sub.apply(type1), final_sub.apply(type2), ) end end end in [Type::Var, _] return Ok[Substitution::EMPTY] if type1 == type2 if ctx.rigid?(type1) if type2.is_a?(Type::Var) && !ctx.rigid?(type2) return Err[UnificationError.new(type2, type1)] if occurs_in?(type2, type1) return Ok[Substitution::EMPTY.bind(type2.id, type1)] end return Err[UnificationError.new(type1, type2)] end return Err[UnificationError.new(type1, type2)] if occurs_in?(type1, type2) Ok[Substitution::EMPTY.bind(type1.id, type2)] in [_, Type::Var] unify(type2, type1, env, ctx) .map_error(&:flip) in [Type::Function, Type::Function] unless type1.args.size == type2.args.size return Err[UnificationError.new(type1, type2)] end unify_many(type1.args, type2.args, env, ctx) .then do |args_r| args_sub = substitution_of(args_r) unify(args_sub.apply(type1.return_type), args_sub.apply(type2.return_type), env, ctx) .on_err { args_r.and_then { Err[it] } } .and_then { |sub| args_r.map_both { it.compose(sub) } } end .map_error do |final_sub| UnificationError.new( final_sub.apply(type1), final_sub.apply(type2), final_sub, ) end in [Type::Constructor, Type::Constructor] type1 == type2 ? Ok[Substitution::EMPTY] : Err[UnificationError.new(type1, type2)] in [Type::AnonymousRecord, Type::AnonymousRecord] f1 = type1.field_names f2 = type2.field_names if type1.closed? && (f2 - f1).any? return Err[UnificationError.new(type1, type2)] end if type2.closed? && (f1 - f2).any? return Err[UnificationError.new(type1, type2)] end unify_shared_fields(type1, type2, env, ctx) .map_error do |final_sub| UnificationError.new( final_sub.apply(type1), final_sub.apply(type2), ) end .and_then do |fields_r| if type1.open? && type2.open? fresh_type = env.fresh Type .anonymous_record(type1.fields.merge(type2.fields), env.fresh) .then { Substitution::EMPTY.bind(fresh_type.id, it)} .bind(type1.row_var.id, fresh_type) .bind(type2.row_var.id, fresh_type) .compose(fields_r) .then { Ok[it] } elsif type1.open? unify(type1.row_var, type2, env, ctx) .map { fields_r.compose(it) } elsif type2.open? unify(type2.row_var, type1, env, ctx) .map { fields_r.compose(it) } else Ok[fields_r] end end in [Type::AnonymousRecord, Type::Application] # Only OPEN anon records (record-access queries like `{a | x: T}`) # can match a nominal struct. Closed records are distinct types. return Err[UnificationError.new(type1, type2)] if type1.closed? = env.lookup_def(type2.constructor.name) return Err[UnificationError.new(type1, type2)] unless .is_a?(StructDef) .type_params.map(&:id).zip(type2.args).to_h .reduce(Substitution::EMPTY) do |acc, (k, v)| acc.bind(k, v) end .apply(.body) .then { unify(type1, it, env, ctx) } .and_then do |body_r| unify(type1.row_var, type2, env, ctx).map { body_r.compose(it) } end .on_err { Err[UnificationError.new(type1, type2)] } in [Type::Application, Type::AnonymousRecord] unify(type2, type1, env, ctx) .map_error(&:flip) else Err[UnificationError.new(type1, type2)] end end |