[Haskell-cafe] Re: Implicit newtype unwrapping

Antoine Latter aslatter at gmail.com
Thu Dec 3 22:10:31 EST 2009


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

Antoine


More information about the Haskell-Cafe mailing list