0

I'm trying to re-write the equal function from the Vg.I module for Ocaml but when I try to do a pattern-matching with an image (of type t) I got an error.

Here is my code :

open Vg;;
open Gg;;

let rec decompose i = match i with
  | I.Primitive x -> Printf.printf "primitive\n\n" 
  | I.Cut(a,p,i) -> Printf.printf "cut\n\n"; decompose i
  | I.Cut_glyphs(a,r,i) -> Printf.printf "cut_glyphs\n\n"; decompose i
  | I.Blend(b,a,i1,i2) -> 
      Printf.printf "blend: t1\n\n"; decompose i1; 
      Printf.printf "blend: t2\n\n"; decompose i2
  | I.Tr(tr,i) -> Printf.printf "tr\n\n"; decompose i
  | _ -> failwith "some error";;

and here is the error

  | I.Primitive x -> Printf.printf "primitive\n\n" 
         ^^^^^^^^^^^
Error: Unbound constructor I.Primitive

I've also tried 'Vg.Primitive' and just 'Primitive' (even if it didn't make a lot of sense '^^) but I've got the same error every time.

If anyone knows how to properly use these constructors in a pattern-matching it would really help Thanks in advance

mrBigoudi
  • 13
  • 2

2 Answers2

2

The type image of the Vg library is abstract.

This means that the Vg library considers that the explicit definition of this type is an implementation detail that no external users should rely upon. In particular, different variants of the library may use different implementation for this type.

Consequently, you should not pattern match on values of this type, and OCaml module system enforces that you cannot do so.

octachron
  • 17,178
  • 2
  • 16
  • 23
  • Plus one for `...should not pattern match on values of this type, and OCaml module system enforces that you cannot do so.` – Nalin Ranjan Jun 13 '22 at 04:51
1

To complement octachron's excellent answer, consider a simple contrived example. A module A which contains a type t. Internally this type has a constructor A_ that takes an int. But the module's signature does not expose this type constructor. We only know the type exists, but nothing else about it. Values of that type, therefore can only be manipulated via the functions the module exposes.

Within the module A, that type can be pattern-matched as normal.

module A : sig
  type t 
  val make : int -> t
  val get_val : t -> int
end = struct
  type t = A_ of int
  let make x = A_ x
  let get_val (A_ x) = x
end
utop # A.A_ 42;;
Error: Unbound constructor A.A_
utop # A.make 42;;
- : A.t = <abstr>
utop # A.(make 42 |> get_val);;
- : int = 42
Chris
  • 26,361
  • 5
  • 21
  • 42