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

open import "prelude.ml"
type identity 'a = Id of 'a
instance functor identity begin
let f <$> Id x = Id (f x)
end
instance applicative identity begin
let pure = Id
let Id f <*> Id x = Id (f x)
end
instance monad identity begin
let Id x >>= f = f x
end
type state 's 'a = State of 's -> 'a * 's
let run_state (State k) = k
instance functor (state 's) begin
let f <$> State x = State (first f # x)
end
instance applicative (state 's) begin
let pure x = State (x,)
let State f <*> State x = State @@ fun s ->
let (f, s) = f s
let (x, s) = x s
(f x, s)
end
instance monad (state 's) begin
let State x >>= f = State @@ fun s ->
let (x, s) = x s
run_state (f x) s
end
let get = State (fun s -> (s, s))
let put x = State (fun _ -> ((), x))
let modify f = State (fun s -> ((), f s))