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
/// Tools to /compose/ different circuit designers.
///
/// Assume we have several sets of columns:
/// Col0 ⊂ Col1 ⊂ Col2, and
///       Col1' ⊂ Col2
///
///
/// For example they are laid out like this:
///
/// |-------------------------------------------| Col2
/// |-|  |----------------------|                 Col1
///        |-----|     |--| |---|                 Col0
/// |---|        |-----|        |---------------| Col1'
///
/// Some columns might be even shared (e.g. Col1 and Col1' share column#0).
///
/// Using capabilities one can define functions that operate over different sets of columns,
/// and does not "know" in which bigger context it operates.
/// - function0<Env: ColumnAccess<Col0>>(env: Env, ...)
/// - function1<Env: ColumnAccess<Col1>>(env: Env, ...)
/// - function1'<Env: ColumnAccess<Col1'>>(env: Env, ...)
/// - function2<Env: ColumnAccess<Col2>>(env: Env, ...)
///
/// This is similar to memory separation: a program function0 might
/// need just three columns for A * B - C constraint, and if it works
/// in a 1000 column environment it needs to be told /which three
/// exactly/ will it see.
///
/// One only needs a single concrete Env (e.g. WitnessBuilder or
/// Constraint Builder) over the "top level" Col2, and then all these
/// functions can be called using lenses. Each lens describes how the
/// layouts will be mapped.
///
/// |-------------------------------------------|                        Col2
///            |                  |      |
///            | Col2To1Lens      |      |
///            V                  |      |
/// |-|  |----------------------| | (compose(Col2To1Lens . Col1To0Lens)  Col1
///            |                  |      |
///            | Col1To0Lens     /       |
///            |                /        |
///            V               V         | Col2To1'Lens
///        |-----|     |--| |---|        |                               Col0
///                                      V
/// |---|        |-----|        |---------------|                        Col1'
///
///
/// Similar "mapping" intuition applies to lookup tables.
use crate::{
    circuit_design::capabilities::{
        ColAccessCap, ColWriteCap, DirectWitnessCap, HybridCopyCap, LookupCap, MultiRowReadCap,
    },
    columns::ColumnIndexer,
    logup::LookupTableID,
};
use ark_ff::PrimeField;
use std::marker::PhantomData;

/// `MPrism` allows one to Something like a Prism, but for Maybe and not just any Applicative.
///
/// See
/// - <https://hackage.haskell.org/package/lens-4.17.1/docs/Control-Lens-Prism.html>
/// - <https://hackage.haskell.org/package/lens-tutorial-1.0.4/docs/Control-Lens-Tutorial.html>
pub trait MPrism {
    /// The lens source type, i.e., the object containing the field.
    type Source;

    /// The lens target type, i.e., the field to be accessed or modified.
    type Target;

    fn traverse(&self, source: Self::Source) -> Option<Self::Target>;

    fn re_get(&self, target: Self::Target) -> Self::Source;
}

/// Identity `MPrism` from any type `T` to itself.
///
/// Can be used in many situations. E.g. when `foo1` and `foo2` both
/// call `bar` that is parameterised by a lens, and `foo1` has
/// identical context to `bar` (so requires the ID lens), but `foo2`
/// needs an actual non-ID lens.
#[derive(Clone, Copy, Debug)]
pub struct IdMPrism<T>(pub PhantomData<T>);

impl<T> Default for IdMPrism<T> {
    fn default() -> Self {
        IdMPrism(PhantomData)
    }
}

impl<T> MPrism for IdMPrism<T> {
    type Source = T;
    type Target = T;

    fn traverse(&self, source: Self::Source) -> Option<Self::Target> {
        Some(source)
    }

    fn re_get(&self, target: Self::Target) -> Self::Source {
        target
    }
}

pub struct ComposedMPrism<LHS, RHS> {
    /// The left-hand side of the composition.
    lhs: LHS,

    /// The right-hand side of the composition.
    rhs: RHS,
}

impl<LHS, RHS> ComposedMPrism<LHS, RHS>
where
    LHS: MPrism,
    LHS::Target: 'static,
    RHS: MPrism<Source = LHS::Target>,
{
    pub fn compose(lhs: LHS, rhs: RHS) -> ComposedMPrism<LHS, RHS> {
        ComposedMPrism { lhs, rhs }
    }
}

impl<LHS, RHS> MPrism for ComposedMPrism<LHS, RHS>
where
    LHS: MPrism,
    LHS::Target: 'static,
    RHS: MPrism<Source = LHS::Target>,
{
    type Source = LHS::Source;
    type Target = RHS::Target;

    fn traverse(&self, source: Self::Source) -> Option<Self::Target> {
        let r1: Option<_> = self.lhs.traverse(source);
        let r2: Option<_> = r1.and_then(|x| self.rhs.traverse(x));
        r2
    }

    fn re_get(&self, target: Self::Target) -> Self::Source {
        self.lhs.re_get(self.rhs.re_get(target))
    }
}

////////////////////////////////////////////////////////////////////////////
// Interpreter and sub-interpreter
////////////////////////////////////////////////////////////////////////////

/// Generic sub-environment struct: don't use directly. It's an
/// internal object to avoid copy-paste.
///
/// We can't use `SubEnv` directly because rust is not idris: it is
/// impossible to instantiate `SubEnv` with two /completely/ different
/// lenses and then write proper trait implementations. Rust complains
/// about conflicting trait implementations.
struct SubEnv<'a, F: PrimeField, CIx1: ColumnIndexer, Env1: ColAccessCap<F, CIx1>, L> {
    env: &'a mut Env1,
    lens: L,
    phantom: PhantomData<(F, CIx1)>,
}

/// Sub environment with a lens that is mapping columns.
pub struct SubEnvColumn<'a, F: PrimeField, CIx1: ColumnIndexer, Env1: ColAccessCap<F, CIx1>, L>(
    SubEnv<'a, F, CIx1, Env1, L>,
);

/// Sub environment with a lens that is mapping lookup tables.
pub struct SubEnvLookup<'a, F: PrimeField, CIx1: ColumnIndexer, Env1: ColAccessCap<F, CIx1>, L>(
    SubEnv<'a, F, CIx1, Env1, L>,
);

////////////////////////////////////////////////////////////////////////////
// Trait implementations
////////////////////////////////////////////////////////////////////////////

impl<'a, F: PrimeField, CIx1: ColumnIndexer, Env1: ColAccessCap<F, CIx1>, L>
    SubEnv<'a, F, CIx1, Env1, L>
{
    pub fn new(env: &'a mut Env1, lens: L) -> Self {
        SubEnv {
            env,
            lens,
            phantom: Default::default(),
        }
    }
}

impl<'a, F: PrimeField, CIx1: ColumnIndexer, Env1: ColAccessCap<F, CIx1>, L>
    SubEnvColumn<'a, F, CIx1, Env1, L>
{
    pub fn new(env: &'a mut Env1, lens: L) -> Self {
        SubEnvColumn(SubEnv::new(env, lens))
    }
}

impl<'a, F: PrimeField, CIx1: ColumnIndexer, Env1: ColAccessCap<F, CIx1>, L>
    SubEnvLookup<'a, F, CIx1, Env1, L>
{
    pub fn new(env: &'a mut Env1, lens: L) -> Self {
        SubEnvLookup(SubEnv::new(env, lens))
    }
}

impl<
        'a,
        F: PrimeField,
        CIx1: ColumnIndexer,
        CIx2: ColumnIndexer,
        Env1: ColAccessCap<F, CIx1>,
        L: MPrism<Source = CIx1, Target = CIx2>,
    > ColAccessCap<F, CIx2> for SubEnv<'a, F, CIx1, Env1, L>
{
    type Variable = Env1::Variable;

    fn assert_zero(&mut self, cst: Self::Variable) {
        self.env.assert_zero(cst);
    }

    fn set_assert_mapper(&mut self, mapper: Box<dyn Fn(Self::Variable) -> Self::Variable>) {
        self.env.set_assert_mapper(mapper);
    }

    fn constant(value: F) -> Self::Variable {
        Env1::constant(value)
    }

    fn read_column(&self, ix: CIx2) -> Self::Variable {
        self.env.read_column(self.lens.re_get(ix))
    }
}

impl<
        'a,
        F: PrimeField,
        CIx1: ColumnIndexer,
        CIx2: ColumnIndexer,
        Env1: ColWriteCap<F, CIx1>,
        L: MPrism<Source = CIx1, Target = CIx2>,
    > ColWriteCap<F, CIx2> for SubEnv<'a, F, CIx1, Env1, L>
{
    fn write_column(&mut self, ix: CIx2, value: &Self::Variable) {
        self.env.write_column(self.lens.re_get(ix), value)
    }
}

impl<
        'a,
        F: PrimeField,
        CIx1: ColumnIndexer,
        CIx2: ColumnIndexer,
        Env1: HybridCopyCap<F, CIx1>,
        L: MPrism<Source = CIx1, Target = CIx2>,
    > HybridCopyCap<F, CIx2> for SubEnv<'a, F, CIx1, Env1, L>
{
    fn hcopy(&mut self, x: &Self::Variable, ix: CIx2) -> Self::Variable {
        self.env.hcopy(x, self.lens.re_get(ix))
    }
}

impl<
        'a,
        F: PrimeField,
        CIx1: ColumnIndexer,
        CIx2: ColumnIndexer,
        Env1: ColAccessCap<F, CIx1>,
        L: MPrism<Source = CIx1, Target = CIx2>,
    > ColAccessCap<F, CIx2> for SubEnvColumn<'a, F, CIx1, Env1, L>
{
    type Variable = Env1::Variable;

    fn assert_zero(&mut self, cst: Self::Variable) {
        self.0.assert_zero(cst);
    }

    fn set_assert_mapper(&mut self, mapper: Box<dyn Fn(Self::Variable) -> Self::Variable>) {
        self.0.set_assert_mapper(mapper);
    }

    fn constant(value: F) -> Self::Variable {
        Env1::constant(value)
    }

    fn read_column(&self, ix: CIx2) -> Self::Variable {
        self.0.read_column(ix)
    }
}

impl<
        'a,
        F: PrimeField,
        CIx1: ColumnIndexer,
        CIx2: ColumnIndexer,
        Env1: ColWriteCap<F, CIx1>,
        L: MPrism<Source = CIx1, Target = CIx2>,
    > ColWriteCap<F, CIx2> for SubEnvColumn<'a, F, CIx1, Env1, L>
{
    fn write_column(&mut self, ix: CIx2, value: &Self::Variable) {
        self.0.write_column(ix, value);
    }
}

impl<
        'a,
        F: PrimeField,
        CIx1: ColumnIndexer,
        CIx2: ColumnIndexer,
        Env1: HybridCopyCap<F, CIx1>,
        L: MPrism<Source = CIx1, Target = CIx2>,
    > HybridCopyCap<F, CIx2> for SubEnvColumn<'a, F, CIx1, Env1, L>
{
    fn hcopy(&mut self, x: &Self::Variable, ix: CIx2) -> Self::Variable {
        self.0.hcopy(x, ix)
    }
}

impl<'a, F: PrimeField, CIx1: ColumnIndexer, Env1: ColAccessCap<F, CIx1>, L> ColAccessCap<F, CIx1>
    for SubEnvLookup<'a, F, CIx1, Env1, L>
{
    type Variable = Env1::Variable;

    fn assert_zero(&mut self, cst: Self::Variable) {
        self.0.env.assert_zero(cst);
    }

    fn set_assert_mapper(&mut self, mapper: Box<dyn Fn(Self::Variable) -> Self::Variable>) {
        self.0.env.set_assert_mapper(mapper);
    }

    fn constant(value: F) -> Self::Variable {
        Env1::constant(value)
    }

    fn read_column(&self, ix: CIx1) -> Self::Variable {
        self.0.env.read_column(ix)
    }
}

impl<'a, F: PrimeField, CIx1: ColumnIndexer, Env1: ColWriteCap<F, CIx1>, L> ColWriteCap<F, CIx1>
    for SubEnvLookup<'a, F, CIx1, Env1, L>
{
    fn write_column(&mut self, ix: CIx1, value: &Self::Variable) {
        self.0.env.write_column(ix, value);
    }
}

impl<'a, F: PrimeField, CIx1: ColumnIndexer, Env1: HybridCopyCap<F, CIx1>, L> HybridCopyCap<F, CIx1>
    for SubEnvLookup<'a, F, CIx1, Env1, L>
{
    fn hcopy(&mut self, x: &Self::Variable, ix: CIx1) -> Self::Variable {
        self.0.env.hcopy(x, ix)
    }
}

impl<
        'a,
        F: PrimeField,
        CIx: ColumnIndexer,
        LT1: LookupTableID,
        LT2: LookupTableID,
        Env1: LookupCap<F, CIx, LT1>,
        L: MPrism<Source = LT1, Target = LT2>,
    > LookupCap<F, CIx, LT2> for SubEnvLookup<'a, F, CIx, Env1, L>
{
    fn lookup(&mut self, lookup_id: LT2, value: Vec<Self::Variable>) {
        self.0.env.lookup(self.0.lens.re_get(lookup_id), value)
    }

    fn lookup_runtime_write(&mut self, lookup_id: LT2, value: Vec<Self::Variable>) {
        self.0
            .env
            .lookup_runtime_write(self.0.lens.re_get(lookup_id), value)
    }
}

impl<
        'a,
        F: PrimeField,
        CIx1: ColumnIndexer,
        CIx2: ColumnIndexer,
        LT: LookupTableID,
        Env1: LookupCap<F, CIx1, LT>,
        L: MPrism<Source = CIx1, Target = CIx2>,
    > LookupCap<F, CIx2, LT> for SubEnvColumn<'a, F, CIx1, Env1, L>
{
    fn lookup(&mut self, lookup_id: LT, value: Vec<Self::Variable>) {
        self.0.env.lookup(lookup_id, value)
    }

    fn lookup_runtime_write(&mut self, lookup_id: LT, value: Vec<Self::Variable>) {
        self.0.env.lookup_runtime_write(lookup_id, value)
    }
}

impl<'a, F: PrimeField, CIx: ColumnIndexer, Env1: MultiRowReadCap<F, CIx>, L>
    MultiRowReadCap<F, CIx> for SubEnvLookup<'a, F, CIx, Env1, L>
{
    /// Read value from a (row,column) position.
    fn read_row_column(&mut self, row: usize, col: CIx) -> Self::Variable {
        self.0.env.read_row_column(row, col)
    }

    /// Progresses to the next row.
    fn next_row(&mut self) {
        self.0.env.next_row();
    }

    /// Returns the current row.
    fn curr_row(&self) -> usize {
        self.0.env.curr_row()
    }
}

impl<'a, F: PrimeField, CIx: ColumnIndexer, Env1: DirectWitnessCap<F, CIx>, L>
    DirectWitnessCap<F, CIx> for SubEnvLookup<'a, F, CIx, Env1, L>
{
    fn variable_to_field(value: Self::Variable) -> F {
        Env1::variable_to_field(value)
    }
}

// TODO add traits for SubEnvColumn