Why is the parameter b of type a in this example?
I am researching an ethereal library and noticed this
type Lens<'a,'b> =
('a -> 'b) * ('b -> 'a -> 'a)
static member (^=) (Set, (_, s): Lens<'a,'b>) =
fun (b: 'b) ->
s b : 'a -> 'a
In the function ^ =, b the if parameter is of type b in the lambda. However, in the body of lamda, why b is type 'now?
source to share
However, in the body of lamda, why b is type 'now?
This is not true.
b
is the input that is typed 'b
as shown in fun (b: 'b) ->
.
We can rewrite this element without a match and use a locally defined function like:
static member (^=) (Set, lens: Lens<'a,'b>) =
// Pattern match to extract out the 2nd portion of the lens, which is a function: 'b -> 'a -> 'a
let (_,s) = lens
// Define a function that takes a 'b and returns a new function of type: 'a -> 'a
let fn (b: 'b) : 'a -> 'a =
s b // this just partially applies s with the input "b"
fn // Return the function
Basically, (Set, (_,s))
in the argument list, bind the "s" to the second part Lens<'a,'b>
or via a function ('b -> 'a -> 'a)
. Above, I broke this to be more explicit and did this extraction in my own binding.
The member then returns a locally defined function (as a lambda). I rewrote this above using the bind function, as it often becomes clearer.
source to share
I think you are just reading the syntax wrong. Code s b : 'a -> 'a
does not mean it b
has a type 'a
.
The correct way to read it is to split it into two parts: the part before the colon is the expression, and the part after the colon is the type of that expression.
So the code s b : 'a -> 'a
actually means it s b
has a type 'a -> 'a
. He doesn't say anything about types s
or b
individually.
source to share