catala icon indicating copy to clipboard operation
catala copied to clipboard

Add a list folding surface syntax

Open denismerigoux opened this issue 1 year ago • 3 comments
trafficstars

Right now, many of our syntactic list manipulation operators are implemented underneath using fold. However, the user cannot directly write a fold in the Catala surface syntax. The CNAF expressed the need for such an operator to be exposed. As it is polymorphic, it will require special syntax.

The signature for this operator would be fold : 'a list -> ('b -> 'a -> 'b) -> 'b -> 'b.

denismerigoux avatar Jul 09 '24 08:07 denismerigoux

One possible syntax: Since we already have <foo(v)> for <v> among <list> for map one possibility would be to extend that with details: <foo(v,acc)> for <v> among <list> accumulating into <acc> starting from <init> (the starting could be removed for conciseness... maybe the into (or even the accumulating ?) as well ?)

E.g. Compute the sum of a list of money with:

x + y for x among revenues accumulating into y starting from $0

AltGr avatar Jul 15 '24 08:07 AltGr

Decision of the syntax committee :

combine <accumulator> initially <initial_value> with <expression depending on accumulator and element> for <element> among <list>

combinaison de <accumulateur> initialement <valeur initiale> avec <expression dépendant de valeur initiale et élément> pour <élément> parmi <liste>

denismerigoux avatar Oct 17 '24 15:10 denismerigoux

@vincent-botbol from the above you can:

  • modify the parser and lexer in https://github.com/CatalaLang/catala/tree/master/compiler/surface accordingly
  • modify your topyari formatter and LSP extension to take it into account
  • update all syntax highlighting plugins.

Thanks!

denismerigoux avatar Oct 17 '24 15:10 denismerigoux