Skip to content

Convoluted type trips the cycle declaration checker, results in generated code that stack overflows #2940

@Ptival

Description

@Ptival

Usually, the PureScript compiler yells about this code:

instance foldableSomething :: Foldable ... where
  foldMap = ...
  foldl = foldlDefault
  foldr = foldrDefault

Instead asking you to eta-expand it to:

instance foldableSomething :: Foldable ... where
  foldMap = ...
  foldl t = foldlDefault t
  foldr t = foldrDefault t

However, the following type:

newtype Scope b f a = Scope (f (Var b (f a)))

Makes the earlier code compile without warning, but generates code that looks like:

var foldableScope = function (dictFoldable) {
    return new Data_Foldable.Foldable(function (dictMonoid) {
      // ...
    },
    Data_Foldable.foldlDefault(foldableScope(dictFoldable)),
    Data_Foldable.foldrDefault(foldableScope(dictFoldable)));
};

which results in a stack overflow as soon as any of the three methods is called.

Eta-expanding the code solves this, and generates the proper code, where the last two methods instead look like:

    }, function (t) {
        return Data_Foldable.foldlDefault(foldableScope(dictFoldable))(t);
    }, function (t) {
        return Data_Foldable.foldrDefault(foldableScope(dictFoldable))(t);
    });

The fact that the compiler does not complain in this case could potentially be a bug.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions