summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthew Pickering <matthewtpickering@gmail.com>2022-03-02 16:53:38 +0000
committerMatthew Pickering <matthewtpickering@gmail.com>2022-03-03 10:50:57 +0000
commitb3f5e346c29c34af3ce72e144d3ba732e12f71c0 (patch)
treee1b323299af9ebd83f4464b2b2dd45ed50ac0b45
parentaeea6bd588060108dea88996c19f48b9e50adad2 (diff)
downloadhaskell-wip/rules-docs.tar.gz
docs: Add note to RULES documentation about locally bound variables [skip ci]wip/rules-docs
Fixes #20100
-rw-r--r--docs/users_guide/exts/rewrite_rules.rst25
1 files changed, 25 insertions, 0 deletions
diff --git a/docs/users_guide/exts/rewrite_rules.rst b/docs/users_guide/exts/rewrite_rules.rst
index b384faeb9e..7ac93bb21d 100644
--- a/docs/users_guide/exts/rewrite_rules.rst
+++ b/docs/users_guide/exts/rewrite_rules.rst
@@ -204,6 +204,31 @@ From a semantic point of view:
(b) large or a redex, then it would not be substituted, and the rule
would not fire.
+- GHC will never match a forall'd variable in a template with an expression
+ which contains locally bound variables. For example, it is permitted to write
+ a rule which contains a case expression::
+
+ {-# RULES
+ "test/case-tup" forall (x :: (Int, Int)) (y :: Int) (z :: Int).
+ test (case x of (l, r) -> y) z = case x of (l, r) -> test y z
+ #-}
+
+ But the rule will not match when ``y`` contains either of ``l`` or ``r`` because
+ they are locally bound. Therefore the following application will fail to trigger
+ the rule::
+
+ prog :: (Int, Int) -> (Int, Int)
+ prog x = test (case x of (p, q) -> p) 0
+
+ because ``y`` would have to match against ``p`` (which is locally bound)
+ but it will fire for::
+
+ prog :: (Int, Int) -> (Int, Int)
+ prog x = test (case x of (p, q) -> 0) 0
+
+ because ``y`` can match against ``0``.
+
+
.. _rules-inline:
How rules interact with ``INLINE``/``NOINLINE`` pragmas