Some people, when confronted with a problem, think "I know, I'll use Typed 
Racket."  Now they have (Pairof Problem Problem).


On Jun 12, 2016, at 2:29 PM, Ben Greenman <benjaminlgreen...@gmail.com> wrote:

> Yeah I agree `inst` needs more documentation.
> And as someone (Matthew B.?) suggested before, we could really use a 
> systematic way of documenting the type variables on each polymorphic function.
> 
> 
> On Sun, Jun 12, 2016 at 5:23 PM, Daniel Prager <daniel.a.pra...@gmail.com> 
> wrote:
> Cc:ing the list ...
> 
> On Sun, Jun 12, 2016 at 4:56 PM, Ben Greenman <benjaminlgreen...@gmail.com> 
> wrote:
> Yep, use `inst` to instantiate the polymorphic variables to sort.
> 
> #lang typed/racket
> 
> ((inst sort (List Symbol Integer) Integer)
>  '((a 45) (b 13) (c 12) (d 16) (e 9) (f 5)) < #:key second)
> 
> 
> [[ I usually start with `(inst f)` and use the error message to figure
> out what types / how many `f` needs. ]]
> 
> Thanks Ben, this is very helpful.
> 
> I had read about inst in the Racket Reference, but the penny hadn't dropped. 
> Perhaps expanding the worked example (or adding a section to The TR Guide) 
> would be helpful to novices.
> 
> For example:
> 
> > (foldl cons null (list 1 2 3 4)) 
> 
> Type Checker: Polymorphic function `foldl' could not be applied to arguments 
> [etc.]
> 
> Both foldl and cons are polymorphic: foldl in a b c d, cons in a b.
>  
> > foldl
> - : (All (a b c d)
>       (case->
>        (-> (-> a b b) b (Listof a) b)
>        (-> (-> a b c c) c (Listof a) (Listof b) c)
>        (-> (-> a b c d d) d (Listof a) (Listof b) (Listof c) d)))
> #<procedure:foldl>
> 
> > cons
> - : (All (a b) 
>       (case-> (-> a (Listof a) (Listof a)) 
>               (-> a b (Pairof a b))))
> #<procedure:cons>
> 
> The type-checker needs the programmer's help -- either by inst-antiating 
> foldl or cons -- to resolve this ambiguity. [Why?]:
> 
> > ((inst foldl Integer (Listof Integer) Null Null)
>    cons null (list 1 2 3 4))
> - : (Listof Integer)
> '(4 3 2 1)
> 
> > (foldl (inst cons Integer Integer) null (list 1 2 3 4))
> - : (Listof Integer)
> '(4 3 2 1) 
> 
> In this case inst-ing cons is more concise than inst-ing foldl (in which the 
> Nulls are placeholders since c & d are ignored), so preferable.
> 
> * * *
> 
> Also, perhaps discoverability would be improved if the error message 
> suggested that an inst could resolve the problem.
> 
> Dan 
> 
> 
> On 6/11/16, Daniel Prager <daniel.a.pra...@gmail.com> wrote:
> > Next example.
> >
> > In Racket:
> >
> >> (sort '((a 45) (b 13) (c 12) (d 16) (e 9) (f 5)) < #:key second)
> >
> > '((f 5) (e 9) (c 12) (b 13) (d 16) (a 45))
> >
> >
> > In Typed Racket I couldn't get a version using #:key to type-check. Pulling
> > second into a comparison function works:
> >
> >> (sort '((a 45) (b 13) (c 12) (d 16) (e 9) (f 5))
> >           (λ ([a : (List Symbol Natural)]
> >               [b : (List Symbol Natural)])
> >             (< (second a) (second b))))
> >
> > Is there a solution using #:key in Typed Racket?
> >
> >
> > Dan
> >
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Racket Users" group.
> > To unsubscribe from this group and stop receiving emails from it, send an
> > email to racket-users+unsubscr...@googlegroups.com.
> > For more options, visit https://groups.google.com/d/optout.
> >
> 
> 
> --
> @ Northeastern University
> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to