PasteRack.org
Paste # 10300
2022-07-08 01:24:22

Fork as a new paste.

Paste viewed 1464 times.


Embed:

  1. #lang racket
  2.  
  3. (require (prefix-in racket: racket))
  4. (require syntax/parse/define)
  5.  
  6. (define-syntax-parser TODO
  7.   [_ #'(error "TODO: unimplemented")])
  8.  
  9. (define-signature cat^ (identity compose))
  10. (define-signature products^ (pi1 pi2 pair into-terminal))
  11. (define-signature sums^ (in1 in2 split from-initial))
  12. (define-signature exponentials^ (lambda eval))
  13.  
  14. (define-signature contexts^
  15.   (context-empty  ;; ctx
  16.    context-get    ;; ctx, sym -> Γ  A
  17.    context-extend ;; ctx, sym -> ctx,  × A  Γ,A)
  18.    ))
  19.  
  20. (define-unit racket@
  21.   (import)
  22.   (export cat^ products^ sums^ exponentials^)
  23.   (define identity racket:identity)
  24.   (define compose racket:compose1)
  25.   (define pi1 car)
  26.   (define pi2 cdr)
  27.   (define ((pair f g) x) (cons (f x) (g x)))
  28.   (define (into-terminal x) '())
  29.   (define (in1 x) `(left ,x))
  30.   (define (in2 x) `(rght ,x))
  31.   (define ((split f g) x)
  32.     (match x [`(left ,y) (f y)] [`(rght ,y) (g y)]))
  33.   (define (from-initial _) (error "impossible"))
  34.   (define (eval x) ((car x) (cdr x)))
  35.   (define (((lambda f) a) b) (f (cons a b)))
  36.   )
  37.  
  38. (define-unit products->contexts@
  39.   (import cat^ products^)
  40.   (export contexts^)
  41.   (define context-empty '())
  42.   (define (context-get ctx sym)
  43.     (match ctx
  44.       ['() (error "unbound symbol")]
  45.       [(cons (== sym) _) pi2]
  46.       [(cons _ xs) (compose pi1 (context-get xs sym))]))
  47.   (define (context-extend ctx sym)
  48.     (values (cons sym ctx) identity)))
  49.  
  50. (define-compound-unit/infer racket+contexts@
  51.   (import)
  52.   ;; ugh, look at all this boilerplate.
  53.   (export cat^ products^ sums^ exponentials^ contexts^)
  54.   (link racket@ products->contexts@))
  55.  
  56. ;; ---------- SIMPLY TYPED λ-CALCULUS ----------
  57. (define-signature stlc^
  58.   (var     ;; x ->   A)      where x:A  Γ
  59.    lambda  ;; x, (Γ,x:A  B) ->   A => B)
  60.    apply   ;;   A => B),   A) ->   B)
  61.    project ;; i, n,   Πᵢⁿ Aᵢ) ->   Aᵢ)
  62.    tuple   ;;   Aᵢ)ᵢⁿ ->   Πᵢⁿ Aᵢ)
  63.    ))
  64.  
  65. (define-unit cat->stlc@
  66.   (import cat^ contexts^ products^ (prefix exp: exponentials^))
  67.   (export stlc^)
  68.   ;; our Γ  A is represented as (ctx ->   A))
  69.   (define ((var name) ctx) (context-get ctx name))
  70.   (define ((apply e1 e2) ctx) (compose (pair (e1 ctx) (e2 ctx)) exp:eval))
  71.   (define ((lambda x e) ctx)
  72.     (define-values (e-ctx extend-morph) (context-extend ctx x))
  73.     ;; exp:lambda :  × A  B) ->   A => B)
  74.     (exp:lambda (compose (e e-ctx) extend-morph)))
  75.   (define ((project i n e) ctx)
  76.     (match n
  77.       [0 (compose into-terminal (e ctx))]
  78.       [1 (e ctx)]
  79.       [2 (compose (match i [0 pi1] [1 pi2]) (e ctx))]
  80.       [_ TODO]))
  81.   (define ((tuple . es) ctx)
  82.     (match es
  83.       [`() TODO]
  84.       [`(,e) TODO]
  85.       [`(,e1 ,e2) TODO]
  86.       [_ TODO])))
  87.  
  88. (define-compound-unit/infer racket-stlc@
  89.   (import)
  90.   (export stlc^ contexts^)
  91.   (link racket+contexts@ cat->stlc@))
  92.  
  93. (module+ test
  94.   (require rackunit)
  95.   (define-values/invoke-unit/infer racket-stlc@)
  96.   (check-eqv? 2 ((((lambda 'x (var 'x)) context-empty) 'ignored) 2))
  97.   )

=>