Opened 3 months ago

Last modified 2 months ago

#15603 new bug

ref6 example from StaticPointers documentation doesn't type check

Reported by: mpickering Owned by:
Priority: normal Milestone: 8.6.1
Component: Compiler Version: 8.4.3
Keywords: StaticPointers, newcomer Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:

Description

The documentation for StaticPointers contains the following example.

ref6 y = let x = 1 in static x

but this doesn't get accepted by the type checker.

sp.hs:27:23: error:
    • ‘x’ is used in a static form but it is not closed because it
      has a non-closed type because it contains the
      type variables: ‘p_a7KP’
    • In the expression: static x
      In the expression: let x = 1 in static x
      In an equation for ‘ref6’: ref6 y = let x = 1 in static x
   |
27 | ref6 y = let x = 1 in static x
   |                       ^^^^^^^

I tested on 8.6.1, 8.4.3, 8.2.2, 8.02, 7.10.3 and all fail.

Change History (9)

comment:1 Changed 3 months ago by monoidal

This is due to Num. How about changing to ref6 y = let x = 'a' in static x?

comment:2 Changed 3 months ago by mpickering

That works. I'm unsure what the y is doing in there as well tbh, it doesn't seem to add anything.

Adding a top-level type signature to this example doesn't fix the problem but specifying (1 :: Int) also fixes it.

comment:3 Changed 3 months ago by monoidal

Yes, we can remove y. Also ref1 is wrong in the same way.

comment:4 Changed 3 months ago by mpickering

You might think so but ref1 type checks without any type signatures.

comment:5 Changed 3 months ago by monoidal

What do you mean? If I create a file

{-# LANGUAGE StaticPointers #-}

ref1 = static 1

then GHCs 8.0-8.4 give an error about missing Typeable t0 instance (or if monomorphism restriction is disabled, about missing Num a)

comment:6 Changed 3 months ago by mpickering

Ah, I had another call to ref1 in my program which was fixing the type. You're right.

comment:7 Changed 3 months ago by mpickering

The static show example also doesn't produce an error exactly like the implied one.

sp.hs:25:7: error:
    • No instance for (Typeable a0) arising from a static form
    • In the expression: static show
      In an equation for ‘foo’: foo = static show
   |
25 | foo = static show
   |       ^^^^^^^^^^^

sp.hs:25:14: error:
    • Ambiguous type variable ‘a0’ arising from a use of ‘show’
      prevents the constraint ‘(Show a0)’ from being solved.
      Relevant bindings include
        foo :: t0 (a0 -> String) (bound at sp.hs:25:1)
      Probable fix: use a type annotation to specify what ‘a0’ should be.
      These potential instances exist:
        instance Show (ST s a) -- Defined in ‘GHC.ST’
        instance Show StaticPtrInfo -- Defined in ‘GHC.StaticPtr’
        instance Show Ordering -- Defined in ‘GHC.Show’
        ...plus 25 others
        ...plus 12 instances involving out-of-scope types
        (use -fprint-potential-instances to see them all)
    • In the body of a static form: show
      In the expression: static show
      In an equation for ‘foo’: foo = static show
   |
25 | foo = static show
   |              ^^^^

Then adding a type signature leads to the confusing error:

foo :: (Typeable a, Show a) => StaticPtr (a -> String)                          
foo = static show
sp.hs:26:14: error:
    • No instance for (Show a) arising from a use of ‘show’
    • In the body of a static form: show
      In the expression: static show
      In an equation for ‘foo’: foo = static show
   |
26 | foo = static show
   | 

The user guide should discuss why these errors happen to help understanding.

comment:8 Changed 3 months ago by simonpj

Keywords: StaticPointers added

comment:9 Changed 2 months ago by mpickering

Keywords: newcomer added
Note: See TracTickets for help on using tickets.