[GHC] #12085: Premature defaulting and variable not in scope
GHC
ghc-devs at haskell.org
Thu May 19 07:41:25 UTC 2016
#12085: Premature defaulting and variable not in scope
-------------------------------------+-------------------------------------
Reporter: Iceland_jack | Owner:
Type: task | Status: new
Priority: normal | Milestone:
Component: Compiler | Version: 8.0.1
Keywords: | Operating System: Unknown/Multiple
TypeApplications |
Architecture: | Type of failure: None/Unknown
Unknown/Multiple |
Test Case: | Blocked By:
Blocking: | Related Tickets:
Differential Rev(s): | Wiki Page:
-------------------------------------+-------------------------------------
{{{#!hs
{-# Language RankNTypes, ScopedTypeVariables, TypeFamilies,
TypeOperators, UnboxedTuples, UnicodeSyntax, ViewPatterns,
QuasiQuotes, TypeInType, ApplicativeDo, TypeApplications,
AllowAmbiguousTypes
#-}
import Data.Kind
todo :: forall (a::Type). (Read a, Show a) => String
todo = show @a (read @a "42")
}}}
there are two things I notice, even with `AllowAmbiguousTypes` the `a`
gets defaulted prematurely
{{{
$ ghci -ignore-dot-ghci -fwarn-type-defaults /tmp/tl0z.hs
GHCi, version 8.0.0.20160511: http://www.haskell.org/ghc/ :? for help
[1 of 1] Compiling Main ( /tmp/tl0z.hs, interpreted )
Ok, modules loaded: Main.
*Main> :t todo
<interactive>:1:1: warning: [-Wtype-defaults]
Defaulting the following constraints to type ‘()’
(Read a0) arising from a use of ‘it’ at <interactive>:1:1
(Show a0) arising from a use of ‘it’ at <interactive>:1:1
foo :: String
*Main>
}}}
instead of something like `todo :: forall a. (Read a, Show a) => String`,
it can be applied to types
{{{
*Main> foo @Int
"42"
*Main> foo @Float
"42.0"
}}}
----
The second thing is that if you want to add an argument independent of `a`
{{{#!hs
-- ghci> foo @Int False
-- "100"
-- ghci> foo @Float True
-- "42.0"
foo :: forall (a::Type). (Read a, Show a) => Bool -> String
foo b = show @a (read @a (if b then "42" else "100"))
}}}
I found no way to define it as
{{{#!hs
-- ghci> foo False @Int
-- "100"
-- ghci> foo True @Float
-- "42.0"
foo :: Bool -> forall (a::Type). (Read a, Show a) => String
foo b = show @a (read @a (if b then "42" else "100"))
}}}
to which GHC persists
{{{
$ ghci -ignore-dot-ghci -fwarn-type-defaults /tmp/tl0z.hs
GHCi, version 8.0.0.20160511: http://www.haskell.org/ghc/ :? for help
[1 of 1] Compiling Main ( /tmp/tl0z.hs, interpreted )
/tmp/tl0z.hs:10:15: error: Not in scope: type variable ‘a’
/tmp/tl0z.hs:10:24: error: Not in scope: type variable ‘a’
Failed, modules loaded: none.
Prelude>
}}}
--
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/12085>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler
More information about the ghc-tickets
mailing list