Why doesn't Haskell have a stronger alternative to Eq?

The reason why Setit is not a functor is given here . It seems to boil down to what is a == b && f a /= f bpossible. So why Haskell doesn't have a standard Eq alternative, something like

class Eq a => StrongEq a where
    (===) :: a -> a -> Bool
    (/==) :: a -> a -> Bool
    x /== y = not (x === y)
    x === y = not (x /== y)

for which specimens must obey the laws

∀a,b,f. not (a === b) || (f a === f b)
∀a. a === a
∀a,b. (a === b) == (b === a)

and maybe some others? Then we could:

instance StrongEq a => Functor (Set a) where
    -- ...

Or am I missing something?


: , " Eq?", , , . : " Eq, ? Eq?", " a == b , Set not Functor?".

, - (thanks @n.m.). :

newtype StrongSet a = StrongSet (Set a)
instance Functor StrongSet where
    fmap :: (StrongEq a, StrongEq b) => (a -> b) -> StrongSet a -> StrongSet b
    fmap (StrongSet s) = StrongSet (map s)
+4
4

Functor . , Set Functor Haskell, fmap . StrongEq , fmap Set.

fmap . , , ( ), Eq StrongEq .

fmap - , :

fmapBoth :: (Functor f, Functor g) => (a -> b, c -> d) -> (f a, g c) -> (f b, g d)
fmapBoth (h, j) (x, y) = (fmap h x, fmap j y)

, f g, h j. , , fmap, - , , , .

, Set Functor Haskell, , () fmap :: (a -> b) -> Set a -> Set b . , Functor. fmap :: (Eq a -> Eq b) => (a -> b) -> Set a -> Set b .

, , ConstraintKinds GHC extendsion Functor, , Functor ( Ord, Eq). , Monad, Set. , , . Sets , , Functor , .

+3
instance StrongEq a => Functor (Set a) where

Haskell, / , , StrongEq.

Haskell Functor * -> *. , . [] () Set . [a] Set a * .

Haskell Set, Functor, , . , Integer->Integer.

,

goedel :: Integer -> Integer -> Integer
goedel x y = -- compute the result of a function with 
             -- Goedel number x, applied to y

, s :: Set Integer. fmap goedel s? ?

, , , Set ( Powerset, ) , .

+14

, / ( , ):

, @leftaroundabout :

== " " ( require a == b ; , "" a b, . , unAlwaysEq ). - , a Eq.

StrongEq, , Eq .

Haskell "" . . ,

data PlatonicSolid = Tetrahedron | Cube |
   Octahedron | Dodecahedron | Icosahedron

. , deriving Eq, .

, , Haskell. , - Node B (Node R Leaf 1 Leaf) 2 Leaf Node B Leaf 1 (Node R Leaf 2 Leaf) {1,2}. deriving Eq , Eq, , ( ).

, Eq ( Ord), ! - Ord, , , , . , , sort :: Ord a => [a] -> [a] , , Haskell, . sort [Bad 1 "Bob", Bad 1 "James"] [Bad 1 "Bob", Bad 1 "James"], [Bad 1 "James", Bad 1 "Bob"], [Bad 1 "James", Bad 1 "James"] [Bad 1 "Bob", Bad 1 "Bob"]. . , unsafePerformIO - , , , == .

tl; dr: - Eq - ; , .

+6

StrongEq . , - , , , , .

, , . , ( , , , data U = U (U -> U)). , , .

, typechecking , , , .

Thus, it StrongEqcan be defined in a limited part of Haskell that contains only types that can be resolved by permission for equality. We can consider this category with arrows as computable functions, and then see that Setas an endofunctor from types to types of sets of values ​​of this type. Unfortunately, these restrictions went far from standard Haskell and made the definition StrongEqeither Functor (Set a)slightly less practical.

+2
source

Source: https://habr.com/ru/post/1544462/


All Articles