0 | module Data.Fun.Extra
4 | import Data.Vect.Quantifiers
10 | uncurry : {0 n : Nat} -> {0 ts : Vect n Type} -> Fun ts cod -> HVect ts -> cod
12 | uncurry f (x::xs) = uncurry (f x) xs
16 | curry : {n : Nat} -> {0 ts : Vect n Type} -> (HVect ts -> cod) -> Fun ts cod
17 | curry {ts = [] } f = f []
18 | curry {ts = _ :: _} f = \x => curry (\xs => f (x :: xs))
34 | homoFunNeut_ext : Fun [] cod -> id cod
35 | homoFunNeut_ext = id
38 | homoFunMult_ext : {n : Nat} -> {0 rs : Vect n Type} -> Fun (rs ++ ss) cod -> (Fun rs . Fun ss) cod
39 | homoFunMult_ext {rs = [] } = id
40 | homoFunMult_ext {rs = _::_} = (.) homoFunMult_ext
43 | homoFunNeut_inv : id cod -> Fun [] cod
44 | homoFunNeut_inv = id
47 | homoFunMult_inv : {n : Nat} -> {0 rs : Vect n Type} -> (Fun rs . Fun ss) cod -> Fun (rs ++ ss) cod
48 | homoFunMult_inv {rs = [] } = id
49 | homoFunMult_inv {rs = _::_} = (.) homoFunMult_inv
54 | applyPartially : {n : Nat} -> {0 ts : Vect n Type}
55 | -> Fun (ts ++ ss) cod -> (HVect ts -> Fun ss cod)
56 | applyPartially = uncurry . homoFunMult_ext
64 | uncurryAll : {0 n : Nat} -> {0 ts : Vect n Type} -> {0 cod : Fun ts Type}
65 | -> All ts cod -> (xs : HVect ts) -> uncurry cod xs
67 | uncurryAll f (x :: xs) = uncurryAll (f x) xs
70 | curryAll : {n : Nat} -> {0 ts : Vect n Type} -> {0 cod : Fun ts Type}
71 | -> ((xs : HVect ts) -> uncurry cod xs)
73 | curryAll {ts = [] } f = f []
74 | curryAll {ts = _::_} f = \x => curryAll (\xs => f (x :: xs))
76 | chainGenUncurried : {n : Nat} -> {0 ts : Vect n Type} -> {0 cod,cod' : Fun ts Type} ->
77 | ((xs : HVect ts) -> uncurry cod xs -> uncurry cod' xs) ->
78 | All ts cod -> All ts cod'
79 | chainGenUncurried {ts = [] } f gs = f [] gs
80 | chainGenUncurried {ts = _::_} f gs = \x => chainGenUncurried (\u => f (x :: u)) (gs x)
83 | homoAllNeut_ext : Fun [] cod -> id cod
84 | homoAllNeut_ext = id
89 | extractWitness : {n : Nat} -> {0 ts : Vect n Type} -> {0 r : Rel ts} -> Ex ts r -> HVect ts
90 | extractWitness {ts = [] } _ = []
91 | extractWitness {ts = _::_} (
w ** f)
= w :: extractWitness f
94 | extractWitnessCorrect : {n : Nat} -> {0 ts : Vect n Type} -> {0 r : Rel ts} -> (f : Ex ts r) ->
95 | uncurry {ts} r (extractWitness {r} f)
96 | extractWitnessCorrect {ts = [] } f = f
97 | extractWitnessCorrect {ts = _::_} (
w ** f)
= extractWitnessCorrect f
100 | introduceWitness : {0 r : Rel ts} -> (witness : HVect ts) ->
101 | uncurry {ts} r witness -> Ex ts r
102 | introduceWitness [] f = f
103 | introduceWitness (w :: witness) f = (
w ** introduceWitness witness f)
107 | data Pointwise : (r : a -> b -> Type) -> (ts : Vect n a) -> (ss : Vect n b) -> Type where
108 | Nil : Pointwise r [] []
109 | (::) : {0 ss, ts : Vect n Type} ->
110 | (f : r t s) -> Pointwise r ts ss -> Pointwise r (t::ts) (s::ss)
113 | precompose : Pointwise (\a,b => a -> b) ts ss -> Fun ss cod -> Fun ts cod
114 | precompose [] h = h
115 | precompose (f :: fs) h = precompose fs . h . f
122 | {0 ts : Vect n Type}
125 | -> (elems : HVect ts)
126 | -> f (uncurry g elems) = uncurry (chain {ts} f g) elems
127 | chainUncurry g f [] = Refl
128 | chainUncurry g f (x :: xs) = rewrite chainUncurry (g x) f xs in Refl