Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
  • Sign in / Register
  • GHC GHC
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
    • Locked Files
  • Issues 5,252
    • Issues 5,252
    • List
    • Boards
    • Service Desk
    • Milestones
    • Iterations
  • Merge requests 577
    • Merge requests 577
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
    • Test Cases
  • Deployments
    • Deployments
    • Releases
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Code review
    • Insights
    • Issue
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Glasgow Haskell CompilerGlasgow Haskell Compiler
  • GHCGHC
  • Issues
  • #10805
Closed
Open
Issue created Aug 28, 2015 by htebalaka@trac-htebalaka

Could not deduce (a ~ b) implies (f a ~ f b), because a type function may not be injective

I don't know if the summary I provided is what's really going on here, but I'm a little baffled why this doesn't typecheck. I can't see what injectivity has to do with it, since we're not trying to deduce that the arguments are the same from the result of an application being the same; rather we're doing the opposite. I've encountered plenty of obvious arithmetic laws that I've needed to unsafeCoerceConstraint away with doing type level arithmetic, but this seems like it should be trivial regardless.

{-# LANGUAGE GADTs, ExplicitNamespaces, TypeOperators, DataKinds #-}
{-# LANGUAGE ConstraintKinds, KindSignatures, PatternGuards #-}

import Data.Type.Equality ((:~:)(..))
import GHC.TypeLits (Nat, type (<=), type (-), type (+), type (<=?))

data SNat :: Nat -> * where
    SSucc   :: SNat a -> SNat (a + 1)
    SZero   :: SNat 0

heqHet :: SNat a -> SNat b -> Maybe (a :~: b)
heqHet SZero SZero = Just Refl
heqHet (SSucc a) (SSucc b)
    | Just Refl <- heqHet a b = Just Refl
heqHet _ _ = Nothing

data Slice :: Nat -> Nat -> * where
    Slice   :: ((start + 1) <= end, end <= numElements)
            => SNat numElements -> SNat start -> SNat end
            -> Slice numElements (end - start)

heqHet' :: Slice a b -> Slice a b' -> Bool
heqHet' (Slice _ start end) (Slice _ start' end')
    | Just _ <- heqHet start start'
    , Just _ <- heqHet end end' = True
    | otherwise = False
Slice.hs:24:10:
    Could not deduce (((start + 1) <=? a) ~ ((start + 1) <=? a))
    from the context (b ~ (end - start), (start + 1) <= end, end <= a)
      bound by a pattern with constructor
                 Slice :: forall (numElements :: Nat) (start :: Nat) (end :: Nat).
                          ((start + 1) <= end, end <= numElements) =>
                          SNat numElements
                          -> SNat start -> SNat end -> Slice numElements (end - start),
               in an equation for ‘heqHet'’
      at Slice.hs:24:10-26
    or from (b' ~ (end1 - start1), (start1 + 1) <= end1, end1 <= a)
      bound by a pattern with constructor
                 Slice :: forall (numElements :: Nat) (start :: Nat) (end :: Nat).
                          ((start + 1) <= end, end <= numElements) =>
                          SNat numElements
                          -> SNat start -> SNat end -> Slice numElements (end - start),
               in an equation for ‘heqHet'’
      at Slice.hs:24:30-48
    NB: ‘GHC.TypeLits.<=?’ is a type function, and may not be injective
    Relevant bindings include
      heqHet' :: Slice a b -> Slice a b' -> Bool (bound at Slice.hs:24:1)
    In the pattern: Slice _ start end
    In an equation for ‘heqHet'’:
        heqHet' (Slice _ start end) (Slice _ start' end')
          | Just _ <- heqHet start start', Just _ <- heqHet end end' = True
          | otherwise = False
Failed, modules loaded: none.
Trac metadata
Trac field Value
Version 7.10.2
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Compiler (Type checker)
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information
Assignee
Assign to
Time tracking