Skip to content

GitLab

  • Menu
Projects Groups Snippets
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
  • Sign in / Register
  • GHC GHC
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
    • Locked Files
  • Issues 4,865
    • Issues 4,865
    • List
    • Boards
    • Service Desk
    • Milestones
    • Iterations
  • Merge requests 461
    • Merge requests 461
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
    • Test Cases
  • Deployments
    • Deployments
    • Releases
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Code review
    • Insights
    • Issue
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Glasgow Haskell Compiler
  • GHCGHC
  • Issues
  • #12085
Closed
Open
Created May 19, 2016 by Icelandjack@IcelandjackReporter

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
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information
Assignee
Assign to
Time tracking