-
Notifications
You must be signed in to change notification settings - Fork 0
/
tba.rkt
84 lines (66 loc) · 1.6 KB
/
tba.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#lang racket
(require redex)
(require redex/tut-subst)
(define-language TBA
(t ::= v
true
false
(if t t t)
(succ t)
(pred t)
(zero? t))
(T ::= Nat Bool)
(p ::= t)
(b ::= true false)
(v ::= b n)
(n ::= number)
(err ::= mismatch underflow)
(c ::= p err)
(o ::= p err)
;; Evaluation Context
(E ::= hole (if E p p) (succ E) (pred E) (zero? E)))
;; Define the metafunction to lift Substitution into Redex
(define x? (redex-match TBA x)) ;; Verifies that the given 'x' belongs to the Grammar of PCF
(define-metafunction TBA
subst : x v e -> e
[(subst x v e)
,(subst/proc x? (list (term x)) (list (term v)) (term e))])
;; Type-Construction Rules
(define-judgment-form TBA
#:mode (⊢ I I O)
#:contract (⊢ e : T)
;;(intro-number)
[------------
(⊢ n : Nat)]
;;(succ-number)
[(⊢ n : Nat)
-------------------
(⊢ (succ n) : Nat)]
;;(pred-number)
[(⊢ n : Nat)
-------------------
(⊢ (pred n) : Nat)]
;;(intro-bool)
[-----------
(⊢ b : Bool)]
;;(if-then-else)
[(⊢ t : Bool)
(⊢ t_1 : T)
(⊢ t_2 : T)
-----------------------
(⊢ (if t t_1 t_2) : T)]
;;(zero?)
[(⊢ n : Nat)
-----------
(⊢ (zero? n) : Bool)])
;; Reduction Semantics
(define red
(reduction-relation
TBA
#:domain o
(--> (in-hole E (if true t1 t2)) (in-hole E t1) "ift")
(--> (in-hole E (if false t1 t2)) (in-hole E t2) "iff")
(--> (in-hole E (zero? n)) false (side-condition
(not (equal? (term n) 0)))
"zero?n")
(--> (in-hole E (zero? 0)) true "zero?0")))