我为此使用lang pl。我已经实现了语言“ ROL”,现在我正尝试扩展该语言以使用替代模型来支持乐趣和通话。我的基本代码如下。我所有的旧测试都通过了,所以我猜没有编译问题。我有3个针对“有趣”和“通话”的新测试,这些测试一直给我一个错误。我在打电话给functiom时遇到了麻烦。
(define-type RegE
[Reg Bit-List]
[And RegE RegE]
[Or RegE RegE]
[Shl RegE]
[Id Symbol]
[With Symbol RegE RegE]
[Bool Boolean]
[Geq RegE RegE]
[Maj RegE]
[If RegE RegE RegE]
[Fun Symbol RegE]
[Call RegE RegE])
(define-type RES
[RES_Bool Boolean]
[RegV Bit-List]
[FunV Symbol RegE])
评估:
(: eval : RegE -> RES)
;; evaluates RegE expressions by reducing them to bit-lists
(define (eval expr)
(cases expr
...
[(With bound-id named-expr bound-body)
(eval (subst bound-body
bound-id
(Reg (RegV->bit-list(eval named-expr)))))]
[(Fun bound-id bound-body) (FunV bound-id bound-body)]
[(Call fun-expr arg-expr)
(let ([fval (eval fun-expr)])
(cases fval
[(FunV bound-id bound-body)
(eval (subst bound-body
bound-id
arg-expr))]
[else (error 'eval "`call' expects a function, got: ~s"
fval)]))]
...))
I'm guessing the problem is in the
[(FunV bound-id bound-body)
(eval (subst bound-body
bound-id
arg-expr))]
部分。我尝试将其更改为
(eval (subst bound-body
bound-id
(Reg (RegV->bit-list(eval named-expr)))))
就像在With
中一样,但是我在RegV->bit-list
函数中遇到了错误。
我也试图使它成为:
[(FunV bound-id bound-body)
(eval (subst bound-body
bound-id
(eval (arg-expr)))]
但是再一次,eval必须获得regE,但可以看到arg-expr是RES类型。
所以我只是不知道该如何处理arg-expr部分。 任何帮助都会得到帮助...
这是无法通过的测试之一:
(test (run "{ reg-len = 3
{with {x {0 0 1}}
{with {f {fun {y} {and x y}}}
{with {x {0 0 0}}
{call f {1 1 1}}}}}}")
=> '(0 0 1))
错误:
RegV->bit-list: Given wrong type of RES (FunV y (And (Id x) (Id y)))
还有RegV->bit-list:
(: RegV->bit-list : RES -> Bit-List)
;; extract a bit-list from RES type
(define (RegV->bit-list r)
(cases r
[(RegV bl) bl]
[else (error 'RegV->bit-list "Given wrong type of RES ~s" r)]
)
)
似乎问题始于with
部分,但是代码在我添加“ fun”和“ call”部分以及所有旧测试通过之前运行良好,所以我猜它已经用call
部分做某事...