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,865
    • Issues 4,865
    • List
    • Boards
    • Service Desk
    • Milestones
    • Iterations
  • Merge requests 461
    • Merge requests 461
  • 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
  • #1061
Closed
Open
Created Dec 19, 2006 by Ian Lynagh <igloo@earth.li>@trac-igloo

default class methods can be given too general a type as GHC tries to share them between instances

GHC does not accept this program:

class Baz v x where
   foo :: x -> x
   foo y = y

instance Baz Int Int

even though it does accept this one:

class Baz v x where
   foo :: x -> x
   foo y = y

instance Baz Int Int where
   foo y = y

I can't find an explicit answer in the report, but I think both should be accepted.

The tc199 test says, in a comment,

This code defines a default method with a highly dubious type,
because 'v' is not mentioned, and there are no fundeps

However, arguably the instance declaration should be accepted,
beause it's equivalent to
 instance Baz Int Int where { foo x = x }
which *does* typecheck

GHC does not actually macro-expand the instance decl.  Instead, it
defines a default method function, thus

 $dmfoo :: Baz v x => x -> x
 $dmfoo y = y

Notice that this is an ambiguous type: you can't call $dmfoo
without triggering an error.  And when you write an instance decl,
it calls the default method:

 instance Baz Int Int where foo = $dmfoo

I'd never thought of that.  You might think that we should just
*infer* the type of the default method (here forall a. a->a), but
in the presence of higher rank types etc we can't necessarily do
that.
Edited Mar 09, 2019 by Ian Lynagh <igloo@earth.li>
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information
Assignee
Assign to
Time tracking