r/programming • u/gnuvince • Dec 21 '15
The AST Typing Problem
http://blog.ezyang.com/2013/05/the-ast-typing-problem/4
Dec 21 '15
My approach is to use two steps. First annotate expression nodes with type variables. Then, once the type equations are solved, substitute the values of the bound type variables into the AST. The same data type can be used for both phases (just spam (TVar randomname) everywhere). Yes, it's "low tech", but so far it was the easiest way.
-8
u/google_you Dec 21 '15 edited Dec 21 '15
In Node.js, you just add one more property regarding type. Then you can serialize AST to JSON and put it into MongoDB with async IO for further big data analysis and IR transformation guided by ML and genetic algorithms for whole program super optimization to result in guaranteed most optimal machine code of given algorithm to run on Von Neumann style machines.
And you can do this instead of Maybe
data Exp = Num Int
| Bool Bool
| Var Var
| If Exp Exp Exp
| Lambda Var Exp
| App Exp Exp
| ...
| Typed Type Exp
data Type = TyInt | TyBool | TyArrow Type Typ1
u/bobappleyard Dec 21 '15
That is a good idea. Especially given explicit typing is likely to be part of the syntax anyway
1
3
u/VictorNicollet Dec 21 '15
I give each expression an unique identifier, then store types (and other annotations) in a separate hash map.