You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
822 B

4 years ago
  1. open import "prelude.ml"
  2. type identity 'a = Id of 'a
  3. instance functor identity begin
  4. let f <$> Id x = Id (f x)
  5. end
  6. instance applicative identity begin
  7. let pure = Id
  8. let Id f <*> Id x = Id (f x)
  9. end
  10. instance monad identity begin
  11. let Id x >>= f = f x
  12. end
  13. type state 's 'a = State of 's -> 'a * 's
  14. let run_state (State k) = k
  15. instance functor (state 's) begin
  16. let f <$> State x = State (first f # x)
  17. end
  18. instance applicative (state 's) begin
  19. let pure x = State (x,)
  20. let State f <*> State x = State @@ fun s ->
  21. let (f, s) = f s
  22. let (x, s) = x s
  23. (f x, s)
  24. end
  25. instance monad (state 's) begin
  26. let State x >>= f = State @@ fun s ->
  27. let (x, s) = x s
  28. run_state (f x) s
  29. end
  30. let get = State (fun s -> (s, s))
  31. let put x = State (fun _ -> ((), x))
  32. let modify f = State (fun s -> ((), f s))