5 ms·
Perhaps I don't want to just type-check, but I want to do type-inference as well.
by amelius 9y ago
Perhaps I don't want to just type-check, but I want to do type-inference as well.
- labbekak 9y agoI recommend the paper "Complete and Easy Bidirectional Typechecking for Higher-Rank Polymorphism" by Dunfield and Krishnaswami. It describes an easy to implement algorithm for type inference for higher ranked types that can subsume Hindley-Milner. Implementation in Haskell: https://github.com/ollef/Bidirectional https://github.com/ollef/Bidirectional Implementation in Javascript/Typescript (by me): https://github.com/atennapel/bidirectional.js https://github.com/atennapel/bidirectional.js
- yorwba 9y agoArXiv link for the paper: https://arxiv.org/abs/1306.6032 https://arxiv.org/abs/1306.6032
- cf 9y agoYour comment expanded out is what I had hoped that blogpost would have been. Thank you for posting this. It's hard to overstate how much easier bidirectional typechecking is compared to other approaches. My favorite starting point is actually Dunfield's lecture notes on bidirectional type checking, https://people.mpi-sws.org/~joshua/bitype.pdf https://people.mpi-sws.org/~joshua/bitype.pdf The paper you mentioned is great at showing the approach scales well to giving you all the fancy features you might want that are in modern languages.
- psygnisfive 9y agoAhh, well! That's a trickier problem! You need a bidirectional system that has unification for metavariables, and then some system for handling unsolved metavars. But to do that requires a more complicated set up. The goal of the post was to introduce the core ideas and show that it's relatively simple to do decent chunks of this stuff. :)
- junke 9y agoType inference can be easy too, here is a 5mn Prolog thing: type(X,_,number) :- number(X). type(V,Env,T) :- ground(V), member(V:T,Env). type(<(X,Y),Env,bool) :- type(X,Env,number), type(Y,Env,number). type(or(A,B),Env,bool) :- type(A,Env,bool), type(B,Env,bool). type(lambda(X,E),Env,XT -> ET) :- type(E,[X:XT|Env],ET). type(apply(F,E),Env,T) :- type(E,Env,ET), type(F,Env,ET->T). type(let(X,XE,E),Env,ET) :- type(XE,Env,XT), type(E,[X:XT|Env],ET). For example: ?- type(lambda(f,lambda(g,lambda(x,apply(f,apply(g,x))))),[],T). T = ((_232 -> _226) -> (_225 -> _232) -> _225 -> _226)
- psygnisfive 9y agoIndeed! Tho this is only half the problem, because `T` there includes metavariables, so it's not a type but rather a schema at the meta-level for types. What we'd like is something like `forall a b c. (b -> c) -> (a -> b) -> a -> c` so that composition can be used at arbitrary choices of the types. So like, while we can use this composition function at any particular place we need composition, we can't define `compose` to have this type because the first use site will force the metavars to have a particular value and then we get a nasty global monomorphism. We need to explicitly have metavariables in the language itself, rather than at the metalevel of doing the synthesis/checking.
- tom_mellior 9y agoProlog variables are metavariables, if you use them as such. ?- abstract_type(lambda(f, lambda(g, apply(f, g))), Typ). Typ = [_G947, _G948]^ ((_G947->_G948)->_G947->_G948) . ?- abstract_type(lambda(f, lambda(g, apply(f, g))), Typ), apply_type(Typ, a, Ta), apply_type(Ta, b, Tab), apply_type(Typ, c, Tc), apply_type(Tc, d, Tcd). Typ = [_G1179, _G1180]^ ((_G1179->_G1180)->_G1179->_G1180), Ta = [_G1213]^ ((a->_G1213)->a->_G1213), Tab = ((a->b)->a->b), Tc = [_G1251]^ ((c->_G1251)->c->_G1251), Tcd = ((c->d)->c->d) . Note how both Tab and Tcd arise from instantiating the same schema Typ, which is unchanged. To avoid binding variables in the schema, you just make sure to only instantiate instances (copies) of the schema: abstract_type(Term, Typ) :- type(Term, [], T), % type/3 defined by parent term_variables(T, Vars), Typ = Vars^T. apply_type(Typ, Arg, Result) :- copy_term(Typ, TypCopy), TypCopy = Vars^T, Vars = [Arg | RemainingVars], ( RemainingVars = [] -> Result = T ; Result = RemainingVars^T ).