[Git][ghc/ghc][wip/romes/9557] 3 commits: Deriving Ord: compare and <= only

Rodrigo Mesquita (@alt-romes) gitlab at gitlab.haskell.org
Fri Nov 8 11:04:18 UTC 2024



Rodrigo Mesquita pushed to branch wip/romes/9557 at Glasgow Haskell Compiler / GHC


Commits:
3d068f70 by Rodrigo Mesquita at 2024-11-08T11:04:06+00:00
Deriving Ord: compare and <= only

Since the implementation of CLC proposal #24, the default
implementations of Ord's `<`, `>`, and `>=` are given in terms of `<=`.

This means we no longer need to generate implementations for these
methods when stock deriving `Ord`. Rather, just derive the
implementation of `compare` and `<=`, and rely on the default
implementations for the others.

- - - - -
6e556741 by Rodrigo Mesquita at 2024-11-08T11:04:07+00:00
Don't eta expand cons when deriving Data

This eta expansion was introduced with the initial commit for Linear
types.

I believe this isn't needed any longer. My guess is it is an artifact
from the initial linear types implementation: data constructors are
linear, but they shouldn't need to be eta expanded to be used as higher
order functions. I suppose in the early days this wasn't true.

For instance, this works now:

    data T x = T x
    f = \(x :: forall y. y -> T y) -> x True
    f T -- ok!

T is linear, but can be passed where an unrestricted higher order
function is expected. I recall there being some magic around to make
this work for data constructors...

Since this works, there's no need to eta_expand the data constructors in
the derived Data instances.

- - - - -
c8e109ec by Rodrigo Mesquita at 2024-11-08T11:04:07+00:00
deriving Traversable: Eta reduce more constructor

We were generating unnecessarily eta-expanded lambdas in derived
Traversable instances (via mkSimpleConMatch2).

We can generate smaller code by eta-reducing all trailing arguments
which do mention the last type variable

- - - - -


6 changed files:

- compiler/GHC/Tc/Deriv/Functor.hs
- compiler/GHC/Tc/Deriv/Generate.hs
- testsuite/tests/deriving/should_compile/T14682.stderr
- testsuite/tests/deriving/should_compile/T20496.stderr
- testsuite/tests/typecheck/should_fail/T15883c.stderr
- testsuite/tests/typecheck/should_fail/T15883e.stderr


Changes:

=====================================
compiler/GHC/Tc/Deriv/Functor.hs
=====================================
@@ -689,9 +689,18 @@ mkSimpleConMatch2 ctxt fold extra_pats con insides = do
         con_expr
           | null asWithTyVar = nlHsApps con_name asWithoutTyVar
           | otherwise =
-              let bs   = filterByList  argTysTyVarInfo bs_RDRs
-                  vars = filterByLists argTysTyVarInfo bs_Vars as_Vars
-              in mkHsLam (noLocA (map nlVarPat bs)) (nlHsApps con_name vars)
+              let -- All trailing b-args can be eta-reduced:
+                  -- (\b1 b2 b3 -> A b1 a2 b2 b3) ==> (\b1 -> A b1 a2)
+                  -- This improves the number of allocations needed to compile
+                  -- the generated code (it is not relevant for correctness)
+                  -- We do this by counting the n of args to keep
+                  keep_n = length $ dropWhileEndLE (== True) argTysTyVarInfo
+                  bs   = filterByList (take keep_n argTysTyVarInfo) bs_RDRs
+                  vars = take keep_n $
+                         filterByLists argTysTyVarInfo bs_Vars as_Vars
+               in if keep_n == 0
+                    then nlHsVar con_name
+                    else mkHsLam (noLocA (map nlVarPat bs)) (nlHsApps con_name vars)
 
     rhs <- fold con_expr exps
     return $ mkMatch ctxt (noLocA (extra_pats ++ [pat])) rhs emptyLocalBinds


=====================================
compiler/GHC/Tc/Deriv/Generate.hs
=====================================
@@ -339,7 +339,7 @@ Several special cases:
   See function unliftedOrdOp
 
 Note [Game plan for deriving Ord]
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 It's a bad idea to define only 'compare', and build the other binary
 comparisons on top of it; see #2130, #4019.  Reason: we don't
 want to laboriously make a three-way comparison, only to extract a
@@ -350,16 +350,22 @@ binary result, something like this:
                                        True  -> False
                                        False -> True
 
-This being said, we can get away with generating full code only for
-'compare' and '<' thus saving us generation of other three operators.
-Other operators can be cheaply expressed through '<':
-a <= b = not $ b < a
-a > b = b < a
-a >= b = not $ a < b
-
 So for sufficiently small types (few constructors, or all nullary)
 we generate all methods; for large ones we just use 'compare'.
 
+This being said, we can get away with generating full code only for
+'compare' and '<=' thus saving us generation of other three operators.
+Other operators can be cheaply expressed through '<=' -- indeed, that's what
+the default implementations of >, <, and >= do.
+
+Historically, derived instances defined '<' and the remaining operators as
+cheap expressions in function of it:
+  a <= b = not $ b < a
+  a > b = b < a
+  a >= b = not $ a < b
+but since the CLC proposal #24 (see 8f174e06185143674d6cbfee75c30e68805d85b8),
+it suffices to derive '<=' and rely on the
+default implementation for the others.
 -}
 
 data OrdOp = OrdCompare | OrdLT | OrdLE | OrdGE | OrdGT
@@ -417,19 +423,10 @@ gen_Ord_binds loc dit@(DerivInstTys{ dit_rep_tc = tycon
     other_ops
       | (last_tag - first_tag) <= 2     -- 1-3 constructors
         || null non_nullary_cons        -- Or it's an enumeration
-      = [mkOrdOp OrdLT, lE, gT, gE]
+      = [mkOrdOp OrdLE]
       | otherwise
       = []
 
-    negate_expr = nlHsApp (nlHsVar not_RDR)
-    pats = noLocA [a_Pat, b_Pat]
-    lE = mkSimpleGeneratedFunBind loc le_RDR pats $
-        negate_expr (nlHsApp (nlHsApp (nlHsVar lt_RDR) b_Expr) a_Expr)
-    gT = mkSimpleGeneratedFunBind loc gt_RDR pats $
-        nlHsApp (nlHsApp (nlHsVar lt_RDR) b_Expr) a_Expr
-    gE = mkSimpleGeneratedFunBind loc ge_RDR pats $
-        negate_expr (nlHsApp (nlHsApp (nlHsVar lt_RDR) a_Expr) b_Expr)
-
     get_tag con = dataConTag con - fIRST_TAG
         -- We want *zero-based* tags, because that's what
         -- con2Tag returns (generated by untag_Expr)!
@@ -1407,7 +1404,7 @@ gen_Data_binds loc (DerivInstTys{dit_rep_tc = rep_tc})
 
     gfoldl_eqn con
       = ([nlVarPat k_RDR, z_Pat, nlConVarPat con_name as_needed],
-                   foldl' mk_k_app (z_Expr `nlHsApp` (eta_expand_data_con con)) as_needed)
+                   foldl' mk_k_app (z_Expr `nlHsApp` (nlHsVar (getRdrName con))) as_needed)
                    where
                      con_name ::  RdrName
                      con_name = getRdrName con
@@ -1427,18 +1424,9 @@ gen_Data_binds loc (DerivInstTys{dit_rep_tc = rep_tc})
 
     gunfold_alt dc = mkHsCaseAlt (mk_unfold_pat dc) (mk_unfold_rhs dc)
     mk_unfold_rhs dc = foldr nlHsApp
-                           (z_Expr `nlHsApp` (eta_expand_data_con dc))
+                           (z_Expr `nlHsApp` (nlHsVar (getRdrName dc)))
                            (replicate (dataConSourceArity dc) (nlHsVar k_RDR))
 
-    eta_expand_data_con dc =
-        mkHsLam (noLocA eta_expand_pats)
-          (foldl nlHsApp (nlHsVar (getRdrName dc)) eta_expand_hsvars)
-      where
-        eta_expand_pats = map nlVarPat eta_expand_vars
-        eta_expand_hsvars = map nlHsVar eta_expand_vars
-        eta_expand_vars = take (dataConSourceArity dc) as_RDRs
-
-
     mk_unfold_pat dc    -- Last one is a wild-pat, to avoid
                         -- redundant test, and annoying warning
       | tag-fIRST_TAG == n_cons-1 = nlWildPat   -- Last constructor
@@ -2570,10 +2558,9 @@ as_RDRs         = [ mkVarUnqual (mkFastString ("a"++show i)) | i <- [(1::Int) ..
 bs_RDRs         = [ mkVarUnqual (mkFastString ("b"++show i)) | i <- [(1::Int) .. ] ]
 cs_RDRs         = [ mkVarUnqual (mkFastString ("c"++show i)) | i <- [(1::Int) .. ] ]
 
-a_Expr, b_Expr, c_Expr, z_Expr, ltTag_Expr, eqTag_Expr, gtTag_Expr, false_Expr,
+a_Expr, c_Expr, z_Expr, ltTag_Expr, eqTag_Expr, gtTag_Expr, false_Expr,
     true_Expr, pure_Expr, unsafeCodeCoerce_Expr :: LHsExpr GhcPs
 a_Expr                = nlHsVar a_RDR
-b_Expr                = nlHsVar b_RDR
 c_Expr                = nlHsVar c_RDR
 z_Expr                = nlHsVar z_RDR
 ltTag_Expr            = nlHsVar ltTag_RDR


=====================================
testsuite/tests/deriving/should_compile/T14682.stderr
=====================================
@@ -26,9 +26,8 @@ Derived class instances:
   
   instance GHC.Internal.Data.Data.Data T14682.Foo where
     GHC.Internal.Data.Data.gfoldl k z (T14682.Foo a1 a2)
-      = ((z (\ a1 a2 -> T14682.Foo a1 a2) `k` a1) `k` a2)
-    GHC.Internal.Data.Data.gunfold k z _
-      = k (k (z (\ a1 a2 -> T14682.Foo a1 a2)))
+      = ((z T14682.Foo `k` a1) `k` a2)
+    GHC.Internal.Data.Data.gunfold k z _ = k (k (z T14682.Foo))
     GHC.Internal.Data.Data.toConstr (T14682.Foo _ _) = $cFoo
     GHC.Internal.Data.Data.dataTypeOf _ = $tFoo
   
@@ -47,18 +46,15 @@ Derived class instances:
                         GHC.Types.LT -> GHC.Types.LT
                         GHC.Types.EQ -> (a2 `GHC.Classes.compare` b2)
                         GHC.Types.GT -> GHC.Types.GT
-    (GHC.Classes.<) a b
+    (GHC.Classes.<=) a b
       = case a of
           T14682.Foo a1 a2
             -> case b of
                  T14682.Foo b1 b2
                    -> case (GHC.Classes.compare a1 b1) of
                         GHC.Types.LT -> GHC.Types.True
-                        GHC.Types.EQ -> (a2 GHC.Classes.< b2)
+                        GHC.Types.EQ -> (a2 GHC.Classes.<= b2)
                         GHC.Types.GT -> GHC.Types.False
-    (GHC.Classes.<=) a b = GHC.Classes.not ((GHC.Classes.<) b a)
-    (GHC.Classes.>) a b = (GHC.Classes.<) b a
-    (GHC.Classes.>=) a b = GHC.Classes.not ((GHC.Classes.<) a b)
   
   instance GHC.Internal.Ix.Ix T14682.Foo where
     GHC.Internal.Ix.range (T14682.Foo a1 a2, T14682.Foo b1 b2)
@@ -178,6 +174,24 @@ GHC.Classes.Eq [T14682.Foo]
 
 
 
+==================== Filling in method body ====================
+GHC.Classes.Ord [T14682.Foo]
+  (GHC.Classes.<) = GHC.Classes.$dm< @T14682.Foo
+
+
+
+==================== Filling in method body ====================
+GHC.Classes.Ord [T14682.Foo]
+  (GHC.Classes.>) = GHC.Classes.$dm> @T14682.Foo
+
+
+
+==================== Filling in method body ====================
+GHC.Classes.Ord [T14682.Foo]
+  (GHC.Classes.>=) = GHC.Classes.$dm>= @T14682.Foo
+
+
+
 ==================== Filling in method body ====================
 GHC.Classes.Ord [T14682.Foo]
   GHC.Classes.max = GHC.Classes.$dmmax @T14682.Foo


=====================================
testsuite/tests/deriving/should_compile/T20496.stderr
=====================================
@@ -32,5 +32,5 @@ rnd
     null (MkT _) = False
   
   instance Traversable T where
-    traverse f (MkT a1) = fmap (\ b1 -> MkT b1) (f a1)
+    traverse f (MkT a1) = fmap MkT (f a1)
 


=====================================
testsuite/tests/typecheck/should_fail/T15883c.stderr
=====================================
@@ -1,4 +1,3 @@
-
 T15883c.hs:14:1: error: [GHC-39999]
     • No instance for ‘Eq (Foo LiftedRep)’
         arising from the superclasses of an instance declaration
@@ -22,7 +21,7 @@ T15883c.hs:14:1: error: [GHC-39999]
         To see the code I am typechecking, use -ddump-deriv
 
 T15883c.hs:14:1: error: [GHC-39999]
-    • Ambiguous type variable ‘a1’ arising from a use of ‘<’
+    • Ambiguous type variable ‘a1’ arising from a use of ‘<=’
       prevents the constraint ‘(Ord a1)’ from being solved.
       Probable fix: use a type annotation to specify what ‘a1’ should be.
       Potentially matching instances:
@@ -31,9 +30,10 @@ T15883c.hs:14:1: error: [GHC-39999]
         ...plus 24 others
         ...plus two instances involving out-of-scope types
         (use -fprint-potential-instances to see them all)
-    • In the expression: a1 < b1
-      In a case alternative: MkFoo b1 -> (a1 < b1)
-      In the expression: case b of MkFoo b1 -> (a1 < b1)
-      When typechecking the code for ‘<’
+    • In the expression: a1 <= b1
+      In a case alternative: MkFoo b1 -> (a1 <= b1)
+      In the expression: case b of MkFoo b1 -> (a1 <= b1)
+      When typechecking the code for ‘<=’
         in a derived instance for ‘Ord (Foo LiftedRep)’:
         To see the code I am typechecking, use -ddump-deriv
+


=====================================
testsuite/tests/typecheck/should_fail/T15883e.stderr
=====================================
@@ -1,71 +1,26 @@
-
-T15883e.hs:16:1: error: [GHC-39999]
-    • Ambiguous type variable ‘d0’ arising from a use of ‘k’
-      prevents the constraint ‘(Data d0)’ from being solved.
-      Probable fix: use a type annotation to specify what ‘d0’ should be.
-      Potentially matching instances:
-        instance (Data a, Data b) => Data (Either a b)
-          -- Defined in ‘GHC.Internal.Data.Data’
-        instance Data a => Data (Down a)
-          -- Defined in ‘GHC.Internal.Data.Data’
-        ...plus 20 others
-        ...plus 47 instances involving out-of-scope types
-        (use -fprint-potential-instances to see them all)
-    • In the expression: z (\ a1 -> MkFoo a1) `k` a1
-      In an equation for ‘GHC.Internal.Data.Data.gfoldl’:
-          GHC.Internal.Data.Data.gfoldl k z (MkFoo a1)
-            = (z (\ a1 -> MkFoo a1) `k` a1)
-      When typechecking the code for ‘GHC.Internal.Data.Data.gfoldl’
-        in a derived instance for ‘Data (Foo LiftedRep)’:
-        To see the code I am typechecking, use -ddump-deriv
-      In the instance declaration for ‘Data (Foo LiftedRep)’
-
-T15883e.hs:16:1: error: [GHC-46956]
-    • Couldn't match expected type ‘a’ with actual type ‘d0’
-        because type variable ‘a’ would escape its scope
-      This (rigid, skolem) type variable is bound by
-        a type expected by the context:
-          forall a. a
-        at T15883e.hs:16:1-52
-    • In the first argument of ‘MkFoo’, namely ‘a1’
-      In the expression: MkFoo a1
-      In the first argument of ‘z’, namely ‘(\ a1 -> MkFoo a1)’
+T15883e.hs:16:1: error: [GHC-91028]
+    • Couldn't match type ‘d0’ with ‘forall a. a’
+      Expected: d0 -> Foo LiftedRep
+        Actual: (forall a. a) -> Foo LiftedRep
+      Cannot instantiate unification variable ‘d0’
+      with a type involving polytypes: forall a. a
+    • In the first argument of ‘z’, namely ‘MkFoo’
+      In the first argument of ‘k’, namely ‘z MkFoo’
+      In the expression: z MkFoo `k` a1
       When typechecking the code for ‘GHC.Internal.Data.Data.gfoldl’
         in a derived instance for ‘Data (Foo LiftedRep)’:
         To see the code I am typechecking, use -ddump-deriv
-    • Relevant bindings include a1 :: d0 (bound at T15883e.hs:16:1)
 
-T15883e.hs:16:1: error: [GHC-39999]
-    • Ambiguous type variable ‘b0’ arising from a use of ‘k’
-      prevents the constraint ‘(Data b0)’ from being solved.
-      Probable fix: use a type annotation to specify what ‘b0’ should be.
-      Potentially matching instances:
-        instance (Data a, Data b) => Data (Either a b)
-          -- Defined in ‘GHC.Internal.Data.Data’
-        instance Data a => Data (Down a)
-          -- Defined in ‘GHC.Internal.Data.Data’
-        ...plus 20 others
-        ...plus 47 instances involving out-of-scope types
-        (use -fprint-potential-instances to see them all)
-    • In the expression: k (z (\ a1 -> MkFoo a1))
-      In an equation for ‘GHC.Internal.Data.Data.gunfold’:
-          GHC.Internal.Data.Data.gunfold k z _ = k (z (\ a1 -> MkFoo a1))
+T15883e.hs:16:1: error: [GHC-91028]
+    • Couldn't match type ‘b0’ with ‘forall a. a’
+      Expected: b0 -> Foo LiftedRep
+        Actual: (forall a. a) -> Foo LiftedRep
+      Cannot instantiate unification variable ‘b0’
+      with a type involving polytypes: forall a. a
+    • In the first argument of ‘z’, namely ‘MkFoo’
+      In the first argument of ‘k’, namely ‘(z MkFoo)’
+      In the expression: k (z MkFoo)
       When typechecking the code for ‘GHC.Internal.Data.Data.gunfold’
         in a derived instance for ‘Data (Foo LiftedRep)’:
         To see the code I am typechecking, use -ddump-deriv
-      In the instance declaration for ‘Data (Foo LiftedRep)’
 
-T15883e.hs:16:1: error: [GHC-46956]
-    • Couldn't match expected type ‘a’ with actual type ‘b0’
-        because type variable ‘a’ would escape its scope
-      This (rigid, skolem) type variable is bound by
-        a type expected by the context:
-          forall a. a
-        at T15883e.hs:16:1-52
-    • In the first argument of ‘MkFoo’, namely ‘a1’
-      In the expression: MkFoo a1
-      In the first argument of ‘z’, namely ‘(\ a1 -> MkFoo a1)’
-      When typechecking the code for ‘GHC.Internal.Data.Data.gunfold’
-        in a derived instance for ‘Data (Foo LiftedRep)’:
-        To see the code I am typechecking, use -ddump-deriv
-    • Relevant bindings include a1 :: b0 (bound at T15883e.hs:16:1)



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/37b40db8ec8b362ee8bd5e1da9eba025648ae2cc...c8e109ec4d8542956b80ecf591ef8faea18b28eb

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/37b40db8ec8b362ee8bd5e1da9eba025648ae2cc...c8e109ec4d8542956b80ecf591ef8faea18b28eb
You're receiving this email because of your account on gitlab.haskell.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20241108/7830d192/attachment-0001.html>


More information about the ghc-commits mailing list