jiji
jiji

Reputation: 115

How to fix Ocaml Unbound constructor error?

let rec natadd_nat c =
    match c with
    | 0 -> O
    | _ -> (S (natadd_nat (c-1))) 

let rec natadd_int c =
    match c with
    | O -> 0
    | S n' -> 1+(natadd_int n')

let natadd a b =
    natadd_nat((natadd_int a) + (natadd_int b))

This is the code I wrote in ocaml. natadd_nat is a function that converts an int to a symbol consisting of O and S. natadd_int is a function that converts the symbols of O and S into int. The natadd function changes the symbols a and b from natadd_int to int. And after adding the two values ​​changed to int, put it in the natadd_nat function and change it back to a symbol.

But in the natadd_nat function,

Unbound constructor O

error occurred in the

| 0 -> O

part.

How can I solve this?

example)

# let two = S (S O) ;;
# let three = S (S (S O)) ;;
# natadd two three ;;
- : nat = S (S (S (S (S O))))

Upvotes: 0

Views: 452

Answers (1)

Chris
Chris

Reputation: 36496

A type constructor requires a type be declared with that constructors (or in this case constructors.

Judging from the code you've posted, you're likely missing something like:

type nat = O | S of nat

Upvotes: 1

Related Questions