adam@6: fun read [r ::: {Unit}] [t ::: Type] (fl : folder r) (r : $(mapU t r)) (v : variant (mapU {} r)) : t = adam@6: match v adam@6: (@fold [fn r => r' :: {Unit} -> [r ~ r'] => $(mapU t (r ++ r')) -> $(mapU ({} -> t) r)] adam@6: (fn [nm :: Name] [u::_] [us::_] [[nm] ~ us] (cs : r' :: {Unit} -> [us ~ r'] => $(mapU t (us ++ r')) -> _) [r'::_] [[nm = u] ++ us ~ r'] r => adam@14: {nm = fn () => r.nm} ++ cs [[nm = u] ++ r'] r) adam@6: (fn [r'::_] [[] ~ r'] _ => {}) fl [[]] ! r) adam@6: adam@6: fun write [r ::: {Unit}] [t ::: Type] (fl : folder r) (r : $(mapU t r)) (v : variant (mapU {} r)) (x : t) : $(mapU t r) = adam@6: match v adam@6: (@fold [fn r => r' :: {Unit} -> [r ~ r'] => $(mapU t (r ++ r')) -> $(mapU ({} -> $(mapU t (r ++ r'))) r)] adam@6: (fn [nm :: Name] [u::_] [us::_] [[nm] ~ us] adam@6: (cs : r' :: {Unit} -> [us ~ r'] => $(mapU t (us ++ r')) -> $(mapU ({} -> $(mapU t (us ++ r'))) us)) adam@6: [r'::_] [[nm = u] ++ us ~ r'] r => adam@14: {nm = fn () => r -- nm ++ {nm = x}} ++ cs [[nm = u] ++ r'] r) adam@6: (fn [r'::_] [[] ~ r'] _ => {}) fl [[]] ! r) adam@6: adam@6: fun search [r] [t] (f : variant (mapU {} r) -> option t) (fl : folder r) : option t = adam@6: @fold [fn r => r' :: {Unit} -> [r ~ r'] => (variant (mapU {} (r ++ r')) -> option t) -> option t] adam@6: (fn [nm :: Name] [u ::_] [r ::_] [[nm] ~ r] adam@6: (acc : r' :: {Unit} -> [r ~ r'] => (variant (mapU {} (r ++ r')) -> option t) -> option t) adam@6: [r' ::_] [[nm] ++ r ~ r'] f' => adam@6: case f' (make [nm] {}) of adam@14: None => acc [[nm] ++ r'] f' adam@6: | v => v) adam@6: (fn [r' ::_] [[] ~ r'] _ => None) fl [[]] ! f adam@6: adam@6: fun find [r] (f : variant (mapU {} r) -> bool) (fl : folder r) : option (variant (mapU {} r)) = adam@6: @search (fn v => if f v then Some v else None) fl adam@6: adam@6: fun test [nm :: Name] [t ::: Type] [ts ::: {Type}] [[nm] ~ ts] (fl : folder ([nm = t] ++ ts)) adam@6: (v : variant ([nm = t] ++ ts)) : option t = adam@6: match v ({nm = Some} adam@6: ++ (@map0 [fn t' => t' -> option t] (fn [t' :: Type] _ => None) fl -- nm)) adam@6: adam@6: fun testLess [nm :: Name] [t ::: Type] [ts ::: {Type}] [[nm] ~ ts] (fl : folder ts) (v : variant ([nm = t] ++ ts)) : option t = adam@6: match v ({nm = Some} adam@6: ++ @map0 [fn t' => t' -> option t] (fn [t' :: Type] _ => None) fl) adam@6: adam@6: fun eq [r] (fl : folder r) (v1 : variant (mapU {} r)) (v2 : variant (mapU {} r)) : bool = adam@6: match v1 adam@6: (@fold [fn r => r' :: {Unit} -> [r ~ r'] => folder (r ++ r') -> variant (mapU {} (r ++ r')) -> $(mapU ({} -> bool) r)] adam@6: (fn [nm ::_] [u ::_] [r ::_] [[nm] ~ r] adam@6: (acc : r' :: {Unit} -> [r ~ r'] => folder (r ++ r') -> variant (mapU {} (r ++ r')) -> $(mapU ({} -> bool) r)) adam@6: [r' ::_] [[nm] ++ r ~ r'] (fl' : folder ([nm] ++ r ++ r')) v => adam@6: {nm = fn () => Option.isSome (@test [nm] ! (@Folder.mp fl') v)} adam@14: ++ @acc [[nm] ++ r'] ! fl' v) adam@6: (fn [r' ::_] [[] ~ r'] _ _ => {}) fl [[]] ! fl v2) adam@6: adam@6: fun fold [r] [t] (fl : folder r) (f : variant (mapU {} r) -> t -> t) : t -> t = adam@6: @Top.fold [fn r => r' :: {Unit} -> [r ~ r'] => (variant (mapU {} (r ++ r')) -> t -> t) -> t -> t] adam@6: (fn [nm :: Name] [u ::_] [r ::_] [[nm] ~ r] adam@6: (acc : r' :: {Unit} -> [r ~ r'] => (variant (mapU {} (r ++ r')) -> t -> t) -> t -> t) adam@6: [r' ::_] [[nm] ++ r ~ r'] f' acc' => adam@14: f' (make [nm] {}) (acc [[nm] ++ r'] f' acc')) adam@6: (fn [r' ::_] [[] ~ r'] _ x => x) fl [[]] ! f adam@6: adam@6: fun destrR [K] [f :: K -> Type] [fr :: K -> Type] [t ::: Type] adam@6: (f : p :: K -> f p -> fr p -> t) adam@6: [r ::: {K}] (fl : folder r) (v : variant (map f r)) (r : $(map fr r)) : t = adam@6: match v adam@6: (@Top.mp [fr] [fn p => f p -> t] adam@6: (fn [p] (m : fr p) (v : f p) => f [p] v m) adam@6: fl r) adam@6: adam@6: fun destr2R [K] [f1 :: K -> Type] [f2 :: K -> Type] [fr :: K -> Type] [t ::: Type] adam@6: (f : p :: K -> f1 p -> f2 p -> fr p -> t) adam@6: [r ::: {K}] (fl : folder r) (v1 : variant (map f1 r)) (v2 : variant (map f2 r)) (r : $(map fr r)) : option t = adam@6: match v1 adam@6: (@foldR [fr] [fn r => others :: {K} -> [others ~ r] => adam@6: folder (r ++ others) adam@6: -> variant (map f2 (r ++ others)) adam@6: -> $(map (fn p => f1 p -> option t) r)] adam@6: (fn [nm ::_] [p ::_] [r ::_] [[nm] ~ r] (meta : fr p) adam@6: (acc : others :: {K} -> [others ~ r] => adam@6: folder (r ++ others) adam@6: -> variant (map f2 (r ++ others)) adam@6: -> $(map (fn p => f1 p -> option t) r)) adam@6: [others :: {K}] [others ~ [nm = p] ++ r] adam@6: (fl : folder ([nm = p] ++ r ++ others)) adam@6: (v2 : variant (map f2 ([nm = p] ++ r ++ others))) => adam@6: {nm = fn x1 => match v2 adam@6: ({nm = fn x2 => Some (f [p] x1 x2 meta)} adam@6: ++ (@map0 [fn p => f2 p -> option t] (fn [p' ::_] _ => None) fl -- nm))} adam@14: ++ @acc [[nm = p] ++ others] ! fl v2) adam@6: (fn [others ::_] [others ~ []] _ _ => {}) adam@6: fl r [[]] ! fl v2) adam@6: adam@6: fun testEq [K] [f :: K -> Type] [nm :: Name] [t ::: K] [ts ::: {K}] [r ::: {K}] [[nm] ~ ts] adam@6: (pf : Eq.eq r ([nm = t] ++ ts)) (fl : folder r) (v : variant (map f r)) : option (f t) = adam@6: @test [nm] ! (@Folder.mp (@Eq.cast pf [folder] fl)) adam@6: (Eq.cast pf [fn r => variant (map f r)] v) adam@6: adam@6: fun makeEq [K] [f :: K -> Type] [nm :: Name] [t ::: K] [ts ::: {K}] [r ::: {K}] [[nm] ~ ts] adam@6: (pf : Eq.eq r ([nm = t] ++ ts)) (x : f t) : variant (map f r) = adam@6: Eq.cast (Eq.sym pf) [fn r => variant (map f r)] (make [nm] x)