OCaml词汇与动态范围

时间:2016-05-25 21:40:59

标签: dynamic scope ocaml lexical-scope lexical-closures

我对某些话题有疑问。总之,我必须在Ocaml中编写一个静态和动态范围的解释器。 现在我通过使用环境(IDE *值)列表和eval(evn * exp)实现了一个带有静态作用域的版本,在语句时传递了evn。

接下来的问题是,'可以通过改变列表的读数或者你必须采取另一种方式来开发具有列表和这样的评估函数的作用域(动态 - 静态);

这是代码的一部分:

type ide = string

type bi_operator = 
     Plus
    |Minus
    |Mul
    |Div
    |Eq
    |LThan
    |LEq
    |And
    |Or

type exp =   
    Var of ide
    |Const of value
    |Fun of ide * exp
    |Not of exp    
    |BOp of exp * bi_operator * exp
    |Let of ide * exp * exp
    |If of exp * exp * exp    
    |FunApp of exp * exp           



and value =  
| Int of int    
| Bool of bool          
| Closure of env * string * exp 
and env = (ide * value) list

评估代码:

let rec eval (evn,e) = match e with
  | Const _ -> expToV(e) 
  | Var x -> lookup (x,evn)
  | BOp (a,b,c) -> ( match ((eval(evn,a)),(eval(evn,c))) with
    | (Int a, Int c) -> 
        ( match b with
          | Plus -> Int (a + c)
          | Minus -> Int (a - c)
          | Mul -> Int (a * c)
          | Div -> Int (a / c)  

          | Eq  -> Bool (a = c)                                                                                     
          | LThan  -> Bool (a < c)
          | LEq  -> Bool (a <= c)
          | _   -> raise (MLFailure "Not a valid Int operator")
        )

    | (Bool a, Bool c) ->
        ( match b with
          | Eq  -> Bool (a = c)
          | And -> Bool (a && c)
          | Or  -> Bool (a || c)
          | _   -> raise (MLFailure "Not a valid Bool operator")
        )
    | _ -> raise (MLFailure "Bin arguments do not match"))


  | Fun (a,b) -> Closure (evn,a,b)

  | Not (a) -> (match (eval(evn,a)) with
    | (Bool a) -> if(a = false) then Bool(true) else Bool(false)
    | _ -> raise (MLFailure "Bin arguments do not match"))

  | Let (a,b,c) -> eval ( ((a,eval (evn,b))::evn) , c)

  | If (a,b,c) -> if (eval (evn,a) = (Bool true)) then (eval (evn,b)) else (eval (evn,c))  

  | FunApp (a,b) -> (match eval (evn,a) with
                 | Closure (environment,funct,args) ->  eval (((funct, eval (evn,b))::environment),args)
                 | _ -> raise (MLFailure "Bin arguments do not match")) 

以下是我发表声明的一个例子:

let _ = eval ([("x", Int 3);("t", Int 5);("z", Int 5);("x", Int 5);("y", Int 1)], (Let ("x", Const (Int 1),
                    Let ("f", Fun ("y", Var "x"),
                      Let ("x", Const (Int 2), FunApp (Var "f", Const(Int 0)))))));;

或者

let _ = eval ([], (Let ("x", Const (Int 1),
                    Let ("f", Fun ("y", Var "x"),
                      Let ("x", Const (Int 2), FunApp (Var "f", Const(Int 0)))))));;

通过这些示例,结果是Int 1。 在我的书中,这个例子给出了:

Lexical:Int 1

动态:Int 2

它看起来是正确的 实现。

1 个答案:

答案 0 :(得分:2)

您需要做的就是替换

| Closure (environment,funct,args) ->  eval ((funct, eval (evn,b))::environment,args)

| Closure (environment,funct,args) ->  eval ((funct, eval (evn,b))::evn,args)

此时,您还可以从Closure中删除env组件,因为它从未使用过。

FWIW,我在上面的代码中保留了你的变量命名,虽然它真的很奇怪,因为funct是参数名称,而args是函数体。