I was writing a generic power function a^b.
This function uses recursion and in its plain integer version the pattern matching works,
but not in a generic module.
Can somebody explain me why I can’t use pattern matching in this code?
module type Tt = sig
type t
val zero : t
val one : t
val ( * ) : t -> t -> t
val ( - ) : t -> t -> t
end
module Expt (M : Tt) = struct
type tt = M.t
let zero = M.zero
let one = M.one
let ( * ) = M.( * )
let ( - ) = M.( - )
let expt (a:tt) (b:tt) =
let rec expt' (acc:tt) (a:tt) (b:tt) : tt=
if zero = b then (acc * one)
else if one = b then (acc * a)
else expt' (acc * a) a (b - one) in
(* This does not work - Compiler says unused variables zero,...*)
(*match b with
| zero -> (acc * one)
| one -> (acc * a)
| _ -> expt' (acc * a) a (b - one) in*)
expt' one a b
end
module IntExpt : Tt with type t = int = struct
type t = int
let zero = 0
let one = 1
let ( * ) = Stdlib.( * )
let ( - ) = Stdlib.( - )
end
module ExptInt = Expt(IntExpt)