Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OCaml syntax error in functor

I'm trying to create a functor that makes a polynomial ring out of a ring. My underlying type, Ring_elt, has the following signature:

module type Ring_elt = sig
  type t
  val add : t -> t -> t
  val mul : t -> t -> t
  val zer : t
  val one : t
  val neg : t -> t
end;;

My polynomial functor looks like:

module Make_Poly2(Underlying:Ring_elt) = struct
  type t = Poly of Underlying.t list
  let rec create lst = 
    match List.rev lst with
    | Underlying.zer :: tl -> create List.rev tl
    | _                    -> Poly of lst
end;;

(so the 'create' function should take a list, remove the leading zeros, and then return the polynomial of the result). However, I get a syntax error and utop underlines the "zer" after "Underlying."

By comparison, the following code (for making integer polynomials) works:

module Make_int_poly = struct
  type t = Poly of int list
  let rec create lst = 
    match List.rev lst with
    | 0 :: tl -> create (List.rev tl)
    | _       -> Poly lst
end;;

Any idea what's going on?

like image 271
John Binder Avatar asked Feb 15 '26 20:02

John Binder


2 Answers

An OCaml pattern is built from constants, data constructors, and new names bound by the pattern match. Underlying.zer isn't any of those things. But 0 is one of them.

Seems like you can just use an if to compare against Underlying.zer.

like image 71
Jeffrey Scofield Avatar answered Feb 18 '26 20:02

Jeffrey Scofield


Jeffrey's answer is good but instead of correcting it with an if construction, what you should do is the following : use algebraic data types

Instead of writing

val zer : t
val one : t

You could write

module type Ring_elt = sig
  type t = Zer | One | Num of t
  val add : t -> t -> t
  val mul : t -> t -> t
  val neg : t -> t
end

module Make_int_poly = struct
  type t = Poly of int list
  let rec create lst = 
    match List.rev lst with
     | Underlying.Zer :: tl -> create (List.rev tl)
     | _                    -> Poly lst
end

It's a much better way of doing it since you can easily pattern match on it and even add some constants to your type t without problems.

like image 31
Lhooq Avatar answered Feb 18 '26 19:02

Lhooq



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!