Last active
August 29, 2015 14:14
-
-
Save Chouser/38e5207a4a096a5f363e to your computer and use it in GitHub Desktop.
Attempt at recursive map type in OCaml
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
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 |
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
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