Skip to content

Commit

Permalink
style(std): remove extraneous implicit parameter bindings in statet a…
Browse files Browse the repository at this point in the history
…nd lazyt
  • Loading branch information
Etherian committed Feb 20, 2019
1 parent a00c6ce commit aceae12
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 32 deletions.
22 changes: 11 additions & 11 deletions std/lazyt.glu
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
//@NO-IMPLICIT-PRELUDE

let { Applicative, apply } = import! std.applicative
let { Applicative, apply, wrap } = 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) =
let map f ma = lazy (\_ -> fr.map f (force ma))
let functor : [Functor m] -> Functor (LazyT m) =
let ltmap f ma = lazy (\_ -> map f (force ma))

{ map }
{ map = ltmap }

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))
let applicative : [Applicative m] -> Applicative (LazyT m) =
let ltwrap a = lazy (\_ -> wrap a)
let ltapply mf ma = lazy (\_ -> apply (force mf) (force ma))

{ functor, apply, wrap }
{ functor, apply = ltapply, wrap = ltwrap }

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

{ applicative, flat_map }
{ applicative, flat_map = ltflat_map }

{
LazyT,
Expand Down
39 changes: 18 additions & 21 deletions std/statet.glu
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
//! The state monad transformer.

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

type StateOut s a = { value : a, state : s }
Expand All @@ -16,24 +15,25 @@ 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
let functor : [Functor m] -> Functor (StateT s m) =
let stmap f sr : (a -> b) -> StateT s m a -> StateT s m b =
map (map_sout f) << sr

{ map }
{ map = stmap }

// the typechecker can't find map and Functor m without help
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 }
let stwrap value : a -> StateT s m a = \state ->
wrap { value, state }

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

let monad ?mo : [Monad m] -> Monad (StateT s m) =
let monad : [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
Expand All @@ -43,20 +43,17 @@ let monad ?mo : [Monad m] -> Monad (StateT s m) =

// FIXME implicit parameter troubles
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}
let wrap_monad ma : [Monad m] -> m a -> StateT s m a = \state ->
ma >>= \value -> 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}
let stor sra srb = or << sra <*> srb
let stempty = transformer.wrap_monad empty

{ applicative, or, empty }
{ applicative, or = stor, empty = stempty }

let put value : [Monad m] -> s -> StateT s m () = \state ->
wrap { value = (), state = value }
Expand Down

0 comments on commit aceae12

Please sign in to comment.