Skip to content

Commit

Permalink
More on Matthews-Findler lump
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelballantyne committed Dec 26, 2024
1 parent f2b9834 commit a7a5822
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 3 deletions.
82 changes: 82 additions & 0 deletions tests/dsls/matthews-findler/for-figures.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
(syntax-spec
(binding-class ml-var
#:reference-compiler
(make-variable-like-reference-compiler
compile-RM))

(nonterminal ml-expr
#:binding-space ml

(~> x:ml-var
(syntax/loc #'x (MR x)))

x:ml-var
n:number
(app e1:ml-expr e2:ml-expr)
(+ e1:ml-expr e2:ml-expr)
(- e1:ml-expr e2:ml-expr)
(if0 e1:ml-expr e2:ml-expr e3:ml-expr)
(lambda ([x:ml-var t:ml-type]) e:ml-expr)
#:binding (scope (bind x) e)

(MR e:racket-expr)
(: e:ml-expr t:ml-type)

(~> (e1 e2)
#'(app e1 e2)))

(nonterminal ml-type
#:binding-space ml
Nat
L
(-> t1:ml-type t2:ml-type))

(host-interface/expression
(RM e:ml-expr)
(compile-RM #'e)))

(struct ml-value [v t])
(struct racket-value [v])

;; MLVar is Identifier
;; MLType is Syntax
;; MLExpr is Syntax

;; RM-translation : Any MLType -> Any
(define (RM-translation v t) #| elided ... |#)

;; MR-translation : Any MLType -> Any
(define (MR-translation v t) #| elided ... |#)

(begin-for-syntax
;; compile-RM : MLExpr -> Syntax
(define (compile-RM e)
(define-values (e^ t) (infer-type e))
#`(RM-translation (ml->racket #,e^) #'#,t))

;; assert-type-equal! : MLType MLType MLExpr -> Void
(define (assert-type-equal! actual expected term) #| elided ... |#)

(define-local-symbol-table type-env)

;; type-env-ref : MLVar -> MLType
(define (type-env-ref x)
(symbol-table-ref type-env x #'Nat))

;; type-env-extend! : MLVar MLType -> Void
(define (type-env-extend! x t)
(symbol-table-set! type-env x t))

;; infer-type : MLExpr -> (Values MLExpr MLType)
(define (infer-type e) #| elided ... |#)

;; check-type! : MLExpr MLType -> MLExpr
(define (check-type! e t) #| elided ... |#))

(define-syntax ml->racket
(syntax-parser
#:datum-literals (app + - if0 lambda MR)
#| elided cases ... |#
[(_ (MR e t))
#'(MR-translation e #'t)]))

14 changes: 13 additions & 1 deletion tests/dsls/matthews-findler/lump-inferred.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,19 @@
(struct ml-value [v t])
(struct racket-value [v])

;; MLVar is Identifier
;; MLType is Syntax
;; MLExpr is Syntax

;; RM-translation : Any MLType -> Any
(define (RM-translation v t)
(if (equal? (syntax->datum t) 'L)
(if (racket-value? v)
(racket-value-v v)
(error 'RM "not a Racket value"))
(ml-value v t)))

;; MR-translation : Any MLType -> Any
(define (MR-translation v t)
(if (equal? (syntax->datum t) 'L)
(racket-value v)
Expand All @@ -76,10 +82,12 @@
(error 'MR "not an ML value"))))

(begin-for-syntax
;; compile-RM : MLExpr -> Syntax
(define (compile-RM e)
(define-values (e^ t) (infer-type e))
#`(RM-translation (ml->racket #,e^) #'#,t))

;; assert-type-equal! : MLType MLType MLExpr -> Void
;; No type variables yet, so should just be datum equality.
(define (assert-type-equal! actual expected term)
(unless (equal? (syntax->datum actual) (syntax->datum expected))
Expand All @@ -93,12 +101,15 @@

(define-local-symbol-table type-env)

;; type-env-ref : MLVar -> MLType
(define (type-env-ref x)
(symbol-table-ref type-env x #'Nat))

;; type-env-extend! : MLVar MLType -> Void
(define (type-env-extend! x t)
(symbol-table-set! type-env x t))

;; infer-type : MLExpr -> (Values MLExpr MLType)
(define (infer-type e)
(syntax-parse e
#:datum-literals (app + - if0 lambda MR)
Expand Down Expand Up @@ -138,6 +149,7 @@
(define e^ (check-type! #'e #'t))
(values e^ #'t)]))

;; check-type! : MLExpr MLType -> MLExpr
(define (check-type! e t)
(syntax-parse e
#:datum-literals (app MR)
Expand All @@ -155,7 +167,7 @@

(define-syntax ml->racket
(syntax-parser
#:datum-literals (app + - if0 lambda)
#:datum-literals (app + - if0 lambda MR)
[(_ x:id)
#'x]
[(_ n:number)
Expand Down
4 changes: 2 additions & 2 deletions tests/dsls/matthews-findler/lump.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@

(define (infer-type e)
(syntax-parse e
#:datum-literals (app + - if0 lambda)
#:datum-literals (app + - if0 lambda MR)
[x:id
(type-env-ref #'x)]
[_:number
Expand Down Expand Up @@ -109,7 +109,7 @@

(define-syntax ml->racket
(syntax-parser
#:datum-literals (app + - if0 lambda)
#:datum-literals (app + - if0 lambda MR)
[(_ x:id)
#'x]
[(_ n:number)
Expand Down

0 comments on commit a7a5822

Please sign in to comment.