Skip to content

Commit

Permalink
ocaml: fix hash equality
Browse files Browse the repository at this point in the history
  • Loading branch information
dubek committed Nov 22, 2015
1 parent 1f8c4c8 commit a8d8d81
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
4 changes: 1 addition & 3 deletions ocaml/core.ml
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ let init env = begin
| _ -> T.Int 0));
Env.set env (Types.symbol "=")
(Types.fn (function
| [T.List a; T.Vector b] -> T.Bool (a = b)
| [T.Vector a; T.List b] -> T.Bool (a = b)
| [a; b] -> T.Bool (a = b)
| [a; b] -> T.Bool (Types.mal_equal a b)
| _ -> T.Bool false));

Env.set env (Types.symbol "pr-str")
Expand Down
19 changes: 19 additions & 0 deletions ocaml/types.ml
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,22 @@ let rec list_into_map target source =
| k :: v :: more -> list_into_map (MalMap.add k v target) more
| [] -> map target
| _ :: [] -> raise (Invalid_argument "Literal maps must contain an even number of forms")

let rec mal_list_equal a b =
List.length a = List.length b && List.for_all2 mal_equal a b

and mal_hash_equal a b =
if MalMap.cardinal a = MalMap.cardinal b
then
let identical_to_b k v = MalMap.mem k b && mal_equal v (MalMap.find k b) in
MalMap.for_all identical_to_b a
else false

and mal_equal a b =
match (a, b) with
| (Types.List a, Types.List b)
| (Types.List a, Types.Vector b)
| (Types.Vector a, Types.List b)
| (Types.Vector a, Types.Vector b) -> mal_list_equal a.Types.value b.Types.value
| (Types.Map a, Types.Map b) -> mal_hash_equal a.Types.value b.Types.value
| _ -> a = b

0 comments on commit a8d8d81

Please sign in to comment.