my blog lives here now
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

609 lines
21 KiB

7 years ago
  1. ---
  2. title: Amulet's New Type Checker
  3. date: February 18, 2018
  4. ---
  5. In the last post about Amulet I wrote about rewriting the type checking
  6. code. And, to everybody's surprise (including myself), I actually did
  7. it.
  8. Like all good programming languages, Amulet has a strong, static type
  9. system. What most other languages do not have, however, is (mostly)
  10. _full type inference_: programs are still type-checked despite (mostly)
  11. having no type annotations.
  12. Unfortunately, no practical type system has truly "full type inference":
  13. features like data-type declarations, integral to actually writing
  14. software, mandate some type annotations (in this case, constructor
  15. arguments). However, that doesn't mean we can't try.
  16. The new type checker, based on a constraint-generating but
  17. _bidirectional_ approach, can type a lot more programs than the older,
  18. Algorithm W-derived, quite buggy checker. As an example, consider the
  19. following definition. For this to check under the old type system, one
  20. would need to annotate both arguments to `map` _and_ its return type -
  21. clearly undesirable!
  22. ```ocaml
  23. let map f =
  24. let go cont xs =
  25. match xs with
  26. | Nil -> cont Nil
  27. | Cons (h, t) -> go (compose cont (fun x -> Cons (f h, x))) t
  28. in go id ;;
  29. ```
  30. Even more egregious is that the η-reduction of `map` would lead to an
  31. ill-typed program.
  32. ```ocaml
  33. let map f xs =
  34. let go cont xs = (* elided *)
  35. in go id xs ;;
  36. (* map : forall 'a 'b. ('a -> 'b) -> list 'a -> list 'b *)
  37. let map' f =
  38. let go cont xs = (* elided *)
  39. in go id ;;
  40. (* map' : forall 'a 'b 'c. ('a -> 'b) -> list 'a -> list 'c *)
  41. ```
  42. Having declared this unacceptable, I set out to rewrite the type
  43. checker, after months of procrastination. As is the case, of course,
  44. with such things, it only took some two hours, and I really shouldn't have
  45. procrastinated it for so long.
  46. Perhaps more importantly, the new type checker also supports rank-N
  47. polymorphism directly, with all appropriate checks in place: expressions
  48. checked against a polymorphic type are, in reality, checked against a
  49. _deeply skolemised_ version of that poly-type - this lets us enforce two
  50. key properties:
  51. 1. the expression being checked _is_ actually parametric over the type
  52. arguments, i.e., it can't unify the skolem constants with any type
  53. constructors, and
  54. 2. no rank-N arguments escape.
  55. As an example, consider the following function:
  56. ```ocaml
  57. let rankn (f : forall 'a. 'a -> 'a) = f ()
  58. ```
  59. Well-typed uses of this function are limited to applying it to the
  60. identity function, as parametricity tells us; and, indeed, trying to
  61. apply it to e.g. `fun x -> x + 1`{.ocaml} is a type error.
  62. ### The Solver
  63. As before, type checking is done by a traversal of the syntax tree
  64. which, by use of a `Writer`{.haskell} monad, produces a list of
  65. constraints to be solved. Note that a _list_ really is needed: a set, or
  66. similar data structure with unspecified order, will not do. The order in
  67. which the solver processes constraints is important!
  68. The support for rank-N types has lead to the solver needing to know
  69. about a new kind of constraint: _subsumption_ constraints, in addition
  70. to _unification_ constraints. Subsumption is perhaps too fancy a term,
  71. used to obscure what's really going on: subtyping. However, whilst
  72. languages like Java and Scala introduce subtyping by means of
  73. inheritance, our subtyping boils down to eliminating ∀s.
  74. ∀s are eliminated from the right-hand-side of subsumption constraints by
  75. _deep skolemisation_: replacing the quantified variables in the type
  76. with fresh type constants. The "depth" of skolemisation refers to the
  77. fact that ∀s to the right of arrows are eliminated along with the ones
  78. at top-level.
  79. ```haskell
  80. subsumes k t1 t2@TyForall{} = do
  81. t2' <- skolemise t2
  82. subsumes k t1 t2'
  83. subsumes k t1@TyForall{} t2 = do
  84. (_, _, t1') <- instantiate t1
  85. subsumes k t1' t2
  86. subsumes k a b = k a b
  87. ```
  88. The function for computing subtyping is parametric over what to do in
  89. the case of two monomorphic types: when this function is actually used
  90. by the solving algorithm, it's applied to `unify`.
  91. The unifier has the job of traversing two types in tandem to find the
  92. _most general unifier_: a substitution that, when applied to one type,
  93. will make it syntatically equal to the other. In most of the type
  94. checker, when two types need to be "equal", they're equal up to
  95. unification.
  96. Most of the cases are an entirely boring traversal, so here are the
  97. interesting ones.
  98. - Skolem type constants only unify with other skolem type constants:
  99. ```haskell
  100. unify TySkol{} TySkol{} = pure ()
  101. unify t@TySkol{} b = throwError $ SkolBinding t b
  102. unify b t@TySkol{} = throwError $ SkolBinding t b
  103. ```
  104. - Type variables extend the substitution:
  105. ```haskell
  106. unify (TyVar a) b = bind a b
  107. unify a (TyVar b) = bind b a
  108. ```
  109. - Polymorphic types unify up to α-renaming:
  110. ```haskell
  111. unify t@(TyForall vs ty) t'@(TyForall vs' ty')
  112. | length vs /= length vs' = throwError (NotEqual t t')
  113. | otherwise = do
  114. fvs <- replicateM (length vs) freshTV
  115. let subst = Map.fromList . flip zip fvs
  116. unify (apply (subst vs) ty) (apply (subst vs') ty')
  117. ```
  118. When binding a variable to a concrete type, an _occurs check_ is
  119. performed to make sure the substitution isn't going to end up containing
  120. an infinite type. Consider binding `'a := list 'a`: If `'a` is
  121. substituted for `list 'a` everywhere, the result would be `list (list
  122. 'a)` - but wait, `'a` appears there, so it'd be substituted again, ad
  123. infinitum.
  124. Extra care is also needed when binding a variable to itself, as is the
  125. case with `'a ~ 'a`. These constraints are trivially discharged, but
  126. adding them to the substitution would mean an infinite loop!
  127. ```haskell
  128. occurs :: Var Typed -> Type Typed -> Bool
  129. occurs _ (TyVar _) = False
  130. occurs x e = x `Set.member` ftv e
  131. ```
  132. If the variable has already been bound, the new type is unified with the
  133. one present in the substitution being accumulated. Otherwise, it is
  134. added to the substitution.
  135. ```haskell
  136. bind :: Var Typed -> Type Typed -> SolveM ()
  137. bind var ty
  138. | occurs var ty = throwError (Occurs var ty)
  139. | TyVar var == ty = pure ()
  140. | otherwise = do
  141. env <- get
  142. -- Attempt to extend the environment, otherwise
  143. -- unify with existing type
  144. case Map.lookup var env of
  145. Nothing -> put (Map.singleton var (normType ty) `compose` env)
  146. Just ty'
  147. | ty' == ty -> pure ()
  148. | otherwise -> unify (normType ty) (normType ty')
  149. ```
  150. Running the solver, then, amounts to folding through the constraints in
  151. order, applying the substitution created at each step to the remaining
  152. constraints while also accumulating it to end up at the most general
  153. unifier.
  154. ```haskell
  155. solve :: Int -> Subst Typed
  156. -> [Constraint Typed]
  157. -> Either TypeError (Subst Typed)
  158. solve _ s [] = pure s
  159. solve i s (ConUnify e a t:xs) = do
  160. case runSolve i s (unify (normType a) (normType t)) of
  161. Left err -> Left (ArisingFrom err e)
  162. Right (i', s') -> solve i' (s' `compose` s) (apply s' xs)
  163. solve i s (ConSubsume e a b:xs) =
  164. case runSolve i s (subsumes unify (normType a) (normType b)) of
  165. Left err -> Left (ArisingFrom err e)
  166. Right (i', s') -> solve i' (s' `compose` s) (apply s' xs)
  167. ```
  168. ### Inferring and Checking Patterns
  169. Amulet, being a member of the ML family, does most data processing
  170. through _pattern matching_, and so, the patterns also need to be type
  171. checked.
  172. The pattern grammar is simple: it's made up of 6 constructors, while
  173. expressions are described by over twenty constructors.
  174. Here, the bidirectional approach to inference starts to shine. It is
  175. possible to have different behaviours for when the type of the
  176. pattern (or, at least, some skeleton describing that type) is known
  177. and for when it is not, and such a type must be produced from the
  178. pattern alone.
  179. In an unification-based system like ours, the inference judgement can be
  180. recovered from the checking judgement by checking against a fresh type
  181. variable.
  182. ```haskell
  183. inferPattern p = do
  184. x <- freshTV
  185. (p', binds) <- checkPattern p x
  186. pure (p', x, binds)
  187. ```
  188. Inferring patterns produces three things: an annotated pattern, since
  189. syntax trees after type checking carry their types; the type of values
  190. that pattern matches; and a list of variables the pattern binds.
  191. Checking omits returning the type, and yields only the annotated syntax
  192. tree and the list of bindings.
  193. As a special case, inferring patterns with type signatures overrides the
  194. checking behaviour. The stated type is kind-checked (to verify its
  195. integrity and to produce an annotated tree), then verified to be a
  196. subtype of the inferred type for that pattern.
  197. ```haskell
  198. inferPattern pat@(PType p t ann) = do
  199. (p', pt, vs) <- inferPattern p
  200. (t', _) <- resolveKind t
  201. _ <- subsumes pat t' pt -- t' pt
  202. case p' of
  203. Capture v _ -> pure (PType p' t' (ann, t'), t', [(v, t')])
  204. _ -> pure (PType p' t' (ann, t'), t', vs)
  205. ```
  206. Checking patterns is where the fun actually happens. Checking `Wildcard`s
  207. and `Capture`s is pretty much identical, except the latter actually
  208. expands the capture list.
  209. ```haskell
  210. checkPattern (Wildcard ann) ty = pure (Wildcard (ann, ty), [])
  211. checkPattern (Capture v ann) ty =
  212. pure (Capture (TvName v) (ann, ty), [(TvName v, ty)])
  213. ```
  214. Checking a `Destructure` looks up the type of the constructor in the
  215. environment, possibly instancing it, and does one of two things,
  216. depending on whether or not the destructuring did not have an inner
  217. pattern.
  218. ```haskell
  219. checkPattern ex@(Destructure con ps ann) ty =
  220. case ps of
  221. ```
  222. - If there was no inner pattern, then the looked-up type is unified with
  223. the "goal" type - the one being checked against.
  224. ```haskell
  225. Nothing -> do
  226. pty <- lookupTy con
  227. _ <- unify ex pty ty
  228. pure (Destructure (TvName con) Nothing (ann, pty), [])
  229. ```
  230. - If there _was_ an inner pattern, we proceed by decomposing the type
  231. looked up from the environment. The inner pattern is checked against the
  232. _domain_ of the constructor's type, while the "goal" gets unified with
  233. the _co-domain_.
  234. ```haskell
  235. Just p -> do
  236. (c, d) <- decompose ex _TyArr =<< lookupTy con
  237. (ps', b) <- checkPattern p c
  238. _ <- unify ex ty d
  239. ```
  240. Checking tuple patterns is a bit of a mess. This is because of a
  241. mismatch between how they're written and how they're typed: a 3-tuple
  242. pattern (and expression!) is written like `(a, b, c)`, but it's _typed_
  243. like `a * (b * c)`. There is a local helper that incrementally converts
  244. between the representations by repeatedly decomposing the goal type.
  245. ```haskell
  246. checkPattern pt@(PTuple elems ann) ty =
  247. let go [x] t = (:[]) <$> checkPattern x t
  248. go (x:xs) t = do
  249. (left, right) <- decompose pt _TyTuple t
  250. (:) <$> checkPattern x left <*> go xs right
  251. go [] _ = error "malformed tuple in checkPattern"
  252. ```
  253. Even more fun is the `PTuple` constructor is woefully overloaded: One
  254. with an empty list of children represents matching against `unit`{.ml}.
  255. One with a single child is equivalent to the contained pattern; Only one
  256. with more than two contained patterns makes a proper tuple.
  257. ```haskell
  258. in case elems of
  259. [] -> do
  260. _ <- unify pt ty tyUnit
  261. pure (PTuple [] (ann, tyUnit), [])
  262. [x] -> checkPattern x ty
  263. xs -> do
  264. (ps, concat -> binds) <- unzip <$> go xs ty
  265. pure (PTuple ps (ann, ty), binds)
  266. ```
  267. ### Inferring and Checking Expressions
  268. Expressions are incredibly awful and the bane of my existence. There are
  269. 18 distinct cases of expression to consider, a number which only seems
  270. to be going up with modules and the like in the pipeline; this
  271. translates to 24 distinct cases in the type checker to account for all
  272. of the possibilities.
  273. As with patterns, expression checking is bidirectional; and, again,
  274. there are a lot more checking cases then there are inference cases. So,
  275. let's start with the latter.
  276. #### Inferring Expressions
  277. Inferring variable references makes use of instantiation to generate
  278. fresh type variables for each top-level universal quantifier in the
  279. type. These fresh variables will then be either bound to something by
  280. the solver or universally quantified over in case they escape.
  281. Since Amulet is desugared into a core language resembling predicative
  282. System F, variable uses also lead to the generation of corresponding
  283. type applications - one for each eliminated quantified variable.
  284. ```haskell
  285. infer expr@(VarRef k a) = do
  286. (inst, old, new) <- lookupTy' k
  287. if Map.null inst
  288. then pure (VarRef (TvName k) (a, new), new)
  289. else mkTyApps expr inst old new
  290. ```
  291. Functions, strangely enough, have both checking _and_ inference
  292. judgements: which is used impacts what constraints will be generated,
  293. and that may end up making type inference more efficient (by allocating
  294. less, or correspondingly spending less time in the solver).
  295. The pattern inference judgement is used to compute the type and bindings
  296. of the function's formal parameter, and the body is inferred in the
  297. context extended with those bindings; Then, a function type is
  298. assembled.
  299. ```haskell
  300. infer (Fun p e an) = do
  301. (p', dom, ms) <- inferPattern p
  302. (e', cod) <- extendMany ms $ infer e
  303. pure (Fun p' e' (an, TyArr dom cod), TyArr dom cod)
  304. ```
  305. Literals are pretty self-explanatory: Figuring their types boils down to
  306. pattern matching.
  307. ```haskell
  308. infer (Literal l an) = pure (Literal l (an, ty), ty) where
  309. ty = case l of
  310. LiInt{} -> tyInt
  311. LiStr{} -> tyString
  312. LiBool{} -> tyBool
  313. LiUnit{} -> tyUnit
  314. ```
  315. The inference judgement for _expressions_ with type signatures is very similar
  316. to the one for patterns with type signatures: The type is kind-checked,
  317. then compared against the inferred type for that expression. Since
  318. expression syntax trees also need to be annotated, they are `correct`ed
  319. here.
  320. ```haskell
  321. infer expr@(Ascription e ty an) = do
  322. (ty', _) <- resolveKind ty
  323. (e', et) <- infer e
  324. _ <- subsumes expr ty' et
  325. pure (Ascription (correct ty' e') ty' (an, ty'), ty')
  326. ```
  327. There is also a judgement for turning checking into inference, again by
  328. making a fresh type variable.
  329. ```haskell
  330. infer ex = do
  331. x <- freshTV
  332. ex' <- check ex x
  333. pure (ex', x)
  334. ```
  335. #### Checking Expressions
  336. Our rule for eliminating ∀s was adapted from the paper [Complete
  337. and Easy Bidirectional Typechecking for Higher-Rank Polymorphism].
  338. Unlike in that paper, however, we do not have explicit _existential
  339. variables_ in contexts, and so must check expressions against
  340. deeply-skolemised types to eliminate the universal quantifiers.
  341. [Complete and Easy Bidirectional Typechecking for Higher-Rank
  342. Polymorphism]: https://www.cl.cam.ac.uk/~nk480/bidir.pdf
  343. ```haskell
  344. check e ty@TyForall{} = do
  345. e' <- check e =<< skolemise ty
  346. pure (correct ty e')
  347. ```
  348. If the expression is checked against a deeply skolemised version of the
  349. type, however, it will be tagged with that, while it needs to be tagged
  350. with the universally-quantified type. So, it is `correct`ed.
  351. Amulet has rudimentary support for _typed holes_, as in dependently
  352. typed languages and, more recently, GHC. Since printing the type of
  353. holes during type checking would be entirely uninformative due to
  354. half-solved types, reporting them is deferred to after checking.
  355. Of course, holes must still have checking behaviour: They take whatever
  356. type they're checked against.
  357. ```haskell
  358. check (Hole v a) t = pure (Hole (TvName v) (a, t))
  359. ```
  360. Checking functions is as easy as inferring them: The goal type is split
  361. between domain and codomain; the pattern is checked against the domain,
  362. while the body is checked against the codomain, with the pattern's
  363. bindings in scope.
  364. ```haskell
  365. check ex@(Fun p b a) ty = do
  366. (dom, cod) <- decompose ex _TyArr ty
  367. (p', ms) <- checkPattern p dom
  368. Fun p' <$> extendMany ms (check b cod) <*> pure (a, ty)
  369. ```
  370. Empty `begin end` blocks are an error.
  371. ```
  372. check ex@(Begin [] _) _ = throwError (EmptyBegin ex)
  373. ```
  374. `begin ... end` blocks with at least one expression are checked by
  375. inferring the types of every expression but the last, and then checking
  376. the last expression in the block against the goal type.
  377. ```haskell
  378. check (Begin xs a) t = do
  379. let start = init xs
  380. end = last xs
  381. start' <- traverse (fmap fst . infer) start
  382. end' <- check end t
  383. pure (Begin (start' ++ [end']) (a, t))
  384. ```
  385. `let`s are pain. Since all our `let`s are recursive by nature, they must
  386. be checked, including all the bound variables, in a context where the
  387. types of every variable bound there are already available; To figure
  388. this out, however, we first need to infer the type of every variable
  389. bound there.
  390. If that strikes you as "painfully recursive", you're right. This is
  391. where the unification-based nature of our type system saved our butts:
  392. Each bound variable in the `let` gets a fresh type variable, the context
  393. is extended and the body checked against the goal.
  394. The function responsible for inferring and solving the types of
  395. variables is `inferLetTy`. It keeps an accumulating association list to
  396. check the types of further bindings as they are figured out, one by one,
  397. then uses the continuation to generalise (or not) the type.
  398. ```haskell
  399. check (Let ns b an) t = do
  400. ks <- for ns $ \(a, _, _) -> do
  401. tv <- freshTV
  402. pure (TvName a, tv)
  403. extendMany ks $ do
  404. (ns', ts) <- inferLetTy id ks (reverse ns)
  405. extendMany ts $ do
  406. b' <- check b t
  407. pure (Let ns' b' (an, t))
  408. ```
  409. We have decided to take [the advice of Vytiniotis, Peyton Jones, and
  410. Schrijvers], and refrain from generalising lets, except at top-level.
  411. This is why `inferLetTy` gets given `id` when checking terms.
  412. [the advice of Vytiniotis, Peyton Jones, and Schrijvers]: https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/tldi10-vytiniotis.pdf
  413. The judgement for checking `if` expressions is what made me stick to
  414. bidirectional type checking instead of fixing out variant of Algorithm
  415. W. The condition is checked against the boolean type, while both
  416. branches are checked against the goal.
  417. ```haskell
  418. check (If c t e an) ty = If <$> check c tyBool
  419. <*> check t ty
  420. <*> check e ty
  421. <*> pure (an, ty)
  422. ```
  423. it is not possible, in general, to recover the type of a function
  424. at an application site, we infer it; The argument given is checked
  425. against that function's domain and the codomain is unified with the
  426. goal type.
  427. ```haskell
  428. check ex@(App f x a) ty = do
  429. (f', (d, c)) <- secondA (decompose ex _TyArr) =<< infer f
  430. App f' <$> check x d <*> fmap (a,) (unify ex ty c)
  431. ```
  432. To check `match`, the type of what's being matched against is first
  433. inferred because, unlike application where _some_ recovery is possible,
  434. we can not recover the type of matchees from the type of branches _at
  435. all_.
  436. ```haskell
  437. check (Match t ps a) ty = do
  438. (t', tt) <- infer t
  439. ```
  440. Once we have the type of the matchee in hands, patterns can be checked
  441. against that. The branches are then each checked against the goal type.
  442. ```haskell
  443. ps' <- for ps $ \(p, e) -> do
  444. (p', ms) <- checkPattern p tt
  445. (,) <$> pure p' <*> extendMany ms (check e ty)
  446. ```
  447. Checking binary operators is like checking function application twice.
  448. Very boring.
  449. ```haskell
  450. check ex@(BinOp l o r a) ty = do
  451. (o', to) <- infer o
  452. (el, to') <- decompose ex _TyArr to
  453. (er, d) <- decompose ex _TyArr to'
  454. BinOp <$> check l el <*> pure o'
  455. <*> check r er <*> fmap (a,) (unify ex d ty)
  456. ```
  457. Checking records and record extension is a hack, so I'm not going to
  458. talk about them until I've cleaned them up reasonably in the codebase.
  459. Record access, however, is very clean: we make up a type for the
  460. row-polymorphic bit, and check against a record type built from the goal
  461. and the key.
  462. ```haskell
  463. check (Access rc key a) ty = do
  464. rho <- freshTV
  465. Access <$> check rc (TyRows rho [(key, ty)])
  466. <*> pure key <*> pure (a, ty)
  467. ```
  468. Checking tuple expressions involves a local helper much like checking
  469. tuple patterns. The goal type is recursively decomposed and made to line
  470. with the expression being checked.
  471. ```haskell
  472. check ex@(Tuple es an) ty = Tuple <$> go es ty <*> pure (an, ty) where
  473. go [] _ = error "not a tuple"
  474. go [x] t = (:[]) <$> check x t
  475. go (x:xs) t = do
  476. (left, right) <- decompose ex _TyTuple t
  477. (:) <$> check x left <*> go xs right
  478. ```
  479. And, to finish, we have a judgement for turning inference into checking.
  480. ```haskell
  481. check e ty = do
  482. (e', t) <- infer e
  483. _ <- subsumes e ty t
  484. pure e'
  485. ```
  486. ### Conclusion
  487. I like the new type checker: it has many things you'd expect from a
  488. typed lambda calculus, such as η-contraction preserving typability, and
  489. substitution of `let`{.ocaml}-bound variables being generally
  490. admissable.
  491. Our type system is fairly complex, what with rank-N types and higher
  492. kinded polymorphism, so inferring programs under it is a bit of a
  493. challenge. However, I am fairly sure the only place that demands type
  494. annotations are higher-ranked _parameters_: uses of higher-rank
  495. functions are checked without the need for annotations.
  496. Check out [Amulet] the next time you're looking for a typed functional
  497. programming language that still can't compile to actual executables.
  498. [Amulet]: https://github.com/zardyh/amulet