RE: [Template-haskell] Using a Typ as a Type
| > TH doesn't support type splices today; that's one of the things I'm | > actively working on. But I'm not sure if this is what you meant. | | It is but it is very important that: | | 1) We can do dictionary lookups using the spliced type. | | | 2) The lookup happen at compile time so that it can affects the | code being generated. | | 3) Instances declared in the code being compiled (like the DIS | instances above) can be found by this mechanism. That is, | I'm not interested in finding any DIS instances that might have | been present in the source code for ghc. Type splices will behave exactly like expression splices today. I'm not sure whether that fulfils your 1-3 reqts. Perhaps the thing to do is to give a simple but concrete example of what you'd like to do. I couldn't understand the example you gave last time, as I mentioned. Do you think it still says what you mean? Could you give a line by line explanation of what you think should take place? Remember, the execution of TH program can be described by ordinary rewriting rules (replace the LHS of a function by the RHS of the function, suitably instantiated), augmented with the one extra rule $[| e |] = e (Unless you use explicit do-notation.) Simon
Perhaps the thing to do is to give a simple but concrete example of what you'd like to do.
The following is a simplified version of what I tried to do in Template Greencard. -- a new class class X a where -- member parameterized on result type f :: String -> a -- member parameterized by argument type ctype :: a -> String -- instances for old types instance X Int where f = read ctype _ = "HsInt" -- FFI-defined C type corresponding to Int -- instances for freshly defined types newtype T = T Int deriving Show -- included to make the example work instance X T where f s = T (read s) g (T x) = g x generate :: String -> Q Typ -> Q [Dec] generate nm typ = do -- The next line contains the type splice. -- Note that it is used to perform a compile-time dictionary -- lookup not a runtime lookup. -- I'm not wedded let x = f "1" :: $typ -- Generate C code (should be written to a file, not stdout) -- Code generated depends on type argument qIO (putStrLn (ctype x ++ " " ++ nm ++ " = " ++ show x ++ ";\n") -- return a variable definition. -- again, the definition returned depends on the type -- because it uses 'x' which was produced as a result of -- the compile-time dictionary lookup. [d| $nm = $(literal x) |]
Remember, the execution of TH program can be described by ordinary rewriting rules (replace the LHS of a function by the RHS of the function, suitably instantiated), augmented with the one extra rule $[| e |] = e
Should this apply to types as well? -- Alastair
On Wednesday 03 September 2003 2:19 pm, Simon Peyton-Jones wrote:
| > TH doesn't support type splices today; that's one of the things I'm | > actively working on. But I'm not sure if this is what you meant.
btw Something that would be very nice is if $ could be used to splice patterns and declarations as well as expressions so that I could write: foo :: String -> Q [Dec] foo x = [| \ $(pat x) -> $(var x) |] or bar :: Q Expr -> Q Dec -> Q [Dec] bar e binding = [| let $binding in $e |] These examples aren't that compelling but I've had to code round not being able to write code like this lots of times. -- Alastair ps This is quite independent of my request for type splices. The above makes it easier to write what you can already write whereas type splices extend the power of TH to let you write things that aren't possible at the moment.
participants (2)
-
Alastair Reid -
Simon Peyton-Jones