|
| 1 | +open Optic |
| 2 | +open Type |
| 3 | + |
| 4 | +type 'a t = 'a Type.t |
| 5 | + |
| 6 | +let empty : type a. a t = W ((), (), Eq.unit, fun x -> x, Dag.empty ()) |
| 7 | + |
| 8 | +let ( & ) (W (c0, s0, _, w0)) (W (c1, s1, _, w1)) = |
| 9 | + W |
| 10 | + ( (None, c0, c1) |
| 11 | + , (s0, s1) |
| 12 | + , Eq.create () |
| 13 | + , fun ((x, (s0, s1), (cache, c0, c1)) as input) -> |
| 14 | + match cache with |
| 15 | + | Some (x', s0', s1', img) when x == x' && s0 == s0' && s1 == s1' -> input, img |
| 16 | + | _ -> |
| 17 | + let (x, s0, c0), img0 = w0 (x, s0, c0) in |
| 18 | + let (x, s1, c1), img1 = w1 (x, s1, c1) in |
| 19 | + let img = Dag.seq img0 img1 in |
| 20 | + let cache = Some (x, s0, s1, img) in |
| 21 | + (x, (s0, s1), (cache, c0, c1)), img ) |
| 22 | + |
| 23 | +let iso iso (W (c, s, _, w)) = |
| 24 | + let iso' = |
| 25 | + { Iso.ltor = (fun (x, y) -> Iso.ltor iso x, y) |
| 26 | + ; rtol = (fun (x, y) -> Iso.rtol iso x, y) |
| 27 | + } |
| 28 | + in |
| 29 | + let eq_iso = Eq.create () in |
| 30 | + W |
| 31 | + ( (None, c) |
| 32 | + , s |
| 33 | + , Eq.create () |
| 34 | + , fun ((x, s, (cache, c)) as input) -> |
| 35 | + match cache with |
| 36 | + | Some (x', s', img) when x == x' && s == s' -> input, img |
| 37 | + | _ -> |
| 38 | + let y = Optic.Iso.ltor iso x in |
| 39 | + let (y, s, c), img = w (y, s, c) in |
| 40 | + let x = Optic.Iso.rtol iso y in |
| 41 | + let img = Dag.iso eq_iso iso' img in |
| 42 | + let cache = Some (x, s, img) in |
| 43 | + (x, s, (cache, c)), img ) |
| 44 | + |
| 45 | +let on lens (W (c, s, _, w)) = |
| 46 | + let eq_lens = Eq.create () in |
| 47 | + W |
| 48 | + ( (None, c) |
| 49 | + , s |
| 50 | + , Eq.create () |
| 51 | + , fun ((x, s, (cache, c)) as input) -> |
| 52 | + match cache with |
| 53 | + | Some (x', s', img) when x == x' && s == s' -> input, img |
| 54 | + | _ -> |
| 55 | + let y = Optic.Lens.get lens x in |
| 56 | + let (y, s, c), img = w (y, s, c) in |
| 57 | + let x = Optic.Lens.put lens y x in |
| 58 | + let img = Dag.on eq_lens lens img in |
| 59 | + let cache = Some (x, s, img) in |
| 60 | + (x, s, (cache, c)), img ) |
| 61 | + |
| 62 | +let into prism (W (c, s, _, w)) = |
| 63 | + let eq_prism = Eq.create () in |
| 64 | + W |
| 65 | + ( (None, c) |
| 66 | + , s |
| 67 | + , Eq.create () |
| 68 | + , fun ((x, s, (cache, c)) as input) -> |
| 69 | + match cache with |
| 70 | + | Some (x', s', img) when x == x' && s == s' -> input, img |
| 71 | + | _ -> |
| 72 | + (match Optic.Prism.extract prism x with |
| 73 | + | None -> |
| 74 | + let img = Dag.empty () in |
| 75 | + let cache = Some (x, s, img) in |
| 76 | + (x, s, (cache, c)), img |
| 77 | + | Some y -> |
| 78 | + let (y, s, c), img = w (y, s, c) in |
| 79 | + let x = Optic.Prism.make prism y in |
| 80 | + let img = Dag.into eq_prism prism img in |
| 81 | + let cache = Some (x, s, img) in |
| 82 | + (x, s, (cache, c)), img) ) |
| 83 | + |
| 84 | +let cond predicate w = into (Prism.satisfy predicate) w |
| 85 | + |
| 86 | +let ifte predicate if_true if_false = |
| 87 | + cond predicate if_true & cond (fun x -> not (predicate x)) if_false |
| 88 | + |
| 89 | +let reorder : type a b c. (a * (b * c), (b * a) * c) Optic.iso = |
| 90 | + { Optic.Iso.ltor = (fun (x, (s0, s1)) -> (s0, x), s1) |
| 91 | + ; rtol = (fun ((s0, x), s1) -> x, (s0, s1)) |
| 92 | + } |
| 93 | + |
| 94 | +let stateful s0 (W (c, s1, _, w)) = |
| 95 | + let eq_iso = Eq.create () in |
| 96 | + W |
| 97 | + ( (None, c) |
| 98 | + , (s0, s1) |
| 99 | + , Eq.create () |
| 100 | + , fun ((x, (s0, s1), (cache, c)) as input) -> |
| 101 | + match cache with |
| 102 | + | Some (x', s0', s1', img) when x == x' && s0 == s0' && s1 == s1' -> input, img |
| 103 | + | _ -> |
| 104 | + let ((s0, x), s1, c), img = w ((s0, x), s1, c) in |
| 105 | + let img = Dag.iso eq_iso reorder img in |
| 106 | + let cache = Some (x, s0, s1, img) in |
| 107 | + (x, (s0, s1), (cache, c)), img ) |
| 108 | + |
| 109 | +let dynamic : type a. (a t * a) t = |
| 110 | + W |
| 111 | + ( () |
| 112 | + , () |
| 113 | + , Eq.unit |
| 114 | + , fun (wx, (), ()) -> |
| 115 | + let W (c, s, seq, w), x = wx in |
| 116 | + let (x, s, c), img = w (x, s, c) in |
| 117 | + let wx = W (c, s, seq, w), x in |
| 118 | + let img = Dag.dynamic seq img in |
| 119 | + (wx, (), ()), img ) |
| 120 | + |
| 121 | +(********************************************************************************) |
| 122 | + |
| 123 | +let ( <*> ) a b = on Lens.fst a & on Lens.snd b |
| 124 | + |
| 125 | +let initialize : type a b. (a -> b) -> (b option * a, b * a) Optic.iso = |
| 126 | + fun fn -> |
| 127 | + { Optic.Iso.ltor = |
| 128 | + (fun (s0, x) -> |
| 129 | + let s0 = |
| 130 | + match s0 with |
| 131 | + | None -> fn x |
| 132 | + | Some s0 -> s0 |
| 133 | + in |
| 134 | + s0, x) |
| 135 | + ; rtol = (fun (s0, x) -> Some s0, x) |
| 136 | + } |
| 137 | + |
| 138 | +let stateful_by fn w = stateful None (iso (initialize fn) w) |
| 139 | +let of_lazy w = stateful_by (fun _ -> Lazy.force w) dynamic |
| 140 | +let apply f x = of_lazy (lazy (f x)) |
| 141 | + |
| 142 | +let fix fn = |
| 143 | + let rec self = lazy (fn (of_lazy self)) in |
| 144 | + Lazy.force self |
| 145 | + |
| 146 | +let of_list ws = List.fold_left ( & ) empty ws |
| 147 | +let list w = fix (fun lst -> into Prism.cons (w <*> lst)) |
0 commit comments