Hakell surplus binding in pattern
I have the following snippet out of an Interpreter:
type Ident = String type Value = Int type State = Ident -> Value iniState :: State iniState = \ident -> error "internal error initial state" updateS :: State -> (Ident, Value) -> State updateS s (ident, val) ident' | ident' == ident = val | otherwise = s ident'
where does the ident' in the pattern of updateS come from?
The third argument is from the Ident argument to the function in the type State.
Using type defines a synonym for another type (quite literally a synonym). In other words, the following type signatures are all the same:
State -> (Ident, Value) -> State (Ident -> Value) -> (Ident, Value) -> (Ident -> Value) (Ident -> Value) -> (Ident, Value) -> Ident -> Value
(You could also replace Ident with String and Value with Int to do a full expansion.)
The last one makes it clear that updateS could take 3 parameters: a function Ident -> Value, a pair (Ident, Value) and a single Ident.
updateS is a function that takes three parameters. If you replace State with its definition in the type signature, you get
updateS :: (Ident -> Value) -> (Ident, Value) -> Ident -> Value
So ident' is the third parameter of the function, which has type Ident.
Looking at the bigger picture, updateS turns a State and an (Ident, Value) pair into a new State. We can reason about what kind of State is returned. If we evaluate updateS s (ident, val), we get a State that acts like s in most situations, except that if it's is called with an argument equal to ident, it returns val.