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?

Answers


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.


Need Your Help

rem unit, how it scale based on screen size

html css css3 web responsive-design

I'm designing a website using rem unit, some people said, rem unit change the size based on screen size.

Remove unwanted White Space in WebView Android

android image android-webview padding space

I have started developing an App using WebView. Actually I am loading an Image with Webview (I like to use the built-in zoom controls of the class). I can successfully load the Image but I can see ...