Since I favor blunt instruments, and I like having the typed & untyped
interfaces in separate files, I adapted Alex’s submodule idea using a new
function called `include-without-lang-line` [1] that pulls out the code
from a typed file so it can be recompiled in an untyped context.

For instance, with the arrangement below, the function `fladd` only gets
written in one place, but it can be required 1) from typed code as a typed
function, 2) from untyped code as an untyped, contracted function 3) from
untyped code with no contract.

;;; typed.rkt
#lang typed/racket/base
(provide fladd)
(: fladd (Flonum Flonum . -> . Flonum))
(define (fladd f1 f2)
  (+ f1 f2))

;;; untyped.rkt
#lang racket/base
(require racket/contract)

(module typed-functions typed/racket/base/no-check
  (require sugar/include)
  (include-without-lang-line "typed.rkt"))

(require 'typed-functions)
(provide fladd)
(module+ safe
  (provide (contract-out [fladd (flonum? flonum? . -> . flonum?)])))


[1]
http://pkg-build.racket-lang.org/doc/sugar/#%28form._%28%28lib._sugar%2Finclude..rkt%29._include-without-lang-line%29%29


On Mon, Mar 23, 2015 at 10:42 AM, Vincent St-Amour <stamo...@ccs.neu.edu>
wrote:

> This is what Ian is referring to:
>
> http://docs.racket-lang.org/ts-reference/Utilities.html#%28part._.Untyped_.Utilities%29
>
> I think `define-typed/untyped-identifier` may be what you're looking for.
>
> Vincent
>
>
> At Sat, 21 Mar 2015 14:18:44 -0400 (EDT),
> J. Ian Johnson wrote:
> >
> > At RacketCon a couple years ago, Vincent and Neil Toronto worked out a
> crazy way to do this with macros and submodules. I think the math library
> uses the technique they came up with.
> > One of them should write a blog post about the technique.
> > -Ian
> >
> > ----- Original Message -----
> > From: "Matthew Butterick" <m...@mbtype.com>
> > To: "users" <us...@racket-lang.org>
> > Sent: Saturday, March 21, 2015 2:09:04 PM
> > Subject: [racket] making libraries work natively with both Racket &
> Typed     Racket
> >
> > Is there an approved way of using #lang typed/racket/base/no-check (or
> maybe `with-type`) to create libraries that have both a typed and untyped
> interface? (The goal being to avoid use of `require/typed`)
> >
> > For instance, the following works, but maybe it's a bad idea for other
> reasons:
> >
> > ;;;;;;;;;;;;;;;;;;;
> >
> > ;; adder.rkt = write typed code, but leave off #lang line & `provide`
> >
> > (: fladd (Flonum Flonum . -> . Flonum))
> > (define (fladd f1 f2)
> >   (+ f1 f2))
> > ;;;;;;;;;;;;;;;;;;;
> >
> >
> > ;;;;;;;;;;;;;;;;;;;
> >
> > ;; typed.rkt = compile in typed context
> >
> > #lang typed/racket/base
> > (require racket/include)
> > (provide fladd)
> > (include "adder.rkt")
> > ;;;;;;;;;;;;;;;;;;;
> >
> >
> > ;;;;;;;;;;;;;;;;;;;
> >
> > ;; untyped.rkt = compile in untyped context with contract
> >
> > #lang typed/racket/base/no-check
> > (require racket/include racket/contract racket/math)
> > (provide (contract-out [fladd (flonum? flonum? . -> . flonum?)]))
> > (include "adder.rkt")
> > ;;;;;;;;;;;;;;;;;;;
> >
> >
> > ;;;;;;;;;;;;;;;;;;;
> >
> > ;; test.rkt
> >
> > #lang racket/base
> >
> > (module typed typed/racket/base
> >   (require "typed.rkt")
> >   (require typed/rackunit)
> >   (check-equal? (fladd 1.0 2.0) 3.0)) ; typechecks correctly
> >
> > (module untyped racket/base
> >   (require "untyped.rkt")
> >   (require rackunit)
> >   (check-equal? (fladd 1.0 2.0) 3.0) ; meets `provide` contract
> >   (check-exn exn:fail:contract? (λ () (fladd 1 2)))) ; violates
> `provide` contract
> >
> > (module violator racket/base
> >   (require "typed.rkt")
> >   (require rackunit)
> >   (check-exn exn:fail:contract? (λ () (fladd 1 2)))) ; violates
> typed/untyped contract barrier
> >
> > (require 'typed)
> > (require 'untyped)
> > (require 'violator)
> > ;;;;;;;;;;;;;;;;;;;
> >
> >
> > ____________________
> >   Racket Users list:
> >   http://lists.racket-lang.org/users
> >
> > ____________________
> >   Racket Users list:
> >   http://lists.racket-lang.org/users
>
> --
> 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.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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.
For more options, visit https://groups.google.com/d/optout.

Reply via email to