Premature defaulting and variable not in scope
{-# 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
-- 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
-- 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>
Trac metadata
Trac field | Value |
---|---|
Version | 8.0.1 |
Type | Task |
TypeOfFailure | OtherFailure |
Priority | normal |
Resolution | Unresolved |
Component | Compiler |
Test case | |
Differential revisions | |
BlockedBy | |
Related | |
Blocking | |
CC | |
Operating system | |
Architecture |