[GHC] #14061: reflection (was: `instance Eq Dyn`)

GHC ghc-devs at haskell.org
Mon Jul 31 05:52:05 UTC 2017


#14061: reflection
-------------------------------------+-------------------------------------
        Reporter:  zaoqi             |                Owner:  (none)
            Type:  feature request   |               Status:  closed
        Priority:  normal            |            Milestone:
       Component:  Runtime System    |              Version:  8.0.2
      Resolution:  invalid           |             Keywords:
Operating System:  Unknown/Multiple  |         Architecture:
                                     |  Unknown/Multiple
 Type of failure:  None/Unknown      |            Test Case:
      Blocked By:                    |             Blocking:
 Related Tickets:                    |  Differential Rev(s):
       Wiki Page:                    |
-------------------------------------+-------------------------------------
Changes (by zaoqi):

 * component:  Compiler => Runtime System


Old description:

> {{{#!hs
> {-# LANGUAGE KindSignatures, GADTs #-}
> data Dyn :: * where
>     Dyn :: a -> Dyn
> }}}
> How to write the correct `instance Eq Dyn`, do not change `data Dyn`?
>
> {{{#!hs
> {-# LANGUAGE MultiParamTypeClasses, FlexibleInstances #-}
>
> class GEq a b where
>     geq :: a -> b -> Bool
>
> instance {-# OVERLAPPABLE #-} (Eq a) => GEq a a where
>     geq = (==)
>
> instance {-# OVERLAPPING #-} GEq a b where
>     geq _ _ = False
> }}}
> {{{#!hs
> instance Eq Dyn where
>     Dyn a == Dyn b = geq a b
> }}}
>
> {{{
> GHCi, version 8.0.2: http://www.haskell.org/ghc/  :? for help
> Prelude> :load Dyn.hs
> [1 of 1] Compiling Main             ( Dyn.hs, interpreted )
> Ok, modules loaded: Main.
> *Main> Dyn () == Dyn ()
> False
> }}}

New description:

 {{{#!hs
 {-# LANGUAGE KindSignatures, GADTs #-}
 data Dyn :: * where
     Dyn :: a -> Dyn
 }}}
 How to write the correct `instance Eq Dyn`, do not change `data Dyn`?

 {{{#!hs
 {-# LANGUAGE MultiParamTypeClasses, FlexibleInstances #-}

 class GEq a b where
     geq :: a -> b -> Bool

 instance {-# OVERLAPPABLE #-} (Eq a) => GEq a a where
     geq = (==)

 instance {-# OVERLAPPING #-} GEq a b where
     geq _ _ = False
 }}}
 {{{#!hs
 instance Eq Dyn where
     Dyn a == Dyn b = geq a b
 }}}

 {{{
 GHCi, version 8.0.2: http://www.haskell.org/ghc/  :? for help
 Prelude> :load Dyn.hs
 [1 of 1] Compiling Main             ( Dyn.hs, interpreted )
 Ok, modules loaded: Main.
 *Main> Dyn () == Dyn ()
 False
 }}}

 If Haskell supports reflections like Java,

--

-- 
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/14061#comment:3>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler


More information about the ghc-tickets mailing list