Opened 13 months ago

Closed 9 months ago

#14556 closed bug (fixed)

Core Lint error: Ill-kinded result in coercion

Reported by: Iceland_jack Owned by:
Priority: normal Milestone:
Component: Compiler Version: 8.3
Keywords: TypeInType Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case: dependent/should_compile/T14556
Blocked By: Blocking:
Related Tickets: #14554 Differential Rev(s):
Wiki Page:

Description

{-# Language UndecidableInstances, DataKinds, TypeOperators, KindSignatures, PolyKinds, TypeInType, TypeFamilies, GADTs, LambdaCase, ScopedTypeVariables #-}

import Data.Kind
import Data.Proxy

data Fn a b where
  IdSym :: Fn Type Type

type family
  (@@) (f::Fn k k') (a::k)::k' where
  IdSym @@ a = a

data KIND = X | FNARR KIND KIND

data TY :: KIND -> Type where
  ID    :: TY (FNARR X X)
  FNAPP :: TY (FNARR k k') -> TY k -> TY k'

data TyRep (kind::KIND) :: TY kind -> Type where
  TID    :: TyRep (FNARR X X)  ID
  TFnApp :: TyRep (FNARR k k') f
         -> TyRep k            a
         -> TyRep k'           (FNAPP f a)

type family
  IK (kind::KIND) :: Type where
  IK X            = Type
  IK (FNARR k k') = Fn (IK k) (IK k')

type family
  IT (ty::TY kind) :: IK kind where
  IT ID          = IdSym
  IT (FNAPP f x) = IT f @@ IT x

zero :: TyRep X a -> IT a
zero = undefined 

which gives Core lint error when run with ghci -ignore-dot-ghci -dcore-lint (8.3.20171122) attached.

It compiles fine with

zero :: TyRep X a -> IT a
zero = zero

but fails with zero = let x = x in x. See #14554.

Attachments (1)

bug.log (8.2 KB) - added by Iceland_jack 13 months ago.

Download all attachments as: .zip

Change History (4)

Changed 13 months ago by Iceland_jack

Attachment: bug.log added

comment:1 Changed 12 months ago by simonpj

I think this is another example of the bugs surrounding "flattened types have flattened kinds". What is happening is this. In the RHS of zero we get a constraint

    [W} (alpha :: TYPE r)  ~  TyRep X a -> (IT a |> ax_IK)
where
    ax_IK :: IK X ~ Type   -- Just an axiom for IK

which should strainghtforwardly unify. But before we do that we flatten (IT a |> ax_IK), but alas we get just

     (fuv :: IK X)
where
     fuv = IT a     -- CFunEqCan

The cast has disappeared. This happens in TcFlatten:

flatten_one (CastTy ty g)

I'm pretty sure it's just another manifestation of our existing set of bugs around flattening (Trac #12919). Richard has a mostly-finished patch that cures this --- it'd be great to get it finished!

comment:2 Changed 9 months ago by Richard Eisenberg <rae@…>

In e3dbb44f/ghc:

Fix #12919 by making the flattener homegeneous.

This changes a key invariant of the flattener. Previously,
flattening a type meant flattening its kind as well. But now,
flattening is always homogeneous -- that is, the kind of the
flattened type is the same as the kind of the input type.
This is achieved by various wizardry in the TcFlatten.flatten_many
function, as described in Note [flatten_many].

There are several knock-on effects, including some refactoring
in the canonicalizer to take proper advantage of the flattener's
changed behavior. In particular, the tyvar case of can_eq_nc' no
longer needs to take casts into account.

Another effect is that flattening a tyconapp might change it
into a casted tyconapp. This might happen if the result kind
of the tycon contains a variable, and that variable changes
during flattening. Because the flattener is homogeneous, it tacks
on a cast to keep the tyconapp kind the same. However, this
is problematic when flattening CFunEqCans, which need to have
an uncasted tyconapp on the LHS and must remain homogeneous.
The solution is a more involved canCFunEqCan, described in
Note [canCFunEqCan].

This patch fixes #13643 (as tested in typecheck/should_compile/T13643)
and the panic in typecheck/should_compile/T13822 (as reported in #14024).
Actually, there were two bugs in T13822: the first was just some
incorrect logic in tryFill (part of the unflattener) -- also fixed
in this patch -- and the other was the main bug fixed in this ticket.

The changes in this patch exposed a long-standing flaw in OptCoercion,
in that breaking apart an AppCo sometimes has unexpected effects on
kinds. See new Note [EtaAppCo] in OptCoercion, which explains the
problem and fix.

Also here is a reversion of the major change in
09bf135ace55ce2572bf4168124d631e386c64bb, affecting ctEvCoercion.
It turns out that making the flattener homogeneous changes the
invariants on the algorithm, making the change in that patch
no longer necessary.

This patch also fixes:
  #14038 (dependent/should_compile/T14038)
  #13910 (dependent/should_compile/T13910)
  #13938 (dependent/should_compile/T13938)
  #14441 (typecheck/should_compile/T14441)
  #14556 (dependent/should_compile/T14556)
  #14720 (dependent/should_compile/T14720)
  #14749 (typecheck/should_compile/T14749)

Sadly, this patch negatively affects performance of type-family-
heavy code. The following patch fixes these performance degradations.
However, the performance fixes are somewhat invasive and so I've
kept them as a separate patch, labeling this one as [skip ci] so
that validation doesn't fail on the performance cases.

comment:3 Changed 9 months ago by goldfire

Resolution: fixed
Status: newclosed
Test Case: dependent/should_compile/T14556
Note: See TracTickets for help on using tickets.