Skip to content

Instantly share code, notes, and snippets.

@philtomson
Created April 15, 2012 17:05
Show Gist options
  • Save philtomson/2393886 to your computer and use it in GitHub Desktop.
Save philtomson/2393886 to your computer and use it in GitHub Desktop.
Prototyping the functors for FSM
(*ocamlc -o test test.ml*)
(* the problem we're trying to solve is to have the FSM module
* be able to deal with different expression types
*)
module type BASETYPE =
sig
type 'a var_t
val set : 'a var_t -> 'a -> unit
val get_var_name : 'a var_t -> string
val get_var_val : 'a var_t -> 'a
end
(* Concrete type *)
module Logic = struct
type 'a var_t = { name: string; mutable value: 'a }
let make_var s v = { name = s; value = v }
let set v n = v.value <- n
let get_var_name v = v.name
let get_var_val v = v.value
end
module type EXP =
sig
type 'a var_t
val get_var_name : 'a var_t -> string
val get_var_val : 'a var_t -> 'a
end
module B (T : BASETYPE) =
struct
include T
(*
type 'a var_t = 'a T.var_t
let get_var_name = T.get_var_name
let get_var_val = T.get_var_val
*)
end
module LogicExp = B(Logic)
module FSM ( Exp : EXP )(*with type 'a var_t = int A.var_t)*) =
struct
let print_var v = Printf.printf "%s = %d\n" (Exp.get_var_name v)
(Exp.get_var_val v)
end
module MyFSM = FSM(LogicExp)
let myvar = Logic.make_var "foo" 1;;
MyFSM.print_var myvar ;;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment