扩展Untyped Lambda演算实现以覆盖Simply Typed Lambda演算需要什么?

时间:2016-06-21 05:14:42

标签: haskell scheme lambda-calculus system-f

Matt Might谈到在Lambda Calculus中实施7 lines of Scheme口译员:

; eval takes an expression and an environment to a value
(define (eval e env) (cond
  ((symbol? e)       (cadr (assq e env)))
  ((eq? (car e) 'λ)  (cons e env))
  (else              (apply (eval (car e) env) (eval (cadr e) env)))))

; apply takes a function and an argument to a value
(define (apply f x)
  (eval (cddr (car f)) (cons (list (cadr (car f)) x) (cdr f))))

; read and parse stdin, then evaluate:
(display (eval (read) '())) (newline)

现在这不是Simply Typed Lambda Calculus。在core of Haskell中,有Intermediate LanguageSystem F非常相似。一些(包括Simon Peyton Joneshave called this Simply Typed Lambda Calculus的实现。

我的问题是:扩展Untyped Lambda演算实现以涵盖简单类型Lambda演算需要什么?

3 个答案:

答案 0 :(得分:4)

目前还不清楚你在问什么,但我能想到几个有效的答案:

  • 需要更改表示以适应lambda抽象引入的变量的类型注释。

  • 根据您的陈述,可能会表示非良好类型的术语。如果是这样,您将要实现类型检查器。

  • 评估时,除了忽略类型注释(type erasure的全部内容)之外,您不需要更改LC评估程序中的任何内容。但是,如果您编写的评估程序基本上是evalUntyped . eraseTypes,那么您可能会比编写定制的evalTyped函数更难以证明它是完整的。

答案 1 :(得分:1)

简单类型的lambda演算(STLC)只是将类型检查器添加到您描述的系统中。也就是说,您可以将此评估程序视为"运行时系统"对于STLC。

侧节点:类型注释通常会添加到语言中,以简化类型检查器的工作,但它们不是必需的。

答案 2 :(得分:0)

上面有一些很好的答案 - 我无意贬低它们。

在实现类型检查器方面 - 它在Haskell中更简单(尽管可以想象这可以移植到Scheme)。

以下是that is an implementation的Haskell Simply Typed Lambda Calculus中的简单类型检查器:

type OType = ObjType (Fix ObjType)
type OEnvironment = Map TermIdentifier OType

check :: OEnvironment -> Term OType -> OType
check env (Var i) = case lookup i env of
                      Nothing -> error $ "Unbound variable " ++ i
                      Just v -> v
check env (App f p) = let t_f = check env f
                          t_p = check env p
                       in case t_f of
                            Fun (Fix t_p') (Fix r)
                              | t_p == t_p' -> r
                              | otherwise -> error "Parameter mismatch"
                            _ -> error "Applied a non-function"
check env (Lam i ty t) = let r = check (insert i ty env) t
                          in Fun (Fix ty) (Fix r)

以下是Haskell中简单类型Lambda演算的another implementation

import Control.Applicative ((<$), (<$>))
import Control.Monad (guard)
import Safe (atMay)

data Type
    = Base
    | Arrow Type Type
    deriving (Eq, Ord, Read, Show)

data Term
    = Const
    | Var Int -- deBruijn indexing; the nearest enclosing lambda binds Var 0
    | Lam Type Term
    | App Term Term
    deriving (Eq, Ord, Read, Show)

check :: [Type] -> Term -> Maybe Type
check env Const = return Base
check env (Var v) = atMay env v
check env (Lam ty tm) = Arrow ty <$> check (ty:env) tm
check env (App tm tm') = do
    Arrow i o <- check env tm
    i' <- check env tm'
    guard (i == i')
    return o

eval :: Term -> Term
eval (App tm tm') = case eval tm of
    Lam _ body -> eval (subst 0 tm' body)
eval v = v

subst :: Int -> Term -> Term -> Term
subst n tm Const = Const
subst n tm (Var m) = case compare m n of
    LT -> Var m
    EQ -> tm
    GT -> Var (m-1)
subst n tm (Lam ty body) = Lam ty (subst (n+1) tm body)
subst n tm (App tm' tm'') = App (subst n tm tm') (subst n tm tm'')

evalMay :: Term -> Maybe Term
evalMay tm = eval tm <$ check [] tm