Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Support record puns and wildcards #36

Closed
feuerbach opened this Issue · 12 comments

2 participants

@feuerbach
Owner

For that, we need to pass the global symbol table to the resolver.

@bergmark
Owner

This is also an issue in Fay (old test case)

data SomeRec = SomeRec { a :: Int, b :: Int }

fun :: SomeRec -> SomeRec
fun SomeRec{a} = SomeRec{a=a+1, b=10}

main = do
  print (fun (SomeRec{a=1, b=2}))

where the a in a+1 resolves to Main.a instead of just a.

@feuerbach
Owner

Note: full support is contingent on haskell-suite/haskell-src-exts#35

@feuerbach
Owner

HEAD (7ba836e) now has support for named field puns (but not for wildcards yet).

@feuerbach
Owner

Record wildcard patterns are now in HEAD (d543f12).

Regarding record wildcard constructions, I'm thinking of adding a new constructor to NameInfo to annotate the wildcard with something like [(OrigName, NameInfo l)], where every tuple corresponds to a field binding (OrigName is the record selector, NameInfo l describes the assigned value).

@bergmark, would that work for you?

Also, do you need any annotations for wildcards in patterns? Right now names from wildcards are introduced into the scope, but wildcards themselves are not annotated.

@bergmark
Owner

Great news! :)

We do need some way of looking up the names for wildcard patterns for code generation, so if you could add that annotation it would be nice. We do that lookup by ourselves right now so it's not critical.

Thanks!

@feuerbach
Owner

Done (as of 66179da). @bergmark, could you take a look at the interface and tell if it suits your needs? (See RecPatWildcard and RecExpWildcard constructors of Language.Haskell.Names.Annotated.NameInfo

@bergmark
Owner

It works!

It's a bit more verbose than what we already have since we need the locally bound names rather than what they resolve to (since we access record fields in JS with rec.field rather than M.field(rec)), but if we move this into the desugaring phase it will come in handy. Either way haskell-names doesn't crash on it anymore :)

Please let me know when this is released, and thanks again!

@feuerbach
Owner

By "a bit more verbose" do you mean just that you need to unpack OrigName to get a Name? Could you point me to the code that deals with this, so that I can see whether I can improve anything on my side?

I plan to make a release as soon as we're done with this issue.

@bergmark
Owner

Right, we already have a map from a constructor's qualified name to its fields so we can just do a lookup on that. But thinking about it more I should perhaps switch anyways...:

wildcardFields1 con = map (UnQual ()) <$> recToFields con

wildcardFields2 l = case l of
  Scoped (RecExpWildcard es) _ -> map (\(OrigName _ o) -> unQualify $ gname2Qname o) . map fst $ es
  _ -> []
@bergmark
Owner

EHM! There was a bug in the old code, it didn't handle R{a=n,..} properly. Definitely switching now :)

@feuerbach
Owner

Yes, that stuff is not trivial at all ;)

E.g. you must only substitute the fields that have corresponding values in scope, except when those values themselves are record selectors of that constructor.

OrigName has field selectors, so you can use those instead of a pattern match in the code above. I also just added field selectors to GName.

If there's anything else I can do to make your life easier, feel free to open separate issues.

@feuerbach feuerbach closed this
@feuerbach
Owner

haskell-names-0.3 has been uploaded

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.