disco
disco copied to clipboard
Patterns and let in container comprehension qualifiers
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:
- Adding a new constructor for
let
to theQual_
type inDisco.AST.Generic
, and modifying theQBind_
constructor to allow a pattern instead of just a name. - Updating the specialized
Surface
andTyped
AST types to match. - Updating the type checker to check the new constructs. Checking the
let
is very similar to normallet
expressions; checking pattern bindings simply requires that the pattern has a type which matches the elements of the term on the RHS of the<-
. - 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.
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.
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.
Could also make sense to split these two things into separate projects.