Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GHC.Prim.Any's created instead of free variable [in polymorphic function]

Tags:

field

haskell

ghc

Is this a bug?

{-# LANGUAGE NoMonomorphismRestriction #-}
import qualified Text.Parsec.Token as P
import Text.Parsec.Language (haskellDef)
(P.TokenParser { P.identifier = ident }) = P.makeTokenParser haskellDef

yields ident of type Text.Parsec.Prim.ParsecT String GHC.Prim.Any Data.Functor.Identity.Identity String, whereas defining

haskell = P.makeTokenParser haskellDef
ident = P.identifier haskell

yields one of type Text.Parsec.Prim.ParsecT String u Data.Functor.Identity.Identity String

edit

The behavior is not identical in ghci,

infixl 4 <++>
(<++>) = liftM2 (++)

(P.TokenParser { P.identifier = ident }) = P.makeTokenParser haskellDef
suitable_macro = ident

parseMacro = many space *> suitable_macro

parseMacro' =
    try (string "{{" *> parseMacro <* string "}}")

parseAll = many (noneOf "{") <++>
    option "" (parseMacro' <|> (string "{" <++> parseAll))

Then, try to run it,

*Hz2.Preproc> parseTest parseAll "asdf{{b}}"

<interactive>:0:11:
    Couldn't match expected type `()' with actual type `GHC.Prim.Any'
    Expected type: Parsec String () a0
    Actual type: ParsecT
                    String GHC.Prim.Any Data.Functor.Identity.Identity [Char]
    In the first argument of `parseTest', namely `parseAll'
    In the expression: parseTest parseAll "asdf{{b}}"
like image 594
gatoatigrado Avatar asked Nov 20 '25 04:11

gatoatigrado


1 Answers

Not really; I believe it's Report-compliant behaviour: fully polymorphic type variables in patterns are instantiated to Any. However, in GHC 7.2 onwards, this works like you'd expect (see especially the commit message at the end).

As for the GHCi behaviour, this is because GHCi's extended defaulting rules default fully-polymorphic variables to ().

like image 77
ehird Avatar answered Nov 21 '25 17:11

ehird



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!