How to parse string into GADT

为君一笑 提交于 2019-12-05 15:52:11

I'm a strong advocate of parsing to an monotyped representation, and then applying a typechecking/elaboration phase to convert that into the typed (GADT) representation. The best tutorial on the general idea is probably from Lennart Augustsson's llvm based compiler

A representation for the SKI calculus might look like

data TyComb t where
  TyS   :: TyComb ((a -> b -> c) -> (a -> b) -> a -> c)
  TyK   :: TyComb (a -> b -> a)
  TyI   :: TyComb (a -> a)
  TyApp :: TyComb (a -> b) -> TyComb a -> TyComb b

evalTyComb :: TyComb t -> t
evalTyComb TyS         = \x y z -> (x z) (y z)
evalTyComb TyK         = const
evalTyComb TyI         = id
evalTyComb (TyApp a b) = (evalTyComb a) (evalTyComb b)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!