Reputation: 658
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?
Upvotes: 2
Views: 169
Reputation: 29136
Mutual recursion is accomplished with the rec
and and
keywords:
let rec eval = ...
and forAll = ...
Upvotes: 3