-
Notifications
You must be signed in to change notification settings - Fork 0
/
WriteR1CS.idr
506 lines (464 loc) · 28.4 KB
/
WriteR1CS.idr
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
502
503
504
505
506
module WriteR1CS
import Data.List
import Examples.SExpr
import Examples.EnvironmentVF
%access export
%default partial
%access public export
data Expression : (expression : SExpr) -> Type where
IsDigit : Expression (MkDigit digit)
IsPlus : Expression (MkArithGate left right PLUS)
IsSTREQ : Expression (MkSTREQ left right)
IsConcat : Expression (MkConcat left right)
IsAppend : Expression (MkAppend item coll)
data VarOrDigit : (expression : SExpr) -> Type where
IsADigit : VarOrDigit (MkDigit d)
IsAVar : VarOrDigit (MkVar name)
data R1CSOperator = PLUS | MINUS | DIGIT | ASGN | MUL
data DigitVarOrNil : Type where
DigitLeaf : Integer -> DigitVarOrNil
VarLeaf : Nat -> DigitVarOrNil
NilLeaf : DigitVarOrNil
Show DigitVarOrNil where
show (DigitLeaf x) = show x
show (VarLeaf k) = "var" ++ show k
show NilLeaf = "nil"
-- a = b + c is a type of R1CS assignment.
-- NoOp is just a NoOp - say you just drop a var or something
data R1CSStatement : Type where
Operation : Nat -> R1CSOperator -> DigitVarOrNil -> DigitVarOrNil -> R1CSStatement
data Variable : SExpr -> Type where
IsVariable : (name : String) -> (Variable (MkVar name))
Show R1CSStatement where
show (Operation varCnt PLUS stuffLeft stuffRight) = ("var" ++ (show (S varCnt))) ++ " <- " ++ "PLUS " ++ (show stuffLeft) ++ " " ++ (show stuffRight)
show (Operation varCnt DIGIT stuffLeft stuffRight) = ("var" ++ (show (S varCnt))) ++ " <- " ++ "DIGIT " ++ (show stuffLeft) ++ " " ++ (show stuffRight)
data VariableLookup : SExpr -> List (String, Nat) -> Nat -> Type where
VariableHere : (varName : String) ->
(varNo : Nat) ->
(remaining : List (String, Nat)) ->
VariableLookup (MkVar varName) ((varName, varNo) :: remaining) varNo
VariableLater : (varName : String) ->
(curName : String) ->
(Not (curName = varName)) ->
(varNo : Nat) ->
(remaining : List (String, Nat)) ->
(remainingPrf : VariableLookup (MkVar varName) remaining theVarNo) ->
VariableLookup (MkVar varName) ((curName, varNo) :: remaining) theVarNo
data Zipped : List a -> List b -> List (a, b) -> Type where
ZipEnd : Zipped [] [] []
Zip : (x:a) -> (y:b) -> (remaining:Zipped xs ys zs) -> Zipped (x::xs) (y::ys) ((x,y)::zs)
unequalLists : (x : b) -> (xs : List b) -> (zs : List (a, b) ** Zipped [] (x :: xs) zs) -> Void
unequalLists x xs (zs ** ZipEnd) impossible
unequalLists x xs (zs ** Zip _ _ _) impossible
unequalLists' : (x : a) -> (xs : List a) -> (zs : List (a, b) ** Zipped (x :: xs) [] zs) -> Void
unequalLists' x xs (zs ** ZipEnd) impossible
unequalLists' x xs (zs ** Zip _ _ _) impossible
restNotZippable : (x : a) -> (xs : List a) -> (y : b) -> (ys : List b) -> (zipContra : (zs : List (a, b) ** Zipped xs ys zs) -> Void) -> (zs : List (a, b) ** Zipped (x :: xs) (y :: ys) zs) -> Void
restNotZippable x xs y ys zipContra ([] ** ZipEnd) impossible
restNotZippable x xs y ys zipContra ([] ** Zip _ _ _) impossible
restNotZippable x xs y ys zipContra ((t :: zs) ** zipElem) =
case (zipElem) of
(Zip x y remaining) => zipContra (zs ** remaining)
getZip : (xs :List a) -> (ys:List b) -> Dec (zs:(List (a,b)) ** Zipped xs ys zs)
getZip [] [] = Yes ([] ** ZipEnd)
getZip [] (x :: xs) = No (unequalLists x xs)
getZip (x :: xs) [] = No (unequalLists' x xs)
getZip (x :: xs) (y :: ys) with (getZip xs ys)
| (Yes (zs ** zipped)) = Yes (((x,y)::zs) ** (Zip x y zipped))
| (No zipContra) = No (restNotZippable x xs y ys zipContra)
isZipped_rhs_1 : Zipped [] [] (x :: xs) -> Void
isZipped_rhs_1 ZipEnd impossible
isZipped_rhs_1 (Zip _ _ _) impossible
isZipped_rhs_2 : Zipped [] (x :: xs) zs -> Void
isZipped_rhs_2 ZipEnd impossible
isZipped_rhs_2 (Zip _ _ _) impossible
isZipped_rhs_3 : Zipped (x :: xs) [] zs -> Void
isZipped_rhs_3 ZipEnd impossible
isZipped_rhs_3 (Zip _ _ _) impossible
isZipped_rhs_4 : Zipped (x :: xs) (y :: ys) [] -> Void
isZipped_rhs_4 ZipEnd impossible
isZipped_rhs_4 (Zip _ _ _) impossible
isZipped : (DecEq a, DecEq b) => (xs :List a) -> (ys:List b) -> (zs:List (a,b)) -> Dec (Zipped xs ys zs)
isZipped [] [] [] = Yes ZipEnd
isZipped [] [] (x :: xs) = No isZipped_rhs_1
isZipped [] (x :: xs) zs = No isZipped_rhs_2
isZipped (x :: xs) [] zs = No isZipped_rhs_3
isZipped (x :: xs) (y :: ys) [] = No isZipped_rhs_4
isZipped (x :: xs) (y :: ys) (z :: zs) with (isZipped xs ys zs)
| (Yes remaining) with(decEq z (x,y))
| (Yes eq) = Yes (rewrite eq in Zip x y remaining)
| (No zNOTxy) = No (\(Zip x y rem) => zNOTxy Refl)
| (No remainingContra) = No (\(Zip x y remaining) => remainingContra remaining)
mutual
data OperationSeq : (expression : SExpr) ->
(startVarNo : Nat) ->
(varBindings : List (String, Nat)) ->
(sexprList : SExprList) ->
(List R1CSStatement) ->
(nextVarNo : Nat) ->
(depth : Nat) -> Type where
-- Assign to nextVar the specified Integer, and then continue
WriteDigit : (startVarNo : Nat) ->
(digit : Integer) ->
(existingVarBindings : List (String, Nat)) ->
(sexprList : SExprList) ->
OperationSeq (MkDigit digit)
startVarNo
existingVarBindings
sexprList
((Operation startVarNo
DIGIT
(DigitLeaf digit)
NilLeaf) :: [])
(S startVarNo)
k -- no expansion so no change in depth
-- Execute left, Execute right, then collect their results in 2 vars
-- Finally do a PLUS of these 2 vars and then return the next available counter
WritePlus : (startVarNo : Nat) ->
(existingVarBindings : List (String, Nat)) ->
(sexprList : SExprList) ->
(leftExpr : SExpr) ->
(rightExpr : SExpr) -> {--)=>--}
(leftExprOperationSeq : OperationSeq leftExpr
startVarNo
existingVarBindings
sexprList
leftExprOperations
(S leftResultVarNo)
k) ->
(rightExprOperationSeq : OperationSeq rightExpr
(S leftResultVarNo)
existingVarBindings
sexprList
rightExprOperations
(S rightResultVarNo)
k) ->
OperationSeq (MkArithGate leftExpr rightExpr PLUS)
startVarNo
existingVarBindings
sexprList
(leftExprOperations ++ rightExprOperations ++ ((Operation (S rightResultVarNo)
PLUS
(VarLeaf leftResultVarNo)
(VarLeaf rightResultVarNo))
:: []))
(S (S rightResultVarNo))
k
-- Execute left, Execute right, then collect their results in 2 vars
-- Finally do a PLUS of these 2 vars and then return the next available counter
WriteMinus : (startVarNo : Nat) ->
(existingVarBindings : List (String, Nat)) ->
(sexprList : SExprList) ->
(leftExpr : SExpr) ->
(rightExpr : SExpr) -> {--)=>--}
(leftExprOperationSeq : OperationSeq leftExpr
startVarNo
existingVarBindings
sexprList
leftExprOperations
(S leftResultVarNo)
k) ->
(rightExprOperationSeq : OperationSeq rightExpr
(S leftResultVarNo)
existingVarBindings
sexprList
rightExprOperations
(S rightResultVarNo)
k) ->
OperationSeq (MkArithGate leftExpr rightExpr MINUS)
startVarNo
existingVarBindings
sexprList
(leftExprOperations ++ rightExprOperations ++ ((Operation (S rightResultVarNo)
MINUS
(VarLeaf leftResultVarNo)
(VarLeaf rightResultVarNo))
:: []))
(S (S rightResultVarNo))
k
-- Execute left, Execute right, then collect their results in 2 vars
-- Finally do a PLUS of these 2 vars and then return the next available counter
WriteMul : (startVarNo : Nat) ->
(existingVarBindings : List (String, Nat)) ->
(sexprList : SExprList) ->
(leftExpr : SExpr) ->
(rightExpr : SExpr) -> {--)=>--}
(leftExprOperationSeq : OperationSeq leftExpr
startVarNo
existingVarBindings
sexprList
leftExprOperations
(S leftResultVarNo)
k) ->
(rightExprOperationSeq : OperationSeq rightExpr
(S leftResultVarNo)
existingVarBindings
sexprList
rightExprOperations
(S rightResultVarNo)
k) ->
OperationSeq (MkArithGate leftExpr rightExpr MUL)
startVarNo
existingVarBindings
sexprList
(leftExprOperations ++ rightExprOperations ++ ((Operation (S rightResultVarNo)
MUL
(VarLeaf leftResultVarNo)
(VarLeaf rightResultVarNo))
:: []))
(S (S rightResultVarNo))
k
-- let consists of a binding var + associated SExpr. and then a body SExpr
-- first execute the binding SExpr, store the result in a Var, insert this result Var
-- into the environment and then execute the body
WriteLet : (startVarNo : Nat) ->
(bindingVarName : String) ->
(bindingExpr : SExpr) ->
(letBody : SExpr) ->
(sexprList : SExprList) -> {--)=>--}
(bindingExprWritten : OperationSeq bindingExpr
startVarNo
existingVarBindings
sexprList
bindingExprOperations
(S bindingExprResultVarNo)
k) ->
(letBodyWritten : OperationSeq letBody
(S bindingExprResultVarNo)
((bindingVarName, bindingExprResultVarNo) :: existingVarBindings)
sexprList
letBodyOperations
(S letBodyResultVarNo)
k) ->
(OperationSeq (MkLet bindingVarName bindingExpr letBody)
startVarNo
existingVarBindings
sexprList
(bindingExprOperations ++ letBodyOperations)
(S letBodyResultVarNo)
k)
-- run is just an invocation so hand off to that guy
WriteRun : (startVarNo : Nat) ->
(runInvocation : SExpr) ->
(sexprList : SExprList) -> {--)=>--}
(invocationWritten : OperationSeq runInvocation
startVarNo
existingVarBindings
sexprList
invocationOpSeq
(S invocationResultVarNo)
k) ->
OperationSeq (MkRun runInvocation)
startVarNo
existingVarBindings
sexprList
invocationOpSeq
(S invocationResultVarNo)
k
WriteVar : (startVarNo : Nat) ->
(varName : String) ->
(sexprList : SExprList) ->
(exprBindings : List (String, Nat)) ->
(varInBindings : VarInBindings varName bindingVarNo exprBindings) ->
(OperationSeq (MkVar varName)
startVarNo
exprBindings
sexprList
((Operation startVarNo ASGN (VarLeaf bindingVarNo) NilLeaf) :: [])
(S startVarNo)
k)
WriteInvocation : (startVarNo : Nat) ->
(existingVarBindings : List (String, Nat)) ->
(sexprList : SExprList) ->
(fnName : String) ->
(invocationArgs : List SExpr) ->
(defArgs : List String) ->
(defBody : SExpr) -> {--)=>--}
(invDefPrf : (ExtractDefinitionFromInvocation (IsInvokationOfDefinition fnName invocationArgs defArgs defBody)
sexprList)) ->
(invocationArgsWritten : OperationSeqFormList (MkFormList invocationArgs)
startVarNo
existingVarBindings
sexprList
invocationArgsWrittenOperations
(S invocationArgsFinalResultVarNo)
constructedArgBindings
k) ->
(invocationBodyWritten : OperationSeq defBody
(S invocationArgsFinalResultVarNo)
zippedList
sexprList
invocationBodyWrittenOperations
(S invocationBodyResultVarNo)
k) ->
(isZippedList : Zipped defArgs constructedArgBindings zippedList ) ->
OperationSeq (MkInvocation fnName (MkFormList invocationArgs))
startVarNo
existingVarBindings
sexprList
(invocationArgsWrittenOperations ++ invocationBodyWrittenOperations)
(S invocationBodyResultVarNo)
(S k)
WriteEq : (startVarNo : Nat) ->
(existingVarBindings : List (String, Nat)) ->
(sexprList : SExprList) ->
(leftExpr : SExpr) -> {--)=>--}
(rightExpr : SExpr) -> {--)=>--}
(leftWritten : (OperationSeq leftExpr
startVarNo
existingVarBindings
sexprList
leftWrittenOperations
(S leftResultVarNo)
k)) -> {--)=>--}
(rightWritten : (OperationSeq rightExpr
(S leftResultVarNo)
existingVarBindings
sexprList
rightWrittenOperations
(S rightResultVarNo)
k)) -> {--)=>--}
(OperationSeq (MkArithGate leftExpr
rightExpr
EQ)
startVarNo
existingVarBindings
sexprList
(leftWrittenOperations ++ rightWrittenOperations ++
[(Operation (S rightResultVarNo) -- this is x - y
MINUS
(VarLeaf leftResultVarNo)
(VarLeaf rightResultVarNo)),
(Operation (S (S (S rightResultVarNo))) -- this is cond
MUL
(VarLeaf (S (S rightResultVarNo))) -- this is tmp
(VarLeaf (S rightResultVarNo)) -- this is x-y
),
(Operation (S rightResultVarNo)
MUL
(VarLeaf (S (S (S rightResultVarNo)))) -- cond
(VarLeaf (S rightResultVarNo)) -- this is x-y
)])
(S (S (S (S rightResultVarNo))))
k)
WriteCond : (startVarNo : Nat) ->
(existingVarBindings : List (String, Nat)) ->
(sexprList : SExprList) ->
(ifExpr : SExpr) ->
(thenExpr : SExpr) ->
(elseExpr : SExpr) -> {--)=>--}
(ifExprWritten : (OperationSeq ifExpr
startVarNo
existingVarBindings
sexprList
ifExprWrittenOps
(S ifWrittenResultVarNo)
k)) -> {--)=>--}
(thenExprWritten : (OperationSeq thenExpr
(S ifWrittenResultVarNo)
existingVarBindings
sexprList
thenExprWrittenOps
(S thenExprWrittenVarNo)
k)) -> {--)=>--}
(elseExprWritten : (OperationSeq elseExpr
(S thenExprWrittenVarNo)
existingVarBindings
sexprList
elseExprWrittenOps
(S elseExprWrittenVarNo)
k)) -> {--)=>--}
(OperationSeq (MkCond ifExpr thenExpr elseExpr)
startVarNo
existingVarBindings
sexprList
(ifExprWrittenOps ++
thenExprWrittenOps ++
elseExprWrittenOps ++
[
(Operation (S elseExprWrittenVarNo)
MUL
(VarLeaf ifWrittenResultVarNo)
(VarLeaf elseExprWrittenVarNo)),
(Operation (S (S elseExprWrittenVarNo))
MINUS
(DigitLeaf 1)
(VarLeaf (ifWrittenResultVarNo))),
(Operation (S (S (S elseExprWrittenVarNo)))
MUL
(VarLeaf (S (S elseExprWrittenVarNo)))
(VarLeaf thenExprWrittenVarNo))
])
(S (S (S (S elseExprWrittenVarNo))))
k)
data OperationSeqFormList : (formlist : FormList) ->
(startVarNo : Nat) ->
(existingVarBindings : List (String, Nat)) ->
(sexprList : SExprList) ->
(operations : List R1CSStatement) ->
(endVarNo : Nat) ->
(producedBindings : List Nat) ->
(depth : Nat) -> Type where
WriteFormList : (startVarNo : Nat) ->
(existingVarBindings : List (String, Nat)) ->
(sexprList : SExprList) ->
(curArg : SExpr) ->
(remainingArgs : List SExpr) -> {--)=>--}
(curArgWritten : OperationSeq curArg
startVarNo
existingVarBindings
sexprList
curArgWrittenOperations
(S curArgResultNo)
k) ->
(remainingWritten : OperationSeqFormList
(MkFormList remainingArgs)
(S curArgResultNo)
existingVarBindings
sexprList
remainingWrittenOperations
(S remainingWrittenResultVarNo)
remainingBindings
k) ->
(operations = curArgWrittenOperations ++ remainingWrittenOperations)->
(OperationSeqFormList
(MkFormList (curArg :: remainingArgs))
startVarNo
existingVarBindings
sexprList
operations
(S remainingWrittenResultVarNo)
([curArgResultNo] ++ remainingBindings)
k)
WriteFormListEmpty : (startVarNo : Nat) ->
(existingVarBindings : List (String, Nat)) ->
(sexprList : SExprList) ->
(OperationSeqFormList
(MkFormList [])
startVarNo
existingVarBindings
sexprList
[]
startVarNo
[]
k)
{--)=>--}
data SExprToOperatorReturnType : (curVar : Nat) -> (sexpr: SExpr) -> (sexprList : SExprList) -> (exprBindings : List (String, Nat)) -> (depth:Nat) -> Type where
RetVal : (curVar : Nat) ->
(sexpr: SExpr) ->
(sexprList : SExprList) ->
(exprBindings : List (String, Nat)) ->
(nextVarNo : Nat) ->
(prfLTE1:LT Z nextVarNo) ->
(operationSeq : List R1CSStatement) ->
(depth:Nat) ->
(opSeqType : OperationSeq sexpr
curVar
exprBindings
sexprList
operationSeq
nextVarNo
depth) ->
SExprToOperatorReturnType curVar sexpr sexprList exprBindings depth
--
--)=>