forked from lives-group/redexPEG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
judgments.rkt
504 lines (393 loc) · 10.5 KB
/
judgments.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
#lang racket
(require redex)
(require "./peg.rkt")
(provide (all-defined-out))
;; BIG STEP
; Syntax for parsing expression evaluation
(define-extended-language WFevalPeg Grammar
[E (e s)]
[R e ⊥]
[D S ⊥]
[S 0 1])
; Syntax for TypedPeg
;; Γ -> list of variables of type τ
;; G -> Peg Grammar
;; τ -> (b, H)
;; b -> T or F
;; H -> var
(define-extended-language TypedPeg Peg
[Γ ((x τ ) ...)]
[τ (b H)]
[b #t #f]
[H (x ...)]
)
; Helpers for TypedPeg
(define-metafunction TypedPeg
[(∨ #t #f) #t]
[(∨ #f #t) #t]
[(∨ #t #t) #t]
[(∨ #f #f) #f]
)
; Helpers functions for TypedPeg
(define-metafunction TypedPeg
[(∪ H_1 H_2 ) ,(set-union (term H_1) (term H_2))]
)
(define-metafunction TypedPeg
[(ΓLook ((x_1 τ_1) (x_2 τ_2) ...) x_1) τ_1]
[(ΓLook ((x_1 τ_1) (x_2 τ_2) ...) x_3) (ΓLook ((x_2 τ_2) ...) x_3)]
)
(define-metafunction TypedPeg
[(ins (b H) x) (b ,(set-union (list (term x)) (term H)))]
)
; Judgment to find the type of a peg
; Return (b H) -> (nullable or not (list of vars))
(define-judgment-form TypedPeg
#:mode (⊢ I I O)
#:contract (⊢ Γ e τ)
[----------------------------"empty"
(⊢ Γ ε (#t ()))]
[----------------------------"terminal"
(⊢ Γ number (#f ()))]
[----------------------------"var"
(⊢ Γ x (ins (ΓLook Γ x) x))]
[(⊢ Γ e (b H))
----------------------------"rep"
(⊢ Γ (* e) (#t H))]
[(⊢ Γ e (b H))
----------------------------"not"
(⊢ Γ (! e) (#t H))]
[(⊢ Γ e_1 (#t H_1))
(⊢ Γ e_2 (b H_2))
----------------------------"seq_1"
(⊢ Γ (• e_1 e_2) (b (∪ H_1 H_2)))]
[(⊢ Γ e_1 (#f H_1))
(⊢ Γ e_2 (b H_2))
----------------------------"seq_2"
(⊢ Γ (• e_1 e_2) (#f H_1))]
[(⊢ Γ e_1 (b_1 H_1))
(⊢ Γ e_2 (b_2 H_2))
----------------------------"alt"
(⊢ Γ (/ e_1 e_2) ((∨ b_1 b_2) (∪ H_1 H_2)))]
)
; Judgment to help verify the evaluation of a grammar
; Return true or false
(define-judgment-form WFevalPeg
#:mode (↛ I I O)
#:contract (↛ G D boolean)
[-------------------------------
(↛ G 0 #f)]
[-------------------------------
(↛ G 1 #t)]
[-------------------------------
(↛ G ⊥ #t)]
[(↛ G D_1 #f)
(↛ G D_2 #t)
-------------------------------
(↛ G (D_1 D_2) #f)]
[(↛ G D_1 #t)
(↛ G D_2 #f)
-------------------------------
(↛ G (D_1 D_2) #f)]
)
; Judgment to verify if the grammar consumes a entry
; Return:
; 0: succed while consuming no input
; 1: succed while consuming at least one terminal
; ⊥: fail on some input
(define-judgment-form WFevalPeg
#:mode (⇀ I I O)
#:contract (⇀ G e D)
;Empty
[-------------------------------
(⇀ G ε 0)]
;Terminal
[-------------------------------
(⇀ G natural 1)]
[-------------------------------
(⇀ G natural ⊥)]
;Non-Terminal
[(lookup G x e)
(⇀ G e D)
-------------------------------
(⇀ G x D)]
#;[(lookup G x ⊥)
-------------------------------
(⇀ G x ⊥)]
;Sequence
[(⇀ G e_1 0)
(⇀ G e_2 0)
-------------------------------
(⇀ G (• e_1 e_2) 0)]
[(⇀ G e_1 1)
(⇀ G e_2 S)
-------------------------------
(⇀ G (• e_1 e_2) 1)]
[(⇀ G e_1 0)
(⇀ G e_2 1)
-------------------------------
(⇀ G (• e_1 e_2) 1)]
[(⇀ G e_1 ⊥)
-------------------------------
(⇀ G (• e_1 e_2) ⊥)]
[(⇀ G e_1 S)
(⇀ G e_2 ⊥)
-------------------------------
(⇀ G (• e_1 e_2) ⊥)]
;Choice
[(⇀ G e_1 S)
-------------------------------
(⇀ G (/ e_1 e_2) S)]
[(⇀ G e_1 ⊥)
(⇀ G e_2 D)
-------------------------------
(⇀ G (/ e_1 e_2) D)]
;Repetition
[(⇀ G e 1)
-------------------------------
(⇀ G (* e) 1)]
[(⇀ G e 0)
-------------------------------
(⇀ G (* e) 0)]
;Not
[(⇀ G e S)
-------------------------------
(⇀ G (! e) ⊥)]
[(⇀ G e ⊥)
;(side-condition (not-zero? e))
-------------------------------
(⇀ G (! e) 0)]
[(⇀ G e ⊥)
-------------------------------
(⇀ G (! e) 1)]
)
; Judgment to verify if a peg and a grammar are well-formed
; Return true or false
(define-judgment-form WFevalPeg
#:mode (WF I I O)
#:contract (WF G e boolean)
;Empty
[-------------------------
(WF G ε #t)]
;Natural
[-------------------------
(WF G natural #t)]
;Non terminal
[(lookup G x e)
(WF G e #t)
-------------------------
(WF G x #t)]
;Sequence
[(WF G e_1 #t)
(⇀ G e_1 0)
(WF G e_2 #t)
-------------------------------
(WF G (• e_1 e_2) #t)]
[(WF G e_1 #t)
(⇀ G e_1 ⊥)
;(WF G e_2 #f)
-------------------------------
(WF G (• e_1 e_2) #t)]
[(WF G e_1 #t)
(⇀ G e_1 1)
-------------------------------
(WF G (• e_1 e_2) #t)]
;Choice
[(WF G e_1 #t)
(WF G e_2 #t)
-------------------------------
(WF G (/ e_1 e_2) #t)]
;Repetition
#;[(⇀ G e 1)
(WF G e #t)
-------------------------------
(WF G (* e) #t)]
#;[(⇀ G e 0)
-------------------------------
(WF G (* e) #f)]
[(⇀ G e D)
(↛ G D boolean)
-------------------------------
(WF G (* e) boolean)]
#;[(⇀ G e D)
(↛ G D #f)
-------------------------------
(WF G (* e) #f)]
#;[(⇀ G e ⊥)
(WF G e #t)
-------------------------------
(WF G (* e) #t)]
;Not
[(WF G e #t)
-------------------------------
(WF G (! e) #t)]
)
; Judgment to look up for the value of some grammar
; Return the value (peg or fail)
(define-judgment-form WFevalPeg
#:mode (lookup I I O)
#:contract (lookup G x R)
[--------------------------------
(lookup (x_1 e G) x_1 e)]
[--------------------------------
(lookup ∅ x ⊥)]
[(lookup G x_2 R)
(side-condition (diffs? x_1 x_2))
--------------------------------
(lookup (x_1 e_1 G) x_2 R)]
)
; Judgment to evaluate if a peg consumes a entry
; Return what left of the entry
(define-judgment-form simpleEvalPeg
#:mode (evalWF I I O)
#:contract (evalWF G E s)
;Terminal
[--------------------------------
(evalWF G (natural_1 (natural_1 natural ...)) (natural ...))]
[(side-condition (diff? natural_1 natural_2))
--------------------------------
(evalWF G (natural_1 (natural_2 natural ...)) ⊥)]
[--------------------------------
(evalWF G (natural_1 ()) ⊥)]
;Empty
[--------------------------------
(evalWF G (ε s) s)]
;Choice
[(evalWF G (e_1 s) s_1)
(side-condition (botton? s_1))
--------------------------------
(evalWF G ((/ e_1 e_2) s) s_1)]
[(evalWF G (e_2 s) s_1)
(evalWF G (e_1 s) ⊥)
-------------------------------
(evalWF G ((/ e_1 e_2) s) s_1)]
#;[------------------------------
(evalWF G ((/ e_1 e_2) ()) ⊥)]
;Sequence
[(evalWF G (e_1 s) s_1)
(evalWF G (e_2 s_1) s_2)
-------------------------------
(evalWF G ((• e_1 e_2) s) s_2)]
[(evalWF G (e_1 s) ⊥)
------------------------------
(evalWF G ((• e_1 e_2) s) ⊥)]
;Not
[(evalWF G (e s) s_1)
(side-condition (botton? s_1))
-------------------------------
(evalWF G ((! e) s) ⊥)]
[(evalWF G (e s) ⊥)
-------------------------------
(evalWF G ((! e) s) s)]
;Repetition
[(evalWF G (e s) ⊥)
-------------------------------
(evalWF G ((* e) s) s)]
[(evalWF G (e s) s_1)
(side-condition (botton? s_1))
(evalWF G ((* e) s_1) s_2)
-------------------------------
(evalWF G ((* e) s) s_2)]
;Non-Terminal
[(lookup G x e)
(evalWF G (e s) s_1)
--------------------------------
(evalWF G (x s) s_1)]
[(lookup G x ⊥)
--------------------------------
(evalWF G (x s) ⊥)]
)
;Helper function of the grammar WFevalPeg
#;(define-metafunction WFevalPeg
[(is-WF x) ])
(define-metafunction WFevalPeg
[(equals? x x) #t]
[(equals? x e) #f])
#;(define-metafunction WFevalPeg
[(diff? natural_1 natural_1) #f]
[(diff? natural_1 natural_2) #t])
(define-metafunction WFevalPeg
[(diffs? x_1 x_1) #f]
[(diffs? x_1 x_2) #t])
(define-metafunction WFevalPeg
[(empty? ()) #f]
[(empty? s) #t])
;Helper function of the grammar simpleEvalPeg
#;(define-metafunction simpleEvalPeg
[(botton? ⊥) #f]
[(botton? s_1) #t])
(define-metafunction simpleEvalPeg
[(not-botton? ⊥) #t]
[(not-botton? s_1) #f])
; Tests for TypedPeg judgment
;(judgment-holds (⊢ () ε τ) τ)
;(judgment-holds (⊢ () (! (/ 1 2)) τ) τ)
;(judgment-holds (⊢ ((A (#f ()))) A τ) τ)
;(judgment-holds (⊢ ((A (#f ())) (B (#t (A)))) B τ) τ)
; Judgment for a simple peg evaluation
(define-judgment-form simpleEvalPeg
#:mode (eval I I O)
#:contract (eval G E s)
;Terminal
[--------------------------------
(eval G (natural_1 (natural_1 natural ...)) (natural ...))]
[(side-condition (diff? natural_1 natural_2))
--------------------------------
(eval G (natural_1 (natural_2 natural ...)) ⊥)]
[--------------------------------
(eval G (natural_1 ()) ⊥)]
;Choice
[(eval G (e_1 s) s_1)
(side-condition (botton? s_1))
--------------------------------
(eval G ((/ e_1 e_2) s) s_1)]
[(eval G (e_2 s) s_1)
(side-condition (botton? s_1))
-------------------------------
(eval G ((/ e_1 e_2) s) s_1)]
[------------------------------
(eval G ((/ e_1 e_2) ()) ⊥)]
;Sequence
[(eval G (e_1 s) s_1)
(eval G (e_2 s_1) s_2)
-------------------------------
(eval G ((• e_1 e_2) s) s_2)]
[(eval G (e_1 s) ⊥)
------------------------------
(eval G ((• e_1 e_2) s) ⊥)]
;Not
[(eval G (e s) s_1)
(side-condition (botton? s_1))
-------------------------------
(eval G ((! e) s) ⊥)]
[(eval G (e s) ⊥)
-------------------------------
(eval G ((! e) s) s)]
;Repetition
[(eval G (e s) ⊥)
-------------------------------
(eval G ((* e) s) s)]
[(eval G (e s) s_1)
(side-condition (botton? s_1))
(eval G ((* e) s_1) s_2)
-------------------------------
(eval G ((* e) s) s_2)]
;Empty
[-------------------------------
(eval G (ε s) s)]
;Non-Terminal
[(lookup G x e)
(eval G (e s) s_1)
--------------------------------
(eval G (x s) s_1)]
[(lookup G x ⊥)
--------------------------------
(eval G (x s) ⊥)]
)
; Checks if natural_1 and natural_2 are different
(define-metafunction simpleEvalPeg
[(diff? natural_1 natural_1) #f]
[(diff? natural_1 natural_2) #t])
; Checks if is botton
(define-metafunction simpleEvalPeg
[(botton? ⊥) #f]
[(botton? s_1) #t])