Guest User

Untitled

a guest
Dec 13th, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. module Optic.Prism
  2.  
  3. import Data.List
  4. %default total
  5.  
  6. -- Const
  7. data Const a b = Constant a
  8.  
  9. implementation Functor (Const a) where
  10. map _ (Constant x) = (Constant x)
  11.  
  12. implementation Monoid m => Applicative (Const m) where
  13. pure _ = Constant neutral
  14. (Constant x) <*> (Constant y) = Constant (x <+> y)
  15.  
  16. runConst : Const a b -> a
  17. runConst (Constant x) = x
  18.  
  19. -- Prism
  20. Prism : Type -> Type -> Type -> Type -> Type
  21. Prism s t a b = {f : Type -> Type} -> Applicative f => (a -> f b) -> s -> f t
  22.  
  23. prism : (a -> s) -> (s -> Maybe a) -> Prism s s a a
  24. prism encode decode fn s =
  25. case (decode s) of
  26. Nothing => pure s
  27. (Just v) => map encode $ fn v
  28.  
  29. view : Prism s s a a -> s -> Maybe a
  30. view p x = runConst $ p (Constant . Just) x
  31.  
  32. -- Maybe Prism
  33. justPrism : Prism (Maybe a) (Maybe a) a a
  34. justPrism = prism Just id
  35.  
  36. getNat : Maybe Nat
  37. getNat = view justPrism (Just 1) -- Can't find applicative of f1.
Add Comment
Please, Sign In to add comment