1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
|
package hm
import "github.com/pkg/errors"
// Cloner is any type that can clone
type Cloner interface {
Clone() interface{}
}
// Fresher keeps track of all the TypeVariables that has been generated so far. It has one method - Fresh(), which is to create a new TypeVariable
type Fresher interface {
Fresh() TypeVariable
}
type inferer struct {
env Env
cs Constraints
t Type
count int
}
func newInferer(env Env) *inferer {
return &inferer{
env: env,
}
}
func (infer *inferer) Fresh() TypeVariable {
retVal := letters[infer.count]
infer.count++
return TypeVariable(retVal)
}
func (infer *inferer) lookup(name string) error {
s, ok := infer.env.SchemeOf(name)
if !ok {
return errors.Errorf("Undefined %v", name)
}
infer.t = Instantiate(infer, s)
return nil
}
func (infer *inferer) consGen(expr Expression) (err error) {
// explicit types/inferers - can fail
switch et := expr.(type) {
case Typer:
if infer.t = et.Type(); infer.t != nil {
return nil
}
case Inferer:
if infer.t, err = et.Infer(infer.env, infer); err == nil && infer.t != nil {
return nil
}
err = nil // reset errors
}
// fallbacks
switch et := expr.(type) {
case Literal:
return infer.lookup(et.Name())
case Var:
if err = infer.lookup(et.Name()); err != nil {
infer.env.Add(et.Name(), &Scheme{t: et.Type()})
err = nil
}
case Lambda:
tv := infer.Fresh()
env := infer.env // backup
infer.env = infer.env.Clone()
infer.env.Remove(et.Name())
sc := new(Scheme)
sc.t = tv
infer.env.Add(et.Name(), sc)
if err = infer.consGen(et.Body()); err != nil {
return errors.Wrapf(err, "Unable to infer body of %v. Body: %v", et, et.Body())
}
infer.t = NewFnType(tv, infer.t)
infer.env = env // restore backup
case Apply:
if err = infer.consGen(et.Fn()); err != nil {
return errors.Wrapf(err, "Unable to infer Fn of Apply: %v. Fn: %v", et, et.Fn())
}
fnType, fnCs := infer.t, infer.cs
if err = infer.consGen(et.Body()); err != nil {
return errors.Wrapf(err, "Unable to infer body of Apply: %v. Body: %v", et, et.Body())
}
bodyType, bodyCs := infer.t, infer.cs
tv := infer.Fresh()
cs := append(fnCs, bodyCs...)
cs = append(cs, Constraint{fnType, NewFnType(bodyType, tv)})
infer.t = tv
infer.cs = cs
case LetRec:
tv := infer.Fresh()
// env := infer.env // backup
infer.env = infer.env.Clone()
infer.env.Remove(et.Name())
infer.env.Add(et.Name(), &Scheme{tvs: TypeVarSet{tv}, t: tv})
if err = infer.consGen(et.Def()); err != nil {
return errors.Wrapf(err, "Unable to infer the definition of a letRec %v. Def: %v", et, et.Def())
}
defType, defCs := infer.t, infer.cs
s := newSolver()
s.solve(defCs)
if s.err != nil {
return errors.Wrapf(s.err, "Unable to solve constraints of def: %v", defCs)
}
sc := Generalize(infer.env.Apply(s.sub).(Env), defType.Apply(s.sub).(Type))
infer.env.Remove(et.Name())
infer.env.Add(et.Name(), sc)
if err = infer.consGen(et.Body()); err != nil {
return errors.Wrapf(err, "Unable to infer body of letRec %v. Body: %v", et, et.Body())
}
infer.t = infer.t.Apply(s.sub).(Type)
infer.cs = infer.cs.Apply(s.sub).(Constraints)
infer.cs = append(infer.cs, defCs...)
case Let:
env := infer.env
if err = infer.consGen(et.Def()); err != nil {
return errors.Wrapf(err, "Unable to infer the definition of a let %v. Def: %v", et, et.Def())
}
defType, defCs := infer.t, infer.cs
s := newSolver()
s.solve(defCs)
if s.err != nil {
return errors.Wrapf(s.err, "Unable to solve for the constraints of a def %v", defCs)
}
sc := Generalize(env.Apply(s.sub).(Env), defType.Apply(s.sub).(Type))
infer.env = infer.env.Clone()
infer.env.Remove(et.Name())
infer.env.Add(et.Name(), sc)
if err = infer.consGen(et.Body()); err != nil {
return errors.Wrapf(err, "Unable to infer body of let %v. Body: %v", et, et.Body())
}
infer.t = infer.t.Apply(s.sub).(Type)
infer.cs = infer.cs.Apply(s.sub).(Constraints)
infer.cs = append(infer.cs, defCs...)
default:
return errors.Errorf("Expression of %T is unhandled", expr)
}
return nil
}
// Instantiate takes a fresh name generator, an a polytype and makes a concrete type out of it.
//
// If ...
// Γ ⊢ e: T1 T1 ⊑ T
// ----------------------
// Γ ⊢ e: T
//
func Instantiate(f Fresher, s *Scheme) Type {
l := len(s.tvs)
tvs := make(TypeVarSet, l)
var sub Subs
if l > 30 {
sub = make(mSubs)
} else {
sub = newSliceSubs(l)
}
for i, tv := range s.tvs {
fr := f.Fresh()
tvs[i] = fr
sub = sub.Add(tv, fr)
}
return s.t.Apply(sub).(Type)
}
// Generalize takes an env and a type and creates the most general possible type - which is a polytype
//
// Generalization
//
// If ...
// Γ ⊢ e: T1 T1 ∉ free(Γ)
// ---------------------------
// Γ ⊢ e: ∀ α.T1
func Generalize(env Env, t Type) *Scheme {
logf("generalizing %v over %v", t, env)
enterLoggingContext()
defer leaveLoggingContext()
var envFree, tFree, diff TypeVarSet
if env != nil {
envFree = env.FreeTypeVar()
}
tFree = t.FreeTypeVar()
switch {
case envFree == nil && tFree == nil:
goto ret
case len(envFree) > 0 && len(tFree) > 0:
defer ReturnTypeVarSet(envFree)
defer ReturnTypeVarSet(tFree)
case len(envFree) > 0 && len(tFree) == 0:
// cannot return envFree because envFree will just be sorted and set
case len(envFree) == 0 && len(tFree) > 0:
// return ?
}
diff = tFree.Difference(envFree)
ret:
return &Scheme{
tvs: diff,
t: t,
}
}
// Infer takes an env, and an expression, and returns a scheme.
//
// The Infer function is the core of the HM type inference system. This is a reference implementation and is completely servicable, but not quite performant.
// You should use this as a reference and write your own infer function.
//
// Very briefly, these rules are implemented:
//
// Var
//
// If x is of type T, in a collection of statements Γ, then we can infer that x has type T when we come to a new instance of x
// x: T ∈ Γ
// -----------
// Γ ⊢ x: T
//
// Apply
//
// If f is a function that takes T1 and returns T2; and if x is of type T1;
// then we can infer that the result of applying f on x will yield a result has type T2
// Γ ⊢ f: T1→T2 Γ ⊢ x: T1
// -------------------------
// Γ ⊢ f(x): T2
//
//
// Lambda Abstraction
//
// If we assume x has type T1, and because of that we were able to infer e has type T2
// then we can infer that the lambda abstraction of e with respect to the variable x, λx.e,
// will be a function with type T1→T2
// Γ, x: T1 ⊢ e: T2
// -------------------
// Γ ⊢ λx.e: T1→T2
//
// Let
//
// If we can infer that e1 has type T1 and if we take x to have type T1 such that we could infer that e2 has type T2,
// then we can infer that the result of letting x = e1 and substituting it into e2 has type T2
// Γ, e1: T1 Γ, x: T1 ⊢ e2: T2
// --------------------------------
// Γ ⊢ let x = e1 in e2: T2
//
func Infer(env Env, expr Expression) (*Scheme, error) {
if expr == nil {
return nil, errors.Errorf("Cannot infer a nil expression")
}
if env == nil {
env = make(SimpleEnv)
}
infer := newInferer(env)
if err := infer.consGen(expr); err != nil {
return nil, err
}
s := newSolver()
s.solve(infer.cs)
if s.err != nil {
return nil, s.err
}
if infer.t == nil {
return nil, errors.Errorf("infer.t is nil")
}
t := infer.t.Apply(s.sub).(Type)
return closeOver(t)
}
// Unify unifies the two types and returns a list of substitutions.
// These are the rules:
//
// Type Constants and Type Constants
//
// Type constants (atomic types) have no substitution
// c ~ c : []
//
// Type Variables and Type Variables
//
// Type variables have no substitutions if there are no instances:
// a ~ a : []
//
// Default Unification
//
// if type variable 'a' is not in 'T', then unification is simple: replace all instances of 'a' with 'T'
// a ∉ T
// ---------------
// a ~ T : [a/T]
//
func Unify(a, b Type) (sub Subs, err error) {
logf("%v ~ %v", a, b)
enterLoggingContext()
defer leaveLoggingContext()
switch at := a.(type) {
case TypeVariable:
return bind(at, b)
default:
if a.Eq(b) {
return nil, nil
}
if btv, ok := b.(TypeVariable); ok {
return bind(btv, a)
}
atypes := a.Types()
btypes := b.Types()
defer ReturnTypes(atypes)
defer ReturnTypes(btypes)
if len(atypes) == 0 && len(btypes) == 0 {
goto e
}
return unifyMany(atypes, btypes)
e:
}
err = errors.Errorf("Unification Fail: %v ~ %v cannot be unified", a, b)
return
}
func unifyMany(a, b Types) (sub Subs, err error) {
logf("UnifyMany %v %v", a, b)
enterLoggingContext()
defer leaveLoggingContext()
if len(a) != len(b) {
return nil, errors.Errorf("Unequal length. a: %v b %v", a, b)
}
for i, at := range a {
bt := b[i]
if sub != nil {
at = at.Apply(sub).(Type)
bt = bt.Apply(sub).(Type)
}
var s2 Subs
if s2, err = Unify(at, bt); err != nil {
return nil, err
}
if sub == nil {
sub = s2
} else {
sub2 := compose(sub, s2)
defer ReturnSubs(s2)
if sub2 != sub {
defer ReturnSubs(sub)
}
sub = sub2
}
}
return
}
func bind(tv TypeVariable, t Type) (sub Subs, err error) {
logf("Binding %v to %v", tv, t)
switch {
// case tv == t:
case occurs(tv, t):
err = errors.Errorf("recursive unification")
default:
ssub := BorrowSSubs(1)
ssub.s[0] = Substitution{tv, t}
sub = ssub
}
logf("Sub %v", sub)
return
}
func occurs(tv TypeVariable, s Substitutable) bool {
ftv := s.FreeTypeVar()
defer ReturnTypeVarSet(ftv)
return ftv.Contains(tv)
}
func closeOver(t Type) (sch *Scheme, err error) {
sch = Generalize(nil, t)
err = sch.Normalize()
logf("closeoversch: %v", sch)
return
}
|