On Mon, Feb 9, 2015 at 3:17 PM, Jonathan S. Shapiro <[email protected]> wrote:
> On Mon, Feb 9, 2015 at 3:01 PM, Jonathan S. Shapiro <[email protected]>
> wrote:
>>
>> I see two possible ways to look at this. The first is that we have a
>> productions that looks like:
>>
>> expr := expr '|' generator
>> expr := '[' expr ']'
>>
>>
>> The other option is that we have a production somewhere that looks like:
>>
>> expr := '[' expr '|' generator ']'
>>
>>
>> But that would appear to lead to the conclusion that we are only able to
>> use comprehensions to produce a limited number of types that are
>> syntactically known to the parser.
>
>
> Full disclosure: I can see a third way that accounts entirely for how we get
> the behavior we want out of [ expr ], but it seems unspeakably ugly to me
> and I think there must surely be a cleaner way.
>
> The third way is to define [ expr ] to buildAList(expr), and then use
> overlapping instance resolution on buildAList(expr) to alter the behavior in
> the event that the input expression satisfies Iterable 'a 'elemType
>
> But that seems like a pretty damned dirty abuse of ad hoc overloading. There
> has to be a way that doesn't require us to build support into the grammar!
Scala (and I believe Fortress) solve this by mechanically translating
comprehensions into iterated calls to flatMap / map / filter, and then
using whatever result types flatMap produces. E.g.,
zs = [ (x,y) | x <- xs; y <- ys ]
would turn into
zs = concatMap (x -> map (y -> (x,y)) ys) xs
If concatMap can generate a certain collection type, so can
comprehensions. If the user wants to make a particular container use
extremely fancy ad-hoc techniques to be flexible, they can choose to
do that for the particular concatMap overloads.
The place where this breaks down in scala is different container
types. E.g., you like a comprehension involving a list and an option
to produce a list (probably), but it is either a compile error or
something useless.
Geoffrey
_______________________________________________
bitc-dev mailing list
[email protected]
http://www.coyotos.org/mailman/listinfo/bitc-dev