[Haskell-cafe] Re: Implicit newtype unwrapping

Antoine Latter aslatter at gmail.com
Thu Dec 3 23:33:06 EST 2009


On Thu, Dec 3, 2009 at 9:10 PM, Antoine Latter <aslatter at gmail.com> wrote:
> On Thu, Dec 3, 2009 at 6:00 PM, Joachim Breitner
> <mail at joachim-breitner.de> wrote:
>>
>> But when I uncommented the definition of toFoo and fromfoo, I got:
>>
>> Demo.hs:11:9:
>>    Couldn't match expected type `Foo' against inferred type `Int'
>>    In the expression: id
>>    In the definition of `toFoo': toFoo = id
>>    In the second argument of `openNewtype', namely
>>        `[d| nullFoo :: Foo
>>             nullFoo = 0
>>             toFoo :: Int -> Foo
>>             toFoo = id
>>             .... |]'
>>
>> And just now, after writing half the code, I find out that $( fun
>> [d|...|] ) runs the type checker on the declarations before passing them
>> to fun, which of course kills my whole approach here, as only having the
>> declarations pass through openNewType will make them type check.
>>
>> Is there any way to pass declarations to a TH function so that their
>> names are resolved, but their type is not checked (or, alternatively,
>> type errors are ignored).
>>
>> If not, what would be a sane work-around?
>>
>
> You could switch over to using a quasi-quoter. I think there's one on
> hackage for parsing haskell declarations you might be able to start
> with:
>
> http://hackage.haskell.org/package/haskell-src-meta
>
> More on GHC quasi-quotations:
>
> http://www.haskell.org/ghc/docs/latest/html/users_guide/template-haskell.html#th-quasiquotation
>

So I gave it a try:

http://hpaste.org/fastcgi/hpaste.fcgi/view?id=13575

But it doesn't work, and I don't understand TH enough to figure out
why. I'm guessing that the TH code can't associate the parsed type
name with the passed-in type name, so we don't know to do the proper
unwrapping/wrapping.

Antoine


More information about the Haskell-Cafe mailing list