3 | import Monocle.Getter
5 | import Monocle.Optional
7 | import Monocle.Setter
8 | import Monocle.Traversal
9 | import Data.List.Quantifiers
29 | record Iso s t a b where
37 | 0 Iso' : (s,a : Type) -> Type
38 | Iso' s a = Iso s s a a
46 | interface ToIso (0 o : Type -> Type -> Type -> Type -> Type) where
47 | toIso : o s t a b -> Iso s t a b
49 | public export %inline
50 | ToIso Iso where toIso = id
58 | toPrism (I g r) = P (Right . g) r
61 | ToOptional Iso where
62 | toOptional (I g r) = O (Right . g) (\f => r . f . g)
66 | toSetter (I g r) = S $
\f => r . f . g
68 | public export %inline
70 | toFold (I g r) = F (. g)
74 | toGetter (I g r) = G g
77 | ToTraversal Iso where
79 | T (\f,v => i.reverseGet <$> f (i.get_ v))
83 | public export %inline
85 | toLens (I g r) = L g $
\f => r . f . g
93 | public export %inline
94 | rev : Iso s t a b -> Iso b a t s
102 | (~>) : Iso s t a b -> Iso a b c d -> Iso s t c d
103 | I f1 g1 ~> I f2 g2 = I (f2 . f1) (g1 . g2)
111 | identity : Iso' a a
117 | idL = toLens identity
122 | idP = toPrism identity
126 | idO : Optional' a a
127 | idO = toOptional identity
131 | idT : Traversal' a a
132 | idT = toTraversal identity
136 | pack : Iso' (List Char) String
137 | pack = I pack unpack
141 | unpack : Iso' String (List Char)
142 | unpack = I unpack pack
146 | chips : Iso (SnocList a) (SnocList b) (List a) (List b)
147 | chips = I (<>> []) ([<] <><)
151 | fish : Iso (List a) (List b) (SnocList a) (SnocList b)
152 | fish = I ([<] <><) (<>> [])
156 | swap : Iso (a,b) (c,d) (b,a) (d,c)
162 | flip : Iso (a -> b -> c) (d -> e -> f) (b -> a -> c) (e -> d -> f)
168 | uncurry : Iso (a -> b -> c) (d -> e -> f) ((a,b) -> c) ((d,e) -> f)
169 | uncurry = I uncurry curry
174 | curry : Iso ((a,b) -> c) ((d,e) -> f) (a -> b -> c) (d -> e -> f)
175 | curry = I curry uncurry
179 | swapE : Iso (Either a b) (Either c d) (Either b a) (Either d c)
180 | swapE = I (either Right Left) (either Right Left)
189 | withDefault : a -> Iso' (Maybe a) a
190 | withDefault v = I (fromMaybe v) Just
195 | leftVoid : Iso (Either Void a) (Either Void b) a b
196 | leftVoid = I (either absurd id) Right
201 | rightVoid : Iso (Either a Void) (Either b Void) a b
202 | rightVoid = I (either id absurd) Left
206 | any1 : Iso (Any f [a]) (Any g [b]) (f a) (g b)
207 | any1 = I (\case Here v => v) Here