[Ur] Using Variant.weaken in a foldUR

Adam Chlipala adamc at csail.mit.edu
Tue Feb 12 10:03:29 EST 2013


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. :])



More information about the Ur mailing list