Interactive: Assigning polymorphic functions commits to dict:free

Bug #711099 reported by Matt Giuca
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Mars
Triaged
Wishlist
Matt Giuca

Bug Description

This bug is documented in the FAQ under "I am getting “Runtime Error: show:free” or “eq:free” at the interactive prompt".

This only occurs if you assign a polymorphic function which calls (or is) show or eq to an interpreter variable, then call that. For example:

?> x = show
?> x(1)
Runtime Error: show:free

This is because when show (with polymorphic type forall a. a -> Array(Int)) is assigned to the interpreter variable, its type becomes monomorphic (a -> Array(Int), but the type 'a' is a free variable which could be instantiated in the future). Because a type dictionary is required immediately, Mars commits (prematurely) to dict:free. This causes the wrong type dictionary to be called when the type is finally unified to Int.

It is a difficult problem to solve. The ultimate solution is to make interpreter variables polymorphic; then x can have type forall a. a -> Array(Int), with expected output:

?> x = show
?> x(1)
[49]
?> x([])
[91, 93]

Another solution is to somehow delay the choice of type dict until x's types are fully bound. This would give the same behaviour as before the type dictionary merge:

?> x = show
?> x(1)
[49]
?> x([])
Type error in expression 'x([])'
        Term: []
        Type: Array(t_2)
        Expected: Int.

(i.e., x is still monomorphic, but on its first use any type can be given).

Note that Haskell's monomorphism restriction causes this example to behave differently (probably not desirable, but you can get the fully polymorphic version above by using an explicit type declaration):

Prelude> let x = show
Prelude> :t x
x :: () -> String
Prelude> x(1::Int)
    Couldn't match expected type `()' against inferred type `Int'

It commits to a default type.

The final solution (probably the simplest for Mars) is to disallow an assignment to a variable without all types bound:

?> x = show
Cannot assign variable with type a -> Array(Int); type variable 'a' is free.

Note that there will be a problem: the :e printout will try to instantiate such things.

Tags: interactive
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.