disco icon indicating copy to clipboard operation
disco copied to clipboard

Patterns and let in container comprehension qualifiers

Open byorgey opened this issue 6 years ago • 3 comments

Haskell supports pattern bindings and let expressions in list comprehensions, e.g.

[ (q,q+1) | Just y <- ys, let q = 3*y + 1 ]

This wouldn't be very hard to add at this point. It would require:

  1. Adding a new constructor for let to the Qual_ type in Disco.AST.Generic, and modifying the QBind_ constructor to allow a pattern instead of just a name.
  2. Updating the specialized Surface and Typed AST types to match.
  3. Updating the type checker to check the new constructs. Checking the let is very similar to normal let expressions; checking pattern bindings simply requires that the pattern has a type which matches the elements of the term on the RHS of the <-.
  4. Updating the desugaring. See https://www.haskell.org/onlinereport/haskell2010/haskellch3.html#x8-420003.11 for details on how desugaring works in Haskell; we should be able to do something very similar.

byorgey avatar Jul 26 '17 19:07 byorgey

Note this makes parsing comprehensions a bit more subtle: see http://www.rntz.net/post/2018-07-10-parsing-list-comprehensions.html . But now the Disco parser already in fact parses patterns as expressions with a post-processing step to ensure they are valid patterns, so this shouldn't be too hard.

byorgey avatar Aug 02 '18 12:08 byorgey

I just wanted this recently (specifically, the ability to have let in a comprehension). Definitely worth doing. Could be a good project for a student to tackle.

byorgey avatar Feb 15 '22 03:02 byorgey

Could also make sense to split these two things into separate projects.

byorgey avatar Mar 11 '24 18:03 byorgey