4

From our lecture notes:

get' state = (state, state)
put' item state = ((), item)     -- () is void value

data State s a = State (s -> (a, s))

-- Functions get and put:        -- (sic!)

get :: State s s
get = State get'

put :: s -> State s ()
put item = State (put' item)

I am totally lost in these two functions get and put.

First, there is no arrow in the type signature of get:

get :: State s s

What does it mean?

What does s mean in both get and put? Are they state?

Will Ness
  • 70,110
  • 9
  • 98
  • 181
user8314628
  • 1,952
  • 2
  • 22
  • 46
  • 2
    `State a b` means a `State` that stores a state of type `a`, and "returns" an element of type `b`, so for `get` you query the state, hence it returns the state so in that case `b ~ a`. `get'` and `put'` are defined on page 32 of the slides. – Willem Van Onsem Dec 08 '18 at 11:45
  • yes, `s` is the mnemonic for "state". – Will Ness Dec 08 '18 at 11:45
  • There is no arrow in the type signature because `get` is not a function. That's all it means. – melpomene Dec 08 '18 at 11:46
  • @WillemVanOnsem "a state that stores a state"?? this bound to be confusing. – Will Ness Dec 08 '18 at 11:46
  • 1
    @WillNess: well I think it is a bit unfortunate nomenclature in the first place, since an item of type `State` itself does not stores a state, it basically is a "transition" between `State`s (a transition that can be a no-op), but not really a `State` itself :). – Willem Van Onsem Dec 08 '18 at 11:50
  • @WillemVanOnsem we can just not skimp on words and say " 'State' monadic value representing a state-altering-and-passing-computation"... :) or "State monad action", or something... – Will Ness Dec 08 '18 at 11:52
  • @melpomene If `get` is not a function, then what is it? I tried to google the definition of Haskell function. But it just comes up with a bunch of examples. – user8314628 Dec 08 '18 at 11:53
  • 1
    @user8314628 no, it is a function. and it is a value. Haskell is ambivalent that way. You should have included more code in your question, like, the definition of `State`. – Will Ness Dec 08 '18 at 11:55
  • 1
    @user8314628 If I do `get = ["fetch", "my", "stuff"]`, what is `get`? Well, it's a list of strings (or `[String]`). Similarly, your `get` is a value of type `State s s`. Or if I were to do `foo = Nothing`, then `foo` would be not a function but a value of type `Maybe s`. – melpomene Dec 08 '18 at 12:09
  • @melpomene but the value of type `State s a` is actually a function of type `s -> (a, s)`, isn't it. – Will Ness Dec 08 '18 at 14:01
  • 2
    @WillNess Absolutely not. A value of type `State s a` *has* a function of type `s -> (a,s)` in it, but it isn't itself one. – Daniel Wagner Dec 08 '18 at 14:07
  • @DanielWagner context is everything. – Will Ness Dec 08 '18 at 14:38

1 Answers1

1

Remember that State s a is essentially a function s -> (a, s), that is, a function that takes a state and returns a value of some type a and a new state.

So get :: State s s is a s -> (s, s), a function that simply returns the current state.

put :: s -> s -> ((), s) is implemented as s -> _ -> ((), s), a function that takes a given state, ignores the current state, returns the given state, and produces no new values.

dcastro
  • 66,540
  • 21
  • 145
  • 155
  • 1
    but the function `s -> (s, s)` "simply returns" a tuple, no? – Will Ness Dec 08 '18 at 11:58
  • 1
    @WillNess yes it returns a tuple where both members are the current state. The first member of this tuple is the value that you can fmap/>>= over, and the second member is the state that is being threaded and will be given to the next stateful step. It just so happens that in `get` both members are the same. We return the current state to be fmap'd over, and also pass it to the next stateful step. – dcastro Dec 08 '18 at 12:05
  • I was trying to point out a part of the answer which I thought would be very confusing for a newbie. – Will Ness Dec 08 '18 at 12:07
  • `get :: State s` is a function that simply returns the current state. So the function `get` takes no argument and return a value, and `State s s` is the type of the return value? – user8314628 Dec 08 '18 at 12:14