

Would it be better to email @asumu directly? I don’t know if he’s active on Slack.

Hello, I met a problem about match
in typed/racket: #lang typed/racket
(define-type Type (U Number Symbol))
(ann (lambda (arg)
(match arg
[(or (? number? x)
(? symbol? x))
x]))
[-> Type Type])
It failes with the following message: Type Checker: type mismatch
expected: Type
given: (U False Type)
in: x
And if I change Type
to a Refine
Type: #lang typed/racket
(define-type Variable (Refine [var : Symbol] (! var 'λ)))
(define-predicate variable? Variable)
(define-type Type (U Number Variable))
(ann (lambda (arg)
(match arg
[(or (? number? x)
(? variable? x))
x]))
[-> Type Type])
It failes: Type Checker: type mismatch
expected: Type
given: (U Complex False Symbol)
If Type
is a recursive Refine
type: #lang typed/racket
(define-type Variable (Refine [var : Symbol] (! var 'λ)))
(define-predicate variable? Variable)
(define-type Type (U Number Variable (Listof Type)))
(ann (lambda (arg)
(match arg
[(or (? number? x)
(? variable? x))
x]))
[-> Type Type])
It will stuck in an endless loop.

Here is another problem about define-predicate
: #lang typed/racket
(define-type Foo (List Any))
(define-predicate foo? Foo)
(define-type Bar-List (Listof Bar))
(define-type Bar (U Foo Bar-List))
(define-predicate bar-list? Bar-List)
(define-predicate bar? Bar)
I tried to define a predicate bar?
for Bar
by define-predicate
, but it failed: Type Checker: Type Bar could not be converted to a predicate: required a flat contract but generated a chaperone contract
in: Bar
then I defined bar?
manually: #lang typed/racket
(define-type Foo (List Any))
(define-predicate foo? Foo)
(define-type Bar-List (Listof Bar))
(define-type Bar (U Foo Bar-List))
(define-predicate bar-list? Bar-List)
(: bar? [-> Any Boolean : Bar])
(define bar?
(λ (arg)
(or (foo? arg)
(bar-list? arg))))
(displayln (bar? '((1) ((2)) (((3)))))) ; #t
It works well. Why doesn’t define-predicate
work for Bar
?

@greg406 has joined the channel

so (string->symbol "1")
results in '\|1\|
. which is the symbol of an integer. how would i convert this to an integer? I can do (symbol->string) and then string to number but im looking for another built in way

@jestarray that’s the way.

ahh alright

I suppose you can do something like: (read (open-input-string (let ([v (open-output-string)])
(display "1" v)
(get-output-string v))))

But I don’t recommend that

You can potentially save a bit by using symbol->immutable-string
to (maybe?) avoid a string copy.

(symbol->string
returns a mutable string for what I assume are historical reasons.)

feels a bit dirty but eh

why do you say that?

its a few extra steps, not to mention the string allocation

The string allocation is what I’m addressing with symbol->immutable-string
. I agree there shouldn’t be a need to allocate to do this. Extra steps though… if you mean going by way of the symbol’s string representation, then that doesn’t seem “extra” to me.

The machine probably doesn’t think of it as dirty, probably 00101010111011100011110 one way and 1101010101010110100011101100110 another way.

Why are you converting from strings to numbers via symbols at all?

ohh wait, i can simplify it to just (string->number)
, in short I have a string “monster_swingsword_north_0”, the filename of image(s) animation for my game and i run it through a (map string->symbol (string-split str "_"))
to dice them up, pickout the pieces and map everything to a number. It just occuried to me i can just store the (string-split)
and index into that just for the number

@andrzej has joined the channel

@robby or @mflatt — I have a student submission saved in v7.8 of DrR. It’s a wxme file containing embedded images. I have a server-side script that tries to load the file and process it, running under xvfb-run. And I’m seeing the following error: insert-file in text%: error loading the file (read-from-file-failed)
context...:
/usr/share/racket/pkgs/gui-lib/mred/private/wxme/text.rkt:2727:2: insert-port method in text%
/usr/share/racket/pkgs/gui-lib/mred/private/editor.rkt:212:23
/usr/share/racket/pkgs/gui-lib/mred/private/editor.rkt:206:19
/opt/bottlenose/lib/assets/render-racket.rkt:173:0: render
(submod "/opt/bottlenose/lib/assets/render-racket.rkt" main): [running body]
temp35_0
for-loop
run-module-instance!
Any ideas what might be causing this? (I don’t have racket 7.8 locally on my laptop (because the ubuntu PPA hasn’t been updated yet) so I can’t reproduce this locally. I can send you the relevant files and scripts, if you like.)

wasn’t it you who told me that that means it is actually a 7.7 file?

But really, I don’t know.

I had thought so! :slightly_smiling_face: But I looked in the file and its preamble says #reader(lib"<http://read.ss\|read.ss>""wxme")WXME0109 ##
#\|
This file uses the GRacket editor format.
Open this file in DrRacket version 7.8 or later to read it.

(this is a different file than that one that I’d commented on)

If you have dev tools installed, building racket consists of just “make” and getting coffee. And the .sh download from will install in a directory that you can just throw away if you don’t want it. It isn’t like it does crazy stuff on your file system or anything.

You don’t need to put it into /usr or anything like that. It’s totally self-contained.

I can’t tell what might be going wrong with that file, I’m sorry.

k, thanks

ok, I downloaded Racket 7.8 and have it in a local dir. Even opening the file in DrRacket 7.8 fails

same error

OK, here’s more confusion related to the student languages. If I type
(string->number "1.00")
into DrRacket’s Interactions window in Beginning Student, it produces the integer 1. But if I put the following into m.rkt
:
#lang htdp/bsl
(inexact? (string->number "1.0"))
and run using racket -t m.rkt
, Racket produces #t, i.e, it converts to an inexact number. How can I make the two environments give me the same answer?

#lang htdp/bsl
is somewhat “broken” in that sense. The recommendation I saw is to use the dropdown menu to choose Beginning student language

Yes, and when using DrRacket I absolutely do. The trouble is that this is arising in a back-end shell script that grades student submissions, so it has to work from the command line. Any way to make that work?

You can run BSL programs from the command line, and they should work as you see in DrR

Are there instructions for doing so?

just run them with racket

or do you mean how to write them?

If so, I’m not sure why you want to write a new program in BSL

Consider the following program:
(require test-engine/racket-tests)
(check-expect (inexact? (string->number "1.0")) false)
(test)
If I enter that program in the Definitions window in BSL and press run, it works OK. Now, put this into a file n.rkt:
#lang htdp/bsl
(require test-engine/racket-tests)
(check-expect (inexact? (string->number "1.0")) false)
(test)
When I run it with racket n.rkt, it fails. What’s the difference? “Just run them with racket” is precisely what I’m hoping to be able to do, but I can’t account for the differences in behaviour.

I want to understand what you want to do better. If “1.0” is written in students’ code, then it’s an integer. If it’s written in instructor’s code, then it’s an inexact number. But the fix is easy. You just change it to “1".

What I’m saying is that if you put that in the definitions window in DrRacket, and then save the file, and then run that file with racket file.rkt
it will do the right thing

#lang htdp/bsl
is not useful for any purposes that I know of

If I leave it out, I get an error message starting with “default-load-handler: expected a `module’ declaration…”

@lmrosso has joined the channel

Are you getting the text of the file that the student writes in the definitions window, or the actual saved file?

And if the former, why?

Ah, you mean include the invisible header information in the saved student file. Let me check that.

No, even with the full header information, the test fails because the number is parsed as an inexact.

#reader(lib "<http://htdp-beginner-reader.ss\|htdp-beginner-reader.ss>" "lang")((modname a02q3) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f)))
(require test-engine/racket-tests)
(check-expect (inexact? (string->number "1.0")) false)
(test)

@sorawee, it’s more subtle than that. I’m distilling a more complicated test scenario down to this simple demonstration of different behaviour.

Sorry. I think we are having the https://en.wikipedia.org/wiki/XY_problem.
Can you show what students’ program look like? And how are you planning to autograde it?

Sure, let me try to expand a little.

What you write above: (check-expect (inexact? (string->number "1.0")) false)
is not really grading, because everything is “fixed” already. There’s no input from student program.

Ok, I can reproduce the problem. If you put (check-expect (inexact? (string->number "1.0")) false)
in a BSL program, save it, and then require the test
submodule of that program, you get a failure.

So it’s related to test-engine/racket-tests?

No.

This program has the same surprising behavior: #lang racket
(require (prefix-in h: lang/htdp-beginner))
(inexact? (h:string->number "1.0"))

as in, it prints #t

Weird. I noticed that there’s a hidden configuration option for string->number that could affect this, but I can’t see why it should flip in this context.

That’s almost certainly the issue then

Note that the beginner docs are wrong for string->number (in the example)

@sorawee Let’s say you wanted to write a function that receives a dollar amount in a string, and produces the corresponding number of cents as an integer. So, for example, I’d want (cents "14.23")
to produce 1423
.

Or rather, the problem is that you need to set (read-decimal-as-inexact? #f)

A student might happily write this in DrRacket:
(define (cents amt)
(number->string (* 100 (string->number amt))))
(check-expect (cents "1.00") "100")
And that works just fine. But when we run the same check-expect
in our command-line testing environment, it fails for the reasons given above.

@samth Yes, I saw that. But first of all, I don’t know how to get access to that function from the student languages (sorry, that’s my own naivety), and second, I don’t know what other traps might be lurking beyond this one which I just happened to discover.

Here’s a workaround:
- Create
grader.rkt
with the content:
#lang racket
(dynamic-require '(submod "file-to-grade.rkt" test) #f)
2. Create launcher.rkt
with the content:
;; The first three lines of this file were inserted by DrRacket. They record metadata
;; about the language level of this file in a form that our tools can easily process.
#reader(lib "<http://htdp-beginner-reader.ss\|htdp-beginner-reader.ss>" "lang")((modname b) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #t)))
(require "grader.rkt")

And run racket launcher.rkt

So It’s unsatisfying to apply a single band-aid here, only to get stuck again (and indeed, see my much earlier question about using errortrace). I really want a big hammer that makes everything behave just like it does in the teaching languages.

@sorawee Whoops, sorry, the function should produce the integer number of cents as a string. (This is extracted from a longer problem…)

Actually, only one file might suffice if we require
dynamic-require
from racket
in launcher.rkt

Yeah, so launcher.rkt
could be:
#reader(lib "<http://htdp-beginner-reader.ss\|htdp-beginner-reader.ss>" "lang")((modname b) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #t)))
(require racket)
(dynamic-require '(submod "file-to-grade.rkt" test) #f)

I would also suggest looking at make-evaluator, which is what I use and which takes an specification that includes specific BSL handling

Yes, I had started to look into make-evaluator a little bit, and it seems like a more industrial strength solution to creating a command line harness for running student code. I’m worried that it won’t solve problems like the one above directly, but it might at least give me the ability to patch problems as I see them.

Hmm, it’s might be challenging to get check-expect tests working inside an evaluator.

@peterzhong2023009 has joined the channel

Anyone have a nice way to format code with line numbers in slideshow
? I’m finding the code
macro very nice, but I haven’t found a nice way to easily reference portions of the code.

@azriel the handin-server does it, you might look at that

Oooh, I hadn’t seen that. Thanks.

Forgive my ignorance, where can I find a copy of the source code?

nvm, found it on github.

@azriel Shriram Krishnamurti also recently wrote a package for autograding using gradescope, you might look into that

Thanks! I’m definitely willing to bug Shriram on twitter :slightly_smiling_face:

