catala icon indicating copy to clipboard operation
catala copied to clipboard

Feature request: syntactic sugar for Variant + Constructor

Open protz opened this issue 3 years ago • 7 comments

This is getting a little verbose:

declaration structure Foo:
  data f₁ content t₁
  ...

declaration structure Bar:
  data g₁ content u₁

declaration enumeration Baz:
  -- Foo content Foo
  -- Bar content Bar

because at construction-time, I need to write Foo (content Foo { f₁ = … })

suggested sugar is:

Foo content { f₁ = … }

with no parentheses

protz avatar Apr 28 '21 21:04 protz

I think this is a good sugar to add. The changes for implementing it will likely cover:

  • the parser (adding the sugar syntax)
  • the desugaring pass (retrieving the struct id and fields from the id of the enum constructor)

denismerigoux avatar Apr 29 '21 08:04 denismerigoux

I'd love to work on this! I'm a bit new to OCaml so I'll need to do some additional learning, but based on your comment @denismerigoux it seems like parser.mly might be a good file to start with, right?

Are there other files in the compiler that would be relevant to this issue? Or do you have any suggested resources for learning more about how the parser currently works? I'm reading up on Obelisk and would be happy to look into other resources that you think might be helpful.

widal001 avatar Sep 18 '22 13:09 widal001

Hi @widal001 ! Thanks for proposing your help. Here are the places where you will probably insert the new sugar :

https://github.com/CatalaLang/catala/blob/fbd6b073722258b9f0edf974c46dfe2229037977/compiler/surface/parser.mly#L89

https://github.com/CatalaLang/catala/blob/fbd6b073722258b9f0edf974c46dfe2229037977/compiler/surface/ast.ml#L429-L432

https://github.com/CatalaLang/catala/blob/fbd6b073722258b9f0edf974c46dfe2229037977/compiler/surface/desugaring.ml#L398-L463

So the parser uses Menhir (and not Obelisk, which is just a visualization tool), whose manual is here. However the menhir manual assumes some knowledge about parser theory ; if you're curious and want to learn you can check out some lecture notes but otherwise you can just look at the Catala grammar and build an intuitive sense of how it works, and copy-paste things to build what you want to do :)

denismerigoux avatar Sep 19 '22 08:09 denismerigoux

@denismerigoux Thanks for pointing me in the right direction and for sharing the additional resources on parser theory and Menhir specifically. I'll dig into this over the weekend, and follow up on this ticket if I have any other questions. Appreciate your time and willingness to welcome me to the project!

widal001 avatar Sep 23 '22 01:09 widal001

Hi @denismerigoux, wanted to ask if this issue has been resolved? If not, I'd like to work on this feature. Thanks !

yokurang avatar Mar 05 '24 17:03 yokurang

@yokurang Yeah sorry I should have commented on the ticket a while ago, but I never got a chance to take this on. Would be great for someone else to pick up!

widal001 avatar Mar 05 '24 20:03 widal001

Hi @yokurang and thanks a lot for your interest. With our extended usage of the language since then we haven't been hitting the case covered by the sugar proposed here very often, which explains there has been no update on this issue.

Therefore, and if you are still willing to lend a hand, your efforts could be more helpful on something else. I just did a review of our open issues to check which ones could be best first picks. Among those, I could suggest:

  • #590 for something that does not involve too much complexity, to get started
  • #248 would get you closer to the backends, with concerns about out Python output
  • #591 if you are willing to get more into the AST analysis parts
  • #282 for something a bit more beefy related to small AST transformations

AltGr avatar Mar 12 '24 11:03 AltGr