[racket] Defining a typed language

From: E. Moran (l-rkt at agate7blue.fastmail.fm)
Date: Sun Oct 26 21:36:19 EDT 2014

On Mon Oct 20 07:08:54 EDT 2014, Konrad Hinsen wrote:
> I have put the code on Github for easier access:
>   https://github.com/khinsen/racket-typed-lang
> Once this works, it can serve as a template for others.

Thanks for posting this...  I was just wondering whether you've considered using
datum->syntax here.  It'd maybe not be the most elegant solution,
but it would at least give you a working language.

  -- a-typed-lang/main.rkt -------------

  #lang typed-racket/minimal

  (require typed/racket
           (only-in typed/racket
                    [#%module-begin tr:module-begin]
                    [require tr:require]))

  (provide (except-out (all-from-out typed/racket) #%module-begin)
           (rename-out [module-begin #%module-begin]))

  (define-syntax (module-begin ix)
    (syntax-case ix ()
      [(_ rest ...)
           ; tr:require keeps the meaning that it has in this module, but the
           ; s-exp as a whole lands in the *caller's* lexical context...
           (datum->syntax ix (list #'tr:require 'a-typed-lang/more))])
             rest ...))]))

  -- a-typed-lang/more.rkt -------------

  #lang typed/racket

  (provide foo)

  (: foo (Integer -> Integer))
  (define (foo x)
    (* 2 x))

  -- example.rkt -----------------------

  #lang a-typed-lang

  (: x Integer)
  (define x 42)

  (displayln x)

  (displayln (foo x))

  (displayln (impersonator? foo))

The above works under v5.3.4, anyway.

Hope this helps...  Evan

Posted on the users mailing list.