Skip to content

Compiler enters infinite loop when attempting SRTP with mutually recursive types #395

Closed

Description

@drvink

(Since these files are resulting in some good exercising of the compiler, I'll put them in each ticket rather than referring to other tickets.)

Functor.fs:

#light "off"
module Functor

open System

let inline konst x _ = x

module Impl = begin
  type CFunctor = class
    static member inline fmap (f : ^a -> ^b, a : ^a list) = List.map f a
    static member inline fmap (f : ^a -> ^b, a : ^a option) =
      match a with
      | None -> None
      | Some x -> Some (f x)

    // default implementation of replace
    static member inline replace< ^a, ^b, ^c, ^d, ^e when
        ^a :> CFunctor and
        (^a or ^d) : (static member fmap : (^b -> ^c) * ^d -> ^e) >
        (a, f) =
      ((^a or ^d) : (static member fmap : (^b -> ^c) * ^d -> ^e) (konst a, f))

    // call overridden replace if present
    static member inline replace< ^a, ^b, ^c when
        ^b : (static member replace : ^a * ^b -> ^c)>
        (a : ^a, f : ^b) =
      (^b : (static member replace : ^a * ^b -> ^c) (a, f))
  end

  let inline replace_instance< ^a, ^b, ^c, ^d when
      (^a or ^c) : (static member replace : ^b * ^c -> ^d)>
      (a : ^b, f : ^c) =
    ((^a or ^c) : (static member replace : ^b * ^c -> ^d) (a, f))

  let inline fmap_instance< ^a, ^b, ^c, ^d, ^e when
      (^a or ^d) : (static member fmap : (^b -> ^c) * ^d -> ^e)>
      (f : ^b -> ^c, a : ^d) =
    ((^a or ^d) : (static member fmap : (^b -> ^c) * ^d -> ^e) (f, a))
end
open Impl

let inline fmap (f : ^a -> ^b) (a : ^c) =
  fmap_instance<CFunctor, _, _, _, _> (f, a)

let inline replace (a : ^a) (f : ^b) =
  replace_instance<CFunctor, _, _, _> (a, f)

(*
type test(arg : string) = class
  member __.data = arg
  static member inline fmap (f : char -> char, a : test) = String.map f a.data
  static member inline replace (a : char, f : test) = test.fmap (konst a, f)
end

let _ =
  printfn "%A" <| fmap id [1;2;3];
  printfn "%A" <| replace 5 [1;2;3];
  printfn "%A" <| fmap ((+) 1) (Some 2);
  printfn "%A" <| replace 'q' (test("HI"))
 *)

Applicative.fs:

#light "off"
module Applicative

open System

open Functor

type CApplicative = class
  static member inline ( *>)< ^a, ^b, ^c, ^d when
    ^a :> CApplicative
    and ^b :> Impl.CFunctor
    and (^a or ^c or ^d) : (static member ( *>) : ^c * ^d -> ^d)
    and (^b or ^c) : (static member replace : ^c * ^c -> ^c)>
    (a : ^c, b : ^d) =
  ((^a or ^c or ^d) : (static member ( *>) : ^c * ^d -> ^d) (replace id a, b))
end

let inline apply_instance< ^a, ^b, ^c, ^d, ^e when
    (^a or ^c or ^d) : (static member (<*>) : (^b -> ^c) * ^d -> ^e)>
    (a, b) =
  ((^a or ^c or ^d) : (static member (<*>) : (^b -> ^c) * ^d -> ^e) (a, b))

let inline seqd1_instance< ^a, ^b, ^c when
    (^a or ^b or ^c) : (static member ( *>) : ^b * ^c -> ^c)>
    (a, b) =
  ((^a or ^b or ^c) : (static member ( *>) : ^b * ^c -> ^c) (a, b))

let inline (<*>) (f : ^a -> ^b) (a : ^c) =
  apply_instance<CApplicative, _, _, _, _> (f, a)

let inline ( *>) (a : ^a) (b : ^b) =
  seqd1_instance<CApplicative, _, _> (a, b)

Applicative.fs makes fsc, fsautocomplete, etc. infinitely loop for me.

Is there a workaround for this, short of combining the two types? [edit] I was able to get what I wanted to do to work, though the code in this post still causes an infinite loop.

Activity

  1. added this to the VS 2015 milestone on Apr 30, 2015
  2. dsyme commented on May 9, 2015

    @dsyme
    Contributor

    I'm going to close this as a duplicate of #351. I suspect (though am not certain) that the underlying causes are the same.

    In any case they should be worked on as part of the same unit of work, which will for reasons noted in that bug need to come after the F# 4.0 RTM milestone.

    I'll note this particular repro as part of that bug.

  3. gusty commented on Sep 20, 2016

    @gusty
    Contributor

    It seems that #351 which you marked as duplicate of this bug will be solved by #1530, however I tested this one and it doesn't solve it.

    So you might consider to re-open this issue.

  4. gusty commented on Sep 20, 2016

    @gusty
    Contributor

    Btw, actually after some time and some warnings it terminates, but with an error:

    c:\test\functor.fs(1,1): warning FS0062: This construct is for ML compatibility. Consider using a file with extension '.m l' or '.mli' instead. You can disable this warning by using '--mlcompatibility' or '--nowarn:62'.

    c:\test\applicative.fs(1,1): warning FS0062: This construct is for ML compatibility. Consider using a file with extension
    '.ml' or '.mli' instead. You can disable this warning by using '--mlcompatibility' or '--nowarn:62'.

    c:\test\applicative.fs(9,29): warning FS1189: Type parameters must be placed directly adjacent to the type name, e.g. "ty
    pe C<'T>", not type "C <'T>"

    c:\test\applicative.fs(15,73): warning FS0193: A type parameter is missing a constraint 'when (Impl.CFunctor or ^c) : (s
    tatic member replace : ('?9781 -> '?9781) * ^c -> ^c)'

    c:\test\applicative.fs(13,9): error FS0073: internal error: recursive class hierarchy (detected in TypeFeasiblySubsumesTy
    pe), ty1 = ^_?733283

    c:\test\applicative.fs(32,3): error FS0073: internal error: recursive class hierarchy (detected in TypeFeasiblySubsumesTy
    pe), ty1 = ^_?1612433

    Both with and without #1530

    So clearly this has less in common with #351

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

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions