Programming languages 证明表达式具有类型
我正在为明天的期末考试做一份练习表,我有点困惑,想弄清楚问题是什么,以及如何解决它。我想在这里检查一下,看看stack overflow的好人是否能帮我解决这个问题 问题是这样设置的: 显示一个派生树(它是类型检查的跟踪),证明以下表达式具有类型:Programming languages 证明表达式具有类型,programming-languages,racket,typechecking,plai,Programming Languages,Racket,Typechecking,Plai,我正在为明天的期末考试做一份练习表,我有点困惑,想弄清楚问题是什么,以及如何解决它。我想在这里检查一下,看看stack overflow的好人是否能帮我解决这个问题 问题是这样设置的: 显示一个派生树(它是类型检查的跟踪),证明以下表达式具有类型: (appC (lamC ‘x ??? (plusC (idC ‘x) (numC 1))) (numC 2) ) 它基于此类型检查的定义: (define (typecheck [a : ExprC] [tenv : TypeEnv])
(appC (lamC ‘x ??? (plusC (idC ‘x) (numC 1)))
(numC 2) )
它基于此类型检查的定义:
(define (typecheck [a : ExprC] [tenv : TypeEnv])
(type-case ExprC a
[numC (n) (numT)]
[plusC (l r) (typecheck-nums l r tenv)]
[multC (l r) (typecheck-nums l r tenv)]
[idC (n) (type-lookup n tenv)]
[lamC (n arg-type body)
(arrowT arg-type
(typecheck body
(extend-env (tbind n arg-type)
tenv)))]
[appC (fun arg)
(type-case Type (typecheck fun tenv)
[arrowT (arg-type result-type)
(if (equal? arg-type
(typecheck arg tenv))
result-type
(type-error arg
(to-string arg-type)))]
[else (type-error fun "function")])]))
(define (typecheck-nums l r tenv)
(type-case Type (typecheck l tenv)
[numT ()
(type-case Type (typecheck r tenv)
[numT () (numT)]
[else (type-error r "num")])]
[else (type-error l "num")]))
(define (type-error a msg)
(error 'typecheck (string-append
"no type: "
(string-append
(to-string a)
(string-append " not "
msg)))))
当我在球拍中跑步时:
(typecheck (appC
(lamC 'x (numT) (plusC (idC 'x) (numC 1)))
(numC 2))
mt-env)
我确实得到:
- Type
(numT)
- Type
(numT)
在底部,它指出:
“您检查'x'的类型必须为:numT”
所以它是关于numT的,但我对追踪部分感到困惑,我知道这个问题很长,但这是所有实践问题中最让我困惑的一个。欢迎提供任何建议/帮助。问题:“显示派生树,证明以下表达式具有类型”意味着您需要证明表达式具有给定类型
考虑以下示例(与您的typecheck示例无关):
计算表达式(+1 2)
的结果具有类型int
。
原因:
由于上述类型,应用程序规则声明
将int->int类型的函数应用于int类型的两个参数,会得到int类型的结果
类型检查器递归地检查表达式。它首先确定子表达式的类型,然后使用其类型推断复合表达式的类型
为了证明给定表达式具有特定类型,可以使用运行类型检查器的跟踪。要获取跟踪,请修改typechecker以打印推断的类型
未经测试的修改:
(define (typecheck [a : ExprC] [tenv : TypeEnv])
(define (tell result-type)
(display "The type of ")
(display a)
(display " is: ")
(display result-type)
(newline))
(tell
(type-case ExprC a
[numC (n) (numT)]
[plusC (l r) (typecheck-nums l r tenv)]
[multC (l r) (typecheck-nums l r tenv)]
[idC (n) (type-lookup n tenv)]
[lamC (n arg-type body) (arrowT arg-type
(typecheck body
(extend-env (tbind n arg-type)
tenv)))]
[appC (fun arg) (type-case Type (typecheck fun tenv)
[arrowT (arg-type result-type)
(if (equal? arg-type
(typecheck arg tenv))
result-type
(type-error arg
(to-string arg-type)))]
[else (type-error fun "function")])])))
(define (typecheck [a : ExprC] [tenv : TypeEnv])
(define (tell result-type)
(display "The type of ")
(display a)
(display " is: ")
(display result-type)
(newline))
(tell
(type-case ExprC a
[numC (n) (numT)]
[plusC (l r) (typecheck-nums l r tenv)]
[multC (l r) (typecheck-nums l r tenv)]
[idC (n) (type-lookup n tenv)]
[lamC (n arg-type body) (arrowT arg-type
(typecheck body
(extend-env (tbind n arg-type)
tenv)))]
[appC (fun arg) (type-case Type (typecheck fun tenv)
[arrowT (arg-type result-type)
(if (equal? arg-type
(typecheck arg tenv))
result-type
(type-error arg
(to-string arg-type)))]
[else (type-error fun "function")])])))