Quantcast

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

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Tom Sydney Kerckhove
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/TyCoRep.html#Type
[2]: https://hackage.haskell.org/package/haskell-src-exts-1.18.2/docs/Language-Haskell-Exts-Syntax.html#t:Type

--
Tom Sydney Kerckhove

_______________________________________________
ghc-devs mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs

attempt.hs (1K) Download Attachment
signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Ben Gamari-2
Tom Sydney Kerckhove <[hidden email]> writes:

> 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


_______________________________________________
ghc-devs mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs

signature.asc (497 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Tom Sydney Kerckhove
On 01-04-17 19:47:15, Ben Gamari wrote:
> Tom Sydney Kerckhove <[hidden email]> 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

_______________________________________________
ghc-devs mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

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

_______________________________________________
ghc-devs mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Ben Gamari-2
Tom Sydney Kerckhove <[hidden email]> writes:

> 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


_______________________________________________
ghc-devs mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs

signature.asc (497 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

GHC - devs mailing list
In reply to this post by Tom Sydney Kerckhove
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:[hidden email]] On Behalf Of Tom
|  Sydney Kerckhove
|  Sent: 02 April 2017 14:02
|  To: Ben Gamari <[hidden email]>
|  Cc: [hidden email]
|  Subject: Re: Translation of GHC typechecker output to haskell-src-
|  exts's 'Type'
|  
|  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
_______________________________________________
ghc-devs mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs
Loading...