Re: [Haskell-cafe] Dynamically typing TH.Exp at runtime

Sorry, maybe it I didn't made it clear enough.
Perhaps I'm misunderstanding something, but since one can infer types in GHCI, that implies one can infer types in the GHC API; since Hint wraps the GHC API, that implies one can infer types in Hint, doesn't it? And indeed, there are functions to infer the type of a String; iirc I've even used them myself in mueval.
That's true, inferring the type of a string is not the problem. However, I am not only interested in the top-level type, but also in the type of any arbitrary subexpression. Consider for example following recursive definition: foo [] = [] foo (x:xs) = (:)(x+1)(foo xs) Typing only the first rule one would says its type is '[a] -> [a]'. However, the second rule specialises the type to 'Num a => [a] -> [a]'. So, if I am interested in the type of the 'x' in the pattern of the second rule, I need to be aware of its binding on the right-hand side. Consequently the type of 'x' is 'Num a'. AFAIK, I need Hendley-Milner type inference for that (please correct me if I am wrong) and I was wondering if I really need to implement it by myself, when it is already somewhere inside GHC. Thanks, Martin

On 2009 Mar 13, at 4:03, Martin Hofmann wrote:
That's true, inferring the type of a string is not the problem. However, I am not only interested in the top-level type, but also in the type of any arbitrary subexpression. Consider for example following recursive definition:
I'm pretty sure you can pull a typed AST out of ghc-api and query the type of any node. -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH

Brandon S. Allbery KF8NH wrote:
I'm pretty sure you can pull a typed AST out of ghc-api and query the type of any node.
Thanks. I am relieved to hear that. Could anybody tell me, where to start in the ghc-api? It's pretty hard, when you don't know where to look. Hoogle doesn't know anything about it. I found following DT describing the core language, but as far as I see, there are no type in there either. http://www.haskell.org/ghc/docs/latest/html/libraries/ghc/CoreSyn.html#t %3AExpr Thanks a lot, Martin

Hi Martin,
Could anybody tell me, where to start in the ghc-api? It's pretty hard, when you don't know where to look. Hoogle doesn't know anything about it. I found following DT describing the core language, but as far as I see, there are no type in there either. I've been working on parsing core in the past few months. For an example, look here: http://git.stderr.nl/gitweb?p=matthijs/projects/fhdl.git;a=blob;f=Translator...
The loadModule and findBind functions are interesting. As for iterating the AST, have a look at Flatten.hs. Good luck, any questions are welcome! Gr. Matthijs

Matthijs Kooijman wrote:
I've been working on parsing core in the past few months. For an example, look here: http://git.stderr.nl/gitweb?p=matthijs/projects/fhdl.git;a=blob;f=Translator...
The loadModule and findBind functions are interesting. As for iterating the AST, have a look at Flatten.hs.
Thanks, Matthijs! This helps a lot and looks very promising! Only to avoid misunderstandings, you only use the ghc-api to get the AST of a CoreModule which you translate into your own data structure (VHDL). At this point, you don't use type information, right? Martin

Hi Martin,
Only to avoid misunderstandings, you only use the ghc-api to get the AST of a CoreModule which you translate into your own data structure (VHDL). That's correct.
At this point, you don't use type information, right? I use some typing information, but that's mostly hidden away. In particular, when creating Signals using genSignalId I annotate them with the type of the expression the signal models. Later on, I translate this type to a VHDL type.
To get at the type of an expression, I use the CoreUtils.exprType function, which gives you the type of any CoreExpr. Gr. Matthijs

Okay, many thanks. That's exactly I need (I hope :-) ). Cheers, Martin
participants (3)
-
Brandon S. Allbery KF8NH
-
Martin Hofmann
-
Matthijs Kooijman