Re: [racket-users] contract for an "overloaded function"

2019-11-29 Thread Ben Greenman
Try ->i. I wouldn't worry about performance until makes itself a problem.

On 11/29/19, Ryan Kramer  wrote:
> Thanks, but I don't think `case->` works for me. It looks like it chooses a
>
> case purely based on the number of arguments. The following example, when
> given two arguments, will always choose the integer? case even if both
> arguments are strings.
>
> (case-> [-> integer? integer? list?]
> [-> string? string? list?])
>
> On Friday, November 29, 2019 at 1:51:52 PM UTC-6, David Storrs wrote:
>>
>> I think you want `case->`:
>> https://docs.racket-lang.org/reference/function-contracts.html#%28form._%28%28lib._racket%2Fcontract%2Fbase..rkt%29._case-~3e%29%29
>>
>
> --
> 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/73132ad9-7722-432a-8328-0d4c38bbb5a1%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/CAFUu9R6dBHvCF8B1WsS43Xr7vL9CHiHRpMzN8NsRuCo8S6Q3%3DA%40mail.gmail.com.


Re: [racket-users] contract for an "overloaded function"

2019-11-29 Thread Ryan Kramer
Thanks, but I don't think `case->` works for me. It looks like it chooses a 
case purely based on the number of arguments. The following example, when 
given two arguments, will always choose the integer? case even if both 
arguments are strings.

(case-> [-> integer? integer? list?]
[-> string? string? list?])

On Friday, November 29, 2019 at 1:51:52 PM UTC-6, David Storrs wrote:
>
> I think you want `case->`: 
> https://docs.racket-lang.org/reference/function-contracts.html#%28form._%28%28lib._racket%2Fcontract%2Fbase..rkt%29._case-~3e%29%29
>

-- 
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/73132ad9-7722-432a-8328-0d4c38bbb5a1%40googlegroups.com.


Re: [racket-users] contract for an "overloaded function"

2019-11-29 Thread David Storrs
I think you want `case->`:
https://docs.racket-lang.org/reference/function-contracts.html#%28form._%28%28lib._racket%2Fcontract%2Fbase..rkt%29._case-~3e%29%29

On Fri, Nov 29, 2019 at 2:28 PM Ryan Kramer 
wrote:

> I'm not exactly sure what I mean by "overloaded function", but I think you
> will understand. I'm looking for something that would allow me to write a
> function contract like
>
> (magic-> [integer? integer? -> integer?]
>  [string? symbol? -> string?]
>  [string? ...+ -> string?])
>
> The above contract would mean:
>   * If the function is given an argument list that satisfies (cons/c
> integer? (cons/c integer? null?)), then the return value must be an integer?
>   * Else if the function is given an argument list that satisfies (cons/c
> string? (cons/c symbol? null?)), then the return value must be a string?
>   * Else if the function is given an argument list that satisfies (cons/c
> string? (list/c string?)), then the return value must be a string?
>   * Else the caller is blamed for not providing correct arguments.
>
> (I don't think I need support for keyword arguments)
>
> Does this already exist in a library somewhere? If not, it doesn't look
> too hard for me to roll my own but I am (perhaps prematurely) concerned
> about the performance. Would the following approach be reasonable?
>
> (define-syntax-rule (magic-> [dom ... range-expr] ...)
>   ; We should make sure that every dom and range is a flat-contract?
>   (make-contract
>#:name '(magic-> [dom ... range-expr] ...)
>#:first-order procedure?
>#:projection
>(λ (blame)
>  (λ (proc)
>(λ args
>  (cond
>[((list/c dom ...) args)
> (let ([range ((contract-projection range-expr) blame)])
>   (range (apply proc args)))]
>...
>[else
> (raise-blame-error
>  (blame-swap blame) args
>  '(expected "a conforming argument list" given: "~e")
>  args)]))
>
> (define/contract (blah a b)
>   (magic-> [integer? string? list?]
>[string? integer? (list/c string? integer?)]
>; This one will throw a contract violation
>[integer? integer? integer?])
>   (list a b))
>
> --
> 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/b21a5512-521f-4c6e-8e4e-ca6d3528df8c%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/CAE8gKoeGJvoP9wfgcmtX%3DopVa9F%2BJzTE6JN7ZjsC%3DcbuxEGNww%40mail.gmail.com.


[racket-users] contract for an "overloaded function"

2019-11-29 Thread Ryan Kramer
I'm not exactly sure what I mean by "overloaded function", but I think you 
will understand. I'm looking for something that would allow me to write a 
function contract like

(magic-> [integer? integer? -> integer?]
 [string? symbol? -> string?]
 [string? ...+ -> string?])

The above contract would mean:
  * If the function is given an argument list that satisfies (cons/c 
integer? (cons/c integer? null?)), then the return value must be an integer?
  * Else if the function is given an argument list that satisfies (cons/c 
string? (cons/c symbol? null?)), then the return value must be a string?
  * Else if the function is given an argument list that satisfies (cons/c 
string? (list/c string?)), then the return value must be a string?
  * Else the caller is blamed for not providing correct arguments.

(I don't think I need support for keyword arguments)

Does this already exist in a library somewhere? If not, it doesn't look too 
hard for me to roll my own but I am (perhaps prematurely) concerned about 
the performance. Would the following approach be reasonable?

(define-syntax-rule (magic-> [dom ... range-expr] ...)
  ; We should make sure that every dom and range is a flat-contract?
  (make-contract
   #:name '(magic-> [dom ... range-expr] ...)
   #:first-order procedure?
   #:projection
   (λ (blame)
 (λ (proc)
   (λ args
 (cond
   [((list/c dom ...) args)
(let ([range ((contract-projection range-expr) blame)])
  (range (apply proc args)))]
   ...
   [else
(raise-blame-error
 (blame-swap blame) args
 '(expected "a conforming argument list" given: "~e")
 args)]))

(define/contract (blah a b)
  (magic-> [integer? string? list?]
   [string? integer? (list/c string? integer?)]
   ; This one will throw a contract violation
   [integer? integer? integer?])
  (list a b))

-- 
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/b21a5512-521f-4c6e-8e4e-ca6d3528df8c%40googlegroups.com.


[racket-users] Confused about Semantics Engineering Exercise 12.6

2019-11-29 Thread Mike MacHenry
Hey everyone,

I am a little confused about Exercise 12.6 from Semantics Engineering with
PLT-Redex. The exercise is as follows:

"Formulate a general reduction relation for ISWIM using a with clause. Use
traces to demonstrate that programs may be reduced to values along several
different paths in a reduction graph. "

The problem I'm having with this is that to accomplish this goal, I needed
to make a change to the ISWIM language definition on page 217 at the
beginning of this chapter, particularly in the contexts section, and I made
absolutely no change to the reduction relation given on page 225 directly
above this exercise.

So for me, that language definition from 217, along with the reduction
relation on 225, as well as the definitions for the meta functions in this
chapter, I get the standard reduction, which permits only one
reduction path for, say, an expression like (+ (1 1) (+ 1 1)). The
left-most (+ 1 1) reduces to 2 first.

When I make the change to the contexts in the language definition, and
change it from

(E hole (V E) (E M) (o V ... E M ...))

to

(E hole (M E) (E M) (o M ... E M ...))

My traces for the above expression gives what I think the exercise is
expecting. I have a diamond shaped in my reduction DAG.

The problem I have with this is that I haven't done anything to the
reduction relation to do this. The exercise seems pretty explicit to create
a reduction relation using a with clause. so it seems like the purpose of
the exercise is to get you to understand the with clause by developing
something new using it. Now granted my reduction relation does use a with
clause, because it's the one I copied out of the book. But I didn't write
that one and use the with clause in my own new way.

So is there a different way to get the reduction relation to support the
general reduction that requires one to change the reduction relation and
not the grammar?

Thanks for the help,
-mike

-- 
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/CAAzRdbcM9w9wi-mPzdpY_4tBWSWv_QaXvZDpgNiXSEr05OaMoA%40mail.gmail.com.


[racket-users] reading scribble include-section code

2019-11-29 Thread Hendrik Boom
I was reading Scribble's include-section code from
https://github.com/racket/scribble/blob/master/scribble-lib/scribble/base.rkt
and I can't figure out how this makes a section:

(define-syntax (include-section stx)
  (syntax-case stx ()
[(_ mod)
 (with-syntax ([doc-from-mod (datum->syntax #'mod 'doc)])
   (unless (module-path? (syntax->datum #'mod))
 (raise-syntax-error #f
 "not a module path"
 stx
 #'mod))
   #'(begin
   (require (only-in mod [doc-from-mod doc]))
doc))]))

Could it be that *any* file read starting with #lang scribble/base
is implicitly wrapped with a @section wrapper of some sort?
Including, presumably, the top-level one presented on the comand line?

(This macro is giving my macro-understanding a real workout.)

(If that's not where I should find a current version of scribble,
someone please let me know)

-- hendrik

-- 
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/20191129165800.k5orams7z2act7ly%40topoi.pooq.com.