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,859
    • Issues 4,859
    • List
    • Boards
    • Service Desk
    • Milestones
    • Iterations
  • Merge requests 454
    • Merge requests 454
  • 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
  • #20815
Closed
Open
Created Dec 13, 2021 by Icelandjack@IcelandjackReporter

Make coerce-derived dictionaries coercible

Take the Alt type class as an example, which is Alternative without the empty method:

{-# Language ConstrainedClassMethods #-}
{-# Language DerivingVia             #-}

import Control.Applicative

class Functor f => Alt f where
  (<!>) :: f a -> f a -> f a

  some :: Applicative f => f a -> f [a]
  some v = some_v
    where many_v = some_v <!> pure []
          some_v = (:) <$> v <*> many_v

  many :: Applicative f => f a -> f [a]
  many v = many_v
    where many_v = some_v <!> pure []
          some_v = (:) <$> v <*> many_v

The applicative constraints on the methods means we cannot derive Alt.

If we try to derive it (say via []) GHC fails to coerce between Applicative [] and Applicative List.

instance Alt [] where
  (<!>) = (<>)

newtype List a = List [a] 
  -- • Couldn't match type ‘[]’ with ‘List’
  --     arising from the coercion of the method ‘many’
  --       from type ‘forall a. Applicative [] => [a] -> [[a]]’
  --         to type ‘forall a. Applicative List => List a -> List [a]’
  -- • When deriving the instance for (Alt List)
  --
  -- .. same for ‘some’
  deriving (Functor, Applicative, Alt)
  via []

GHC can make use of the provenence of the Applicative instance: we know that that Applicative List was derived via Applicative [] using coerce-based deriving (either newtype, via) and so the dictionaries can be considered Coercible (?). If not I'm curious if this fails.¹

This is not the first time I want this (I forgot in what context though), let me know if it's sensible/useful.


¹ Instead of coercing method-by-method could coerce-based deriving coerce the whole dictionary?

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information
Assignee
Assign to
Time tracking