|
|
# Comprehensive comprehensions
|
|
|
|
|
|
|
|
|
As part of his final year work at Cambridge, Max Bolingbroke is working on implementing the "Comprehensive Comprehensions" described in a paper [ available here](http://research.microsoft.com/~simonpj/papers/list-comp/index.htm) in GHC. This page will contain notes on the implementation as it unfolds.
|
|
|
|
|
|
## Ordering Syntax
|
|
|
|
|
|
|
|
|
The paper uses a syntax based around the new keywords "order" and "by". For example:
|
|
|
|
|
|
```wiki
|
|
|
[ (name, salary)
|
|
|
| (name, dept, salary) <- employees
|
|
|
, salary > 70
|
|
|
, order by salary ]
|
|
|
```
|
|
|
|
|
|
|
|
|
It has been noted that introducing a new keyword may not be desirable, especially given the fact that you can use "order" to achieve things which aren't really ordering:
|
|
|
|
|
|
```wiki
|
|
|
[ (the dept, sum salary)
|
|
|
| (name, dept, salary) <- employees
|
|
|
, order by salary
|
|
|
, order by salary < 50 using takeWhile
|
|
|
, order using take 5 ]
|
|
|
```
|
|
|
|
|
|
|
|
|
For those reasons, Max's implementation is currently based around the syntax proposed in section 6.1 of the paper:
|
|
|
|
|
|
```wiki
|
|
|
[ (the dept, sum salary)
|
|
|
| (name, dept, salary) <- employees
|
|
|
, then sortWith by salary
|
|
|
, then takeWhile by salary < 50
|
|
|
, then take 5 ]
|
|
|
```
|
|
|
|
|
|
|
|
|
This reuses the "then" keyword and is probably less confusing. However, no final decision has been made on the optimal syntax: in particular it might be better to write:
|
|
|
|
|
|
```wiki
|
|
|
[ (the dept, sum salary)
|
|
|
| (name, dept, salary) <- employees
|
|
|
, then sortWith using salary
|
|
|
, then takeWhile using salary < 50
|
|
|
, then take 5 ]
|
|
|
```
|
|
|
|
|
|
|
|
|
Suggestions?
|
|
|
|
|
|
## Grouping Syntax
|
|
|
|
|
|
|
|
|
Some of the same concerns about keyword introduction apply here, but ordering is being implemented first so not much thought has been given to syntax improvements. The main suggestion from the paper is:
|
|
|
|
|
|
```wiki
|
|
|
[ (the dept, sum salary)
|
|
|
| (name, dept, salary) <- employees
|
|
|
, group by dept ]
|
|
|
```
|
|
|
|
|
|
|
|
|
We could equally well substitute "using" for the "by" if desired:
|
|
|
|
|
|
```wiki
|
|
|
[ (the dept, sum salary)
|
|
|
| (name, dept, salary) <- employees
|
|
|
, group using dept ]
|
|
|
```
|
|
|
|
|
|
|
|
|
Or we could even do an implicit call to "the" on the grouped-by variables:
|
|
|
|
|
|
```wiki
|
|
|
[ (the_dept, namesalary)
|
|
|
| (name, dept, salary) <- employees
|
|
|
, the_dept <- group by dept
|
|
|
where (name,salary) -> namesalary
|
|
|
]
|
|
|
```
|
|
|
|
|
|
|
|
|
We would be interested in hearing peoples thoughts on these issues.
|
|
|
|
|
|
## Bracketing Syntax
|
|
|
|
|
|
|
... | ... | @@ -61,7 +145,8 @@ When the parser reaches the bracket after "e" it is valid to either reduce "(i, |
|
|
z <- zs },
|
|
|
x > y + 3 ]
|
|
|
|
|
|
-- 2) Trying to suggest pulling things out of a sublist without having to mention binders
|
|
|
-- 2) Trying to suggest pulling things out of a sublist
|
|
|
-- without having to mention binders
|
|
|
[ foo | x <- e,
|
|
|
<- [ .. | y <- ys,
|
|
|
z <- zs ],
|
... | ... | @@ -79,95 +164,14 @@ When the parser reaches the bracket after "e" it is valid to either reduce "(i, |
|
|
z <- zs ],
|
|
|
x > y + 3 ]
|
|
|
|
|
|
-- 5) Another variation on 2), moving the ".." into the pattern rather than the comprehension body
|
|
|
-- 5) Another variation on 2), moving the ".." into
|
|
|
-- the pattern rather than the comprehension body
|
|
|
[ foo | x <- e,
|
|
|
.. <- [ y <- ys,
|
|
|
z <- zs ],
|
|
|
x > y + 3 ]
|
|
|
```
|
|
|
|
|
|
## Ordering Syntax
|
|
|
|
|
|
|
|
|
The paper uses a syntax based around the new keywords "order" and "by". For example:
|
|
|
|
|
|
```wiki
|
|
|
[ (name, salary)
|
|
|
| (name, dept, salary) <- employees
|
|
|
, salary > 70
|
|
|
, order by salary ]
|
|
|
```
|
|
|
|
|
|
|
|
|
It has been noted that introducing a new keyword may not be desirable, especially given the fact that you can use "order" to achieve things which aren't really ordering:
|
|
|
|
|
|
```wiki
|
|
|
[ (the dept, sum salary)
|
|
|
| (name, dept, salary) <- employees
|
|
|
, order by salary
|
|
|
, order by salary < 50 using takeWhile
|
|
|
, order using take 5 ]
|
|
|
```
|
|
|
|
|
|
|
|
|
For those reasons, Max's implementation is currently based around the syntax proposed in section 6.1 of the paper:
|
|
|
|
|
|
```wiki
|
|
|
[ (the dept, sum salary)
|
|
|
| (name, dept, salary) <- employees
|
|
|
, then sortWith by salary
|
|
|
, then takeWhile by salary < 50
|
|
|
, then take 5 ]
|
|
|
```
|
|
|
|
|
|
|
|
|
This reuses the "then" keyword and is probably less confusing. However, no final decision has been made on the optimal syntax: in particular it might be better to write:
|
|
|
|
|
|
```wiki
|
|
|
[ (the dept, sum salary)
|
|
|
| (name, dept, salary) <- employees
|
|
|
, then sortWith using salary
|
|
|
, then takeWhile using salary < 50
|
|
|
, then take 5 ]
|
|
|
```
|
|
|
|
|
|
|
|
|
Suggestions?
|
|
|
|
|
|
## Grouping Syntax
|
|
|
|
|
|
|
|
|
Some of the same concerns about keyword introduction apply here, but ordering is being implemented first so not much thought has been given to syntax improvements. The main suggestion from the paper is:
|
|
|
|
|
|
```wiki
|
|
|
[ (the dept, sum salary)
|
|
|
| (name, dept, salary) <- employees
|
|
|
, group by dept ]
|
|
|
```
|
|
|
|
|
|
|
|
|
We could equally well substitute "using" for the "by" if desired:
|
|
|
|
|
|
```wiki
|
|
|
[ (the dept, sum salary)
|
|
|
| (name, dept, salary) <- employees
|
|
|
, group using dept ]
|
|
|
```
|
|
|
|
|
|
|
|
|
Or we could even do an implicit call to "the" on the grouped-by variables:
|
|
|
|
|
|
```wiki
|
|
|
[ (the_dept, namesalary)
|
|
|
| (name, dept, salary) <- employees
|
|
|
, the_dept <- group by dept
|
|
|
where (name,salary) -> namesalary
|
|
|
]
|
|
|
```
|
|
|
|
|
|
|
|
|
We would be interested in hearing peoples thoughts on these issues.
|
|
|
|
|
|
## Extending To Arbitrary Monads
|
|
|
|
|
|
|
... | ... | |