[GHC] #15078: base: Customary type class laws (e.g. for Eq) and non-abiding instances (e.g. Float) should be documented

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

[GHC] #15078: base: Customary type class laws (e.g. for Eq) and non-abiding instances (e.g. Float) should be documented

GHC - devs mailing list
#15078: base: Customary type class laws (e.g. for Eq) and non-abiding instances
(e.g. Float) should be documented
-------------------------------------+-------------------------------------
           Reporter:  sjakobi        |             Owner:  (none)
               Type:  feature        |            Status:  new
  request                            |
           Priority:  normal         |         Milestone:  8.6.1
          Component:  Core           |           Version:  8.4.2
  Libraries                          |
           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:
-------------------------------------+-------------------------------------
 As beginning Haskellers regularly ask about these laws and instances I
 think it would be good to have them documented where they are defined.

 Here's a first draft of what I have in mind for `Float`'s `Eq` and `Ord`
 instances:

 {{{
 -- | Note that in the presence of @NaN@, this instance does not satisfy
 -- reflexivity:
 --
 -- >>> nan = 0/0 :: Float
 -- >>> nan == nan
 -- False
 --
 -- Also note that this instance does not encode structural equality:
 --
 -- >>> 0 == (-0 :: Float)
 -- True
 -- >>> recip 0 == recip (-0 :: Float)
 -- False
 instance Eq Float where
     (==) = eqFloat
 }}}

 {{{
 -- | Due to the peculiarities of @NaN@, this instance does not satisfy
 totality:
 --
 -- >>> nan = 0/0 :: Float
 -- >>> nan <= nan
 -- False
 --
 -- Another special case with @NaN@ is:
 --
 -- @
 -- 'compare' x y
 --   | 'isNaN' x || 'isNaN' y = 'GT'
 -- @
 --
 -- However
 --
 -- @
 -- nan > _ = False
 -- _ > nan = False
 -- @
 --
 -- In consequence we also have:
 --
 -- @
 -- 'max' x y | 'isNaN' x || 'isNaN' y = x
 -- 'min' x y | 'isNaN' x || 'isNaN' y = y
 -- @
 --
 -- Ignoring @NaN@, @Infinity@ and @-Infinity@ are the respective greatest
 -- and least elements of 'Float'.
 instance Ord Float where
     (F# x) `compare` (F# y)
         = if      isTrue# (x `ltFloat#` y) then LT
           else if isTrue# (x `eqFloat#` y) then EQ
           else                                  GT

     (F# x) <  (F# y) = isTrue# (x `ltFloat#` y)
     (F# x) <= (F# y) = isTrue# (x `leFloat#` y)
     (F# x) >= (F# y) = isTrue# (x `geFloat#` y)
     (F# x) >  (F# y) = isTrue# (x `gtFloat#` y)
 }}}

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

_______________________________________________
ghc-tickets mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-tickets
Reply | Threaded
Open this post in threaded view
|

Re: [GHC] #15078: base: Customary type class laws (e.g. for Eq) and non-abiding instances (e.g. Float) should be documented

GHC - devs mailing list
#15078: base: Customary type class laws (e.g. for Eq) and non-abiding instances
(e.g. Float) should be documented
-------------------------------------+-------------------------------------
        Reporter:  sjakobi           |                Owner:  (none)
            Type:  feature request   |               Status:  new
        Priority:  normal            |            Milestone:  8.6.1
       Component:  Core Libraries    |              Version:  8.4.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 dfeuer):

 That's perfectly documentation that Haddock will, sadly, chew up and turn
 into something almost completely unreadable. Ryan Scott thinks it's good
 to write documentation like that to drive Haddock development, but I'm not
 sure. As for what the customary laws ''are'', here's a rough draft:

 == `Eq`

 `Eq` is expected to be an equivalence relation, and values that compare
 "equal" should be indistinguishable (except perhaps when using an
 "private" interface).

  Reflexivity:: `x == x  =  True`
  Symmetry:: `x == y  =  y == x`
  Transitivity:: If `x == y && y == z = True`, then `x == z = True`
  Substitutivity:: If `x == y = True` and `f` is a "public" function, then
 `f x == f y = True`
  Negation:: `x /= y  =  not (x == y)`

 == `Ord`

 === Basics
  Transitivity:: If `x <= y && y <= z = True`, then `x <= z = True`
  Reflexivity:: relative to `==`: = True`
  Antisymmetry:: If `x <= y && y <= x = True`, then `x == y = True`

 === Operator interactions

 1. `x >= y = y <= x`
 2. `x < y = x <= y && x /= y`
 3. `x > y = y < x`
 4. `x < y = compare x y == LT`
 5. `x > y = compare x y == GT`
 6. `x == y = compare x y == EQ`

 The `max` and `min` rules look a bit funny. Note that `max x y` needn't be
 ''identical'' to either `x` or `y`; it just needs to ''look'' the same.

 7. `min x y == if x <= y then x else y  =  True`
 8. `max x y == if x >= y then x else y  =  True`

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

_______________________________________________
ghc-tickets mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-tickets
Reply | Threaded
Open this post in threaded view
|

Re: [GHC] #15078: base: Customary type class laws (e.g. for Eq) and non-abiding instances (e.g. Float) should be documented

GHC - devs mailing list
In reply to this post by GHC - devs mailing list
#15078: base: Customary type class laws (e.g. for Eq) and non-abiding instances
(e.g. Float) should be documented
-------------------------------------+-------------------------------------
        Reporter:  sjakobi           |                Owner:  (none)
            Type:  feature request   |               Status:  new
        Priority:  normal            |            Milestone:  8.6.1
       Component:  Core Libraries    |              Version:  8.4.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 dfeuer):

 * cc: dfeuer (added)


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

_______________________________________________
ghc-tickets mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-tickets
Reply | Threaded
Open this post in threaded view
|

Re: [GHC] #15078: base: Customary type class laws (e.g. for Eq) and non-abiding instances (e.g. Float) should be documented

GHC - devs mailing list
In reply to this post by GHC - devs mailing list
#15078: base: Customary type class laws (e.g. for Eq) and non-abiding instances
(e.g. Float) should be documented
-------------------------------------+-------------------------------------
        Reporter:  sjakobi           |                Owner:  (none)
            Type:  feature request   |               Status:  new
        Priority:  normal            |            Milestone:  8.6.1
       Component:  Core Libraries    |              Version:  8.4.2
      Resolution:                    |             Keywords:  newcomer
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 bgamari):

 * keywords:   => newcomer


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

_______________________________________________
ghc-tickets mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-tickets
Reply | Threaded
Open this post in threaded view
|

Re: [GHC] #15078: base: Customary type class laws (e.g. for Eq) and non-abiding instances (e.g. Float) should be documented

GHC - devs mailing list
In reply to this post by GHC - devs mailing list
#15078: base: Customary type class laws (e.g. for Eq) and non-abiding instances
(e.g. Float) should be documented
-------------------------------------+-------------------------------------
        Reporter:  sjakobi           |                Owner:  Azel
            Type:  feature request   |               Status:  new
        Priority:  normal            |            Milestone:  8.6.1
       Component:  Core Libraries    |              Version:  8.4.2
      Resolution:                    |             Keywords:  newcomer
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 Azel):

 * owner:  (none) => Azel


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

_______________________________________________
ghc-tickets mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-tickets