Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] feat(std): add monad transformer interface, StateT, and LazyT #686

Merged
merged 17 commits into from
May 4, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions std/lazyt.glu
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//@NO-IMPLICIT-PRELUDE

let { Applicative, apply } = import! std.applicative
let { (<<) } = import! std.function
let { Functor, map } = import! std.functor
let { lazy, force } = import! std.lazy
let { Monad, flat_map } = import! std.monad

type LazyT m a = Lazy (m a)

let functor ?fr : [Functor m] -> Functor (LazyT m) =
Etherian marked this conversation as resolved.
Show resolved Hide resolved
let map f ma = lazy (\_ -> fr.map f (force ma))

{ map }

let applicative ?ap : [Applicative m] -> Applicative (LazyT m) =
let wrap a = lazy (\_ -> ap.wrap a)
let apply mf ma = lazy (\_ -> ap.apply (force mf) (force ma))

{ functor, apply, wrap }

let monad ?mo : [Monad m] -> Monad (LazyT m) =
let flat_map f ma = lazy (\_ -> mo.flat_map (force << f) (force ma))

{ applicative, flat_map }

{
LazyT,

lazy,
force,

functor,
applicative,
monad,
}
82 changes: 82 additions & 0 deletions std/statet.glu
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//! The state monad transformer.

let { Applicative, Functor, Monad } = import! std.prelude
let { Alternative, (<|>), or, empty } = import! std.alternative
let { wrap, (<*>) } = import! std.applicative
let { (>>), (<<), ? } = import! std.function
let { map } = import! std.functor
let { (>>=) } = import! std.monad
let { Transformer } = import! std.transformer

type StateOut s a = { value : a, state : s }
type WrStateOut s m a = m { value : a, state : s }

type StateT s m a = s -> m { value : a, state : s }

let map_sout f st : (a -> b) -> StateOut s a -> StateOut s b =
{value = f st.value, state = st.state}

let functor ?fr : [Functor m] -> Functor (StateT s m) =
let map f sr : (a -> b) -> StateT s m a -> StateT s m b =
fr.map (map_sout f) << sr

{ map }

let applicative ?mo : [Monad m] -> Applicative (StateT s m) =
let apply srf sr : StateT s m (a -> b) -> StateT s m a -> StateT s m b = \state ->
srf state >>= \fout ->
let {value = f, state = state'} = fout
mo.applicative.functor.map (map_sout f) (sr state')

let wrap value : a -> StateT s m a = \state ->
mo.applicative.wrap { value, state }

{ functor = functor ?mo.applicative.functor, apply, wrap }

let monad ?mo : [Monad m] -> Monad (StateT s m) =
let flat_map f sr : (a -> StateT s m b) -> StateT s m a -> StateT s m b = \state ->
sr state >>= \sout ->
let {value, state = state'} = sout
f value state'

{ applicative, flat_map }

// FIXME implicit parameter troubles
Marwes marked this conversation as resolved.
Show resolved Hide resolved
let transformer : Transformer (StateT s) =
let wrap_monad ?mo ma : [Monad m] -> m a -> StateT s m a = \state ->
ma >>= \value ->
mo.applicative.wrap {value, state}

{ /* monad, */ wrap_monad }

// needs [transformer (StateT s)] parameter?
let alternative ?_ ?alt : [Monad m] -> [Alternative m] -> Alternative (StateT s m) =
let or sra srb = alt.or << sra <*> srb
let empty = transformer.wrap_monad empty // TODO switch back to this after Transformer (StateT s) is fixed
// let empty = \state -> empty >>= \value ->
// mo.applicative.wrap {value, state}

{ applicative, or, empty }

let put ?mo value : [Monad m] -> s -> StateT s m () = \state ->
mo.applicative.wrap { value = (), state = value }

let get ?mo : [Monad m] -> StateT s m s = \state ->
mo.applicative.wrap { value = state, state }

let gets ?mo f : [Monad m] -> (s -> a) -> StateT s m a =
get >>= (wrap << f)

let modify f : [Monad m] -> (s -> s) -> StateT s m () =
get >>= (put << f)

let run_state_t f state : StateT s m a -> s -> m { value : a, state : s } =
f state

let eval_state_t f state : [Functor m] -> StateT s m a -> s -> m a =
map (\x -> x.value) (run_state_t f state)

let exec_state_t f state : [Functor m] -> StateT s m a -> s -> m s =
map (\x -> x.state) (run_state_t f state)

{ StateT, applicative, functor, monad, put, get, modify, run_state_t, eval_state_t, exec_state_t }
11 changes: 11 additions & 0 deletions std/transformer.glu
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//@NO-IMPLICIT-PRELUDE

let { Monad } = import! std.prelude

#[implicit]
type Transformer t = {
Marwes marked this conversation as resolved.
Show resolved Hide resolved
/* monad : forall m . [Monad m] -> Monad (t m), */
wrap_monad : forall a m . [Monad m] -> m a -> t m a
}

{ Transformer }