简体   繁体   中英

Circular dependencies between functions in Ocaml

I have seen some questions about circular dependencies in ocaml, but they were all about dependencies between types .

In my case, I have a function called eval . It simulates an interpreter for a made-up language and needs to call functions like this:

let rec eval e r = 
match e with
(* ... *)
    Forall(p, s) ->
        let pred = eval p r in
            let set = eval s r in
                forall pred s |
(* ... *)

Problem is, forall looks something like this:

let forAll (p : exp) (s : evT) : evT =
    match s with
    SetVal(Empty(_)) -> (Bool true) |
    SetVal(Set(lst, _)) ->
        match p with
                   (* irrelevant code *)
                        match l with
                            [] -> acc |
                            h::t -> aux t ((eval (FunCall(p, (evTToExp h))) env0)::acc)
                in
                    (* irrelevant code *)

As you can see, it both needs to be called by and call eval .

If I put the definition of eval first in my.ml file, I get Unbound value forall , otherwise I get Unbound value eval .

How do I solve this kind of dependency? How do I let ocaml know that it will find the definition of the missing function, somewhere else in the file?

Mutual recursion is accomplished with the rec and and keywords:

let rec eval = ...

and forAll = ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM