question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Issue utilising Free.fold

See original GitHub issue

Description

I’m trying to use Free.fold to write an interpreter for my free monad, but I’m struggling to satisfy the type checker with my implementation of the mapping function f. It’s entirely possible that there is a problem with my code, but oddly I can satisfy the compiler (and run the code without errors) if I copy the definition of Free.fold into my project and remove all type hints from the signature.

Repro steps

I have a DSL and associated workflow like so:

open FSharpPlus
open FSharpPlus.Data

type FooId = FooId of string

type Foo = { Id: FooId }

module GetFoo =
    type Error = NotFound

    type Instruction<'next> =
        | Read of (FooId * (Foo option -> 'next))
        static member Map(instruction, f: 'a -> 'b) =
            match instruction with
            | Read (id, next) -> Read(id, next >> f)

    type Program<'a> = Free<Instruction<'a>, 'a>

    let read fooId = Read(fooId, id) |> Free.liftF

    type Request = { Id: FooId }
    type Response = Result<Foo, Error>

    let handle request: Program<Response> =
        monad {
            let! foo = read request.Id
            return foo |> Option.toResultWith NotFound
        }

Then, to keep things simple, in my tests I would like to write an interpreter which targets the identity monad. So I have written:

let interpreter: Program<Response> -> Response =
    Free.fold (function
        | Read (fooId, next) -> monad { return Some({Id = fooId}) |> next })
    >> Identity.run

Expected behavior

I would expect this to compile

Actual behavior

But it produces the following compiler error:

Type mismatch. Expecting a
    'Program<Response> -> 'a'    
but given a
    'Free<Instruction<Response>,Response> -> 'b'    
Type mismatch. Expecting a
    'Instruction<Response>'    
but given a
    'Instruction<Free<Instruction<Response>,Response>>'    
The type 'Response' does not match the type 'Free<Instruction<Response>,Response>'

Am I missing a type hint somewhere, or is my implementation of the mapping function from Instruction to Identity monad fundamentally wrong somehow?

Known workarounds

Currently I can get this to compile by redefining fold without the type hints in the signature:

open FSharpPlus
open FSharpPlus.Data

[<RequireQualifiedAccess>]
module Free =
    let inline fold2 f x =
        let rec loop f x =
            match Free.run x with
            | Pure a -> monad { return a }
            | Roll x -> f x >>= loop f

        loop f x

And then switching the above implementation of the interpreter to use Free.fold2.

Related information

  • Operating system - Mac OSX
  • FSharpPlus Version - 1.1.5
  • .NET Runtime - .NET 5.0
  • F# lang version - 5

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
Choc13commented, Dec 2, 2020

@gusty thanks for the tip regarding result x instead of monad { return x }.

0reactions
gustycommented, Dec 2, 2020

Fixed by #406

Read more comments on GitHub >

github_iconTop Results From Across the Web

Common Samsung Galaxy Z Fold 3 problems and how to ...
Here's our helpful rundown of common Galaxy Z Fold 3 problems and how ... Z Fold 3 is, it may not be entirely...
Read more >
7 foldable phone problems that haven't been fixed yet
But we're definitely holding out for a crease-free future across all foldables. Lack of dust resistance. Samsung Galaxy Z Flip 4 with water ......
Read more >
5 problems that could keep the Galaxy Fold from true success
There are five problems the Galaxy Fold is facing that have the potential to get in the way of it being a success...
Read more >
Samsung Galaxy Fold Screen Has a Creasing Issue ...
The company is reportedly considering offering free screen replacements to Galaxy Fold buyers after the device launches.
Read more >
Above the Fold vs. Below the Fold: Does it Still Matter in ...
The above-the-fold concept is always a relevant topic in UX design. Encourage visitors to convert with the most important info at the top....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found