1. 33
  1.  

  2. 10

    Regarding Eq and PartialEq being better in Rust than in Haskell:

    It is slightly better, but still terrible.

    Eq being a subtype of PartialEq is a mistake, conveniently obscured by its terrible naming.

    Instead, have Equality and Identity (i. e. say what it does not what it is) and suddenly things just work:

    trait Equality
      fun ==: Bool = ...
      fun !=: Bool = ...
    
    trait Identity
      fun ===: Bool = ...
      fun !==: Bool = ...
    
    NaN !=  NaN // NaNs are not equal ...
    NaN === NaN // ... but are identical!
    

    Same for Ord and PartialOrd: call it Comparable and Sortable and don’t have a subtyping relationship between them!

    1. 3

      Not even true for nan, you can have many NaNs that are not bitwise equal either, so NaN === NaN wouldn’t hold anyway. It’s even used in NaN packing.

      I don’t think there is a useful notion of identity as you describe, besides “bitwise equality” (which is a form of equality, being transitive, reflexive, etc.) Rust designers made a good call on that one.

      1. 5

        you can have many NaNs that are not bitwise equal either

        Different NaNs are different under ===, it’s bitwise equality.

        I don’t think there is a useful notion of identity as you describe

        It’s in the IEEE754 spec.

        Rust designers made a good call on that one.

        This design works. There is nothing more to say about it. Rust’s doesn’t.

        1. 1

          Yup. I would call it semantic vs representation equality, but yeah :)

          1. 1

            Fair enough, you have a very specific bitwise equality on floats, some of which can’t even be printed differently. I don’t think it’s a good idea to have a whole typeclass/trait just for that though.

            1. 1

              A typeclass is fairly cheap. The mess Haskell/Rust produce? Not so much.

      2. 6

        A takeaway for other language designers: If the language does not enforce correctness of algebraic laws, then community convention is insufficient to make up the difference.

        1. 3

          I hadn’t seen the first part of this before. That part contains the first time I see someone acknowledging foldl is awful. I always thought so, but I also thought it was me not ‘getting’ something, as it figures prominently in introductions and examples. I feel equal parts reassured and annoyed.

          1. 2

            Yeah, haskell being this old language, also heavily used for experiments has a ton of historical cruft. It takes time to build a good intuition about what libraries and patterns are good :/