Translation of GHC typechecker output to haskell-src-exts's 'Type'

Dear GHC Devs, Because of the unwieldy nature of the data that the GHC type checker outputs, I am trying to convert a GHC 'Type' [1] to a haskell-src-ext 'Type' [2]. The translation does not need to be perfect for now, but I would at least like to be able to translate function types and types that involve type-class constraints. (See my initial attempt in attachment) Has this ever been done before? Could you point me to some documentation on GHC's 'Type' [1] that could help me with writing this function? (The comments in code aren't nearly enough for me.) In particular, I am having trouble finding type class constraints in the 'Type'. Thank you for your time. [1]: https://downloads.haskell.org/~ghc/8.0.2/docs/html/libraries/ghc-8.0.2/src/T... [2]: https://hackage.haskell.org/package/haskell-src-exts-1.18.2/docs/Language-Ha... -- Tom Sydney Kerckhove

Tom Sydney Kerckhove
Dear GHC Devs,
Hi Tom,
Because of the unwieldy nature of the data that the GHC type checker outputs, I am trying to convert a GHC 'Type' [1] to a haskell-src-ext 'Type' [2].
The translation does not need to be perfect for now, but I would at least like to be able to translate function types and types that involve type-class constraints. (See my initial attempt in attachment)
Has this ever been done before?
I'm not aware of any implementations, but I can't claim to have seen every usage of haskell-src-exts.
Could you point me to some documentation on GHC's 'Type' [1] that could help me with writing this function? (The comments in code aren't nearly enough for me.)
I'm afraid the notes in TyCoRep are really all we have. Note that you should also familiarize yourself with the TyCon type (for reasons you'll see below).
In particular, I am having trouble finding type class constraints in the 'Type'.
During typechecking class constraints are turned into dictionary arguments. If I'm not mistaken you can pick these out by looking for AlgTyCons (a constructor of the TyCon type, see TyCon.hs) with a algTcParent matching (ClassTyCon cls _). cls will be the name of the associated class. I hope this helps. Cheers, - Ben

On 01-04-17 19:47:15, Ben Gamari wrote:
Tom Sydney Kerckhove
writes: Dear GHC Devs,
Hi Tom,
Hi Mr Gamari
I'm afraid the notes in TyCoRep are really all we have. Note that you should also familiarize yourself with the TyCon type (for reasons you'll see below).
In particular, I am having trouble finding type class constraints in the 'Type'.
During typechecking class constraints are turned into dictionary arguments.
Oh, that's already done by then? It makes a lot more sense now why I get this: ghcs translation: Ord a -> [a] -> [a] haskell-src-exts: Ord a => [a] -> [a] I'm guessing that this 'Ord a' that I get, is the type of the dictionary for 'Ord' in the case of 'a'. Is there a way to access the types before this translation happens? It's okay if I have to assume that type-checking succeeds...
If I'm not mistaken you can pick these out by looking for AlgTyCons (a constructor of the TyCon type, see TyCon.hs) with a algTcParent matching (ClassTyCon cls _). cls will be the name of the associated class.
I hope this helps.
This does help, thank you!
Cheers,
Thank you for your time.
- Ben
-- Tom Sydney Kerckhove

On 02-04-17 14:40:05, Tom Sydney Kerckhove wrote:
Is there a way to access the types before this translation happens? It's okay if I have to assume that type-checking succeeds...
I have found a way to do what I want, even after this translation. It relies on the fact that type class constraints always occur on the left side of the result of splitFunTy. Thank you for your help! -- Tom Sydney Kerckhove

Tom Sydney Kerckhove
On 02-04-17 14:40:05, Tom Sydney Kerckhove wrote:
Is there a way to access the types before this translation happens? It's okay if I have to assume that type-checking succeeds...
I have found a way to do what I want, even after this translation. It relies on the fact that type class constraints always occur on the left side of the result of splitFunTy.
Yep, that is what I would do as well.
Thank you for your help!
No worries and good luck. Cheers, - Ben

Tom
Please start a wiki page, somewhere under https://ghc.haskell.org/trac/ghc/wiki/Commentary
that gives the information you wish was more clearly described.
And/or add a Note in TyCoRep to explain.
My answer to your question would be:
The type that Haskell programmers write
Ord a => a -> a
is represented in Type using FunTy, thus
Ord a -> a -> a
There is no "=>" data contructor.
Instead you can tell the difference between -> and => by looking at the kind of the bit before the ->. Thus
ty1 -> ty2
if ty1 :: Constraint, then it it's a "=>" otherwise a "->".
Use (isPredTy ty1) to tell the difference.
Does that help
Simon
| -----Original Message-----
| From: ghc-devs [mailto:ghc-devs-bounces@haskell.org] On Behalf Of Tom
| Sydney Kerckhove
| Sent: 02 April 2017 14:02
| To: Ben Gamari
participants (3)
-
Ben Gamari
-
Simon Peyton Jones
-
Tom Sydney Kerckhove