Constructors like Plus
as you have declared it take arguments that are parenthesized. You appear to be using the syntax appropriate to a record type (with curly braces and field names). But there are no record types in your definition of expr
.
Here is a valid value of type expr
:
Plus (Const 3, Const 8)
Your eval
function doesn't handle the evaluation of variables (the Var
constructor). This is only a warning, not an error, but it will cause a runtime exception if you try to evaluate something like Var "abc"
.
You don't say which of these errors you're talking about, but I hope this is helpful nonetheless.
Update
As @CraigFe at discuss.ocaml.org pointed out, you have a mismatch between the definition of expr
and the test case. You could rewrite your definition of expr
so that the test case works, or you could rewrite the test case so it works with your current definition of expr
.
To match the test case, you would want a definition like this:
type expr2 =
| Const of int
| Plus of { arg1 : expr2; arg2: expr2 }
Then you can have a value like this:
Plus { arg1 = Const 3; arg2 = Const 8 }
However, you can't have field names starting with a capital letter in OCaml. This means that Arg1
and Arg2
need to be arg1
and arg2
. So personally I would suspect that the test case is the part that needs revision.
I don't understand the part about the mutually recursive definitions (though I know what those are of course). Generally speaking I'd say your biggest difficulty is with the problem statement, not your code.