Re: [racket-users] Understanding recursion in the function normalize-definition form the racket sources

2021-04-17 Thread Dan Synek
(blush)
No you are not misunderstanding my question. I was wrong about 
define-values and confused by an error message related to 
normalize-definition not being known in another
part of the program due to phase problems. Thank you so much for taking the 
time to help me out!
Dan


On Sunday, April 18, 2021 at 12:25:38 AM UTC+3 johnbclements wrote:

> I’m confused by your assertion that define-values can’t be used 
> recursively. Here’s a program that does this:
>
> #lang racket
>
> (define-values (fact)
> (λ (x) (if (= x 0) 1 (* x (fact (sub1 x))
>
> (fact 14)
>
> Am I misunderstanding your message?
>
> John Clements
>
> > On Apr 17, 2021, at 05:02, Dan Synek  wrote:
> > 
> > I am trying to implement a variation of define. In order to do that I am 
> trying to understand the source of the define macro in racket.
> > It calls a function normalize-definition which looks like this:
> > (define-values (normalize-definition)
> > (case-lambda 
> > [(stx lambda-stx check-context? allow-key+opt?)
> > (let-values ([(id mk-rhs body)
> > (normalize-definition/mk-rhs stx lambda-stx check-context? 
> allow-key+opt? #t)])
> > (values id (mk-rhs body)))]
> > [(stx lambda-stx check-context?) (normalize-definition stx lambda-stx 
> check-context? #f)]
> > [(stx lambda-stx) (normalize-definition stx lambda-stx #t #f)])))
> > 
> > In the two last cases of the case-lambda there is a call to 
> normalize-definition. It is not a recursive call, since define-values 
> cannot be used recursively. I cannot find any other normalize-definition in 
> the modules required by the norm-define module.
> > If I try to enter a copy of the definition (renamed to 
> normalize-definition1) I get (as expected) that normalize-definition1 is 
> undefined.
> > What is going on?
> > Thanks
> > 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...@googlegroups.com.
> > To view this discussion on the web visit 
> https://groups.google.com/d/msgid/racket-users/ce5ce36e-03ee-44b1-a5ae-4f5a6e7b9d1en%40googlegroups.com
> .
>
>

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/16ec142a-af81-4fa4-8ce7-3fcf833c68f5n%40googlegroups.com.


Re: [racket-users] Typed Racket: type relations

2021-04-17 Thread Sam Tobin-Hochstadt
Ok, three parts:

1. Is it possible to make `average` on `Byte` provably produce a
`Byte`? This is not going to be possible with plain Typed Racket, even
with refinements to the numeric tower. The problem is that maintaining
the invariant that a <= (* n 255) is not something that we can express
just with the sets of values Typed Racket reasons about.

2. The rgb-distance^2 issue is really just that there's no negative
counterpart to `Byte`, so `Byte` minus `Byte` is `Fixnum`. We could
add that, at the cost of extra complexity in the numeric tower
generally.

However, I would suggest that the right fix here is to use refinement
types, and specify exactly what you want. Unfortunately, the
refinement types feature (good intro here:
https://blog.racket-lang.org/2017/11/adding-refinement-types.html)
isn't quite able to prove everything you want, but that's the
direction we should go. For example, it can already prove that rd in
the rgb-distance^2 function has this type: (define-type PMByte (Refine
[v : Fixnum] (and (< v 256) (< -256 v

3. I don't see any boundaries where you describe -- can you say more?

Sam

On Fri, Apr 16, 2021 at 12:06 PM Dominik Pantůček
 wrote:
>
> I wanted to polish things a bit before starting a longer discussion, but
> here we go ;-)
>
> The code in question[1] is part of my work into exchanging unsafe
> modules which can be used either contracted or uncontracted for TR
> modules. The goal is to replace racket/unsafe/ops with TR to provide
> compile-time type consistency and to have modules which are internally
> consistent and if they are used with other TR code the consistency
> remains. More on that later.
>
> On 16. 04. 21 15:51, Sam Tobin-Hochstadt wrote:
> > To improve this, we'd have to extend the type of `fxquotient`, which
> > is reasonable, but I'm not sure what the addition would be. In
> > particular, your addition is not sound:
> >
> > (fxquotient 1024 2) produces 512 which is not a Byte.
>
> Please, take a quick look at the typed-color.rkt in the repository.
>
> The "Color" type is just a Nonnegative-Fixnum. It is a generic RGB value
> with blue in lowest 8 bits, green shifted above blue and red on top. 24
> bits in total. The split-rgb splits it into 3 R,G,B values - where each
> of those values is a byte.
>
> Then the rgb-average function takes an arbitrary number of Color values,
> computes sums of their distinct R,G,B components and divides all of them
> by the number of values before merging them together into one RGB Color
> value.
>
> Average value of (listof Byte) is definitely a Byte again. I am sorry I
> didn't send the whole code in question. I was focused on other parts and
> this is just a side issue in my current work.
>
> Is there a way to express this in TR while still generating a code that
> is on-par with the unsafe version? In theory, TR should excel in this.
>
> A similar problem arises with the rgb-distance^2 function where the
> component difference can be either positive or negative. But definitely
> a square of whatever Integer is positive. And definitely the result
> cannot be bigger than 3*255^2=195075, which is definitely
> Nonnegative-Fixnum on any platform.
>
> Again - is there a way to express this in TR and generate a code that
> has no runtime checks?
>
> The motivation here is that the performance and type soundness should go
> hand in hand. If I prove the code will never get a value of a wrong
> type, there is no need for runtime checks. Actually the more strict
> rules, the better code can be (in theory) generated.
>
> Then the typed/untyped boundaries introduce contracts - or they don't. I
> am really confused, why there is a typed/untyped boundary between
> typed-unsafe-color.rkt and typed-color.rkt. I assume this comes from my
> poor understanding of TR - it will probably get better in the
> forthcoming months as TR has proven to be an invaluable tool for
> improving the quality of performance-oriented modules so far. More on
> that later too...
>
>
> Cheers,
> Dominik
>
> [1] https://gitlab.com/racketeer/typed-racket-performance
>
> --
> 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.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/racket-users/d95b9140-fd78-0230-439b-aba654505fd0%40trustica.cz.

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CAK%3DHD%2BZ_6fqz3pcXNC0Jm-aG1__zvruHgDz_w0xSVuh1R1pY7w%40mail.gmail.com.


[racket-users] Checking for fallbacks in generic contracts

2021-04-17 Thread Sage Gerard
Playing with this program. Its output is in the comments.

; /tmp/gen.rkt:9:10: assertion violation
; expected: a procedure
; given: #f
; in: method m
; (I/c (m (-> I? 1)))
; [...removed the rest of the error...]
; 1

(module gen-contracts racket/base
(require racket/contract racket/exn racket/generic)
(define-generics I [m I] #:fallbacks [(define (m _) 1)])
(struct noop () #:methods gen:I [])
(struct impl () #:methods gen:I [(define (m _) 1)])
(define (try ctor)
(displayln
(with-handlers ([exn:fail:contract? exn->string])
(m (invariant-assertion (I/c [m (-> I? 1)]) (ctor))
(try noop)
(try impl))

I expected the output to be just two "1" lines, because I assumed the contract 
combinator from `define-generics` would consider fallback implementations. I do 
think it makes sense for `I/c` to fail to apply a contract to a method in 
`noop` because `noop` doesn't implement `m`. But I think the combinator reuses 
some logic from whatever is making `#:defined-predicate` work, since it 
represents missing methods as `#f`, and doesn't look at the fallbacks.

But... I wanted a contract that's okay with a missing method if there's a 
fallback. The only contracts I can write with the `I/c` combinator seem require 
me to either A) write repetitive method definitions that just parrot the 
fallback, or B) write its method contracts like `(I/c [m (or/c #f (-> I? 
1))])`, which feels icky.

Apologies if I'm totally off the mark or just missed something in the manual, 
but is there a way to define a contract for a generics implementation that 
won't complain if it sees a fallback for a missing method?

--
~slg

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/fed6c561-881b-08af-4318-25750f198133%40sagegerard.com.


Re: [racket-users] Understanding recursion in the function normalize-definition form the racket sources

2021-04-17 Thread 'John Clements' via Racket Users
I’m confused by your assertion that define-values can’t be used recursively. 
Here’s a program that does this:

#lang racket

(define-values (fact)
  (λ (x) (if (= x 0) 1 (* x (fact (sub1 x))

(fact 14)

Am I misunderstanding your message?

John Clements

> On Apr 17, 2021, at 05:02, Dan Synek  wrote:
> 
> I am trying to implement a variation of define. In order to do that I am 
> trying to understand the source of the define macro in racket.
>  It calls a function normalize-definition which looks like this:
>   (define-values (normalize-definition)
> (case-lambda 
>  [(stx lambda-stx check-context? allow-key+opt?)
>   (let-values ([(id mk-rhs body)
> (normalize-definition/mk-rhs stx lambda-stx 
> check-context? allow-key+opt? #t)])
> (values id (mk-rhs body)))]
>  [(stx lambda-stx check-context?) (normalize-definition stx lambda-stx 
> check-context? #f)]
>  [(stx lambda-stx) (normalize-definition stx lambda-stx #t #f)])))
> 
> In the two last cases of the case-lambda there is a call to 
> normalize-definition. It is not a recursive call, since define-values cannot 
> be used recursively. I cannot find any other normalize-definition in the 
> modules required by the norm-define module.
>  If I try to enter a copy of the definition (renamed to 
> normalize-definition1) I get (as expected) that normalize-definition1 is 
> undefined.
> What is going on?
> Thanks
> 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.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/racket-users/ce5ce36e-03ee-44b1-a5ae-4f5a6e7b9d1en%40googlegroups.com.

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/3e1d308f-ab29-47e5-b42e-fdff07739645%40mtasv.net.


[racket-users] Understanding recursion in the function normalize-definition form the racket sources

2021-04-17 Thread Dan Synek
I am trying to implement a variation of define. In order to do that I am 
trying to understand the source of the define macro in racket.
 It calls a function normalize-definition which looks like this:
  (define-values (normalize-definition)
(case-lambda 
 [(stx lambda-stx check-context? allow-key+opt?)
  (let-values ([(id mk-rhs body)
(normalize-definition/mk-rhs stx lambda-stx 
check-context? allow-key+opt? #t)])
(values id (mk-rhs body)))]
 [(stx lambda-stx check-context?) (normalize-definition stx lambda-stx 
check-context? #f)]
 [(stx lambda-stx) (normalize-definition stx lambda-stx #t #f)])))

In the two last cases of the case-lambda there is a call to 
normalize-definition. It is not a recursive call, since define-values 
cannot be used recursively. I cannot find any other normalize-definition in 
the modules required by the norm-define module.
 If I try to enter a copy of the definition (renamed to 
normalize-definition1) I get (as expected) that normalize-definition1 is 
undefined.
What is going on?
Thanks
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/ce5ce36e-03ee-44b1-a5ae-4f5a6e7b9d1en%40googlegroups.com.