Another Typed Racket question: I am trying to use in-fxvector
, which apparently (according to the raco make
error message) requires me to import unsafe-fxvector-length
, and it seems that I should do so by using require/typed
.
So, I have put (require/typed racket/unsafe/ops [unsafe-fxvector-length (FxVector -> Fixnum)])
at the top of the relevant file. Best as I can tell from the documentation (https://docs.racket-lang.org/ts-reference/special-forms.html#%28form._%28%28lib._typed-racket%2Fbase-env%2Fprims..rkt%29._require%2Ftyped%29%29) that is the correct way to import the function. The compiler still complains however, saying > Type Checker: missing type for identifier; > consider using require/typed' to import it
> identifier: unsafe-fxvector-length
> from module: unsafe/ops.rkt
Any suggestions on what I need to do to use
in-fxvector? I assume it's just that I haven't quite for the
require/typed` declaration correct, but I can’t work out what the right thing to put is.
I don’t think it requires you to import unsafe-fxvector-length
Can I see the error message?
You should be able to use in-fxvector
without importing anything that is in its implementation
Oh
there are bugs in Typed Racket
The error message is: > raco make src/montecarlopi.rkt > src/montecarlopi.rkt:47:2: Type Checker: missing type for identifier; > consider using `require/typed’ to import it > identifier: unsafe-fxvector-length > from module: unsafe/ops.rkt > in: (for ((ts (in-fxvector threads-per-place-vec)) (is (in-fxvector iters-per-place-vec))) (montecarlopi/place is ts tx-ch)) (you can view the file in question at https://github.com/jcoo092/CML_benchmarks/blob/TypedRacket/TypedRacket/src/montecarlopi.rkt#L43)
Ah, I see what’s going on now
When in-fxvector
is used in ordinary expressions, it expands to in-fxvector*
, which is typed by Typed Racket
But when in-fxvector
is used in for
, it expands to a more efficient operation, which has unsafe-fxvector-length
and it needs to be typed
:thinking_face:
I would ping @samth
probably unsafe-fxvector-length
needs to be added to https://github.com/racket/typed-racket/blob/master/typed-racket-lib/typed-racket/base-env/base-env.rkt#L3162
sequence position in for
is apparently a can of worms
(in-list 1) ; not type checked
(for ([x (in-list 1)]) 2) ; type checked
I can certainly believe that it would be an issue. Seems like one of those fiddly areas that at first glance looks like it would be simple, but in reality can cause all sorts of problems.
Thanks for all the info! :slightly_smiling_face: I tried imitating the base-env file that you linked to (very much in a ‘cargo cult’ sort of way), but unsurprisingly Racket didn’t like that either.
I think I’ll wait a while with this one and carry on with other things I need to do. Hopefully in a while one of the Typed Racket team or someone will be able to suggest something.
Yes it is my own but I thought there was some simple more dynamic way of getting that info. Thanks
I guess technically I could have used any value but since my function was called is-valid?
I really wanted it to return a boolean
Hi, why this code: (define (pieces-are-equal? piece1)
(cond
[(equal? (piece-space1 piece1)(piece-space2 piece1)) #t]
[else #f]
))
(define (higher-pieces piece1 piece2)
(cond
[(> (and(piece-space1 piece1)(piece-space2 piece1)) (and(piece-space1 piece2)(piece-space2 piece2))) #t]
[else #f]
)
)
(define (higher-list-pieces list)
(cond
[(empty? list) "no hay fichas"]
[(boolean=? (pieces-are-equal? (first list)) #t) (first list)]; if only there one piece double
[(boolean=? (and(pieces-are-equal? (first list))(and(higher-pieces(first list) (first(rest list)))))#t) (higher-list-pieces (cons (first list)(cddr list)))]; if the list have various doubles, returns the double higher
[(boolean=? (higher-pieces (first list) (first (rest list)))#t) (higher-list-pieces (cons (first list)(cddr list)))]; if there not doubles returns the piece higher
[else (higher-list-pieces (rest list))]
)
)
throws me this error: first: contract violation
expected: (and / c list? (not / c empty?))
given: '()
and it show me this part (first (rest list))
in the fourth condition
the code work with other lists,like this, (list (make-piece 3 6)(make-piece 4 4)(make-piece 4 5)(make-piece 4 6)(make-piece 5 5)(make-piece 5 6)(make-piece 6 6))
but it don’t with this list
(higher-list-pieces (list (make-piece 2 3)(make-piece 2 4)(make-piece 2 5)(make-piece 2 6)(make-piece 3 3)(make-piece 3 4)(make-piece 3 5)))
That error message is saying that first
should be called with a nonempty list, but it’s being called with the empty list '()
instead. (There’s no way to get the first element of an empty list, since it doesn’t have one.)
Before you call (first (rest list))
, try checking to make sure there are at least two elements in list
so that (rest list)
isn’t empty. I notice you describe one of the branches as “if only there one piece double,” so you might be intending for that branch to take care of the one-element case, but that’s not actually what it’s checking for right now.
The code works with a list that ends with a piece that has both its spaces equal, like your first example, because the equality of those spaces is actually what that branch is checking for.