[GHC] #11174: Traversable can't be derived for datatypes with unboxed arguments

#11174: Traversable can't be derived for datatypes with unboxed arguments -------------------------------------+------------------------------------- Reporter: RyanGlScott | Owner: RyanGlScott Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.10.2 Keywords: | Operating System: Unknown/Multiple Architecture: | Type of failure: GHC rejects Unknown/Multiple | valid program Test Case: | Blocked By: Blocking: | Related Tickets: Differential Rev(s): | Wiki Page: -------------------------------------+------------------------------------- Unlike `Functor` and `Foldable`, `Traversable` cannot be derived for datatypes that contain arguments with unboxed types. A simple example: {{{#!hs {-# LANGUAGE DeriveTraversable, MagicHash #-} import GHC.Prim (Int#) data IntHash a = IntHash Int# deriving (Functor, Foldable, Traversable) }}} The generated `Traversable` instance reveals the issue: {{{#!hs instance Traversable IntHash where traverse f (IntHash a1) = fmap IntHash (pure a1) }}} {{{ Couldn't match kind `*' with `#' When matching types a0 :: * Int# :: # Expected type: a0 -> IntHash b Actual type: Int# -> IntHash b In the first argument of `fmap', namely `IntHash' In the expression: fmap IntHash (pure a1) When typechecking the code for `traverse' in a derived instance for `Traversable IntHash': To see the code I am typechecking, use -ddump-deriv }}} We have to avoid calling `pure` on `a1`, since `pure` expects an argument with a `*`-kinded type, not a `#`-kinded one. One way to fix this would be restructuring the derived `traverse` implementation such that unboxed arguments are moved to the function initially lifted with `pure`, and doing nothing with them later. To better articulate what I mean, envision something like this: {{{#!hs data IntHash2 a = IntHash2 Int# a (IntHash2 a) Int# deriving (Functor, Foldable) }}} Then a derived `Traversable` instance that would typecheck would be: {{{#!hs instance Traversable IntHash2 where traverse f (IntHash2 a1 a2 a3 a4) = pure (\x2 x3 -> IntHash2 a1 x2 x3 a4) <*> f a2 <*> traverse f a3 }}} Conceptually, this doesn't sound hard to implement. The tricky part is figuring out how much of the existing `Functor`/`Foldable`/`Traversable` deriving machinery would need to be tweaked to make this work. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/11174 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#11174: Traversable can't be derived for datatypes with unboxed arguments -------------------------------------+------------------------------------- Reporter: RyanGlScott | Owner: RyanGlScott Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.10.2 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: Type of failure: GHC rejects | Unknown/Multiple valid program | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Description changed by RyanGlScott: Old description:
Unlike `Functor` and `Foldable`, `Traversable` cannot be derived for datatypes that contain arguments with unboxed types. A simple example:
{{{#!hs {-# LANGUAGE DeriveTraversable, MagicHash #-}
import GHC.Prim (Int#)
data IntHash a = IntHash Int# deriving (Functor, Foldable, Traversable) }}}
The generated `Traversable` instance reveals the issue:
{{{#!hs instance Traversable IntHash where traverse f (IntHash a1) = fmap IntHash (pure a1) }}}
{{{ Couldn't match kind `*' with `#' When matching types a0 :: * Int# :: # Expected type: a0 -> IntHash b Actual type: Int# -> IntHash b In the first argument of `fmap', namely `IntHash' In the expression: fmap IntHash (pure a1) When typechecking the code for `traverse' in a derived instance for `Traversable IntHash': To see the code I am typechecking, use -ddump-deriv }}}
We have to avoid calling `pure` on `a1`, since `pure` expects an argument with a `*`-kinded type, not a `#`-kinded one.
One way to fix this would be restructuring the derived `traverse` implementation such that unboxed arguments are moved to the function initially lifted with `pure`, and doing nothing with them later. To better articulate what I mean, envision something like this:
{{{#!hs data IntHash2 a = IntHash2 Int# a (IntHash2 a) Int# deriving (Functor, Foldable) }}}
Then a derived `Traversable` instance that would typecheck would be:
{{{#!hs instance Traversable IntHash2 where traverse f (IntHash2 a1 a2 a3 a4) = pure (\x2 x3 -> IntHash2 a1 x2 x3 a4) <*> f a2 <*> traverse f a3 }}}
Conceptually, this doesn't sound hard to implement. The tricky part is figuring out how much of the existing `Functor`/`Foldable`/`Traversable` deriving machinery would need to be tweaked to make this work.
New description: Unlike `Functor` and `Foldable`, `Traversable` cannot be derived for datatypes that contain arguments with unboxed types. A simple example: {{{#!hs {-# LANGUAGE DeriveTraversable, MagicHash #-} import GHC.Prim (Int#) data IntHash a = IntHash Int# deriving (Functor, Foldable, Traversable) }}} The generated `Traversable` instance reveals the issue: {{{#!hs instance Traversable IntHash where traverse f (IntHash a1) = fmap IntHash (pure a1) }}} {{{ Couldn't match kind `*' with `#' When matching types a0 :: * Int# :: # Expected type: a0 -> IntHash b Actual type: Int# -> IntHash b In the first argument of `fmap', namely `IntHash' In the expression: fmap IntHash (pure a1) When typechecking the code for `traverse' in a derived instance for `Traversable IntHash': To see the code I am typechecking, use -ddump-deriv }}} We have to avoid calling `pure` on `a1`, since `pure` expects an argument with a `*`-kinded type, not a `#`-kinded one. One way to fix this would be restructuring the derived `traverse` implementation such that arguments which do not mention the last type parameter are moved to the function initially lifted with `pure`, and doing nothing with them later. To better articulate what I mean, envision something like this: {{{#!hs data IntHash2 a = IntHash2 Int# a (IntHash2 a) Int deriving (Functor, Foldable) }}} Then a derived `Traversable` instance that would type-check (and kind- check) would be: {{{#!hs instance Traversable IntHash2 where traverse f (IntHash2 a1 a2 a3 a4) = pure (\x2 x3 -> IntHash2 a1 x2 x3 a4) <*> f a2 <*> traverse f a3 }}} Conceptually, this doesn't sound hard to implement. The tricky part is figuring out how much of the existing `Functor`/`Foldable`/`Traversable` deriving machinery would need to be tweaked to make this work. -- -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/11174#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#11174: Traversable can't be derived for datatypes with unboxed arguments -------------------------------------+------------------------------------- Reporter: RyanGlScott | Owner: RyanGlScott Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.10.2 Resolution: | Keywords: Generics Operating System: Unknown/Multiple | Architecture: Type of failure: GHC rejects | Unknown/Multiple valid program | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Changes (by simonpj): * keywords: => Generics -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/11174#comment:2 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#11174: Traversable can't be derived for datatypes with unboxed arguments -------------------------------------+------------------------------------- Reporter: RyanGlScott | Owner: RyanGlScott Type: bug | Status: patch Priority: normal | Milestone: Component: Compiler | Version: 7.10.2 Resolution: | Keywords: Generics Operating System: Unknown/Multiple | Architecture: Type of failure: GHC rejects | Unknown/Multiple valid program | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Phab:D1908 Wiki Page: | -------------------------------------+------------------------------------- Changes (by RyanGlScott): * status: new => patch * differential: => Phab:D1908 Comment: I've uploaded a Phab:D1908, based on the design philosophy outlined in [https://ghc.haskell.org/trac/ghc/wiki/Commentary/Compiler/DeriveFunctor#Prop... this wiki page]. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/11174#comment:3 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#11174: Traversable can't be derived for datatypes with unboxed arguments -------------------------------------+------------------------------------- Reporter: RyanGlScott | Owner: RyanGlScott Type: bug | Status: closed Priority: normal | Milestone: 8.2.1 Component: Compiler | Version: 7.10.2 Resolution: fixed | Keywords: Generics Operating System: Unknown/Multiple | Architecture: Type of failure: GHC rejects | Unknown/Multiple valid program | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Phab:D1908 Wiki Page: | -------------------------------------+------------------------------------- Changes (by bgamari): * status: patch => closed * resolution: => fixed * milestone: => 8.2.1 Comment: Thanks RyanGlScott! -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/11174#comment:4 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#11174: Traversable can't be derived for datatypes with unboxed arguments
-------------------------------------+-------------------------------------
Reporter: RyanGlScott | Owner: RyanGlScott
Type: bug | Status: closed
Priority: normal | Milestone: 8.2.1
Component: Compiler | Version: 7.10.2
Resolution: fixed | Keywords: Generics
Operating System: Unknown/Multiple | Architecture:
Type of failure: GHC rejects | Unknown/Multiple
valid program | Test Case:
Blocked By: | Blocking:
Related Tickets: | Differential Rev(s): Phab:D1908
Wiki Page: |
-------------------------------------+-------------------------------------
Comment (by Ben Gamari

#11174: Traversable can't be derived for datatypes with unboxed arguments -------------------------------------+------------------------------------- Reporter: RyanGlScott | Owner: RyanGlScott Type: bug | Status: closed Priority: normal | Milestone: 8.0.1 Component: Compiler | Version: 7.10.2 Resolution: fixed | Keywords: Generics Operating System: Unknown/Multiple | Architecture: Type of failure: GHC rejects | Unknown/Multiple valid program | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Phab:D1908 Wiki Page: | -------------------------------------+------------------------------------- Changes (by bgamari): * milestone: 8.2.1 => 8.0.1 Comment: I went ahead and merged this to `ghc-8.0`. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/11174#comment:6 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#11174: Traversable can't be derived for datatypes with unboxed arguments -------------------------------------+------------------------------------- Reporter: RyanGlScott | Owner: RyanGlScott Type: bug | Status: closed Priority: normal | Milestone: 8.0.1 Component: Compiler | Version: 7.10.2 Resolution: fixed | Keywords: deriving Operating System: Unknown/Multiple | Architecture: Type of failure: GHC rejects | Unknown/Multiple valid program | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Phab:D1908 Wiki Page: | -------------------------------------+------------------------------------- Changes (by RyanGlScott): * keywords: Generics => deriving -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/11174#comment:7 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC