/
access.llrl
146 lines (126 loc) · 4.28 KB
/
access.llrl
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
(no-implicit-std)
(import "std/prelude/stage-0" _)
(import "std/assert" assert)
(import "std/num-literals" U64)
(import "std/s" s/unsnoc)
(export get unsafe-get get? set! unsafe-set!)
(export Load load)
(export Store store!)
(export LoadAt load-at load-at? unsafe-load-at load-bound)
(export StoreAt store-at! unsafe-store-at!)
(macro (get s)
(s/match s
[(_ ,t)
(ok `(,\load ,t))]
[(_ ,t ,@accessors)
(expand-get-accessors t accessors)]
[_
(err "Expected (get loadable accessor ...)")]))
(function (expand-get-accessors t accessors)
(s/match accessors
[()
(ok t)]
[([,index] ,@accessors)
(expand-get-accessors `(,\load-at ,index ,t) accessors)]
[_
(err "Expected get accessor: [index]")]))
(macro (unsafe-get s)
(s/match s
[(_ ,t)
(ok `(,\load ,t))]
[(_ ,t ,@accessors)
(expand-unsafe-get-accessors t accessors)]
[_
(err "Expected (unsafe-get loadable accessor ...)")]))
(function (expand-unsafe-get-accessors t accessors)
(s/match accessors
[()
(ok t)]
[([,index] ,@accessors)
(expand-unsafe-get-accessors `(,\unsafe-load-at ,index ,t) accessors)]
[_
(err "Expected unsafe-get accessor: [index]")]))
(macro (get? s)
(s/match s
[(_ ,t)
(ok `(,\some (,\load ,t)))]
[(_ ,t ,@accessors)
(expand-get?-accessors t accessors)]
[_
(err "Expected (get? loadable accessor ...)")]))
(function (expand-get?-accessors t accessors)
(s/match accessors
[()
(ok `(,\some ,t))]
[([,index] ,@accessors)
@let ([next-t (gensym)])
(ok
`(match (,\load-at? ,index ,t)
[(,\some (let ,next-t))
,(expand-get?-accessors next-t accessors)!]
[_
,\none]))]
[_
(err "Expected get accessor: [index]")]))
(macro (set! s)
(s/match s
[(_ ,t ,@accessors-value)
(match (s/unsnoc accessors-value)!
[(some (: (let accessors) (let value)))
(match (s/unsnoc accessors)!
[(some (: (let accessors) (let accessor)))
(expand-set-accessor (expand-get-accessors t accessors)! value accessor)]
[none
(ok `(,\store! ,value ,t))])]
[none
(err "Expected (set! storable accessor ... value)")])]
[_
(err "Expected (set! storable accessor ... value)")]))
(function (expand-set-accessor t value accessor)
(s/match accessor
[[,index]
(ok `(,\store-at! ,index ,value ,t))]
[_
(err "Expected set! accessor: [index]")]))
(macro (unsafe-set! s)
(s/match s
[(_ ,t ,@accessors-value)
(match (s/unsnoc accessors-value)!
[(some (: (let accessors) (let value)))
(match (s/unsnoc accessors)!
[(some (: (let accessors) (let accessor)))
(expand-unsafe-set-accessor (expand-unsafe-get-accessors t accessors)! value accessor)]
[none
(ok `(,\store! ,value ,t))])]
[none
(err "Expected (unsafe-set! storable accessor ... value)")])]
[_
(err "Expected (unsafe-set! storable accessor ... value)")]))
(function (expand-unsafe-set-accessor t value accessor)
(s/match accessor
[[,index]
(ok `(,\unsafe-store-at! ,index ,value ,t))]
[_
(err "Expected unsafe-set! accessor: [index]")]))
(class (Load T)
(function (load t) {(forall A) (-> (T A) A)}))
(class (Store T) (where (Load T))
(function (store! a t) {(forall A) (-> A (T A) unit)}))
(class (LoadAt T)
(function (load-at i t) {(forall A) (-> U64 (T A) A)}
(assert (<bound/lt?> i (load-bound t)) "Out of range")
(unsafe-load-at i t))
(function (load-at? i t) {(forall A) (-> U64 (T A) (Option A))}
(if (<bound/lt?> i (load-bound t))
(some (unsafe-load-at i t))
none))
; Caller must ensure that the specified index is in [0,bound)
(function (unsafe-load-at i t) {(forall A) (-> U64 (T A) A)})
(function (load-bound t) {(forall A) (-> (T A) U64)}))
(class (StoreAt T) (where (LoadAt T))
(function (store-at! i a t) {(forall A) (-> U64 A (T A) unit)}
(assert (<bound/lt?> i (load-bound t)) "Out of range")
(unsafe-store-at! i a t))
; Caller must ensure that the specified index is in [0,bound)
(function (unsafe-store-at! i a t) {(forall A) (-> U64 A (T A) unit)}))
(builtin-op <bound/lt?> {(-> U64 U64 Bool)} "unsigned.lt")