-
Notifications
You must be signed in to change notification settings - Fork 0
/
split-xlist.rkt
138 lines (130 loc) · 5.51 KB
/
split-xlist.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
#lang typed/racket
(require (for-syntax phc-toolkit/untyped
syntax/parse
syntax/parse/experimental/template
racket/pretty
racket/list)
(submod "implementation.rkt" typed)
"caret-identifier.rkt"
"infinity-identifier.rkt"
"once-identifier.rkt"
type-expander)
(provide split-xlist f-split-list m-split-xlist*)
(: f-split-list (∀ (A B) (→ (→ Any Boolean : B)
(→ (Rec R (U (Pairof A R) B))
(List (Listof A)
B)))))
(define (f-split-list pred-b?)
(: recur (→ (Rec R (U (Pairof A R) B))
(List (Listof A)
B)))
(define (recur l)
(if (pred-b? l)
(list '() l)
(let ([split-rest (recur (cdr l))])
(cons (cons (car l)
(car split-rest))
(cdr split-rest)))))
recur)
(define-syntax-rule (m-split-list v (xlist τ₁ ^ *₁ . whole-τ-rest))
(((inst f-split-list τ₁ (xlist . whole-τ-rest))
;; TODO: could drop the tail type after the first mandatory repeat
;; Not sure if that would make it possible to typecheck more easily
;; though, as the rest of the type will be used to split the rest
;; anyway.
(make-predicate (xlist . whole-τ-rest)))
v))
(define-syntax (bounded-filter stx)
(syntax-case stx ()
[(_ 0 heads t l)
#'(values (list . heads) l)]
[(_ n (headᵢ …) t l)
#`(if ((make-predicate t) l)
(values (list headᵢ …) l)
(bounded-filter #,(sub1 (syntax-e #'n))
(headᵢ … (car l))
t
(cdr l)))]))
(define-syntax m-split-xlist*
(λ (stx)
((syntax-parser
#:literals (^ + - * once ∞)
[(_ v [v₁ vᵢ …] τ₁ ^ (once) {~seq τᵢ ^ *ᵢ} … #:rest r)
(template
(begin
(define v₁ (car v))
(m-split-xlist* (cdr v) [vᵢ …] (?@ τᵢ ^ *ᵢ) … #:rest r)))]
[(_ v [v₁ vᵢ …] τ₁ ^ (power:nat) {~seq τᵢ ^ *ᵢ} … #:rest r)
#:with (tmp-car …) (map (λ _ (gensym 'car)) (range (syntax-e #'power)))
(template
(begin
(define-values (v₁ remaining-v)
(let* ([remaining-v v]
(?@ [tmp-car (car remaining-v)]
[remaining-v (cdr remaining-v)])
…)
(values (list tmp-car …) remaining-v)))
(m-split-xlist* remaining-v [vᵢ …] (?@ τᵢ ^ *ᵢ) … #:rest r)))]
[(_ v [v₁ vᵢ …] τ₁ ^ (power:nat +) {~seq τᵢ ^ *ᵢ} … #:rest r)
#:with (tmp-car …) (map (λ _ (gensym 'car)) (range (syntax-e #'power)))
(template
(begin
(define-values (v₁ remaining-v)
(let* ([remaining-v v]
(?@ [tmp-car (car remaining-v)]
[remaining-v (cdr remaining-v)])
…)
(define remaining-split
(m-split-list remaining-v
(xlist τ₁ ^ *₁ (?@ τᵢ ^ *ᵢ) … #:rest r)))
(values (list* tmp-car … (car remaining-split))
(cdr remaining-split))))
(m-split-xlist* remaining-v
[vᵢ …] (?@ τᵢ ^ *ᵢ) … #:rest r)))]
[(_ v [v₁ vᵢ …] τ₁ ^ (from:nat - to:nat) {~seq τᵢ ^ *ᵢ} … #:rest r)
#:with (tmp-car …) (map (λ _ (gensym 'car)) (range (syntax-e #'from)))
#:with difference (- (syntax-e #'to) (syntax-e #'from))
(when (< (syntax-e #'difference) 0)
(raise-syntax-error 'xlist "invalid range: m is larger than n" #'-))
(template
(begin
(define-values (v₁ remaining-v)
(let* ([remaining-v v]
(?@ [tmp-car (car remaining-v)]
[remaining-v (cdr remaining-v)])
…)
(define-values (before remaining-after)
(bounded-filter difference
(tmp-car …)
(xlist (?@ τᵢ ^ *ᵢ) … #:rest r)
remaining-v))
(values before
remaining-after)))
(m-split-xlist* remaining-v
[vᵢ …] (?@ τᵢ ^ *ᵢ) … #:rest r)))]
[(_ v [v₁ vᵢ …] τ₁ ^ *₁ {~seq τᵢ ^ *ᵢ} … #:rest r)
(template
(begin
(define split
(m-split-list v (xlist τ₁ ^ *₁ (?@ τᵢ ^ *ᵢ) … #:rest r)))
(define v₁ (car split))
(m-split-xlist* (cadr split) [vᵢ …] (?@ τᵢ ^ *ᵢ) … #:rest r)))]
[(_ v [vr] #:rest r)
#'(define vr v)])
stx)))
(define-match-expander split-xlist
(syntax-parser
#:literals (^)
[(_ pat . whole-τ)
(define/with-parse ({~seq normalized-τᵢ ^ normalized-*ᵢ} … #:rest τ-rest)
(normalize-xlist-type #'whole-τ this-syntax))
(define-temp-ids "~a/v" (normalized-τᵢ …))
((λ (x) #;(pretty-write (syntax->datum x)) x)
(template
(app (λ (l)
(m-split-xlist* l
[normalized-τᵢ/v … rest/v]
(?@ normalized-τᵢ ^ normalized-*ᵢ) …
#:rest τ-rest)
(list normalized-τᵢ/v … rest/v))
pat)))]))