Created
April 15, 2012 17:05
-
-
Save philtomson/2393886 to your computer and use it in GitHub Desktop.
Prototyping the functors for FSM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(*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