An exercise in dependent types: A well-typed interpreter
An exercise in dependent types: A well-typed interpreter
复制标题
依赖类型的练习:类型良好的解释器
DOI:
--
复制
发表时间:
1999
期刊:
影响因子:
--
通讯作者:
M. Carlsson
中科院分区:
文献类型:
--
作者:
L. Augustsson;M. Carlsson
The result type of an interpreter written in a typed language is normally a tagged union. By using depent types, we can be more precise about the type of values that the intepreter returns. There is no need for tagging these values with their type, something which opens the door to more eecient interpreters. 1 An annoying problem We are ignoring ? for now. Writing an interpreter for a language which can produce results of diierent types requires some kind of encoding of the result, as in gure 1. The encoding is captured in the tagging in the data type Value. This tagging is ineecient since each return value must be tagged. In addition, the tags must be tested and the real values extracted again when they are used. interp :: Expr-> Value interp (EBool b) = VBool b interp (EInt i) = VInt i interp (EAdd e1 e2) = case (interp e1, interp e2) of (VInt i1, VInt i2)-> VInt (i1+i2) _-> VError Fig. 1. A very simple language and its interpreter. The tags serve two purposes: rst, they turn the result into a union type so that the result is a single type; second, they are used to make sure that the