dimanche 6 août 2017

How to match on constructors of abstract data types in OCAML

1) I have this module type module type MOD_SIG = sig type t val min val max;;

2) I create two modules based on the signature above

module MOD_UINT8 = struct type t = int32 let min = Int32.of_int 0 let max = Int32.of_int 255 end;; module MOD_UINT32 = struct type t = int64 let min = Int64.zero let max = Int64.of_int 4294967295 end ;;

4) Now i created first-class modules of 2 and 3

let uint_8 = (module MOD_UINT8:MOD_SIG) ;; let uint_32 = (module MOD_UINT32:MOD_SIG);;

5) Now i want to write a function that accepts a first-class module as an arg and tries to match whether a particular value in that module was of Int32 or Int64.

such as

let module M=(val m:MOD_SIG) in match M.t with Int32 -> "Int32" | Int64 -> "Int64"

I learned from @octachron that it is not possible. Though i just wanted to raise this question to understand, if there could be a workaround ?

Aucun commentaire:

Enregistrer un commentaire