Simon Peyton Jones pushed to branch wip/T26115 at Glasgow Haskell Compiler / GHC
Commits:
-
0d1363c0
by Simon Peyton Jones at 2025-06-28T23:00:40+01:00
-
66114346
by Simon Peyton Jones at 2025-06-28T23:37:33+01:00
6 changed files:
- compiler/GHC/Core/Opt/Specialise.hs
- compiler/GHC/HsToCore/Binds.hs
- compiler/GHC/Tc/Gen/Sig.hs
- compiler/GHC/Tc/Solver/Dict.hs
- compiler/GHC/Tc/Solver/Monad.hs
- compiler/GHC/Tc/Solver/Solve.hs
Changes:
... | ... | @@ -3375,7 +3375,7 @@ beats_or_same (CI { ci_key = args1 }) (CI { ci_key = args2 }) |
3375 | 3375 | go_arg (SpecDict {}) (SpecDict {}) = True
|
3376 | 3376 | go_arg UnspecType UnspecType = True
|
3377 | 3377 | go_arg UnspecArg UnspecArg = True
|
3378 | - go_arg _ _ = False
|
|
3378 | + go_arg _ _ = False
|
|
3379 | 3379 | |
3380 | 3380 | ----------------------
|
3381 | 3381 | splitDictBinds :: FloatedDictBinds -> IdSet -> (FloatedDictBinds, OrdList DictBind, IdSet)
|
... | ... | @@ -3491,7 +3491,6 @@ newSpecIdSM old_name new_ty details info |
3491 | 3491 | ; return (assert (not (isCoVarType new_ty)) $
|
3492 | 3492 | mkLocalVar details new_name ManyTy new_ty info) }
|
3493 | 3493 | |
3494 | - |
|
3495 | 3494 | {-
|
3496 | 3495 | Old (but interesting) stuff about unboxed bindings
|
3497 | 3496 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
... | ... | @@ -796,216 +796,6 @@ Note [Desugaring new-form SPECIALISE pragmas] |
796 | 796 | which is desugared in this module by `dsSpec`. For the context see
|
797 | 797 | Note [Handling new-form SPECIALISE pragmas] in GHC.Tc.Gen.Sig
|
798 | 798 | |
799 | -Suppose we have f :: forall p q. (Ord p, Eq q) => p -> q -> q, and a pragma
|
|
800 | - |
|
801 | - {-# SPECIALISE forall x. f @[a] @[Int] x [3,4] #-}
|
|
802 | - |
|
803 | -In `dsSpec` the `SpecPragE` will look something like this:
|
|
804 | - |
|
805 | - SpecPragE { spe_fn_id = f
|
|
806 | - , spe_bndrs = @a (d:Ord a) (x:[a])
|
|
807 | - , spe_call = let d2:Ord [a] = $dfOrdList d
|
|
808 | - d3:Eq [Int] = $dfEqList $dfEqInt
|
|
809 | - in f @[a] @[Int] d2 d3 x [3,4] }
|
|
810 | -We want to get
|
|
811 | - |
|
812 | - RULE forall a (d2:Ord a) (d3:Eq [Int]) (x:[a]).
|
|
813 | - f @[a] @[Int] d2 d3 x [3,4] = $sf d2 x
|
|
814 | - |
|
815 | - $sf :: forall a. Ord [a] => a -> Int
|
|
816 | - $sf = /\a. \d2 x.
|
|
817 | - let d3 = $dfEqList $dfEqInt
|
|
818 | - in <f-rhs> @[a] @[Int] d2 d3 x [3,4]
|
|
819 | - |
|
820 | -Notice that
|
|
821 | - |
|
822 | -(SP1) If the expression in the SPECIALISE pragma had a type signature, such as
|
|
823 | - SPECIALISE f :: Eq b => Int -> b -> b
|
|
824 | - then the desugared expression may have type abstractions and applications
|
|
825 | - "in the way", like this:
|
|
826 | - (/\b. (\d:Eq b). let d1 = $dfOrdInt in f @Int @b d1 d) @b (d2:Eq b)
|
|
827 | - The lambdas come from the type signature, which is then re-instantiated,
|
|
828 | - hence the applications of those lambdas.
|
|
829 | - |
|
830 | - We use the simple optimiser to simplify this to
|
|
831 | - let { d = d2; d1 = $dfOrdInt } in f @Int @b d1 d
|
|
832 | - |
|
833 | - Important: do no inlining in this "simple optimiser" phase:
|
|
834 | - use `simpleOptExprNoInline`. E.g. we don't want to turn it into
|
|
835 | - f @Int @b $dfOrdInt d2
|
|
836 | - because the latter is harder to match.
|
|
837 | - |
|
838 | - Similarly if we have
|
|
839 | - let { d1=d; d2=d } in f d1 d2
|
|
840 | - we don't want to inline d1/d2 to get this
|
|
841 | - f d d
|
|
842 | - |
|
843 | -(SP2) $sf does not simply quantify over (d:Ord a). Instead, to figure out what
|
|
844 | - it should quantify over, and to include the 'd3' binding in the body of $sf,
|
|
845 | - we use the function `prepareSpecLHS`. It takes the simplified LHS `core_call`,
|
|
846 | - and uses the dictionary bindings to figure out the RULE LHS and RHS.
|
|
847 | - |
|
848 | - This is described in Note [prepareSpecLHS].
|
|
849 | - |
|
850 | -Note [prepareSpecLHS]
|
|
851 | -~~~~~~~~~~~~~~~~~~~~~
|
|
852 | -To compute the appropriate RULE LHS and RHS for a new-form specialise pragma,
|
|
853 | -as described in Note [Desugaring new-form SPECIALISE pragmas], we use a function
|
|
854 | -called prepareSpecLHS.
|
|
855 | -It takes as input a list of (type and evidence) binders, and a Core expression.
|
|
856 | -For example, suppose its input is of the following form:
|
|
857 | - |
|
858 | - spe_bndrs = @a (d:Ord a)
|
|
859 | - spe_call =
|
|
860 | - let
|
|
861 | - -- call these bindings the call_binds
|
|
862 | - d1 :: Num Int
|
|
863 | - d1 = $dfNumInt
|
|
864 | - d2 :: Ord [a]
|
|
865 | - d2 = $dfOrdList d
|
|
866 | - d3 :: Eq a
|
|
867 | - d3 = $p1Ord d3
|
|
868 | - d4 :: Ord (F a)
|
|
869 | - d4 = d |> co1
|
|
870 | - d5 :: Ord (G a)
|
|
871 | - d5 = d4 |> co2
|
|
872 | - in
|
|
873 | - f @[a] @Int d1 d2 d3 d5
|
|
874 | - |
|
875 | -The goal of prepareSpecLHS is then to generate the following two things:
|
|
876 | - |
|
877 | - - A specialisation, of the form:
|
|
878 | - |
|
879 | - $sf <spec_args> =
|
|
880 | - let <spec_binds>
|
|
881 | - in <f-rhs> @[a] @Int d1 d2 d3 d5
|
|
882 | - |
|
883 | - - A rule, of the form:
|
|
884 | - |
|
885 | - RULE forall a d1 d2 d3 d5. f @[a] @Int d1 d2 d3 d5 =
|
|
886 | - let <rule_binds>
|
|
887 | - in $sf <spec_args>
|
|
888 | - |
|
889 | -That is, we must compute 'spec_args', 'rule_binds' and 'spec_binds'. A first
|
|
890 | -approach might be:
|
|
891 | - |
|
892 | - - take spec_args = spe_bndrs,
|
|
893 | - - spec_binds = call_binds.
|
|
894 | - |
|
895 | -If we did so, the RULE would look like:
|
|
896 | - |
|
897 | - RULE forall a d1 d2 d3 d5. f @[a] @Int d1 d2 d3 d5 =
|
|
898 | - let d = <???>
|
|
899 | - in $sf @a d
|
|
900 | - |
|
901 | -The problem is: how do we recover 'd' from 'd1', 'd2', 'd3', 'd5'? Essentially,
|
|
902 | -we need to run call_binds in reverse. In this example, we had:
|
|
903 | - |
|
904 | - d1 :: Num Int
|
|
905 | - d1 = $dfNumInt
|
|
906 | - d2 :: Ord [a]
|
|
907 | - d2 = $dfOrdList d
|
|
908 | - d3 :: Eq a
|
|
909 | - d3 = $p1Ord d3
|
|
910 | - d4 :: Ord (F a)
|
|
911 | - d4 = d |> co1
|
|
912 | - d5 :: Ord (G a)
|
|
913 | - d5 = d4 |> co2
|
|
914 | - |
|
915 | -Let's try to recover (d: Ord a) from 'd1', 'd2', 'd4', 'd5':
|
|
916 | - |
|
917 | - - d1 is a constant binding, so it doesn't help us.
|
|
918 | - - d2 uses a top-level instance, which we can't run in reverse; we can't
|
|
919 | - obtain Ord a from Ord [a].
|
|
920 | - - d3 uses a superclass selector which prevents us from making progress.
|
|
921 | - - d5 is defined using d4, and both involve a cast.
|
|
922 | - In theory we could define d = d5 |> sym (co1 ; co2), but this gets
|
|
923 | - pretty complicated.
|
|
924 | - |
|
925 | -This demonstrates the following:
|
|
926 | - |
|
927 | - 1. The bindings can't necessarily be "run in reverse".
|
|
928 | - 2. Even if the bindings theoretically can be "run in reverse", it is not
|
|
929 | - straightforward to do so.
|
|
930 | - |
|
931 | -Now, we could strive to make the let-bindings reversible. We already do this
|
|
932 | -to some extent for quantified constraints, as explained in
|
|
933 | -Note [Fully solving constraints for specialisation] in GHC.Tc.Gen.Sig,
|
|
934 | -using the TcSSpecPrag solver mode described in Note [TcSSpecPrag] in GHC.Tc.Solver.Monad.
|
|
935 | -However, given (2), we don't bother ensuring that e.g. we don't use top-level
|
|
936 | -class instances like in d2 above. Instead, we handle these bindings in
|
|
937 | -prepareSpecLHS as follows:
|
|
938 | - |
|
939 | - (a) Go through the bindings in order.
|
|
940 | - |
|
941 | - (1) Bindings like
|
|
942 | - d1 = $dfNumInt
|
|
943 | - depend only on constants and move to the specialised function body.
|
|
944 | - That is crucial -- it makes those specialised methods available in the
|
|
945 | - specialised body. These are the `spec_const_binds`.
|
|
946 | - |
|
947 | - Note that these binds /can/ depend on locally-quantified /type/ variables.
|
|
948 | - For example, if we have
|
|
949 | - instance Monad (ST s) where ...
|
|
950 | - then the dictionary for (Monad (ST s)) is effectively a constant dictionary.
|
|
951 | - This is important to get specialisation for such types. Example in test T8331.
|
|
952 | - |
|
953 | - (2) Other bindings, like
|
|
954 | - d2:Ord [a] = $dfOrdList d
|
|
955 | - d3 = d
|
|
956 | - depend on a locally-quantifed evidence variable `d`.
|
|
957 | - Surprisingly, /we want to drop these bindings entirely!/
|
|
958 | - This is because, as explained above, it is too difficult to run these
|
|
959 | - in reverse. Instead, we drop them, and re-compute which dictionaries
|
|
960 | - we will quantify over.
|
|
961 | - |
|
962 | - (3) Finally, inside those dictionary bindings we should find the call of the
|
|
963 | - function itself
|
|
964 | - f @[a] @[Int] d2 d3 x [3,4]
|
|
965 | - 'prepareSpecLHS' takes the call apart and returns its arguments.
|
|
966 | - |
|
967 | - (b) Now, (a)(2) means that the RULE does not quantify over 'd' any more; it
|
|
968 | - quantifies over 'd1' 'd2' 'd3' 'd5'. So we recompute the `rule_bndrs`
|
|
969 | - from scratch.
|
|
970 | - |
|
971 | - Moreover, the specialised function also no longer quantifies over 'd',
|
|
972 | - it quantifies over 'd2' 'd3' 'd5'. This set of binders is computed by
|
|
973 | - taking the RULE binders and subtracting off the binders from
|
|
974 | - the `spec_const_binds`.
|
|
975 | - |
|
976 | -[Shortcoming] This whole approach does have one downside, compared to running
|
|
977 | -the let-bindings in reverse: it doesn't allow us to common-up dictionaries.
|
|
978 | -Consider for example:
|
|
979 | - |
|
980 | - g :: forall a b. ( Eq a, Ord b ) => a -> b -> Bool
|
|
981 | - {-# SPECIALISE g :: forall c. Ord c => c -> c -> Bool #-}
|
|
982 | - |
|
983 | -The input to prepareSpecLHS will be (more or less):
|
|
984 | - |
|
985 | - spe_bndrs: @c (d:Ord c)
|
|
986 | - spe_call =
|
|
987 | - let
|
|
988 | - d1 :: Eq c
|
|
989 | - d1 = $p1Ord d
|
|
990 | - d2 :: Ord c
|
|
991 | - d2 = d
|
|
992 | - in
|
|
993 | - g @c @c d1 d2
|
|
994 | - |
|
995 | -The approach described in (2) will thus lead us to generate:
|
|
996 | - |
|
997 | - RULE g @c @c d1 d2 = $sg @c d1 d2
|
|
998 | - $sg @c d1 d2 = <g-rhs> @c @c d1 d2
|
|
999 | - |
|
1000 | -when we would rather avoid passing both dictionaries, and instead generate:
|
|
1001 | - |
|
1002 | - RULE g @c @c d1 d2 = let { d = d2 } in $sg @c d
|
|
1003 | - $sg @c d = let { d1 = $p1Ord d; d2 = d } in <g-rhs> @c @c d1 d2
|
|
1004 | - |
|
1005 | -For now, we accept this infelicity.
|
|
1006 | - |
|
1007 | -Note [Desugaring new-form SPECIALISE pragmas] -- Take 2
|
|
1008 | -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
1009 | 799 | Suppose we have
|
1010 | 800 | f :: forall a b c d. (Ord a, Ord b, Eq c, Ix d) => ...
|
1011 | 801 | f = rhs
|
... | ... | @@ -1027,11 +817,11 @@ We /could/ generate |
1027 | 817 | RULE f d1 d2 d3 d4 e1..en = $sf d1 d2 d3 d4
|
1028 | 818 | $sf d1 d2 d3 d4 = <rhs> d1 d2 d3 d4
|
1029 | 819 | |
1030 | -But that would do no specialisation! What we want is this:
|
|
820 | +But that would do no specialisation at all! What we want is this:
|
|
1031 | 821 | RULE f d1 _d2 _d3 d4 e1..en = $sf d1 d4
|
1032 | 822 | $sf d1 d4 = let d7 = d1 -- Renaming
|
1033 | - dx1 = d7 -- Renaming
|
|
1034 | - d6 = dx1
|
|
823 | + dx1 = d1 -- Renaming
|
|
824 | + d6 = d1 -- Renaming
|
|
1035 | 825 | d2 = $fOrdList d6
|
1036 | 826 | d3 = $fEqList $fEqInt
|
1037 | 827 | in rhs d1 d2 d3 d4
|
... | ... | @@ -1045,46 +835,61 @@ Notice that: |
1045 | 835 | to line things up
|
1046 | 836 | |
1047 | 837 | The transformation goes in these steps
|
838 | + |
|
1048 | 839 | (S1) decomposeCall: decomopose `the_call` into
|
1049 | - - `rev_binds`: the enclosing let-bindings (actually reversed)
|
|
840 | + - `binds`: the enclosing let-bindings
|
|
1050 | 841 | - `rule_lhs_args`: the arguments of the call itself
|
1051 | - We carefully arrange that the dictionary arguments of the actual
|
|
1052 | - call, `rule_lhs_args` are all distinct dictionary variables,
|
|
1053 | - not expressions. How? We use `simpleOptExprNoInline` to avoid
|
|
1054 | - inlining the let-bindings.
|
|
842 | + |
|
843 | + If the expression in the SPECIALISE pragma had a type signature, such as
|
|
844 | + SPECIALISE g :: Eq b => Int -> b -> b
|
|
845 | + then the desugared expression may have type abstractions and applications
|
|
846 | + "in the way", like this:
|
|
847 | + (/\b. (\d:Eq b). let d1 = $dfOrdInt in f @Int @b d1 d) @b (d2:Eq b)
|
|
848 | + The lambdas come from the type signature, which is then re-instantiated,
|
|
849 | + hence the applications of those lambdas.
|
|
850 | + |
|
851 | + so `decomposeCall` uses the simple optimiser to simplify this to
|
|
852 | + let { d = d2; d1 = $dfOrdInt } in f @Int @b d1 d
|
|
853 | + |
|
854 | + Wrinkle (S1a): do no inlining in this "simple optimiser" phase:
|
|
855 | + use `simpleOptExprNoInline`. E.g. we don't want to turn it into
|
|
856 | + f @Int @b $dfOrdInt d2
|
|
857 | + because the latter is harder to match. Similarly if we have
|
|
858 | + let { d1=d; d2=d } in f d1 d2
|
|
859 | + we don't want to inline d1/d2 to get this
|
|
860 | + f d d
|
|
861 | + |
|
862 | + TL;DR: as a result the dictionary arguments of the actual call,
|
|
863 | + `rule_lhs_args` are all distinct dictionary variables, not
|
|
864 | + expressions.
|
|
1055 | 865 | |
1056 | 866 | (S2) Compute `rule_bndrs`: the free vars of `rule_lhs_args`, which
|
1057 | 867 | will be the forall'd template variables of the RULE. In the example,
|
1058 | 868 | rule_bndrs = d1,d2,d3,d4
|
1059 | - |
|
1060 | -(S3) grabSpecBinds: transform `rev_binds` into `spec_binds`: the
|
|
1061 | - bindings we will wrap around the call in the RHS of `$sf`
|
|
1062 | - |
|
1063 | -(S4) Find `spec_bndrs`, the subset of `rule_bndrs` that we actually
|
|
1064 | - need to pass to `$sf`, simply by filtering out those that are
|
|
1065 | - bound by `spec_binds`. In the example
|
|
1066 | - spec_bndrs = d1,d4
|
|
1067 | - |
|
1068 | - |
|
1069 | - Working inner
|
|
1070 | -* Grab any bindings we can that will "shadow" the forall'd
|
|
1071 | - rule-bndrs, giving specialised bindings for them.
|
|
1072 | - * We keep a set of known_bndrs starting with {d1,..,dn}
|
|
1073 | - * We keep a binding iff no free var is
|
|
1074 | - (a) in orig_bndrs (i.e. not totally free)
|
|
1075 | - (b) not in known_bndrs
|
|
1076 | - * If we keep it, add its binder to known_bndrs; if not, don't
|
|
1077 | - |
|
1078 | -To maximise what we can "grab", start by extracting /renamings/ of the
|
|
1079 | -forall'd rule_bndrs, and bringing them to the top. A renaming is
|
|
1080 | - rule_bndr = d
|
|
1081 | -If we see this:
|
|
1082 | - * Bring d=rule_bndr to the top
|
|
1083 | - * Add d to the set of variables to look for on the right.
|
|
1084 | - e.g. rule_bndrs = d1, d2
|
|
1085 | - Bindings { d7=d9; d1=d7 }
|
|
1086 | - Bring to the top { d7=d1; d9=d7 }
|
|
1087 | - |
|
869 | + These variables will get values from a successful RULE match.
|
|
870 | + |
|
871 | +(S3) `getRenamings`: starting from the rule_bndrs, make bindings for
|
|
872 | + all other variables that are equal to them. In the example, we
|
|
873 | + make renaming-bindings for d7, dx1, d6.
|
|
874 | + |
|
875 | + NB1: we don't actually have to remove the original bindings;
|
|
876 | + it's harmless to leave them
|
|
877 | + NB2: We also reverse bindings like d1 = d2 |> co, to get
|
|
878 | + d2 = d1 |> sym co
|
|
879 | + It's easy and may help.
|
|
880 | + |
|
881 | +(S4) `pickSpecBinds`: pick the bindings we want to keep in the
|
|
882 | + specialised function. We start from `known_vars`, the variables we
|
|
883 | + know, namely the `rule_bndrs` and the binders from (S3), which are
|
|
884 | + all equal to one of the `rule_bndrs`.
|
|
885 | + |
|
886 | + Then we keep a binding if the free vars of its RHS are all known.
|
|
887 | + In our example, `d2` and `d3` are both picked, but `d4` is not.
|
|
888 | + The non-picked ones won't end up being specialised.
|
|
889 | + |
|
890 | +(S5) Finally, work out which of the `rule_bndrs` we must pass on to
|
|
891 | + specialised function. We just filter out ones bound by a renaming
|
|
892 | + or a picked binding.
|
|
1088 | 893 | -}
|
1089 | 894 | |
1090 | 895 | ------------------------
|
... | ... | @@ -1155,7 +960,9 @@ dsSpec_help :: Name -> Id -> CoreExpr -- Function to specialise |
1155 | 960 | -> InlinePragma -> [Var] -> CoreExpr
|
1156 | 961 | -> DsM (Maybe (OrdList (Id,CoreExpr), CoreRule))
|
1157 | 962 | dsSpec_help poly_nm poly_id poly_rhs spec_inl orig_bndrs ds_call
|
1158 | - = do { mb_call_info <- decomposeCall poly_id ds_call
|
|
963 | + = do { -- Decompose the call
|
|
964 | + -- Step (S1) of Note [Desugaring new-form SPECIALISE pragmas]
|
|
965 | + mb_call_info <- decomposeCall poly_id ds_call
|
|
1159 | 966 | ; case mb_call_info of {
|
1160 | 967 | Nothing -> return Nothing ;
|
1161 | 968 | Just (binds, rule_lhs_args) ->
|
... | ... | @@ -1167,12 +974,17 @@ dsSpec_help poly_nm poly_id poly_rhs spec_inl orig_bndrs ds_call |
1167 | 974 | is_local :: Var -> Bool
|
1168 | 975 | is_local v = v `elemVarSet` locals
|
1169 | 976 | |
977 | + -- Find `rule_bndrs`: (S2) of Note [Desugaring new-form SPECIALISE pragmas]
|
|
1170 | 978 | rule_bndrs = scopedSort (exprsSomeFreeVarsList is_local rule_lhs_args)
|
1171 | 979 | |
980 | + -- getRenamings: (S3) of Note [Desugaring new-form SPECIALISE pragmas]
|
|
1172 | 981 | rn_binds = getRenamings orig_bndrs binds rule_bndrs
|
982 | + |
|
983 | + -- pickSpecBinds: (S4) of Note [Desugaring new-form SPECIALISE pragmas]
|
|
1173 | 984 | known_vars = mkVarSet rule_bndrs `extendVarSetList` bindersOfBinds rn_binds
|
1174 | 985 | picked_binds = pickSpecBinds is_local known_vars binds
|
1175 | 986 | |
987 | + -- Fins `spec_bndrs`: (S5) of Note [Desugaring new-form SPECIALISE pragmas]
|
|
1176 | 988 | -- Make spec_bndrs, the variables to pass to the specialised
|
1177 | 989 | -- function, by filtering out the rule_bndrs that aren't needed
|
1178 | 990 | spec_binds_bndr_set = mkVarSet (bindersOfBinds picked_binds)
|
... | ... | @@ -1262,12 +1074,12 @@ dsSpec_help poly_nm poly_id poly_rhs spec_inl orig_bndrs ds_call |
1262 | 1074 | decomposeCall :: Id -> CoreExpr
|
1263 | 1075 | -> DsM (Maybe ([CoreBind], [CoreExpr] ))
|
1264 | 1076 | -- Decompose the call into (let <binds> in f <args>)
|
1077 | +-- See (S1) in Note [Desugaring new-form SPECIALISE pragmas]
|
|
1265 | 1078 | decomposeCall poly_id ds_call
|
1266 | - = do { -- Simplify the (desugared) call; see wrinkle (SP1)
|
|
1267 | - -- in Note [Desugaring new-form SPECIALISE pragmas]
|
|
1268 | - ; dflags <- getDynFlags
|
|
1079 | + = do { dflags <- getDynFlags
|
|
1269 | 1080 | ; let simpl_opts = initSimpleOpts dflags
|
1270 | 1081 | core_call = simpleOptExprNoInline simpl_opts ds_call
|
1082 | + -- simpleOpeExprNoInlint: see Wrinkle (S1a)!
|
|
1271 | 1083 | |
1272 | 1084 | ; case go [] core_call of {
|
1273 | 1085 | Nothing -> do { diagnosticDs (DsRuleLhsTooComplicated ds_call core_call)
|
... | ... | @@ -759,9 +759,7 @@ This is done in three parts. |
759 | 759 | |
760 | 760 | (1) Typecheck the expression, capturing its constraints
|
761 | 761 | |
762 | - (2) Solve these constraints, but in special TcSSpecPrag mode which ensures
|
|
763 | - each original Wanted is either fully solved or left untouched.
|
|
764 | - See Note [Fully solving constraints for specialisation].
|
|
762 | + (2) Solve these constraints
|
|
765 | 763 | |
766 | 764 | (3) Compute the constraints to quantify over, using `getRuleQuantCts` on
|
767 | 765 | the unsolved constraints returned by (2).
|
... | ... | @@ -797,68 +795,6 @@ This is done in three parts. |
797 | 795 | of the form:
|
798 | 796 | forall @a @b d1 d2 d3. f d1 d2 d3 = $sf d1 d2 d3
|
799 | 797 | |
800 | -Note [Fully solving constraints for specialisation]
|
|
801 | -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
802 | -As far as specialisation is concerned, it is actively harmful to simplify
|
|
803 | -constraints without /fully/ solving them.
|
|
804 | - |
|
805 | -Example:
|
|
806 | - |
|
807 | - f :: ∀ a t. (Eq a, ∀x. Eq x => Eq (t x)). t a -> Char
|
|
808 | - {-# SPECIALISE f @Int #-}
|
|
809 | - |
|
810 | - Typechecking 'f' will result in [W] Eq Int, [W] ∀x. Eq x => Eq (t x).
|
|
811 | - We absolutely MUST leave the quantified constraint alone, because we want to
|
|
812 | - quantify over it. If we were to try to simplify it, we would emit an
|
|
813 | - implication and would thereafter never be able to quantify over the original
|
|
814 | - quantified constraint.
|
|
815 | - |
|
816 | - However, we still need to simplify quantified constraints that can be
|
|
817 | - /fully solved/ from instances, otherwise we would never be able to
|
|
818 | - specialise them away. Example: {-# SPECIALISE f @a @[] #-}.
|
|
819 | - |
|
820 | -The conclusion is this:
|
|
821 | - |
|
822 | - when solving the constraints that arise from a specialise pragma, following
|
|
823 | - the recipe described in Note [Handling new-form SPECIALISE pragmas], all
|
|
824 | - Wanted quantified constraints should either be:
|
|
825 | - - fully solved (no free evidence variables), or
|
|
826 | - - left untouched.
|
|
827 | - |
|
828 | -To achieve this, we run the solver in a special "all-or-nothing" solving mode,
|
|
829 | -described in Note [TcSSpecPrag] in GHC.Tc.Solver.Monad.
|
|
830 | - |
|
831 | -Note that a similar problem arises in other situations in which the solver takes
|
|
832 | -an irreversible step, such as using a top-level class instance. This is currently
|
|
833 | -less important, as the desugarer can handle these cases. To explain, consider:
|
|
834 | - |
|
835 | - g :: ∀ a. Eq a => a -> Bool
|
|
836 | - {-# SPECIALISE g @[e] #-}
|
|
837 | - |
|
838 | - Typechecking 'g' will result in [W] Eq [e]. Were we to simplify this to
|
|
839 | - [W] Eq e, we would have difficulty generating a RULE for the specialisation:
|
|
840 | - |
|
841 | - $sg :: Eq e => [e] -> Bool
|
|
842 | - |
|
843 | - RULE ∀ e (d :: Eq [e]). g @[e] d = $sg @e (??? :: Eq e)
|
|
844 | - -- Can't fill in ??? because we can't run instances in reverse.
|
|
845 | - |
|
846 | - RULE ∀ e (d :: Eq e). g @[e] ($fEqList @e d) = $sg @e d
|
|
847 | - -- Bad RULE matching template: matches on the structure of a dictionary
|
|
848 | - |
|
849 | - Moreover, there is no real benefit to any of this, because the specialiser
|
|
850 | - can't do anything useful from the knowledge that a dictionary for 'Eq [e]' is
|
|
851 | - constructed from a dictionary for 'Eq e' using the 'Eq' instance for lists.
|
|
852 | - |
|
853 | -Here, it would make sense to also use the "solve completely" mechanism in the
|
|
854 | -typechecker to avoid producing evidence terms that we can't "run in reverse".
|
|
855 | -However, the current implementation tackles this issue in the desugarer, as is
|
|
856 | -explained in Note [prepareSpecLHS] in GHC.HsToCore.Binds.
|
|
857 | -So, for the time being at least, in TcSSpecPrag mode, we don't attempt to "fully solve"
|
|
858 | -constraints when we use a top-level instance. This might change in the future,
|
|
859 | -were we to decide to attempt to address [Shortcoming] in Note [prepareSpecLHS]
|
|
860 | -in GHC.HsToCore.Binds.
|
|
861 | - |
|
862 | 798 | Note [Handling old-form SPECIALISE pragmas]
|
863 | 799 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
864 | 800 | NB: this code path is deprecated, and is scheduled to be removed in GHC 9.18, as per #25440.
|
... | ... | @@ -1039,7 +975,7 @@ tcSpecPrag poly_id (SpecSigE nm rule_bndrs spec_e inl) |
1039 | 975 | <- tcRuleBndrs skol_info rule_bndrs $
|
1040 | 976 | tcInferRho spec_e
|
1041 | 977 | |
1042 | - -- (2) Solve the resulting wanteds in TcSSpecPrag mode.
|
|
978 | + -- (2) Solve the resulting wanteds
|
|
1043 | 979 | ; ev_binds_var <- newTcEvBinds
|
1044 | 980 | ; spec_e_wanted <- setTcLevel rhs_tclvl $
|
1045 | 981 | runTcSWithEvBinds ev_binds_var $
|
... | ... | @@ -462,30 +462,20 @@ from the instance that we have in scope: |
462 | 462 | case x of { GHC.Types.I# x1 -> GHC.Types.I# (GHC.Prim.+# x1 1#) }
|
463 | 463 | |
464 | 464 | ** NB: It is important to emphasize that all this is purely an optimization:
|
465 | -** exactly the same programs should typecheck with or without this
|
|
466 | -** procedure.
|
|
467 | - |
|
468 | -Solving fully
|
|
469 | -~~~~~~~~~~~~~
|
|
470 | -There is a reason why the solver does not simply try to solve such
|
|
471 | -constraints with top-level instances. If the solver finds a relevant
|
|
472 | -instance declaration in scope, that instance may require a context
|
|
473 | -that can't be solved for. A good example of this is:
|
|
474 | - |
|
475 | - f :: Ord [a] => ...
|
|
476 | - f x = ..Need Eq [a]...
|
|
477 | - |
|
478 | -If we have instance `Eq a => Eq [a]` in scope and we tried to use it, we would
|
|
479 | -be left with the obligation to solve the constraint Eq a, which we cannot. So we
|
|
480 | -must be conservative in our attempt to use an instance declaration to solve the
|
|
481 | -[W] constraint we're interested in.
|
|
482 | - |
|
483 | -Our rule is that we try to solve all of the instance's subgoals
|
|
484 | -recursively all at once. Precisely: We only attempt to solve
|
|
485 | -constraints of the form `C1, ... Cm => C t1 ... t n`, where all the Ci
|
|
486 | -are themselves class constraints of the form `C1', ... Cm' => C' t1'
|
|
487 | -... tn'` and we only succeed if the entire tree of constraints is
|
|
488 | -solvable from instances.
|
|
465 | +** exactly the same programs should typecheck with or without this procedure.
|
|
466 | + |
|
467 | +Consider
|
|
468 | + f :: Ord [a] => ...
|
|
469 | + f x = ..Need Eq [a]...
|
|
470 | +We could use the Eq [a] superclass of the Ord [a], or we could use the top-level
|
|
471 | +instance `Eq a => Eq [a]`. But if we did the latter we'd be stuck with an
|
|
472 | +insoluble constraint (Eq a).
|
|
473 | + |
|
474 | +So the ShortCutSolving rule is this:
|
|
475 | + If we could solve a constraint from a local Given,
|
|
476 | + try first to /completely/ solve the constraint using only top-level instances.
|
|
477 | + - If that succeeds, use it
|
|
478 | + - If not, use the local Given
|
|
489 | 479 | |
490 | 480 | An example that succeeds:
|
491 | 481 | |
... | ... | @@ -511,7 +501,8 @@ An example that fails: |
511 | 501 | f :: C [a] b => b -> Bool
|
512 | 502 | f x = m x == []
|
513 | 503 | |
514 | -Which, because solving `Eq [a]` demands `Eq a` which we cannot solve, produces:
|
|
504 | +Which, because solving `Eq [a]` demands `Eq a` which we cannot solve. so short-cut
|
|
505 | +solving fails and we use the superclass of C:
|
|
515 | 506 | |
516 | 507 | f :: forall a b. C [a] b => b -> Bool
|
517 | 508 | f = \ (@ a) (@ b) ($dC :: C [a] b) (eta :: b) ->
|
... | ... | @@ -521,23 +512,49 @@ Which, because solving `Eq [a]` demands `Eq a` which we cannot solve, produces: |
521 | 512 | (m @ [a] @ b $dC eta)
|
522 | 513 | (GHC.Types.[] @ a)
|
523 | 514 | |
524 | -Note [Shortcut solving: type families]
|
|
525 | -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
526 | -Suppose we have (#13943)
|
|
527 | - class Take (n :: Nat) where ...
|
|
528 | - instance {-# OVERLAPPING #-} Take 0 where ..
|
|
529 | - instance {-# OVERLAPPABLE #-} (Take (n - 1)) => Take n where ..
|
|
530 | - |
|
531 | -And we have [W] Take 3. That only matches one instance so we get
|
|
532 | -[W] Take (3-1). Really we should now rewrite to reduce the (3-1) to 2, and
|
|
533 | -so on -- but that is reproducing yet more of the solver. Sigh. For now,
|
|
534 | -we just give up (remember all this is just an optimisation).
|
|
535 | - |
|
536 | -But we must not just naively try to lookup (Take (3-1)) in the
|
|
537 | -InstEnv, or it'll (wrongly) appear not to match (Take 0) and get a
|
|
538 | -unique match on the (Take n) instance. That leads immediately to an
|
|
539 | -infinite loop. Hence the check that 'preds' have no type families
|
|
540 | -(isTyFamFree).
|
|
515 | +The moving parts are relatively simple:
|
|
516 | + |
|
517 | +* To attempt to solve the constraint completely, we just recursively
|
|
518 | + call the constraint solver. See the use of `tryTcS` in
|
|
519 | + `tcShortCutSolver`.
|
|
520 | + |
|
521 | +* When this attempted recursive solving, we set a special mode
|
|
522 | + `TcSShortCut`, which signals that we are trying to solve using only
|
|
523 | + top-level instances. We switch on `TcSShortCut` mode in
|
|
524 | + `tryShortCutSolver`.
|
|
525 | + |
|
526 | +* When in TcSShortCut mode, we behave specially in a few places:
|
|
527 | + - `tryInertDicts`, where we would otherwise look for a Given to solve our Wantee
|
|
528 | + - `noMatchableGivenDicts`, which also consults the Givens
|
|
529 | + - `matchLocalInst`, which would otherwise consult Given quantified constraints
|
|
530 | + - `GHC.Tc.Solver.Instance.Class.matchInstEnv`: when short-cut solving, don't
|
|
531 | + pick overlappable top-level instances
|
|
532 | + |
|
533 | + |
|
534 | +Some wrinkles:
|
|
535 | + |
|
536 | +(SCS1) Note [Shortcut solving: incoherence]
|
|
537 | + |
|
538 | +(SCS2) The short-cut solver just uses the solver recursively, so we get its
|
|
539 | + full power:
|
|
540 | + |
|
541 | + * We need to be able to handle recursive super classes. The
|
|
542 | + solved_dicts state ensures that we remember what we have already
|
|
543 | + tried to solve to avoid looping.
|
|
544 | + |
|
545 | + * As #15164 showed, it can be important to exploit sharing between
|
|
546 | + goals. E.g. To solve G we may need G1 and G2. To solve G1 we may need H;
|
|
547 | + and to solve G2 we may need H. If we don't spot this sharing we may
|
|
548 | + solve H twice; and if this pattern repeats we may get exponentially bad
|
|
549 | + behaviour.
|
|
550 | + |
|
551 | + * Suppose we have (#13943)
|
|
552 | + class Take (n :: Nat) where ...
|
|
553 | + instance {-# OVERLAPPING #-} Take 0 where ..
|
|
554 | + instance {-# OVERLAPPABLE #-} (Take (n - 1)) => Take n where ..
|
|
555 | + |
|
556 | + And we have [W] Take 3. That only matches one instance so we get
|
|
557 | + [W] Take (3-1). Then we should reduce the (3-1) to 2, and continue.
|
|
541 | 558 | |
542 | 559 | Note [Shortcut solving: incoherence]
|
543 | 560 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
... | ... | @@ -603,37 +620,6 @@ The output of `main` if we avoid the optimization under the effect of |
603 | 620 | IncoherentInstances is `1`. If we were to do the optimization, the output of
|
604 | 621 | `main` would be `2`.
|
605 | 622 | |
606 | -Note [Shortcut try_solve_from_instance]
|
|
607 | -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
608 | -The workhorse of the short-cut solver is
|
|
609 | - try_solve_from_instance :: (EvBindMap, DictMap CtEvidence)
|
|
610 | - -> CtEvidence -- Solve this
|
|
611 | - -> MaybeT TcS (EvBindMap, DictMap CtEvidence)
|
|
612 | -Note that:
|
|
613 | - |
|
614 | -* The CtEvidence is the goal to be solved
|
|
615 | - |
|
616 | -* The MaybeT manages early failure if we find a subgoal that
|
|
617 | - cannot be solved from instances.
|
|
618 | - |
|
619 | -* The (EvBindMap, DictMap CtEvidence) is an accumulating purely-functional
|
|
620 | - state that allows try_solve_from_instance to augment the evidence
|
|
621 | - bindings and inert_solved_dicts as it goes.
|
|
622 | - |
|
623 | - If it succeeds, we commit all these bindings and solved dicts to the
|
|
624 | - main TcS InertSet. If not, we abandon it all entirely.
|
|
625 | - |
|
626 | -Passing along the solved_dicts important for two reasons:
|
|
627 | - |
|
628 | -* We need to be able to handle recursive super classes. The
|
|
629 | - solved_dicts state ensures that we remember what we have already
|
|
630 | - tried to solve to avoid looping.
|
|
631 | - |
|
632 | -* As #15164 showed, it can be important to exploit sharing between
|
|
633 | - goals. E.g. To solve G we may need G1 and G2. To solve G1 we may need H;
|
|
634 | - and to solve G2 we may need H. If we don't spot this sharing we may
|
|
635 | - solve H twice; and if this pattern repeats we may get exponentially bad
|
|
636 | - behaviour.
|
|
637 | 623 | |
638 | 624 | Note [No Given/Given fundeps]
|
639 | 625 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
... | ... | @@ -942,20 +942,16 @@ The constraint solver can operate in different modes: |
942 | 942 | * TcSVanilla: Normal constraint solving mode. This is the default.
|
943 | 943 | |
944 | 944 | * TcSPMCheck: Used by the pattern match overlap checker.
|
945 | - Like TcSVanilla, but the idea is that the returned InertSet will
|
|
946 | - later be resumed, so we do not want to restore type-equality cycles
|
|
947 | - See also Note [Type equality cycles] in GHC.Tc.Solver.Equality
|
|
945 | + Like TcSVanilla, but the idea is that the returned InertSet will
|
|
946 | + later be resumed, so we do not want to restore type-equality cycles
|
|
947 | + See also Note [Type equality cycles] in GHC.Tc.Solver.Equality
|
|
948 | 948 | |
949 | 949 | * TcSEarlyAbort: Abort (fail in the monad) as soon as we come across an
|
950 | 950 | insoluble constraint. This is used to fail-fast when checking for hole-fits.
|
951 | 951 | See Note [Speeding up valid hole-fits].
|
952 | 952 | |
953 | -* TcSSpecPrag: Solve constraints fully or not at all. This is described in
|
|
954 | - Note [TcSSpecPrag].
|
|
955 | - |
|
956 | - This mode is currently used in one place only: when solving constraints
|
|
957 | - arising from specialise pragmas.
|
|
958 | - See Note [Fully solving constraints for specialisation] in GHC.Tc.Gen.Sig.
|
|
953 | +* TcSShortCut: Solve constraints fully or not at all. This is described in
|
|
954 | + Note [Shortcut solving] in GHC.Tc.Solver.Dict
|
|
959 | 955 | -}
|
960 | 956 | |
961 | 957 | data TcSEnv
|
... | ... | @@ -1126,54 +1122,6 @@ runTcSEarlyAbort tcs |
1126 | 1122 | = do { ev_binds_var <- TcM.newTcEvBinds
|
1127 | 1123 | ; runTcSWithEvBinds' TcSEarlyAbort ev_binds_var tcs }
|
1128 | 1124 | |
1129 | --- | Run the 'TcS' monad in 'TcSSpecPrag' mode, which either fully solves
|
|
1130 | --- individual Wanted quantified constraints or leaves them alone.
|
|
1131 | ---
|
|
1132 | - |
|
1133 | -{- Note [TcSSpecPrag]
|
|
1134 | -~~~~~~~~~~~~~~~~~~~~~
|
|
1135 | -The TcSSpecPrag mode is a specialized constraint solving mode that guarantees
|
|
1136 | -that Wanted quantified constraints are either:
|
|
1137 | - - Fully solved with no free evidence variables, or
|
|
1138 | - - Left completely untouched (no simplification at all)
|
|
1139 | - |
|
1140 | -Examples:
|
|
1141 | - |
|
1142 | - * [W] forall x. Eq x => Eq (f x).
|
|
1143 | - |
|
1144 | - In TcSSpecPrag mode, we **do not** process this quantified constraint by
|
|
1145 | - creating a new implication constraint; we leave it alone instead.
|
|
1146 | - |
|
1147 | - * [W] Eq (Maybe Int).
|
|
1148 | - |
|
1149 | - This constraint is solved fully, using two top-level Eq instances.
|
|
1150 | - |
|
1151 | - * [W] forall x. Eq x => Eq [x].
|
|
1152 | - |
|
1153 | - This constraint is solved fully as well, using the Eq instance for lists.
|
|
1154 | - |
|
1155 | -This functionality is crucially used by the specialiser, for which taking an
|
|
1156 | -irreversible constraint solving steps is actively harmful, as described in
|
|
1157 | -Note [Fully solving constraints for specialisation] in GHC.Tc.Gen.Sig.
|
|
1158 | - |
|
1159 | -Note that currently we **do not** refrain from using top-level instances,
|
|
1160 | -even though we also can't run them in reverse; this isn't a problem for the
|
|
1161 | -specialiser (which is currently the sole consumer of this functionality).
|
|
1162 | - |
|
1163 | -The implementation is as follows: in TcSSpecPrag mode, when we are about to
|
|
1164 | -solve a Wanted quantified constraint by emitting an implication, we call the
|
|
1165 | -special function `solveCompletelyIfRequired`. This function recursively calls
|
|
1166 | -the solver but in TcSVanilla mode (i.e. full-blown solving, with no restrictions).
|
|
1167 | -If this recursive call manages to solve all the remaining constraints fully,
|
|
1168 | -then we proceed with that outcome (i.e. we continue with that inert set, etc).
|
|
1169 | -Otherwise, we discard everything that happened in the recursive call, and
|
|
1170 | -continue with the original quantified constraint unchanged.
|
|
1171 | - |
|
1172 | -In the future, we could consider re-using this functionality for the short-cut
|
|
1173 | -solver (see Note [Shortcut solving] in GHC.Tc.Solver.Dict), but we would have to
|
|
1174 | -be wary of the performance implications.
|
|
1175 | --}
|
|
1176 | - |
|
1177 | 1125 | -- | This can deal only with equality constraints.
|
1178 | 1126 | runTcSEqualities :: TcS a -> TcM a
|
1179 | 1127 | runTcSEqualities thing_inside
|
... | ... | @@ -1282,20 +1230,21 @@ setTcLevelTcS lvl (TcS thing_inside) |
1282 | 1230 | nestImplicTcS :: EvBindsVar
|
1283 | 1231 | -> TcLevel -> TcS a
|
1284 | 1232 | -> TcS a
|
1285 | -nestImplicTcS ref inner_tclvl (TcS thing_inside)
|
|
1233 | +nestImplicTcS ev_binds_var inner_tclvl (TcS thing_inside)
|
|
1286 | 1234 | = TcS $ \ env@(TcSEnv { tcs_inerts = old_inert_var }) ->
|
1287 | 1235 | do { inerts <- TcM.readTcRef old_inert_var
|
1288 | 1236 | |
1289 | 1237 | -- Initialise the inert_cans from the inert_givens of the parent
|
1290 | 1238 | -- so that the child is not polluted with the parent's inert Wanteds
|
1239 | + -- See Note [trySolveImplication] in GHC.Tc.Solver.Solve
|
|
1240 | + -- All other InertSet fields are inherited
|
|
1291 | 1241 | ; let nest_inert = inerts { inert_cycle_breakers = pushCycleBreakerVarStack
|
1292 | 1242 | (inert_cycle_breakers inerts)
|
1293 | 1243 | , inert_cans = (inert_givens inerts)
|
1294 | 1244 | { inert_given_eqs = False } }
|
1295 | - -- All other InertSet fields are inherited
|
|
1296 | 1245 | ; new_inert_var <- TcM.newTcRef nest_inert
|
1297 | 1246 | ; new_wl_var <- TcM.newTcRef emptyWorkList
|
1298 | - ; let nest_env = env { tcs_ev_binds = ref
|
|
1247 | + ; let nest_env = env { tcs_ev_binds = ev_binds_var
|
|
1299 | 1248 | , tcs_inerts = new_inert_var
|
1300 | 1249 | , tcs_worklist = new_wl_var }
|
1301 | 1250 | ; res <- TcM.setTcLevel inner_tclvl $
|
... | ... | @@ -1306,7 +1255,7 @@ nestImplicTcS ref inner_tclvl (TcS thing_inside) |
1306 | 1255 | |
1307 | 1256 | #if defined(DEBUG)
|
1308 | 1257 | -- Perform a check that the thing_inside did not cause cycles
|
1309 | - ; ev_binds <- TcM.getTcEvBindsMap ref
|
|
1258 | + ; ev_binds <- TcM.getTcEvBindsMap ev_binds_var
|
|
1310 | 1259 | ; checkForCyclicBinds ev_binds
|
1311 | 1260 | #endif
|
1312 | 1261 | ; return res }
|
... | ... | @@ -260,6 +260,11 @@ more meaningful error message (see T19627) |
260 | 260 | This also applies for quantified constraints; see `-fqcs-fuel` compiler flag and `QCI.qci_pend_sc` field.
|
261 | 261 | -}
|
262 | 262 | |
263 | +{- ********************************************************************************
|
|
264 | +* *
|
|
265 | +* Solving implication constraints *
|
|
266 | +* *
|
|
267 | +******************************************************************************** -}
|
|
263 | 268 | |
264 | 269 | solveNestedImplications :: Bag Implication
|
265 | 270 | -> TcS (Bag Implication)
|
... | ... | @@ -280,7 +285,22 @@ solveNestedImplications implics |
280 | 285 | |
281 | 286 | ; return unsolved_implics }
|
282 | 287 | |
288 | +{- Note [trySolveImplication]
|
|
289 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
290 | +`trySolveImplication` may be invoked while solving simple wanteds, notably from
|
|
291 | +`solveWantedForAll`. It returns a Bool to say if solving succeeded or failed.
|
|
292 | + |
|
293 | +It used `nestImplicTcS` to build a nested scope. One subtle point is that
|
|
294 | +`nestImplicTcS` uses the `inert_givens` (not the `inert_cans`) of the current
|
|
295 | +inert set to initialse the `InertSet` of the nested scope. It super-important not
|
|
296 | +to pollute the sub-solving problem with the unsolved Wanteds of the current scope.
|
|
297 | + |
|
298 | +Whenever we do `solveSimpleGivens`, we snapshot the `inert_cans` into `inert_givens`.
|
|
299 | +(At that moment there should be no Wanteds.)
|
|
300 | +-}
|
|
301 | + |
|
283 | 302 | trySolveImplication :: Implication -> TcS Bool
|
303 | +-- See Note [trySolveImplication]
|
|
284 | 304 | trySolveImplication (Implic { ic_tclvl = tclvl
|
285 | 305 | , ic_binds = ev_binds_var
|
286 | 306 | , ic_given = given_ids
|
... | ... | @@ -977,6 +997,7 @@ solveSimpleGivens givens |
977 | 997 | |
978 | 998 | -- Capture the Givens in the inert_givens of the inert set
|
979 | 999 | -- for use by subsequent calls of nestImplicTcS
|
1000 | + -- See Note [trySolveImplication]
|
|
980 | 1001 | ; updInertSet (\is -> is { inert_givens = inert_cans is })
|
981 | 1002 | |
982 | 1003 | ; cans <- getInertCans
|
... | ... | @@ -1368,6 +1389,8 @@ solveWantedForAll qci tvs theta body_pred |
1368 | 1389 | , unitBag (mkNonCanonical $ CtWanted wanted_ev)) }
|
1369 | 1390 | |
1370 | 1391 | ; traceTcS "solveForAll {" (ppr skol_tvs $$ ppr given_ev_vars $$ ppr wanteds $$ ppr w_id)
|
1392 | + |
|
1393 | + -- Try to solve the constraint completely
|
|
1371 | 1394 | ; ev_binds_var <- TcS.newTcEvBinds
|
1372 | 1395 | ; solved <- trySolveImplication $
|
1373 | 1396 | (implicationPrototype loc_env)
|
... | ... | @@ -1381,9 +1404,13 @@ solveWantedForAll qci tvs theta body_pred |
1381 | 1404 | ; traceTcS "solveForAll }" (ppr solved)
|
1382 | 1405 | ; evbs <- TcS.getTcEvBindsMap ev_binds_var
|
1383 | 1406 | ; if not solved
|
1384 | - then do { addInertForAll qci
|
|
1407 | + then do { -- Not completely solved; abandon that attempt and add the
|
|
1408 | + -- original constraint to the inert set
|
|
1409 | + addInertForAll qci
|
|
1385 | 1410 | ; stopWith (CtWanted wtd) "Wanted forall-constraint:unsolved" }
|
1386 | - else do { setWantedEvTerm dest EvCanonical $
|
|
1411 | + |
|
1412 | + else do { -- Completely solved; build an evidence terms
|
|
1413 | + setWantedEvTerm dest EvCanonical $
|
|
1387 | 1414 | EvFun { et_tvs = skol_tvs, et_given = given_ev_vars
|
1388 | 1415 | , et_binds = evBindMapBinds evbs, et_body = w_id }
|
1389 | 1416 | ; stopWith (CtWanted wtd) "Wanted forall-constraint:solved" } }
|
... | ... | @@ -1404,36 +1431,68 @@ solveWantedForAll qci tvs theta body_pred |
1404 | 1431 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
1405 | 1432 | Solving a wanted forall (quantified) constraint
|
1406 | 1433 | [W] df :: forall a b. (Eq a, Ord b) => C x a b
|
1407 | -is delightfully easy. Just build an implication constraint
|
|
1434 | +is delightfully easy in principle. Just build an implication constraint
|
|
1408 | 1435 | forall ab. (g1::Eq a, g2::Ord b) => [W] d :: C x a
|
1409 | 1436 | and discharge df thus:
|
1410 | 1437 | df = /\ab. \g1 g2. let <binds> in d
|
1411 | 1438 | where <binds> is filled in by solving the implication constraint.
|
1412 | 1439 | All the machinery is to hand; there is little to do.
|
1413 | 1440 | |
1414 | -We can take a more straightforward parth when there is a matching Given, e.g.
|
|
1415 | - [W] dg :: forall c d. (Eq c, Ord d) => C x c d
|
|
1416 | -In this case, it's better to directly solve the Wanted from the Given, instead
|
|
1417 | -of building an implication. This is more than a simple optimisation; see
|
|
1418 | -Note [Solving Wanted QCs from Given QCs].
|
|
1441 | +There are some tricky corners though:
|
|
1442 | + |
|
1443 | +(WFA1) We can take a more straightforward parth when there is a matching Given, e.g.
|
|
1444 | + [W] dg :: forall c d. (Eq c, Ord d) => C x c d
|
|
1445 | + In this case, it's better to directly solve the Wanted from the Given, instead
|
|
1446 | + of building an implication. This is more than a simple optimisation; see
|
|
1447 | + Note [Solving Wanted QCs from Given QCs].
|
|
1448 | + |
|
1449 | +(WFA2) Termination: see #19690. We want to maintain the invariant (QC-INV):
|
|
1450 | + |
|
1451 | + (QC-INV) Every quantified constraint returns a non-bottom dictionary
|
|
1452 | + |
|
1453 | + just as every top-level instance declaration guarantees to return a non-bottom
|
|
1454 | + dictionary. But as #19690 shows, it is possible to get a bottom dicionary
|
|
1455 | + by superclass selection if we aren't careful. The situation is very similar
|
|
1456 | + to that described in Note [Recursive superclasses] in GHC.Tc.TyCl.Instance;
|
|
1457 | + and we use the same solution:
|
|
1458 | + |
|
1459 | + * Give the Givens a CtOrigin of (GivenOrigin (InstSkol IsQC head_size))
|
|
1460 | + * Give the Wanted a CtOrigin of (ScOrigin IsQC NakedSc)
|
|
1461 | + |
|
1462 | + Both of these things are done in solveForAll. Now the mechanism described
|
|
1463 | + in Note [Solving superclass constraints] in GHC.Tc.TyCl.Instance takes over.
|
|
1464 | + |
|
1465 | +(WFA3) We do not actually emit an implication to solve later. Rather we
|
|
1466 | + try to solve it completely immediately using `trySolveImplication`
|
|
1467 | + - If successful, we can build evidence
|
|
1468 | + - If unsuccessful, we abandon the attempt and add the unsolved
|
|
1469 | + forall-constraint to the inert set.
|
|
1419 | 1470 | |
1471 | + Several reasons for this "solve immediately" approach
|
|
1420 | 1472 | |
1421 | -The tricky point is about termination: see #19690. We want to maintain
|
|
1422 | -the invariant (QC-INV):
|
|
1473 | + - It saves quite a bit of plumbing, tracking the emitted implications for
|
|
1474 | + later solving; and the evidence would have to contain as-yet-incomplte
|
|
1475 | + bindings which complicates tracking of unused Givens.
|
|
1423 | 1476 | |
1424 | - (QC-INV) Every quantified constraint returns a non-bottom dictionary
|
|
1477 | + - We get better error messages, about failing to solve, say
|
|
1478 | + (forall a. a->a) ~ (forall b. b->Int)
|
|
1425 | 1479 | |
1426 | -just as every top-level instance declaration guarantees to return a non-bottom
|
|
1427 | -dictionary. But as #19690 shows, it is possible to get a bottom dicionary
|
|
1428 | -by superclass selection if we aren't careful. The situation is very similar
|
|
1429 | -to that described in Note [Recursive superclasses] in GHC.Tc.TyCl.Instance;
|
|
1430 | -and we use the same solution:
|
|
1480 | + - Consider
|
|
1481 | + f :: forall f a. (Ix a, forall x. Eq x => Eq (f x)) => a -> f a
|
|
1482 | + {-# SPECIALISE f :: forall f. (forall x. Eq x => Eq (f x)) => Int -> f Int #-}
|
|
1483 | + This SPECIALISE is treated like an expression with a type signature, so
|
|
1484 | + we instantiate the constraints, simplify them and re-generalise. From the
|
|
1485 | + instantaiation we get [W] d :: (forall x. Eq a => Eq (f x))
|
|
1486 | + and we want to generalise over that. We do not want to attempt to solve it
|
|
1487 | + and them get stuck, and emit an error message. If we can't solve it, better
|
|
1488 | + to leave it alone
|
|
1431 | 1489 | |
1432 | -* Give the Givens a CtOrigin of (GivenOrigin (InstSkol IsQC head_size))
|
|
1433 | -* Give the Wanted a CtOrigin of (ScOrigin IsQC NakedSc)
|
|
1490 | + We still need to simplify quantified constraints that can be
|
|
1491 | + /fully solved/ from instances, otherwise we would never be able to
|
|
1492 | + specialise them away. Example: {-# SPECIALISE f @[] @a #-}.
|
|
1434 | 1493 | |
1435 | -Both of these things are done in solveForAll. Now the mechanism described
|
|
1436 | -in Note [Solving superclass constraints] in GHC.Tc.TyCl.Instance takes over.
|
|
1494 | + You might worry about the wasted work, but it is seldom repeated (because the
|
|
1495 | + constraint solver seldom iterates much).
|
|
1437 | 1496 | |
1438 | 1497 | Note [Solving a Given forall-constraint]
|
1439 | 1498 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|