Re: [racket-users] Issue with define-type and typed classes

2020-03-31 Thread Ben Greenman
On 3/5/20, hashim muqtadir  wrote:
> So I have this file, found here:
> https://gitlab.com/hashimmm/remap/-/blob/3682db9fff3bc5007833e07bf9a9ed6e8e0170a9/private/tables.rkt
>
> Lines 51 through 55 say:
>
> (define-type (Func<%> A)  (BaseFunc<%> A FuncAnyParam))(define-type
> BoolFunc<%> (Func<%> BooleanColumn))(define-type AnyFunc<%> (Func<%>
> ColIdent))
>
> And this used to work on older versions of racket, say Racket BC 7.4
>
> But on Racket CS 7.6, I get an error on this line:
> (define-type AnyFunc<%> (Func<%> ColIdent))
>
> Even though (a) it doesn't throw an error on the line before it, and
> (b) replacing it with (define-type AnyFunc<%> (BaseFunc<%> ColIdent
> FuncAnyParam))
> works.
>
> I thought the way define-type works is that
> (Func<%> ColIdent)
> would expand into (BaseFunc<%> ColIdent FuncAnyParam)
>
> I've had similar problems with define-type previously where I'd think it
> would just expand
> by substituting the parameter but it doesn't seem to really do that. I don't
> have any
> trivial example to show, though. Fiddling around eventually gets it to
> work.
>
> I'd really appreciate some help as to how to think about what define-type
> does in my head.
>

That code should work. There's a bug somewhere, either TR or deeper.

I am thinking its deeper because the code sometimes compiles.

1. When I first tried `raco pkg install remap` (using a current CS), it worked
2. Then I tried compiling with BC 7.6 and CS 7.6. BC succeeded and CS failed.
3. Later, I saw 2 consecutive runs of CS fail and succeed:

```
(20:02)% rm -r compiled

(20:02)% /Applications/RacketCS-v7.6/bin/raco make -v tables.rkt
"tables.rkt":
  making #
/Users/ben/code/racket/fork/extra-pkgs/remap/private/tables.rkt:59:3:
Type Checker: parse error in type;
 expected a class type for #:implements clause
  given: (Opaque Func<%>)
  in: (Class #:implements BoolFunc<%>)
  compilation context...:
   /Users/ben/code/racket/fork/extra-pkgs/remap/private/tables.rkt
  context...:
   do-raise-syntax-error
   winder-dummy
   .../private/parse-type.rkt:1234:0: merge-with-parent-type
   .../private/parse-type.rkt:1364:14: for-loop
   .../private/parse.rkt:852:26
   .../private/runtime.rkt:80:24: temp5975
   .../env/type-alias-helper.rkt:230:4: for-loop
   .../env/type-alias-helper.rkt:101:0: register-all-type-aliases
   .../typecheck/tc-toplevel.rkt:359:0: type-check
   .../typecheck/tc-toplevel.rkt:602:0: tc-module
   .../typed-racket/tc-setup.rkt:96:12
   winder-dummy
   .../typed-racket/typed-racket.rkt:23:4
   call-in-empty-metacontinuation-frame
   apply-transformer
   dispatch-transformer

(1:20:02)% rm -r compiled
rm: compiled: No such file or directory

(1:20:02)% /Applications/RacketCS-v7.6/bin/raco make -v tables.rkt
"tables.rkt":
  making #
 [output to "compiled/tables_rkt.zo"]
```

-- 
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/CAFUu9R5aKWqDnSsTf-jJf-VnkDfQ0H38otTkcfLvY%2Buu5yuG2A%40mail.gmail.com.


[racket-users] racket/draw: how to extract the path from a font% object?

2020-03-31 Thread Matthew Butterick
IIUC every `font%` object must correspond to a particular font file on disk.

If so, given a `font%` object, how do I extract the path to that file?

-- 
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/39ABE79E-4319-4B75-8BB4-B93694F7E203%40mbtype.com.


Re: [racket-users] HTDP2e Exercise 342 find-all

2020-03-31 Thread Ben Greenman
On 3/21/20, Aron Zvi  wrote:
> Hi guys,
>
> Can I get a hint for Exercise 342 find-all
>

Don't use `find` as a helper function; instead, design a function that
returns a list of successes

-- 
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/CAFUu9R7SU%2B1QLwZfPSnVXNV%3DG60VuX4oVeNU4tz%2BdE5jEunSmA%40mail.gmail.com.


Re: [racket-users] HTDP2E 22.3 Domain-Specific Languages: Configurations

2020-03-31 Thread Ben Greenman
On 3/30/20, Aron Zvi  wrote:
> Hey guys
>
> In 22.3 Domain-Specific Languages: Configurations, the book introduces the
> following data example and data definitions for FSM configurations
>
>
> (define
> 
>  xm0
>   '(machine ((initial "red"))
>  (action ((state "red") (next "green")))
>  (action ((state "green") (next "yellow")))
>  (action ((state "yellow") (next "red")
>
> ; An XMachine is a nested list of this shape:
> ;   `(machine ((initial ,FSM-State
> )) [
> List-of
> 
>
> X1T ])
> ; An X1T is a nested list of this shape:
> ;   `(action ((state ,FSM-State
> ) (
> next ,FSM-State
> )))
> I do no understand the XMachine data definition properly. In the definition
>
> it appears that it would be a list with 3 elements:
>
> 1*. *'machine
> 2. `((initial ,FSM-State
> ))
> 3. [List-of
> 
>
> X1T ]
>
> However, from the data example (and the fact that it is an Xexpr
> ) I know
> that the intention is different and that it is a list of the form:
>
> 1. 'machine
> 2.  `((initial ,FSM-State
> ))
> 3. X1T -1
> ...
> X1T -n
>
> where is seems that [List-of
> 
>
> X1T ]is
> expanded and included in the list  ([List-of
> 
>  X1T ] is
> consed
> onto)
>
> Can you please explain how the given  `(machine ((initial ,FSM-State
> )) [
> List-of
> 
>
> X1T ])
> translates
> to this?
>

I think you are right. An XMachine should be a list of 3 elements by
that data definition.

The definition should probably have a "." (or use `cons`es):

`(machine ((initial ,FSM-State)) . [List-of X1T])

-- 
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/CAFUu9R7z4NZMpvRmNMqFsgxoaWQReZDokS19E4o60GJX8rye-Q%40mail.gmail.com.


Re: [racket-users] Re: Working with JSON using Typed Racket

2020-03-31 Thread epi
Thanks Wesley and Matthias! Very helpful advice.

March 31, 2020 2:40 AM, "Wesley Bitomski" mailto:wesley.bitom...@gmail.com?to=%22Wesley%20Bitomski%22%20)>
 wrote:
Hello Ed,
I generally get some mileage out of using custom predicates (those that are 
made with define-predicate) and pattern matching when tearing into JSON 
structures in Typed Racket. Occurrence typing seems to work with if, cond and 
assert, along with match's predicate matcher (? form).

So, something like this I think could less tedious to produce, and does exactly 
what your example does:
(: jsexpr->cd-users-data (JSExpr -> (Option (Listof String
(define (jsexpr->cd-users-data js)
(define-predicate names-list? (Listof String))
(match js
[(hash-table ['Type "DTHidden"]
['Data (hash-table ['Type "CDUsers"]
['Data (? names-list? users)])])
users]
[_ #false]))
I hope this is what you were looking for.
On Sunday, March 29, 2020 at 8:39:56 AM UTC-4, e...@disroot.org wrote:
 Hi everyone,

Recently I've been experimenting with Typed Racket and trying to 
gradually type my code base.
One of the functions that I need to write is to extract a list of strings from 
a JSON object, if it has following form:

{
"Type": "DTHidden",
"Data": { "Type": "CDUsers",
"Data": ["datum1", "datum2"] }
}

The way I used to structure it in Racket is to have a function 
`is-cdusers?` with the contract `jsexpr? -> boolean?`, which would check that 
the JSON object has the right shape; and a separate function `get-cdusers-data` 
with the contract `is-cdusers? -> listof string?`.

However, after playing a bit with Typed Racket I decided that it was 
necessary, according to my understanding of occurrence typing, to have a single 
function `get-cdusers-data` with the type `JSExpr -> (U False (Listof String))`.
In order to get it to work I ended up writing a long chain of conditionals:
(: get-cdusers-data (-> JSExpr (U False (Listof Any
(define (get-cdusers-data js)
(if (and (hash? js)
(equal? DTHidden (hash-ref js 'Type #f)))
(let ([js (hash-ref-def js 'Data [ann #hasheq() JSExpr])])
(if (and (hash? js)
(equal? CdUsers (hash-ref js 'Type #f)))
(let ([data (hash-ref js 'Data)])
(if (hash? data)
(let ([x (hash-ref js 'Data #f)])
(and (list? x) x))
#f))
#f))
#f))

Needless to say, this is a bit impractical and error-prone to write.
Does anyone know if there is a better approach to this?

From my experience with typed languages I would get that the most 
principle approach is to have an algebraic data type that represents all the 
underlying data structures, something like

type reply = ... | CDUsers of string list | ...

and then have a single function to converts a JSExpr into that data 
type.

I was hoping to avoid that, because I do enjoy working with the JSExpr 
type directly in Racket.

Does anyone have advice/experience with problems like this?

Best wishes,
-Ed--
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 
(mailto:racket-users+unsubscr...@googlegroups.com).
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/f85a4a4d-0d0a-47e3-909b-2b378def368a%40googlegroups.com
 
(https://groups.google.com/d/msgid/racket-users/f85a4a4d-0d0a-47e3-909b-2b378def368a%40googlegroups.com?utm_medium=email_source=footer).

-- 
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/e678bbf9d87ef3c72db8ed45f9459c60%40disroot.org.


Re: [racket-users] questions about top-level-bind-scope in root-expand-context

2020-03-31 Thread Yongming Shen
I have done some further digging. Looks like it is non-trivial to remove 
the top-level-bind-scope and still preserve the current behaviors of 
top-level `define-values` and `define-syntaxes`. In particular, through 
`as-expand-time-top-level-bindings` in "expander/expand/bind-top.rkt", both 
top-level `define-values` and `define-syntaxes` use 
`select-defined-syms-and-bind!/ctx` from "expander/expand/def-id.rkt", 
which selects variable-symbols and binds them at the same time. So after a 
variable symbol is selected, a binding must be created for it. For 
top-level `define-values` and `define-syntaxes`, using the 
top-level-bind-scope for that mandatory binding is necessary to achieve the 
current top-level behaviors. On the other hand, uses of the 
top-level-bind-scope that are related to `#%top` (such as in 
"expander/expand/expr.rkt" and "expander/expand/main.rkt") appear to be 
non-essential, but may offer minor performance advantages (really not sure).

On Tuesday, March 24, 2020 at 2:15:32 AM UTC-4, Yongming Shen wrote:
>
> Hi Matthew,
>
> Thanks for the explanations. But I'm still not convinced that the 
> top-level-bind-scope is needed. This is my current understanding. The 
> purpose of the  top-level-bind-scope is to support recursion better at the 
> top level. But for the case of `(define-values (x) ...)`, if `x` is not 
> defined yet, then implicit #%top in `...` will let `...` refer to `x`. If 
> `x` is defined, then the old definition will be used by `...`. Either way, 
> the top-level-bind-scope is not needed. For the case of `(define-syntaxes 
> (x) ...)`. As you explained, a macro can naturally recursively refer to 
> itself, simply because of how macro expansion works, so the 
> top-level-bind-scope is again not needed for recursion. Is my understanding 
> correct?
>
>
> On Monday, March 23, 2020 at 10:05:12 AM UTC-4, Matthew Flatt wrote:
>>
>> At Mon, 23 Mar 2020 01:45:40 -0700 (PDT), Yongming Shen wrote: 
>> > I tried the example you gave for my first question and it resulted in 
>> an 
>> > error. 
>>
>> Oops --- you're right. I lost track of what we try to make work at the 
>> top level. 
>>
>> > I think this is because `(define-values (x) ...)` expands `...` without 
>> the 
>> > top-level-bind-scope, even when expand-context-to-parsed? is #t 
>> (according 
>> > to expander/expand/top.rkt). Is this a bug? 
>>
>> Since the behavior goes far back, I think this is the behavior that we 
>> decided to settle for. 
>>
>> > Related to your answer to my second question, `define-syntaxes` 
>> similarly 
>> > does not add the top-level-bind-scope when expanding `...`. Does this 
>> mean 
>> > that even for `define-syntaxes`, `...` won't use the 
>> top-level-bind-scope 
>> > binding(s) after all? 
>>
>> The way that evaluation, binding, and expansion are interleaved means 
>> that a `define-syntaxes` macro can refer to itself in expansions. The 
>> binding of an identifier in a macro template is resolved after the 
>> macro is applied. 
>>
>> The difference with `define` is that the right-hand side is 
>> expanded/compiled before `define` binds. 
>>
>> > A little bit off-topic, in the definition of define-values (in 
>> > expander/expand/top.rkt), there is `(define-match m s ...)`, but for 
>> > define-syntaxes it is `(define-match m disarmed-s ...)`. Is this 
>> difference 
>> > significant? Or does define-match not care whether `s` or `disarmed-s` 
>> is 
>> > used? 
>>
>> Using `disarmed-s` in the definition of `define-values` is probably 
>> a better idea, and I'll look into that more. 
>>
>> It think it turns out not to matter, normally, because `define-values` 
>> is transparent to syntax arming via `syntax-arm` with a #t second 
>> argument (which is what the expander does). But it would be better to 
>> not rely on that. 
>>
>>

-- 
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/09bf14a5-1c1c-4718-80d7-c4414bf98d52%40googlegroups.com.