[racket] Typed racket and keywords

From: Norman Gray (norman at astro.gla.ac.uk)
Date: Thu Feb 21 17:28:44 EST 2013

Greetings, again.

On 2013 Feb 21, at 19:34, Norman Gray <norman at astro.gla.ac.uk> wrote:

> It might also be worth looking at the docs for 'lambda:', since I can't see any way of adding keywords to an anonymous function.

...and following up with tangent to my own question, it appears that make-keyword-procedure and keyword-apply don't work well with Typed Racket.

Consider:

(define make-null
  (let ((proc values))
    (make-keyword-procedure
     (λ (kws kw-args)
        (keyword-apply proc kws kw-args '(null))))))
(make-null)

With '#lang racket' that works fine, and prints 'null.

With '#lang typed/racket', however, it produces:

Welcome to DrRacket, version 5.3.3 [3m].
Language: typed/racket; memory limit: 128 MB.
. Type Checker: untyped identifier make-keyword-procedure imported from module <typed/racket> in: make-keyword-procedure
. Type Checker: untyped identifier keyword-apply imported from module <typed/racket> in: keyword-apply
. Type Checker: Summary: 2 errors encountered in:
  make-keyword-procedure
  keyword-apply
> 

This is part of an effort to produce a procedure which returns a (anonymous) procedure within Typed Racket.  Now, I'm not sure what the type of make-keyword-procedure would be, so I'm not sure if this is a sensible thing to want.

But I'm now quite securely stuck, I think.

----

In case anyone is curious (and in case the clear and obvious solution is indeed clear and obvious), what I'm trying to do is produce a typed version of the procedure below:

#lang racket

(struct asn-value (tag ; integer
                   rep ; symbol or string
                   type) ; integer in (0--30)
  #:transparent)

(define (make-asn-value-maker type-tag)
  (define (make-asn-value* tag rep type)
    (asn-value tag rep type))
  (make-keyword-procedure
   (λ (kws kw-args rep)
     (let loop ((k kws)
                (v kw-args)
                (tag 0))
       (cond ((null? k)
              (make-asn-value* tag rep type-tag))
             (else
              (let ((class-offset 
                     (case (car k)
                       ((#:application) #x40)
                       ((#:context)     #x80)
                       ((#:private)     #xc0)
                       (else (error 'der-codec "asn-value constructor: unrecognised keyword: ~s" (car k))))))
                (unless (= tag 0)
                  (error 'der-codec "Multiple class keywords in asn-value-maker: ~s" kws))
                (loop (cdr k) (cdr v) (+ class-offset (car v))))))))
   (λ (rep)
     (make-asn-value* type-tag rep type-tag))))

(define make0 (make-asn-value-maker 0))
(make0 'first) ; => (asn-value 0 'first 0)
(make0 'second #:application 1) ; => (asn-value 65 'second 0)
;(make0 'third #:context 2 #:private 3) ; should error

(and in case anyone's wondering about the keyword names, yes, I am indeed trying to tidy up a DER encoding/decoding library, which works, but which could be improved by typification)

Best wishes,

Norman


-- 
Norman Gray  :  http://nxg.me.uk
SUPA School of Physics and Astronomy, University of Glasgow, UK



Posted on the users mailing list.