Re: [racket-users] `apply`ing polymorphic functions in typed/racket

2017-01-05 Thread WarGrey Gyoudmon Ju
On Thu, Jan 5, 2017 at 3:28 PM, Matthew Eric Bassett 
wrote:
>
> the type signatures of this functions are:
> (-> (Setof e) (Setof e) * (Setof e))
>
> > (define list0
> (list (set 2) (set 3 2)))
>   (apply set-union list0)
>
> > (define list2 : (Listof (Setof Positive-Byte))
> (list (set 2) (set 3 2)))
>   (apply set-union list2)
>
> Why does the former work and the latter fail ?
>

because,
`list0` has a type `(List (Setof Positive-Byte) (Setof Positive-Byte))`
which is guaranteed to hold at least one set value.
`list2` may be `null`.

So you get it. `set-union` requires at least one set value as input.

-- 
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.


[racket-users] `apply`ing polymorphic functions in typed/racket

2017-01-04 Thread Matthew Eric Bassett
Hi everyone,

I have a strange example for you.  The following code throws an error in
typed/racket 6.7:

> (define (list1) : (Listof (Setof  Positive-Byte))
(list (set 2) (set 3 2)))
  (apply set-union (list1))
Type Checker: Bad arguments to function in `apply':
Domain: (Setof e) (Setof e) *
Arguments: (Listof (Setof Positive-Byte)) *
in: (apply set-union (list1))

At first I thought "doh! There's a note in the docs about polymorphic
function, just use inst".  But on the other hand,

> (define list0 
(list (set 2) (set 3 2)))
  (apply set-union list0)

Happily compiles and evaluates to the expected result.  For the record,
(inst set-union Positive-Byte) does not help.

What is weirder is that if I open a fresh repl and try:

> (define list0 : (Listof (Setof Positive-Byte)) 
(list (set 2) (set 3 2)))
  (apply set-union list0)

It then throws the same error.

I wasn't able to get the same error with other polymorphic functions as
much as I tried. It turns out this is limited to set-union and
set-subtract.  the type signatures of this functions are:
(-> (Setof e) (Setof e) * (Setof e))

Therein lies our problem.  we need to curry set-union, et al, to get rid
of that leading argument!  But to curry we run into polymorphic type
problems.  so we do:

> (apply (curry (inst set-union Positive-Byte) (ann (set) (Setof 
> Positive-Byte))) (list1))

That's a bit of a mouthful, but racket is as happy as a clam.  Might I
hope for a cleaner way to write this in a later version?
Somewhat related, I have one parting question: Out of the two:

> (define list0 
(list (set 2) (set 3 2)))
  (apply set-union list0)

> (define list2 : (Listof (Setof Positive-Byte)) 
(list (set 2) (set 3 2)))
  (apply set-union list2)

Why does the former work and the latter fail ?

Thanks,

Matthew Eric

-- 
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.


signature.asc
Description: OpenPGP digital signature