[GHC] #14061: `instance Eq Dyn`

#14061: `instance Eq Dyn` -------------------------------------+------------------------------------- Reporter: zaoqi | Owner: (none) Type: feature | Status: new request | Priority: normal | Milestone: Component: Compiler | Version: 8.0.2 Keywords: | Operating System: Unknown/Multiple Architecture: | Type of failure: None/Unknown Unknown/Multiple | Test Case: | Blocked By: Blocking: | Related Tickets: Differential Rev(s): | Wiki Page: -------------------------------------+------------------------------------- {{{#!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 }}} -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14061 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14061: `instance Eq Dyn` -------------------------------------+------------------------------------- Reporter: zaoqi | Owner: (none) Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.0.2 Resolution: | 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: | -------------------------------------+------------------------------------- Comment (by AntC): Repeat of ticket #14004. The answer is already posted there. You can't make `instance Eq Dyn` with that definition for `Dyn`. But there are two suggestions for different approaches. This is not a bug; not a feature request. @zaoqi please do not use Trac as a help forum: use StackOverflow or reddit or haskell cafe. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14061#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14061: `instance Eq Dyn` -------------------------------------+------------------------------------- Reporter: zaoqi | Owner: (none) Type: feature request | Status: closed Priority: normal | Milestone: Component: Compiler | 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 RyanGlScott): * status: new => closed * resolution: => invalid -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14061#comment:2 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#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

#14061: reflection -------------------------------------+------------------------------------- Reporter: zaoqi | Owner: (none) Type: feature request | Status: new Priority: normal | Milestone: Component: Runtime System | Version: 8.0.2 Resolution: | 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): * status: closed => new * resolution: invalid => -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14061#comment:4 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14061: reflection -------------------------------------+------------------------------------- Reporter: zaoqi | Owner: (none) Type: feature request | Status: new Priority: normal | Milestone: Component: Runtime System | Version: 8.0.2 Resolution: | 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: | -------------------------------------+------------------------------------- Comment (by AntC): Replying to [comment:3 zaoqi]:
Reflection
The approach in ticket:14004#comment:8 uses reflection. In the code, the line: {{{ | Just HRefl <- ra `eqTypeRep` rb = a == b }}} For `Just HRefl`, the `Refl` is reflection. (Defined in base library `Data.Type.Equality`) This is advanced Haskell. It is very unlikely you need this. Your questions are at beginner level, so it would not help you to just copy the code. You have not explained why you cannot change the definition of `Dyn`. I have put out a request for some Chinese speaking Haskellers to try to help you. I hope they will contact you soon. (Apologies to the GHC team in advance: you might see Chinese text on this ticket.) -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14061#comment:5 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14061: reflection -------------------------------------+------------------------------------- Reporter: zaoqi | Owner: (none) Type: feature request | Status: new Priority: normal | Milestone: Component: Runtime System | Version: 8.0.2 Resolution: | 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: | -------------------------------------+------------------------------------- Comment (by bgamari): Quite alright! I wish I could do more to help but my Chinese skills are quite non-existent. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14061#comment:6 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14061: reflection -------------------------------------+------------------------------------- Reporter: zaoqi | Owner: (none) Type: feature request | Status: new Priority: normal | Milestone: Component: Runtime System | Version: 8.0.2 Resolution: | 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: | -------------------------------------+------------------------------------- Comment (by winter): 对于帮助类的问题,还请使用 Haskell-cafe 邮件组(对于初学者相当友好)。或者 去知乎、segmentfault这样的网站(尤其是中文类的问题),Ghc trac主要是用来反 馈 bug 或者提出特性需求的,所以如果你还是认为 GHC 存在 bug 的话,请解释你 希望你的程序做什么,仅仅贴出代码的话我们很难给出解释,于是你也会很难理解。 -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14061#comment:7 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14061: reflection -------------------------------------+------------------------------------- Reporter: zaoqi | Owner: (none) Type: feature request | Status: new Priority: normal | Milestone: Component: Runtime System | Version: 8.0.2 Resolution: | 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: | -------------------------------------+------------------------------------- Comment (by zaoqi): Replying to [comment:7 winter]:
对于帮助类的问题,还请使用 Haskell-cafe 邮件组(对于初学者相当友好)。或 者去知乎、segmentfault这样的网站(尤其是中文类的问题),Ghc trac主要是用来 反馈 bug 或者提出特性需求的,所以如果你还是认为 GHC 存在 bug 的话,请解释 你希望你的程序做什么,仅仅贴出代码的话我们很难给出解释,于是你也会很难理解 。 我需要能判断是否有`instance Eq a`,在运行时。
-- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14061#comment:8 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14061: reflection -------------------------------------+------------------------------------- Reporter: zaoqi | Owner: (none) Type: feature request | Status: new Priority: normal | Milestone: Component: Runtime System | Version: 8.0.2 Resolution: | 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: | -------------------------------------+------------------------------------- Comment (by AntC): Replying to [comment:8 zaoqi]:
我需要能判断是否有`instance Eq a`,在运行时。
Google translate: I need to be able to judge whether there is `instance Eq a` at run time
No: '''You''' must explain why you refuse to change the definition of `Dyn`. Google translate: 否:你必須解釋為什麼你拒絕改變 `Dyn` 的定義。 -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14061#comment:9 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14061: reflection -------------------------------------+------------------------------------- Reporter: zaoqi | Owner: (none) Type: feature request | Status: new Priority: normal | Milestone: Component: Runtime System | Version: 8.0.2 Resolution: | 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: | -------------------------------------+------------------------------------- Comment (by zaoqi): Replying to [comment:9 AntC]:
Replying to [comment:8 zaoqi]:
我需要能判断是否有`instance Eq a`,在运行时。
Google translate: I need to be able to judge whether there is `instance Eq a` at run time
No: '''You''' must explain why you refuse to change the definition of `Dyn`.
Google translate: 否:你必須解釋為什麼你拒絕改變 `Dyn` 的定義。 instance Show Dyn
instance Enum Dyn instance Num Dyn ...... -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14061#comment:10 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC