0 | module Control.Lens.OptionalFold
2 | import Data.Bicontravariant
3 | import Data.Profunctor
4 | import Data.Profunctor.Costrong
5 | import Control.Lens.Optic
6 | import Control.Lens.Indexed
7 | import Control.Lens.Optional
8 | import Control.Lens.Getter
19 | record IsOptFold p where
20 | constructor MkIsOptFold
21 | runIsOptFold : (Strong p, Choice p, Bicontravariant p)
24 | optFoldToOptional : IsOptFold p => IsOptional p
25 | optFoldToOptional @{MkIsOptFold _} = MkIsOptional %search
28 | optFoldToGetter : IsOptFold p => IsGetter p
29 | optFoldToGetter @{MkIsOptFold _} = MkIsGetter %search
32 | indexedOptFold : IsOptFold p => IsOptFold (Indexed i p)
33 | indexedOptFold @{MkIsOptFold _} = MkIsOptFold %search
39 | 0 OptionalFold : (s,a : Type) -> Type
40 | OptionalFold = Simple (Optic IsOptFold)
44 | 0 IndexedOptionalFold : (i,s,a : Type) -> Type
45 | IndexedOptionalFold = Simple . IndexedOptic IsOptFold
55 | folding : (s -> Maybe a) -> OptionalFold s a
56 | folding f @{MkIsOptFold _} =
57 | contrabimap (\x => maybe (Left x) Right (f x)) Left . right
61 | ifolding : (s -> Maybe (i, a)) -> IndexedOptionalFold i s a
62 | ifolding f @{MkIsOptFold _} @{ind} =
63 | contrabimap (\x => maybe (Left x) Right (f x)) Left . right . indexed @{ind}