Skip to content

GitLab

  • Projects
  • Groups
  • Snippets
  • Help
    • Loading...
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
  • Sign in / Register
GHC
GHC
  • Project overview
    • Project overview
    • Details
    • Activity
    • Releases
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
    • Locked Files
  • Issues 4,245
    • Issues 4,245
    • List
    • Boards
    • Labels
    • Service Desk
    • Milestones
    • Iterations
  • Merge Requests 397
    • Merge Requests 397
  • Requirements
    • Requirements
    • List
  • CI / CD
    • CI / CD
    • Pipelines
    • Jobs
    • Schedules
  • Security & Compliance
    • Security & Compliance
    • Dependency List
    • License Compliance
  • Operations
    • Operations
    • Incidents
    • Environments
  • Analytics
    • Analytics
    • CI / CD
    • Code Review
    • Insights
    • Issue
    • Repository
    • Value Stream
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Members
    • Members
  • Collapse sidebar
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • Glasgow Haskell Compiler
  • GHCGHC
  • Issues
  • #10634

Closed
Open
Opened Jul 13, 2015 by Lemming@trac-Lemming

Type class with injective type functions

See the attached module.

$ cat TypeFunctionBijection.hs
{-# LANGUAGE TypeFamilies #-}
module TypeFunctionBijection where

import Data.Int (Int8, Int16, Int32)

type family Up a
type instance Up Int8  = Int16
type instance Up Int16 = Int32

class (Up (Down a) ~ a) => Convert a where
   type Down a
   down :: a -> Down a

instance Convert Int16 where
   type Down Int16 = Int8
   down = fromIntegral

instance Convert Int32 where
   type Down Int32 = Int16
   down = fromIntegral

x :: Int8
x = down 8
$ ghci-7.8.4 -Wall TypeFunctionBijection.hs
GHCi, version 7.8.4: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
[1 of 1] Compiling TypeFunctionBijection ( TypeFunctionBijection.hs, interpreted )
Ok, modules loaded: TypeFunctionBijection.
*TypeFunctionBijection> :q
Leaving GHCi.
$ ghci-7.10.1 -Wall TypeFunctionBijection.hs
GHCi, version 7.10.1: http://www.haskell.org/ghc/  :? for help
[1 of 1] Compiling TypeFunctionBijection ( TypeFunctionBijection.hs, interpreted )

TypeFunctionBijection.hs:24:5:
    Couldn't match expected type ‘Int8’ with actual type ‘Down a0’
    The type variable ‘a0’ is ambiguous
    In the expression: down 8
    In an equation for ‘x’: x = down 8
Failed, modules loaded: none.
Prelude> :q
Leaving GHCi.

Up to GHC-7.8.4 I could make a type function like Down a bijection by adding equality constraints to the Convert class. In GHC-7.10.1 this fails. Is this a bug or a feature?

Trac metadata
Trac field Value
Version 7.10.1
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Compiler
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
Edited Mar 10, 2019 by Lemming
Assignee
Assign to
None
Milestone
None
Assign milestone
Time tracking
None
Due date
None
Reference: ghc/ghc#10634