Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

71%
+3 −0
Q&A Why does `let map f = id >=> switch f` work in F#?

when I look at the type signatures, it is not supposed to work. The types work because they're parameterized. The types of the combinators involved are (renaming all parameters to be unique fo...

posted 1mo ago by r~~‭

Answer
#1: Initial revision by user avatar r~~‭ · 2024-04-02T23:33:53Z (about 1 month ago)
> when I look at the type signatures, it is not supposed to work.

The types work because they're parameterized. The types of the combinators involved are (renaming all parameters to be unique for clarity):

```
id : 'a -> 'a
switch : ('b -> 'c) -> 'b -> Result<'c, 'err>
(>=>) :
  ('d -> Result<'e, 'err>) ->
  ('e -> Result<'f, 'err>) ->
  ('d -> Result<'f, 'err>)
map : ('x -> 'y) -> Result<'x, 'err> -> Result<'y, 'err>
```

When `(>=>) id` is type checked, `'d` must unify with `'a`, and `Result<'e, 'err>` must also unify with `'a`. Since `'a` and `'d` are currently unknown, this is fine; we simply define them both to be equal to `Result<'e, 'err>`, with the specialized type of `(>=>)` now
```
(>=>) : // (specialized)
  (Result<'e, 'err> -> Result<'e, 'err>) ->
  ('e -> Result<'f, 'err>) ->
  (Result<'e, 'err> -> Result<'f, 'err>)
```

For the second argument, we have `switch f`, which (inside the definition of `map`, where `f : 'x -> 'y`) has type `'x -> Result<'y, 'err>`. This is a much simpler match: define `'e` to be `'x` and `'f` to be `'y`.

So the final type of `id >=> switch f` is `Result<'x, 'err> -> Result<'y, 'err>`, which is exactly what the result of `map f` should be.

> This is how I would think evaluation goes, but apparently this is not it:
>
> ```
> id >=>> switch ((+) 2)
>           |
>           V
> (>=>>) id (switch ((+) 2))
>           |
>           V
>     match (id x) with
>     |    Ok o -> (switch ((+) 2)) o
>     | Error e -> Error e
> ```

No, that looks right! Keep going:
```
    match x with
    |    Ok o -> (switch ((+) 2)) o
    | Error e -> Error e
         |
         V
    match x with
    |    Ok o -> Ok (2 + o)
    | Error e -> Error e
```
which is exactly what `map ((+) 2)` should do.

Of course the reductions under the match clauses don't happen until that branch is reached, but that doesn't stop us from performing them ahead of time to show that they're equivalent to a different expression.