Skip to content

Instantly share code, notes, and snippets.

@Chouser
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Chouser/38e5207a4a096a5f363e to your computer and use it in GitHub Desktop.
Save Chouser/38e5207a4a096a5f363e to your computer and use it in GitHub Desktop.
Attempt at recursive map type in OCaml
module MakeVal (MapType : Map.S) =
struct
let compare = Pervasives.compare
type map_type = t MapType.t
and 'a with_meta = { value : 'a; meta : map_type }
and t =
| List of t list with_meta
| Vector of t list with_meta
| Map of map_type with_meta
| Int of int
| Symbol of string with_meta
| Keyword of string
| Nil
| Bool of bool
| String of string
| Fn of (t list -> t)
end
module type MakeValSig =
sig
val compare : 'a -> 'a -> int
type map_type
and 'a with_meta
and t =
List of t list with_meta
| Vector of t list with_meta
| Map of map_type with_meta
| Int of int
| Symbol of string with_meta
| Keyword of string
| Nil
| Bool of bool
| String of string
| Fn of (t list -> t)
end
module rec MalVal : MakeValSig = MakeVal(MalMap)
and MalMap : Map.S = Map.Make(MalVal)
let to_bool x = match x with
| MalVal.Nil | MalVal.Bool false -> false
| _ -> true
module MakeVal (MapType : Map.S) =
struct
let compare = Pervasives.compare
type 'a with_meta = { value : 'a; meta : t MapType.t }
and t =
| List of t list with_meta
| Vector of t list with_meta
| Map of t MapType.t with_meta
| Int of int
| Symbol of string with_meta
| Keyword of string
| Nil
| Bool of bool
| String of string
| Fn of (t list -> t)
end
module rec MalVal : Map.OrderedType = MakeVal(MalMap)
and MalMap : Map.S = Map.Make(MalVal)
let to_bool x = match x with
| MalVal.Nil | MalVal.Bool false -> false
| _ -> true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment