Skip to content

Instantly share code, notes, and snippets.

@joelonsql
Last active December 26, 2018 14:51
Show Gist options
  • Save joelonsql/b7d66647200f80f486b76e57ee5d01eb to your computer and use it in GitHub Desktop.
Save joelonsql/b7d66647200f80f486b76e57ee5d01eb to your computer and use it in GitHub Desktop.
(*
ocamldep .Re0.eobjs/Letter2Set.ml.d (exit 2)
(cd _build/default && /Users/joel/.opam/ocaml-base-compiler/bin/ocamldep.opt -modules -impl Letter2Set.ml) > _build/default/.Re0.eobjs/Letter2Set.ml.d
File "Letter2Set.ml", line 11, characters 4-8:
Error: Syntax error
*)
module Pair =
struct
type t = (Letter.t* Letter.t)
let compare ((_,w),(_,x)) ((_,y),(_,z)) =
match Int32.compare w y with | 0 -> Int32.compare x z | i -> i
end
include Set.Make(Pair)
let (<+>) = union
let (>>=) m k = LetterSet.fold (fun x -> fun s -> union (k x) s) m empty
[@@ocaml.doc " Flat map "]
let <*>: LetterSet.t -> LetterSet.t -> t =
fun l -> fun r -> l >>= (fun x -> r >>= (fun y -> singleton (x, y)))
[@@ocaml.doc
" Cartensian product, i.e. all elements in l combined with all elements in r "]
let to_string l2s =
String.concat " "
(List.rev
(fold
(fun (letter1,letter2) ->
fun l ->
((Letter.to_string letter1) ^ (Letter.to_string letter2)) :: l)
l2s []))
let example letter1 letter2 = singleton (letter1, letter2)
let test () =
let abc = example (Letter.example ['a'] 0) (Letter.example ['b'; 'c'] 1) in
assert ((to_string abc) = "a<sub>0</sub>[bc]<sub>1</sub>");
(let a = LetterSet.example ['a'] 0 in
let bc = LetterSet.example ['b'; 'c'] 1 in
let abc' = a <*> bc in
assert (equal abc abc');
(let abc'' = a >>= (fun x -> bc >>= (fun y -> singleton (x, y))) in
assert (equal abc abc'')))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment