Skip to content

Instantly share code, notes, and snippets.

@YoEight
Last active August 29, 2015 14:07
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save YoEight/bfaf9acbfe570820f6f2 to your computer and use it in GitHub Desktop.
Save YoEight/bfaf9acbfe570820f6f2 to your computer and use it in GitHub Desktop.
A different State encoding (solution here https://gist.github.com/YoEight/0541aa293effbfff8d02)
{-# LANGUAGE RankNTypes #-}
newtype State s a = State { runState :: forall r. s -> (a -> s -> r) -> r }
stateReturn :: a -> State s a
stateReturn = undefined
stateMap :: (a -> b) -> State s a -> State s b
stateMap = undefined
stateBind :: (a -> State s b) -> State s a -> State s b
stateBind = undefined
stateGet :: State s s
stateGet = undefined
statePut :: s -> State s ()
statePut = undefined
stateModify :: (s -> s) -> State s ()
stateModify = undefined
execState :: s -> State s a -> s
execState = undefined
evalState :: s -> State s a -> a
evalState = undefined
@earldouglas
Copy link

I ran this through Google Translate to convert it to Scala:

case class State[S,A](run: (S => (A => S => R) => R) forSome { type R }) {

  def map[B](f: A => B): State[S,B] = State.map(f)(this)

  def flatMap[B](f: A => State[S,B]): State[S,B] = State.bind(f)(this)

}

object State {

  def pure[S,A]: A => State[S,A] = ???

  def map[S,A,B]: (A => B) => State[S,A] => State[S,B] = ???

  def bind[S,A,B]: (A => State[S,B]) => State[S,A] => State[S,B] = ???

  def get[S]: State[S,S] = ???

  def put[S]: S => State[S,Unit] = ???

  def modify[S]: (S => S) => State[S,Unit] = ???

  def exec[S,A]: State[S,A] => S = ???

  def eval[S,A]: State[S,A] => A = ???

}

@FranklinChen
Copy link

Types for execState and evalState are wrong: both need to take an initial seed, otherwise there is no possible non-bottom implementation!

@YoEight
Copy link
Author

YoEight commented Oct 2, 2014

Oops... It's fixed now. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment