Skip to content
This repository has been archived by the owner on Apr 25, 2020. It is now read-only.

Type constraints #731

Merged
merged 13 commits into from
Jul 10, 2016
Merged

Type constraints #731

merged 13 commits into from
Jul 10, 2016

Conversation

lierdakil
Copy link
Collaborator

As discussed in #69.

This is quite obviously a work-in-progress.

Travis build will most likely fail, I choose to ignore that for the moment.

P.S. Please take a look at https://github.com/atom-haskell/ghc-mod/commit/966c694dbf9133064c5fc3996eefc51afc3585f7#diff-a6e0dcd858fc676b788ac178c989b8a0R99 -- I have no idea how to proceed there (and if it's at all possible) nvm, it seems I figured it out. Not sure if it work in all cases though.

I try to compute constraints for "compound" types.
While constraint-building itself is simple, types
do not match due to different representation
between 'internal' and 'exported' signatures.
* Instead of listify, it walks the tree instead, so that constraints are only visible in relevant context.
* Uses Map instead of [(,)]
* Performs type substitutions in case of one child identifier found (relevant for constructor bindings)
It didn't do much anyway
This is required for GHC<7.10 due to a panic
@lierdakil
Copy link
Collaborator Author

Okay, this should work reasonably well (at least for some cases). I'm a bit concerned about this part https://github.com/DanielG/ghc-mod/pull/731/files#diff-1d7b042f1347cb873a911bf788cbad95R79, since it assumes that polymorphic and respective monomorphic types have the same number of type variables in the same order. I think it's a reasonable assumption, but I can't be sure. There might be a better way to do this.

I'd like some test cases thrown my way, my imagination comes to a grinding halt when I try to invent something reasonably strange to test this on.

@DanielG
Copy link
Owner

DanielG commented Jan 19, 2016

I'll have a look this weekend, I have a bunch of stuff to do this week.

@lierdakil
Copy link
Collaborator Author

Sure, no urgency here.

@DanielG
Copy link
Owner

DanielG commented Jan 26, 2016

Argh sorry, I just didn't have any time last weekend and exam period has started now so it's going to be a while until I can find some time :/

@lierdakil
Copy link
Collaborator Author

Can't say I'm in a hurry with this, so take your time. Only concern here is I might forget how all this works eventually, but I doubt it will be that long :)

@lierdakil
Copy link
Collaborator Author

Okay, so I've half-forgotten why or how this works... Any updates?

@DanielG
Copy link
Owner

DanielG commented Mar 1, 2016

In fact I have some time :)

Do you feel like dropping by IRC so we can discuss this in realtime?

@lierdakil
Copy link
Collaborator Author

Okay. Don't exactly have much time atm, though, but I can spare half an hour or so. ghc-mod@irc.freenode.org, right?

@DanielG
Copy link
Owner

DanielG commented Mar 1, 2016

Oh, don't worry about it then I'll just have a play around with it and report back.

@lierdakil
Copy link
Collaborator Author

That works. Have fun :)

@DanielG
Copy link
Owner

DanielG commented Mar 1, 2016

Oh and yes #ghc-mod @ irc.freenode.net is right. If you ever do find the time just drop by there and ping me (dxld or dxld_ in there). There are some other things we should talk about one of these days.

@DanielG
Copy link
Owner

DanielG commented Mar 9, 2016

Sorry I took so long. I was tracking down what I thought to be a bug in optparse-applicative, turns out I had the wrong ghc-mod exe on my PATH, doh! Anyways I can't find anything wrong with this, seems to work with more complex binds which have patterns on the lhs too :)

So with the remaining half-knowledge of how this works could you write some docs for collectSpansTypes? I'm curious to know how you solved the problem with zipping up AbsBindrs for VarPats with larger patterns.

When I was working on this something like the thing below seemed to present a major problem since you'd only get AbsBindrs for foo and bar seperately and you'd have to merge them into that larger tuple pattern bind somehow. This seems to work with your code though.

complex_bind :: Int
complex_bind = 1
  where
    foo :: Show a => a
    bar :: Show b => b
    zoo@(foo, bar) = (undefined, undefined)
    goo = zoo

@lierdakil
Copy link
Collaborator Author

I'll try to add some haddock comments to my code later this week (probably should've done this in the first place, but you know how it is sometimes).

For a general overview, I just walk whole AST fragment. For AbsBinds I collect all variables into Map Var Type, where Var is essentially monomorphic type variable, and Type is the constrained type associated with it. That's the simple part.
Then I collect everything with Id (most things that would have type), and for each of those I check if there's corresponding constrained type in my map. Then I just make a list of all constraints found, and substitute type variables in monomorphic type to correspond to those in polymorphic one. This, however, assumes that monomorphic and polymorphic types have the same number of type variables in the same order. (consider, e.g. foo :: Show a => a: it has polymorphic type Show a => a and monomorphic type t) I tried to find some examples where it wouldn't be true, but couldn't really think of any (there might be some exotic case with language extensions though -- not sure)

As for why this works for pattern bindings, you have to consider how AST represents those: a pattern binding is a left-pattern with essentially several left-bindings in it. Each binding has its own AbsBinds. So, type of zoo in your example is essentially (Show a => a, Show b => b). I just have to basically rearrange that into (Show a, Show b) => (a, b). Makes sense?

@DanielG
Copy link
Owner

DanielG commented Mar 9, 2016

Yup, makes sense. Sounds pretty much how I would've done it. I just don't understand what those syb bits are doing since I don't have much of a clue of that so if you could document that when you get around to it that would be awesome :)

@lierdakil
Copy link
Collaborator Author

@DanielG, I added some comments. Hopefully this more or less explains what's going on there.

@lierdakil lierdakil added this to the v5.6.0.0 milestone Mar 15, 2016
@DanielG DanielG merged commit 432f8bf into DanielG:master Jul 10, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants