Download Plug-In Development Guide - Frama-C
Transcript
4.9. THE TYPE LIBRARY: TYPE VALUES AND DATATYPES type α ab = A of α| B of int module Poly_ab = Datatype.Polymorphic (struct type α t = α ab let name ty = Type.name ty ^ " ab" let module_name = "Ab" let reprs ty = [ A ty ] let structural_descr d = Structural_descr.Structure (Structural_descr.Sum [| [| Structural_descr.pack d |]; [| Structural_descr.p_int |] |] let mk_equal f x y = match x, y with | A x, A y → f x y | B x, B y → x = y | A _, B _ | B _, A _ → false let mk_compare f x y = match x, y with | A x, A y → f x y | B x, B y → Pervasives.compare x y | A _, B _ → 1 | B _, A _ → -1 let mk_hash f = function A x → f x | B x → 257 * x let map f = function A x → A (f x) | B x → B x let mk_internal_pretty_code f prec_caller fmt = function | A x → Type.par prec_caller Type.Basic fmt (fun fmt → Format.fprintf fmt "A %a" (f Type.Call) x) | B n → Type.par prec_caller Type.Call fmt (fun fmt → Format.fprintf fmt "B %d" n) let mk_pretty f fmt x = mk_internal_pretty_code (fun _ → f) Type.Basic fmt x let mk_varname _ = "ab" let mk_mem_project mem f = function | A x → mem f x | B _ → false end) module Ab = Poly_AB.Make (∗ datatype corresponding to the type [int ab] ∗) module Ab_int = Ab(Datatype.Int) (∗ datatype corresponding to the type [int list ab] ∗) module Ab_Ab_string = Ab(Datatype.List(Datatype.Int)) (∗ datatype corresponding to the type [(string, int) Hashtbl.t ab] ∗) module HAb = Ab(Datatype.String.Hashtbl.Make(Datatype.Int)) Clearly it is a bit painful. However you probably will never apply this functor yourself. It is already applied for the standard OCaml polymorphic types like list and function (respectively 63