[Ur] Using Variant.weaken in a foldUR
Patrick Hurst
phurst at mit.edu
Tue Feb 12 18:11:38 EST 2013
On Tue, Feb 12, 2013 at 10:03 AM, Adam Chlipala <adamc at csail.mit.edu> wrote:
> On 02/12/2013 12:48 AM, Patrick Hurst wrote:
>
>> My code so far is
>>
>> [...]
>>
>>
>> fun formResultToRows (fl : folder assns) (result : $(mapU assignmentData
>> assns)) : list assignmentVariant =
>> @foldUR [assignmentData] [fn cols => list (variant (mapU unit cols))]
>> (fn [nam :: Name] [rest ::_] [[nam] ~ rest] res acc =>
>> make [nam] () :: List.mp Variant.weaken acc
>> )
>> [] fl result
>>
>> [...]
>>
>>
>> /home/phurst/code/assignments/**assignments.ur:34:27: (to 34:41) Can't
>> resolve type class instance
>> Class constraint: Top.folder[[Type]] (map (fn _ :: Unit => {}) rest)
>>
>> so I assume I need to explicitly pass in a folder somewhere (possibly to
>> Variant.weaken?), but I'm not sure how. Any advice?
>>
>
> I hope that file locations from the error message are indeed pointing to
> the [Variant.weaken] call; that's where I expect the problem is.
>
> The compiler only infers [folder]s for types with known field names, which
> isn't the case here, so a [folder] needs to be passed explicitly.
> Generally you preface an identifier with [@] to turn off automatic
> insertion of arguments for [folder]s and type class witnesses. You could
> use that method here to allow you to give an appropriate [folder], if you
> had one.
>
> However, I think it would be hard to come up with that [folder] (actually
> a sequence of [folder]s, one for each fold iteration). Instead, I think a
> more specialize fold operation is a better match.
>
> In particular, I bet [Variant.fold] from the meta library is just what you
> need here.
>
> Also, if you are building a list of rows to insert into the database, it
> will probably work better to instead execute one SQL INSERT per fold
> iteration, so that no intermediate list needs to be materialized.
> Something like [Variant.app] would be appropriate for such a pattern.
> (Actually, it seems I only implemented a fancier version [Variant.appR] so
> far, but I bet you could implement the natural [Variant.app] and submit a
> patch with it. :])
>
I'm not sure how [Variant.fold] helps me out here, since the thing I'm
folding over with the [@foldUR] isn't a variant. I'm fine with inserting
something directly into the database inside the fold as opposed to creating
the list and then iterating over it, but again I'm not sure how to use
[Variant.app] (what would that be? I'm not sure what the R suffix means
exactly) to implement it since I'm not folding over a variant.
Do you mean I want to use [Variant.app] or [Variant.fold] inside the
[@foldUR]? If so, I'm not sure how to do so by directly inserting inside
the fold, since the type doesn't guarantee me [nam] is in the relevant
variant as far as I can tell, so I can't insert it.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.impredicative.com/pipermail/ur/attachments/20130212/9713693c/attachment.html>
More information about the Ur
mailing list