Skip to content

Compiler outputs unusable type error containing stuff like ^?2863761 #9717

Closed

Description

@realvictorprm

See title

Repro steps

Try compiling this code:

type Adjazenzliste< ^Node, ^Weight when  ^Node : comparison and ^Weight : comparison and ^Weight : (static member (+) : ^Weight * ^Weight -> ^Weight) and ^Weight : (static member Zero : ^Weight)> = Map< ^Node, ( ^Node * ^Weight) seq>

type MaybeBuilder() =

    member this.Bind(x, f) = 
        match x with
        | None -> None
        | Some a -> f a

    member _.Zero() = None

    member this.Return(x) = 
        Some x
   
let maybe = new MaybeBuilder()

let inline dijkstra< ^Node, ^Weight when ^Node : comparison and ^Weight : comparison and ^Weight : (static member (+) : ^Weight * ^Weight -> ^Weight) and ^Weight : (static member Zero : ^Weight)> (startNode: ^Node) (endNode: ^Node) (graph: Adjazenzliste< ^Node, ^Weight>) =
    let nodes = graph |> Map.toSeq |> Seq.map (fun (key, _) -> key)
    let n = nodes |> Seq.length
    let initialDistances =
        nodes
        |> Seq.map(fun node -> node, None)
        |> Map.ofSeq
        |> Map.add startNode (Some LanguagePrimitives.GenericZero< ^Weight>)
    let initialPredecessors = nodes |> Seq.map(fun node -> node, None) |> Map.ofSeq

    let mutable unvisitedNodes = nodes |> Set.ofSeq
    let mutable dist = initialDistances
    let mutable prev = initialPredecessors

    while unvisitedNodes |> Set.isEmpty |> not do
        let (currentNode, nodeDistance) =
            // next unvisited node with min distance
            dist
            |> Map.toSeq
            |> Seq.filter(fun (node, _) -> unvisitedNodes |> Set.contains node)
            |> Seq.choose(fun (node, value) -> value |> Option.map(fun value -> node, value))
            |> Seq.minBy (fun (_, value) -> value)
        unvisitedNodes <- unvisitedNodes |> Set.remove currentNode

        let neighbours = graph.[currentNode]
        
        for (neighbour, weight) in neighbours do
            let alt = LanguagePrimitives.GenericZero< ^Weight> + nodeDistance 
            maybe {
                let! neighbourDistance = dist.[neighbour]
                if alt < neighbourDistance then
                    dist <- dist |> Map.add currentNode (Some alt)
                    prev <- prev |> Map.add currentNode (Some neighbour)
                    return ()
                else return ()
            } |> (function Some _ -> () | None -> failwithf "Internal error for node %A with neighbour %A" currentNode neighbour)
    
    let rec extractPath node path =
        let newPath =  seq { yield! path; yield node }
        if node = endNode then Some newPath
        else
            match prev.[node] with
            | Some pre -> extractPath pre newPath
            | None -> None
    extractPath startNode Seq.empty

Expected behavior

Should compile or print out a better error message

Actual behavior

Compiler spits out:

error FS0001: A type parameter is missing a constraint 'when ( ^Weight or  ^?2863761) : (static member ( + ) :  ^Weight *  ^?2863761 ->  ^?2863762)'

Known workarounds

None so far

Related information

Also fails in sharplab.

Provide any related information (optional):

  • Operating system: Windows 10
  • .NET Runtime kind (.NET Core, .NET Framework, Mono): .NET 5.0.100-preview.3.20216.6

PS:

Please ignore how bad my code is 😂

Activity

  1. abelbraaksma commented on Jul 19, 2020

    @abelbraaksma
    Contributor

    That reminds me of this, basically the same situation, I think: #3508.

  2. realvictorprm commented on Jul 19, 2020

    @realvictorprm
    ContributorAuthor

    Side note, if you replace the generic part with e.g. int it compiles fine.

  3. realvictorprm commented on Jul 19, 2020

    @realvictorprm
    ContributorAuthor

    I somehow have the feeling that this stuff is related to basic members which are expressed all over via SRTP (like get_Zero and op_Add) as these things are the only special thing I'm doing here.

  4. cartermp commented on Jul 20, 2020

    @cartermp
    Contributor

    There are other issues tracking this so I'll mark it as a duplicate but link it up with #9633 for traceability

  5. abelbraaksma commented on May 16, 2024

    @abelbraaksma
    Contributor

    Just chiming in after 4 years: I can confirm that this behavior has since been resolved in public releases of F#. The code of the OP does not show errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions