Skip to content

GitLab

  • Menu
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 4,862
    • Issues 4,862
    • List
    • Boards
    • Service Desk
    • Milestones
    • Iterations
  • Merge requests 455
    • Merge requests 455
  • 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 Compiler
  • GHCGHC
  • Issues
  • #20821
Closed
Open
Created Dec 14, 2021 by Sebastian Graf@sgraf812Developer

`coerce`ing a function should be possible without type applications when we can newtype derive the method

Today, I've been bitten again by the fact that I can write

{-# LANGUAGE GeneralisedNewtypeDeriving #-}
{-# LANGUAGE RoleAnnotations #-}

module Lib where

import Data.Coerce

class C a where
  emp :: a

instance C [a] where
  emp = []

newtype MyList a = MyList [a]
  deriving C
type role MyList representational -- just to be sure this isn't an issue

But I can't write

empty :: [a]
empty = []

myEmpty :: MyList a
myEmpty = coerce empty

It works if I insert the necessary -XTypeApplications. But that is so ugly! I really want it to be inferred for me, just as the deriving strategy does (and hence GHC already knows what to do):

==================== Derived instances ====================
Derived class instances:
  instance Lib.C (Lib.MyList a) where
    Lib.emp :: Lib.MyList a_ago
    Lib.emp
      = GHC.Prim.coerce
          @([a_ago]) @(Lib.MyList a_ago) (Lib.emp @([a_ago]))

This came up while I wanted to have

newtype Prio = Prio Int
newtype PrioMap a = PrioMap (IntMap a)

insertPrioMap :: Prio -> a -> PrioMap a -> PrioMap a
insertPrioMap = coerce IntMap.insert

Which also doesn't work without further elaboration.

Edited Dec 14, 2021 by Sebastian Graf
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information
Assignee
Assign to
Time tracking