Lift instances of the AST-related types

View: New views
6 Messages — Rating Filter:   Alert me  

Lift instances of the AST-related types

by Alfonso Acosta :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

It has been suggested a few types in this list [1,2] that it would be
helpful to include a Lift instance of Exp, Type, Dec and friends.

Having such instances allow to store the AST in a TH-generated
structure for later processing during runtime (something curcial in
the embedded compiler I'm currently developing).

It would be great that the instances were included in the library
itself or if (even better) GHC supported automatic deriving of Lift.

Until that happens, I have used a modified version Ian's th-lift[3] to
automatically generate the instances.

Just in case anyone is interested, I attached the patch of th-lift and
the instantiation module.

Cheers,

Fons


[1] http://www.haskell.org/pipermail/template-haskell/2007-February/000593.html
[2] http://www.haskell.org/pipermail/template-haskell/2004-June/000289.html
[3] http://hackage.haskell.org/cgi-bin/hackage-scripts/package/th-lift-0.2

[LiftInstances.hs]

{-# OPTIONS_GHC -fth #-} -- Due to the use of Template Haskell  
-----------------------------------------------------------------------------
-- |
-- Module      :  ForSyDe.Netlist
-- Copyright   :  (c) The ForSyDe Team 2007
-- License     :  BSD-style (see the file LICENSE)
--
-- Maintainer  :  forsyde_development@...
-- Stability   :  experimental
-- Portability :  portable
--
-- This module provides  'Lift' instances for all the AST-types defined
-- in "Language.Haskell.Syntax":
-- 'Guard' 'Strict', 'Callconv', 'Safety','Body', 'Con', 'FunDep', 'Foreign',
-- 'Lit', 'Pat', 'Match', 'Stmt', 'Range', 'Clause', 'Type', 'Dec', 'Exp'
--
-- Furthermore it provides a 'Lift' instance of 'Ratio'
-- (essential for some of the other instantiations)
--
-----------------------------------------------------------------------------
module Language.Haskell.TH.LiftInstances where

import Language.Haskell.TH
import Language.Haskell.TH.Lift (deriveLift)
import Language.Haskell.TH
 (Guard,
  Strict,
  Callconv,
  Safety,
  Body,
  Con,
  FunDep,
  Foreign,
  Lit,
  Pat,
  Match,
  Stmt,
  Range,
  Clause,
  Type,
  Dec,
  Exp)

import Control.Monad (mapM)
import Data.Ratio (Ratio)

$(mapM deriveLift
      [''Ratio,
       ''Guard,
       ''Strict,
       ''Callconv,
       ''Safety,
       ''Body,
       ''Con,
       ''FunDep,
       ''Foreign,
       ''Lit,
       ''Pat,
       ''Match,
       ''Stmt,
       ''Range,
       ''Clause,
       ''Type,
       ''Dec,
       ''Exp])


[th-lift.patch]

--- Haskell/TH/Lift.hs 2007-02-22 00:44:59.000000000 +0100
+++ /home/fons/asignaturas/ForSyDe/src/Language/Haskell/TH/Lift.hs 2007-10-30 01:56:08.000000000 +0100
@@ -1,10 +1,14 @@
-
+{-# OPTIONS_GHC -fglasgow-exts -fth -fno-warn-deprecations #-}  
+-- Due to the use of unboxed types, TH, and deprecated Packed Strings
+-- Taken from HackageDB
+-- (c) Ian Lynagh, 2006
 module Language.Haskell.TH.Lift where
 
 import GHC.Exts
 import Data.PackedString
 import Language.Haskell.TH
 import Language.Haskell.TH.Syntax
+import Control.Monad (liftM)
 
 modName :: String
 modName = "Language.Haskell.TH.Lift"
@@ -13,8 +17,9 @@
 deriveLift n
  = do i <- reify n
       case i of
-          TyConI (DataD _ _ vs cons _) ->
-              let ctxt = cxt [conT ''Lift `appT` varT v | v <- vs]
+          TyConI (DataD dcxt _ vs cons _) ->
+              let ctxt = liftM (++ dcxt) $
+                         cxt  [conT ''Lift `appT` varT v | v <- vs]
                   typ = foldl appT (conT n) $ map varT vs
                   fun = funD 'lift (map doCons cons)
               in instanceD ctxt (conT ''Lift `appT` typ) [fun]
@@ -27,6 +32,10 @@
           args = [ [| lift $(varE (mkName n)) |] | n <- ns ]
           e = foldl (\e1 e2 -> [| appE $e1 $e2 |]) con args
       clause [conP c (map (varP . mkName) ns)] (normalB e) []
+doCons (InfixC st1 n st2) = doCons (NormalC n [st1,st2])
+doCons (RecC n vsts)  
+ = let st (_, s, t) = (s, t)
+   in doCons (NormalC n (map st vsts))
 doCons c = error (modName ++ ".doCons: Unhandled constructor: " ++ pprint c)
 
 instance Lift Name where


_______________________________________________
template-haskell mailing list
template-haskell@...
http://www.haskell.org/mailman/listinfo/template-haskell

RE: Lift instances of the AST-related types

by Simon Peyton-Jones :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Make a Trac feature request?  As usual that doesn't guaranteed it'll get done, but it saves it getting forgotten.

Using TH to generate the Lift instances seems like the right way to go -- it's what TH is for, and it's 95% as good as deriving( Lift ).

Augmenting Ian's library so that when you got the library you thereby got the Lift instances (themselves generated by the library) for the TH data types.

In that way the whole thing could be done by modifying Ian's library without touching GHC, which is always good.

Simon

| -----Original Message-----
| From: template-haskell-bounces@... [mailto:template-haskell-bounces@...] On Behalf Of Alfonso
| Acosta
| Sent: 30 October 2007 01:43
| To: template-haskell@...
| Subject: [Template-haskell] Lift instances of the AST-related types
|
| Hi,
|
| It has been suggested a few types in this list [1,2] that it would be
| helpful to include a Lift instance of Exp, Type, Dec and friends.
|
| Having such instances allow to store the AST in a TH-generated
| structure for later processing during runtime (something curcial in
| the embedded compiler I'm currently developing).
|
| It would be great that the instances were included in the library
| itself or if (even better) GHC supported automatic deriving of Lift.
|
| Until that happens, I have used a modified version Ian's th-lift[3] to
| automatically generate the instances.
|
| Just in case anyone is interested, I attached the patch of th-lift and
| the instantiation module.
|
| Cheers,
|
| Fons
|
|
| [1] http://www.haskell.org/pipermail/template-haskell/2007-February/000593.html
| [2] http://www.haskell.org/pipermail/template-haskell/2004-June/000289.html
| [3] http://hackage.haskell.org/cgi-bin/hackage-scripts/package/th-lift-0.2
_______________________________________________
template-haskell mailing list
template-haskell@...
http://www.haskell.org/mailman/listinfo/template-haskell

Re: Lift instances of the AST-related types

by Alfonso Acosta :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Forgot to send this answer to the list, sorry.

On 11/1/07, Simon Peyton-Jones <simonpj@...> wrote:
> Make a Trac feature request?

I'll be happy to do it.

> Augmenting Ian's library so that when you got the library you thereby got the Lift instances (themselves generated by the library) for the TH data types.
>
> In that way the whole thing could be done by modifying Ian's library without touching GHC, which is always good.

That's exactly what my patch+new module are supposed to do. I'm still
waiting for Ian to review it and maybe include them in his library.

Besides, since automatic instantiation of Lift seems to be a basic
need for a TH programmer, Wouldn't it be a good idea to merge th-lift
with the mainstream template-haskell library?


Cheers,

Fons
_______________________________________________
template-haskell mailing list
template-haskell@...
http://www.haskell.org/mailman/listinfo/template-haskell

Parent Message unknown Re: Lift instances of the AST-related types

by Alfonso Acosta :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

This recent haskell-cafe thread[1] provides a way to implicity give
support for automatic derivation of Lift in GHC.

Since Data can be derived automatically and there's a way to lift any
"Data a => a" it's like having
automatic derivation for lift.

It's not a substitute of Ian's package (the instace Data a => Lift a
is illegal), but it complements it.

What do you think about including a function like "liftData :: Data a
=> a -> ExpQ" in the library?


On 11/2/07, Alfonso Acosta <alfonso.acosta@...> wrote:

> On 11/2/07, Simon Peyton-Jones <simonpj@...> wrote:
> > It'd be good to check that the Haddock docs for TH.Lift are in good shape.
>
> Well, right now they are inexistent but the library is tiny, that
> shouldn't be a problem.
>
> > I'll leave this up to you two to finalise.
>
> Let's see what Ian thinks
>
> > (In fact it'd be great if you could improve the Haddock docs for the other TH modules.  They are currently laughably bad.  And you are clearly an expert!)
>
> I cannot agree more, when I began to use TH I was forced (and I keep
> doing it) to look at the source comments to check what the AST-types
> meant.
>
> I cannot promess anything for the short-term, but I'll try to send a
> documentation patch at some point.
>
_______________________________________________
template-haskell mailing list
template-haskell@...
http://www.haskell.org/mailman/listinfo/template-haskell

Re: Lift instances of the AST-related types

by Alfonso Acosta :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Forgot to include the link to the aforementioned thread.

[1] http://www.haskell.org/pipermail/haskell-cafe/2007-November/033955.html
_______________________________________________
template-haskell mailing list
template-haskell@...
http://www.haskell.org/mailman/listinfo/template-haskell

Re: Lift instances of the AST-related types

by Ian Lynagh :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Sat, Nov 03, 2007 at 04:47:48PM +0100, Alfonso Acosta wrote:
>
> It's not a substitute of Ian's package (the instace Data a => Lift a
> is illegal), but it complements it.

You can have that instance with undecidable and overlapping instances,
can't you? Or is there another problem I'm missing?


Thanks
Ian

_______________________________________________
template-haskell mailing list
template-haskell@...
http://www.haskell.org/mailman/listinfo/template-haskell
LightInTheBox - Buy quality products at wholesale price