[Git][ghc/ghc][master] Give more guarntees about ImplicitParams (#23289)

Marge Bot (@marge-bot) gitlab at gitlab.haskell.org
Tue Apr 25 22:13:36 UTC 2023



Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC


Commits:
74c55712 by Andrei Borzenkov at 2023-04-25T18:13:19-04:00
Give more guarntees about ImplicitParams (#23289)

- Added new section in the GHC user's guide that legends behavior of
nested implicit parameter bindings in these two cases:

  let ?f = 1 in let ?f = 2 in ?f

and

  data T where MkT :: (?f :: Int) => T

  f :: T -> T -> Int
  f MkT MkT = ?f

- Added new test case to examine this behavior.

- - - - -


4 changed files:

- docs/users_guide/exts/implicit_parameters.rst
- + testsuite/tests/simplCore/should_run/T23289.hs
- + testsuite/tests/simplCore/should_run/T23289.stdout
- testsuite/tests/simplCore/should_run/all.T


Changes:

=====================================
docs/users_guide/exts/implicit_parameters.rst
=====================================
@@ -180,6 +180,27 @@ parameter. So we get the following results in GHCi:
 Adding a type signature dramatically changes the result! This is a
 rather counter-intuitive phenomenon, worth watching out for.
 
+Implicit parameters scoping guarantees
+-------------------------------------
+
+GHC always takes the most nested implicit parameter binding from the
+context to find the value. Consider the following code::
+
+      let ?f = 1 in let ?f = 2 in ?f
+
+This expression will always return 2.
+
+Another example of this rule is matching over constructors with constraints.
+For example::
+
+      data T where
+        MkT :: (?f :: Int) => T
+
+      f :: T -> T -> Int
+      f MkT MkT = ?f
+
+Here GHC will always take ``?f`` from the last match.
+
 Implicit parameters and monomorphism
 ------------------------------------
 
@@ -199,5 +220,3 @@ a type signature for ``y``, then ``y`` will get type
 ``(?x::Int) => Int``, so the occurrence of ``y`` in the body of the
 ``let`` will see the inner binding of ``?x``, so ``(f 9)`` will return
 ``14``.
-
-


=====================================
testsuite/tests/simplCore/should_run/T23289.hs
=====================================
@@ -0,0 +1,13 @@
+{-# LANGUAGE ImplicitParams, GADTs #-}
+module Main where
+
+data T where
+  MkT :: (?f :: Int) => T
+
+f :: T -> T -> Int
+f MkT MkT = ?f
+
+main :: IO ()
+main = do
+  print (let ?g = 1 in let ?g = 2 in ?g)
+  print $ f (let ?f = 3 in MkT) (let ?f = 4 in MkT)


=====================================
testsuite/tests/simplCore/should_run/T23289.stdout
=====================================
@@ -0,0 +1,2 @@
+2
+4


=====================================
testsuite/tests/simplCore/should_run/all.T
=====================================
@@ -111,3 +111,4 @@ test('T22448', normal, compile_and_run, ['-O1'])
 test('T22998', normal, compile_and_run, ['-O0 -fspecialise -dcore-lint'])
 test('T23184', normal, compile_and_run, ['-O'])
 test('T23134', normal, compile_and_run, ['-O0 -fcatch-nonexhaustive-cases'])
+test('T23289', normal, compile_and_run, [''])



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/74c557121fbcae32abd3b4a69513f8aa7d536073

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/74c557121fbcae32abd3b4a69513f8aa7d536073
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/20230425/7fafc0d1/attachment-0001.html>


More information about the ghc-commits mailing list