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

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

Asked How to implement map using the fish (>=>, Kleisli composition) operator in F#? a couple of hours ago, and r~~'s answer blew my mind: let map f = id >=> switch f It is perfect ...

1 answer  ·  posted 9mo ago by toraritte‭  ·  edited 8mo ago by Alexei‭

#4: Post edited by user avatar Alexei‭ · 2024-04-10T15:22:44Z (8 months ago)
added relevant tag
#3: Post edited by user avatar toraritte‭ · 2024-04-03T18:02:31Z (9 months ago)
  • Asked [How to implement `map` using the fish (>=>, Kleisli composition) operator in F#?](https://software.codidact.com/posts/291227) a couple of hours ago, and [r~~'s answer blew my mind](https://software.codidact.com/posts/291227/291228#answer-291228):
  • ```
  • let map f = id >=> switch f
  • ```
  • It is perfect in its simplicity, but when I look at the type signatures, it is not supposed to work. I've been at it for almost an hour now, so I'm probably missing something fundamental how F# evaluates expressions...
  • For the record, these are the implementations of `bind`,`switch`, and `>=>`:
  • ```
  • let bind
  • ( f : 'a -> Result<'b,'c>)
  • (result : Result<'a,'c>)
  • =
  • match result with
  • | Ok o -> f o
  • | Error e -> Error e
  • let switch
  • (f : 'a -> 'b)
  • (x : 'a )
  • =
  • f x |> Ok
  • let (>=>)
  • (f : 'a -> Result<'b,'error>)
  • (g : 'b -> Result<'c,'error>)
  • =
  • f >> (bind g)
  • ```
  • My next attempt was using an alternative implementation for `>=>`:
  • ```
  • let (>=>>)
  • (f : 'a -> Result<'b,'error>)
  • (g : 'b -> Result<'c,'error>)
  • x
  • =
  • match (f x) with
  • | Ok o -> g o
  • | Error e -> Error e
  • ```
  • Here's the test invocation on `dotnet fsi`:
  • ```
  • (id >=>> (switch ((+) 2) : int -> Result<int,string>))
  • ((Ok 27) : Result<int,string>)
  • //=> Ok 29
  • ```
  • I'm already stuck at why `>=>>` does not blow up on `id` as its first argument?
  • 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
  • ```
  • ---
  • Note to future self:...
  • ```
  • (>=>>) (>=>>)
  • (f : 'a -> Result<'b,'error>) id
  • (g : 'b -> Result<'c,'error>) (Ok << ((+) 2) : int -> Result<int,string>))
  • x ((Ok 27) : Result<int,string>)
  • ```
  • Asked [How to implement `map` using the fish (>=>, Kleisli composition) operator in F#?](https://software.codidact.com/posts/291227) a couple of hours ago, and [r~~'s answer blew my mind](https://software.codidact.com/posts/291227/291228#answer-291228):
  • ```
  • let map f = id >=> switch f
  • ```
  • It is perfect in its simplicity, but when I look at the type signatures, it is not supposed to work. I've been at it for almost an hour now, so I'm probably missing something fundamental how F# evaluates expressions...
  • For the record, these are the implementations of `bind`,`switch`, and `>=>`:
  • ```
  • let bind
  • ( f : 'a -> Result<'b,'c>)
  • (result : Result<'a,'c>)
  • =
  • match result with
  • | Ok o -> f o
  • | Error e -> Error e
  • let switch
  • (f : 'a -> 'b)
  • (x : 'a )
  • =
  • f x |> Ok
  • let (>=>)
  • (f : 'a -> Result<'b,'error>)
  • (g : 'b -> Result<'c,'error>)
  • =
  • f >> (bind g)
  • ```
  • My next attempt was using an alternative implementation for `>=>`:
  • ```
  • let (>=>>)
  • (f : 'a -> Result<'b,'error>)
  • (g : 'b -> Result<'c,'error>)
  • x
  • =
  • match (f x) with
  • | Ok o -> g o
  • | Error e -> Error e
  • ```
  • Here's the test invocation on `dotnet fsi`:
  • ```
  • (id >=>> (switch ((+) 2) : int -> Result<int,string>))
  • ((Ok 27) : Result<int,string>)
  • //=> Ok 29
  • ```
  • I'm already stuck at why `>=>>` does not blow up on `id` as its first argument?
  • 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
  • ```
  • ---
  • Note to future self:...
  • ```
  • (>=>>) (>=>>)
  • (f : 'a -> Result<'b,'error>) id
  • (g : 'b -> Result<'c,'error>) (Ok << ((+) 2) : int -> Result<int,string>))
  • x ((Ok 27) : Result<int,string>)
  • ```
  • (... and make sure to convert a point-free function if it does not make sense at first.)
#2: Post edited by user avatar toraritte‭ · 2024-04-03T10:10:43Z (9 months ago)
  • Asked [How to implement `map` using the fish (>=>, Kleisli composition) operator in F#?](https://software.codidact.com/posts/291227) a couple of hours ago, and [r~~'s answer blew my mind](https://software.codidact.com/posts/291227/291228#answer-291228):
  • ```
  • let map f = id >=> switch f
  • ```
  • It is perfect in its simplicity, but when I look at the type signatures, it is not supposed to work. I've been at it for almost an hour now, so I'm probably missing something fundamental how F# evaluates expressions...
  • For the record, these are the implementations of `bind`,`switch`, and `>=>`:
  • ```
  • let bind
  • ( f : 'a -> Result<'b,'c>)
  • (result : Result<'a,'c>)
  • =
  • match result with
  • | Ok o -> f o
  • | Error e -> Error e
  • let switch
  • (f : 'a -> 'b)
  • (x : 'a )
  • =
  • f x |> Ok
  • let (>=>)
  • (f : 'a -> Result<'b,'error>)
  • (g : 'b -> Result<'c,'error>)
  • =
  • f >> (bind g)
  • ```
  • My next attempt was using an alternative implementation for `>=>`:
  • ```
  • let (>=>>)
  • (f : 'a -> Result<'b,'error>)
  • (g : 'b -> Result<'c,'error>)
  • x
  • =
  • match (f x) with
  • | Ok o -> g o
  • | Error e -> Error e
  • ```
  • Here's the test invocation on `dotnet fsi`:
  • ```
  • (id >=>> (switch ((+) 2) : int -> Result<int,string>))
  • ((Ok 27) : Result<int,string>)
  • //=> Ok 29
  • ```
  • I'm already stuck at why `>=>>` does not blow up on `id` as its first argument?
  • 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
  • ```
  • Asked [How to implement `map` using the fish (>=>, Kleisli composition) operator in F#?](https://software.codidact.com/posts/291227) a couple of hours ago, and [r~~'s answer blew my mind](https://software.codidact.com/posts/291227/291228#answer-291228):
  • ```
  • let map f = id >=> switch f
  • ```
  • It is perfect in its simplicity, but when I look at the type signatures, it is not supposed to work. I've been at it for almost an hour now, so I'm probably missing something fundamental how F# evaluates expressions...
  • For the record, these are the implementations of `bind`,`switch`, and `>=>`:
  • ```
  • let bind
  • ( f : 'a -> Result<'b,'c>)
  • (result : Result<'a,'c>)
  • =
  • match result with
  • | Ok o -> f o
  • | Error e -> Error e
  • let switch
  • (f : 'a -> 'b)
  • (x : 'a )
  • =
  • f x |> Ok
  • let (>=>)
  • (f : 'a -> Result<'b,'error>)
  • (g : 'b -> Result<'c,'error>)
  • =
  • f >> (bind g)
  • ```
  • My next attempt was using an alternative implementation for `>=>`:
  • ```
  • let (>=>>)
  • (f : 'a -> Result<'b,'error>)
  • (g : 'b -> Result<'c,'error>)
  • x
  • =
  • match (f x) with
  • | Ok o -> g o
  • | Error e -> Error e
  • ```
  • Here's the test invocation on `dotnet fsi`:
  • ```
  • (id >=>> (switch ((+) 2) : int -> Result<int,string>))
  • ((Ok 27) : Result<int,string>)
  • //=> Ok 29
  • ```
  • I'm already stuck at why `>=>>` does not blow up on `id` as its first argument?
  • 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
  • ```
  • ---
  • Note to future self:...
  • ```
  • (>=>>) (>=>>)
  • (f : 'a -> Result<'b,'error>) id
  • (g : 'b -> Result<'c,'error>) (Ok << ((+) 2) : int -> Result<int,string>))
  • x ((Ok 27) : Result<int,string>)
  • ```
#1: Initial revision by user avatar toraritte‭ · 2024-04-02T22:08:47Z (9 months ago)
Why does `let map f = id >=> switch f` work in F#?
Asked [How to implement `map` using the fish (>=>, Kleisli composition) operator in F#?](https://software.codidact.com/posts/291227) a couple of hours ago, and [r~~'s answer blew my mind](https://software.codidact.com/posts/291227/291228#answer-291228):

```
let map f = id >=> switch f
```

It is perfect in its simplicity, but when I look at the type signatures, it is not supposed to work. I've been at it for almost an hour now, so I'm probably missing something fundamental how F# evaluates expressions...

For the record, these are the implementations of `bind`,`switch`, and `>=>`:

```
let bind
    (     f : 'a -> Result<'b,'c>)
    (result :       Result<'a,'c>)
    =
    match result with 
    |    Ok o -> f o
    | Error e -> Error e

let switch
   (f : 'a -> 'b)
   (x : 'a      )
   =
   f x |> Ok

let (>=>)
    (f : 'a -> Result<'b,'error>)
    (g : 'b -> Result<'c,'error>)
    =
    f >> (bind g)
```

My next attempt was using an alternative implementation for `>=>`:

```
let (>=>>)
    (f : 'a -> Result<'b,'error>)
    (g : 'b -> Result<'c,'error>)
    x
    =
    match (f x) with
    |    Ok o -> g o
    | Error e -> Error e
```

Here's the test invocation on `dotnet fsi`:

```
(id >=>> (switch ((+) 2) : int -> Result<int,string>))
((Ok 27) : Result<int,string>)
//=> Ok 29
```

I'm already stuck at why `>=>>` does not blow up on `id` as its first argument?

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
```