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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
/*! Static (typing-time) substitutions. */

//use std::fmt;
use std::rc::Rc;

use normal;
use ast::*;
use bitype::{Term};

// TODO-Someday: Use Rc-based lists instead vectors to represent the
// bound variable list, for cheaper O(1) clones.


/// Simplistic policy to create a "primed" version of `x`
pub fn alpha_vary(x:&String) -> String {
    format!("{}~",x)
}    

/// Test if a variable term appears in a vector of variable terms
pub fn fv_contains(fv:&Vec<Term>, x:&Term) -> bool {
    for y in fv.iter() {
        if y == x { return true }
    };
    return false
}

/// Test if an index term variable appears in a vector of variable terms
pub fn fv_contains_idxtm(fv:&Vec<Term>, x:&String) -> bool {
    let x = &Term::IdxTm(IdxTm::Var(x.clone()));
    fv_contains(fv, x)
}


/// Compute a list of variables (as terms) that appear free in the given term
pub fn fv_of_term(t:&Term) -> Vec<Term> {
    let mut out : Vec<Term> = vec![];
    match t {
        &Term::NmTm (ref n) => fv_of_nmtm (n, vec![], &mut out),
        &Term::IdxTm(ref i) => fv_of_idxtm(i, vec![], &mut out),
        &Term::Type (ref t) => fv_of_type (t, vec![], &mut out),        
    }
    return out
}

/// Compute the free variables of a name term
pub fn fv_of_nmtm(n:&NameTm, bound:Vec<Term>, out:&mut Vec<Term>) {
    use ast::NameTm::*;
    match n {
        &Var(_) |
        &ValVar(_) => {
            let x = Term::NmTm(n.clone());
            if let Some(_) = bound.iter().position(|y| &x == y) { /* x is bound. */ }
            else { /* x is free */ out.push(x) }
        },
        &Name(_) => {},
        &Bin(ref n, ref m) |
        &App(ref n, ref m) => {
            fv_of_nmtm(n, bound.clone(), out);
            fv_of_nmtm(m, bound,         out);
        }
        &Lam(ref x, _, ref m) => {
            let mut bound = bound;
            bound.push(Term::NmTm(NameTm::Var(x.clone())));
            fv_of_nmtm(m, bound, out);
        }
        &WriteScope => { out.push(Term::NmTm(
            NameTm::WriteScope
        )) },
        &NoParse(_) => { },
        &Ident(_) => { },
    }
}

/// Compute the free variables of an index term
pub fn fv_of_idxtm(i:&IdxTm, bound:Vec<Term>, out:&mut Vec<Term>) {
    use ast::IdxTm::*;
    match i {
        &Unknown => { },          
        &Var(ref _x) => {
            let x = Term::IdxTm(i.clone());
            if let Some(_) = bound.iter().position(|y| &x == y) { /* x is bound. */ }
            else { /* x is free */ out.push(x) }
        }
        &Lam(ref x, ref _g, ref i) => {
            let mut bound = bound;
            bound.push(Term::IdxTm(IdxTm::Var(x.clone())));
            fv_of_idxtm(i, bound, out);
        }
        &Ident(_)   |
        &NoParse(_) |
        &Unit       |
        &Empty      => {}
        &WriteScope => { out.push(Term::IdxTm(IdxTm::WriteScope)) }
        &Sing(ref n) => {
            fv_of_nmtm(n, bound, out)
        }
        &Map(ref n, ref i)     |
        &MapStar(ref n, ref i) => {
            fv_of_nmtm(n, bound.clone(), out);
            fv_of_idxtm(i, bound,        out);
        }
        &Proj1(ref i) |
        &Proj2(ref i) => {
            fv_of_idxtm(i, bound, out);
        }
        &NmSet(ref nms) => {
            use normal::NmSetTm::*;
            for t in nms.terms.iter() {
                match t {
                    &Single(ref n) => { fv_of_nmtm(n, bound.clone(), out)  }
                    &Subset(ref i) => { fv_of_idxtm(i, bound.clone(), out) }
                };
            }
        }
        &NmTm(ref n) => { fv_of_nmtm(n, bound, out) }
        &Apart(ref i, ref j)   |
        &Union(ref i, ref j)   |
        &Bin(ref i, ref j)     |
        &Pair(ref i, ref j)    |
        &App(ref i, ref j)     |
        &FlatMap(ref i, ref j) |
        &FlatMapStar(ref i, ref j)    => {
            fv_of_idxtm(i, bound.clone(), out);
            fv_of_idxtm(j, bound        , out);
        }
    }
}
pub fn fv_of_type(_t:&Type, _bound:Vec<Term>, _out:&mut Vec<Term>) {
    // XXX
    //unimplemented!()
    // TODO!
}
pub fn fv_of_ctype(_ct:&CType, _bound:Vec<Term>, _out:&mut Vec<Term>) {
    unimplemented!()
}
pub fn fv_of_ceffect(_ct:&CEffect, _bound:Vec<Term>, _out:&mut Vec<Term>) {
    unimplemented!()
}
pub fn fv_of_effect(_ct:&Effect, _bound:Vec<Term>, _out:&mut Vec<Term>) {
    unimplemented!()
}




/// Substitute a type for a type variable in another type
pub fn subst_type_type(a:Type, x:&String, b:Type) -> Type {
    subst_term_type(Term::Type(a), x, b)
}

/// Substitute an index for an index variable in another type
pub fn subst_idxtm_type(i:IdxTm, x:&String, b:Type) -> Type {
    subst_term_type(Term::IdxTm(i), x, b)
}


/// Predicate for type terms
pub fn term_is_type(t:&Term) -> bool {
    match t { &Term::Type(_) => true, _ => false }
}

/// Predicate for index terms
pub fn term_is_idxtm(t:&Term) -> bool {
    match t { &Term::IdxTm(_) => true, _ => false }
}

/// Predicate for name terms
pub fn term_is_nmtm(t:&Term) -> bool {
    match t { &Term::NmTm(_) => true, _ => false }
}

/// Substitute terms into types
pub fn subst_term_type_rec(t:Term, x:&String, a:Rc<Type>) -> Rc<Type> {
    Rc::new(subst_term_type(t, x, (*a).clone()))
}

/// Substitute terms into computation types
pub fn subst_term_ctype(t:Term, x:&String, ct:CType) -> CType {
    match ct {
        CType::Lift(a) => {
            CType::Lift(subst_term_type(t,x,a))
        }
        CType::Arrow(a, ce) => {
            CType::Arrow(subst_term_type(t.clone(),x,a),
                         subst_term_ceffect_rec(t,x,ce))
        }
        CType::NoParse(s) => CType::NoParse(s)
    }
}

/// Substitute terms into effects
pub fn subst_term_effect_rec(t:Term, x:&String, eff:Rc<Effect>) -> Rc<Effect> {
    Rc::new(subst_term_effect(t, x, (*eff).clone()))
}

/// Substitute terms into effects
pub fn subst_term_effect(t:Term, x:&String, eff:Effect) -> Effect {
    match eff {
        Effect::WR(i, j) => {
            Effect::WR(subst_term_idxtm(t.clone(), x, i),
                       subst_term_idxtm(t, x, j))
        },
        // Effect::Then(e1, e2) => {
        //     Effect::Then(subst_term_effect_rec(t.clone(), x, e1),
        //                  subst_term_effect_rec(t, x, e2))
        // }
        Effect::NoParse(s) => Effect::NoParse(s)
    }
}

/// Substitute terms into computation effects 
pub fn subst_term_ceffect_rec(t:Term, x:&String, ce:Rc<CEffect>) -> Rc<CEffect> {
    Rc::new(subst_term_ceffect(t, x, (*ce).clone()))
}

/// Substitute terms into computation effects 
pub fn subst_term_ceffect(t:Term, x:&String, ce:CEffect) -> CEffect {
    match ce {
        CEffect::Cons(ct, eff) => {
            CEffect::Cons(subst_term_ctype(t.clone(), x, ct),
                          subst_term_effect(t, x, eff))
        }
        CEffect::ForallType(y, k, ce) => {
            if term_is_type(&t) && x == &y {
                CEffect::ForallType(y, k, ce)
            } else {
                CEffect::ForallType(y, k, subst_term_ceffect_rec(t, x, ce))
            }
        }
        CEffect::ForallIdx(y, g, p, ce) => {
            if term_is_idxtm(&t) && x == &y {
                CEffect::ForallIdx(y, g, p, ce)
            } else {
                CEffect::ForallIdx(y, g,
                                   subst_term_prop(t.clone(), x, p),
                                   subst_term_ceffect_rec(t, x, ce))
            }
        }
        CEffect::NoParse(s) => CEffect::NoParse(s)
    }
}

/// Substitute terms into types
pub fn subst_term_type(t:Term, x:&String, a:Type) -> Type {
    match a {
        Type::Unit => Type::Unit,
        Type::Abstract(y) => Type::Abstract(y),
        Type::Ident(y) => Type::Ident(y),
        Type::IdentUndef(y) => Type::IdentUndef(y),
        Type::Prim(pt) => Type::Prim(pt),
        Type::IdentDef(y, ydef) => {
            // If the substitution of [t/x] changes the body
            // of the definition, then we "forget" the
            // identifier x.  Otherwise, we keep it around.
            let ydef2 = subst_term_type(t, x, (*ydef).clone());
            if *ydef != ydef2 { ydef2 } else {
                Type::IdentDef(y, ydef)
            }
        }
        Type::Var(y) => {
            if term_is_type(&t) && x == &y {
                match t {
                    Term::Type(b) => b.clone(),
                    _ => unreachable!(),
                }
            }
            else {
                Type::Var(y)
            }
        }
        Type::Sum(a1, a2) => {
            Type::Sum(
                subst_term_type_rec(t.clone(), x, a1),
                subst_term_type_rec(t,         x, a2),
            )
        }
        Type::Prod(a1, a2) => {
            Type::Prod(
                subst_term_type_rec(t.clone(), x, a1),
                subst_term_type_rec(t,         x, a2),
            )
        }
        Type::Ref(i, a0) => {
            Type::Ref(
                subst_term_idxtm(t.clone(), x, i),
                subst_term_type_rec(t, x, a0),
            )
        }
        Type::Thk(i, ce) => {
            //
            // The scope of the write scope variable `WriteScope`:
            // ---------------------------------------------------
            //
            // Conceptually, there is a hidden binder to bind the `@@`
            // and `@!` variables for each `Thk` type (determined by
            // the allocation context's dynamic write scope); these
            // variables may appear free in each `ce`.
            //
            let ce = {
                if x == idxtm_writescope_var_str() ||
                    x == nmtm_writescope_var_str()
                {
                    ce
                } else {
                    subst_term_ceffect_rec(t.clone(), x, ce)
                }
            };
            Type::Thk(
                subst_term_idxtm(t, x, i),
                ce,
            )
        }        
        Type::IdxApp(a0, i) => {
            Type::IdxApp(
                subst_term_type_rec(t.clone(), x, a0),
                subst_term_idxtm(t, x, i)
            )
        }
        Type::TypeApp(a1, a2) => {
            Type::TypeApp(
                subst_term_type_rec(t.clone(), x, a1),
                subst_term_type_rec(t, x, a2)
            )
        }
        Type::Nm(i) => {
            Type::Nm(subst_term_idxtm(t, x, i))
        }
        Type::NmFn(n) => {
            Type::NmFn(subst_term_nmtm(t, x, n))
        }
        Type::Rec(y, a1) => {
            if term_is_type(&t) && x == &y {
                Type::Rec(y, a1)
            } else {
                Type::Rec(y, subst_term_type_rec(t, x, a1))
            }            
        }
        Type::TypeFn(y, k, a1) => {
            if term_is_type(&t) && x == &y {
                Type::TypeFn(y, k, a1)
            } else {
                Type::TypeFn(y, k, subst_term_type_rec(t, x, a1))
            }
        }
        Type::IdxFn(y, g, a1) => {
            if term_is_idxtm(&t) && x == &y {
                Type::IdxFn(y, g, a1)
            } else {
                let fv = fv_of_term(&t);
                if fv_contains_idxtm(&fv, &y) {
                    // When free variables of `t` contains var `y`, we
                    // need to alpha-vary `y` before substituting into `a1`.
                    let y_ = alpha_vary(&y);
                    assert!(!fv_contains_idxtm(&fv, &y_));
                    let t_y_ = Term::IdxTm(IdxTm::Var(y_.clone()));
                    let a1 = subst_term_type_rec(t_y_, &y, a1);
                    Type::IdxFn(y_, g, subst_term_type_rec(t, x, a1))
                        
                } else {
                    Type::IdxFn(y, g, subst_term_type_rec(t, x, a1))
                }
            }
        }
        //  [i/b] (exists a:g |      p.      A)
        //     == (exists a:g | [i/b]p. [i/b]A)
        //
        Type::Exists(y, g, p, a1) => {
            if term_is_idxtm(&t) && x == &y {
                Type::Exists(y, g, p, a1)
            } else {
                let fv = fv_of_term(&t);
                if fv_contains_idxtm(&fv, &y) {
                    let y_ = alpha_vary(&y);
                    assert!(!fv_contains_idxtm(&fv, &y_));
                    let t_y_ = Term::IdxTm(IdxTm::Var(y_.clone()));
                    let a1 = subst_term_type_rec(t_y_.clone(), &y, a1);
                    let p  = subst_term_prop(t_y_, &y, p);
                    Type::Exists(
                        y_, g,
                        subst_term_prop(t.clone(), x, p),
                        subst_term_type_rec(t, x, a1),
                    )
                } else {
                    Type::Exists(
                        y, g,
                        subst_term_prop(t.clone(), x, p),
                        subst_term_type_rec(t, x, a1),
                    )
                }
            }
        }
        Type::NoParse(s) => Type::NoParse(s)
    }
}

/// Substitute terms into propositions
pub fn subst_term_prop(t:Term, x:&String, p:Prop) -> Prop {
    match p {
        Prop::Tt => Prop::Tt,
        Prop::Equiv(i, j, g) => {
            Prop::Equiv(
                subst_term_idxtm(t.clone(), x, i),
                subst_term_idxtm(t        , x, j),
                g)
        },
        Prop::Apart(i, j, g) => {
            Prop::Apart(
                subst_term_idxtm(t.clone(), x, i),
                subst_term_idxtm(t        , x, j),
                g)
        },
        Prop::Conj(p1, p2) => {
            Prop::Conj(
                subst_term_prop_rec(t.clone(), x, p1),
                subst_term_prop_rec(t        , x, p2),
            )
        }
        Prop::NoParse(s) => Prop::NoParse(s)
    }
}

/// Substitute terms into propositions
pub fn subst_term_prop_rec(t:Term, x:&String, p:Rc<Prop>) -> Rc<Prop> {
    Rc::new(subst_term_prop(t,x,(*p).clone()))
}

/// Substitute terms into index terms
pub fn subst_term_idxtm_rec(t:Term, x:&String, i:Rc<IdxTm>) -> Rc<IdxTm> {
    Rc::new(subst_term_idxtm(t, x, (*i).clone()))
}

/// magic variable name for the write scope function over name sets (`"@!"`)
pub fn idxtm_writescope_var_str() -> &'static str { "@!" }

/// magic variable name for the write scope function over names (`"@@"`)
pub fn nmtm_writescope_var_str() -> &'static str { "@@" }

/// Substitute terms into index terms
pub fn subst_term_idxtm(t:Term, x:&String, i:IdxTm) -> IdxTm {
    // Types never appear in index terms
    if term_is_type(&t) { i.clone() } else { match i {
        // Variables and identifiers are lexically distinct
        IdxTm::Unknown => IdxTm::Unknown,
        IdxTm::Ident(y) => IdxTm::Ident(y),
        IdxTm::WriteScope => {
            if x == idxtm_writescope_var_str() { match t {
                Term::IdxTm(i) => i,
                _ => unreachable!(),
            }} else {
                IdxTm::WriteScope
            }
        },
        IdxTm::Var(y) => {
            if term_is_idxtm(&t) && x == &y {
                match t {
                    Term::IdxTm(i) => i.clone(),
                    _ => unreachable!(),
                }
            }
            else {
                IdxTm::Var(y)
            }
        }
        IdxTm::Sing(n) => {
            IdxTm::Sing(subst_term_nmtm(t,x,n))
        },
        IdxTm::NmTm(n) => {
            IdxTm::NmTm(subst_term_nmtm(t,x,n))
        },
        IdxTm::Unit  => IdxTm::Unit,
        IdxTm::Empty => IdxTm::Empty,
        IdxTm::NmSet(s) => {
            let mut terms = vec![];
            for tm in s.terms {
                let tm = match tm {
                    normal::NmSetTm::Single(n) => {
                        normal::NmSetTm::Single(subst_term_nmtm(t.clone(),x,n))
                    },
                    normal::NmSetTm::Subset(i) => {
                        normal::NmSetTm::Subset(subst_term_idxtm(t.clone(),x,i))
                    }
                };
                terms.push(tm)
            }
            IdxTm::NmSet(normal::NmSet{cons:s.cons, terms:terms})
        }
        IdxTm::Apart(i, j) => {
            IdxTm::Apart(subst_term_idxtm_rec(t.clone(),x,i),
                        subst_term_idxtm_rec(t,x,j))
        }
        IdxTm::Union(i, j) => {
            IdxTm::Union(subst_term_idxtm_rec(t.clone(),x,i),
                         subst_term_idxtm_rec(t,x,j))
        }
        IdxTm::Bin(i, j) => {
            IdxTm::Bin(subst_term_idxtm_rec(t.clone(),x,i),
                       subst_term_idxtm_rec(t,x,j))
        }
        IdxTm::Pair(i, j) => {
            IdxTm::Pair(subst_term_idxtm_rec(t.clone(),x,i),
                        subst_term_idxtm_rec(t,x,j))
        }
        IdxTm::Proj1(i) => {
            IdxTm::Proj1(subst_term_idxtm_rec(t,x,i))
        }
        IdxTm::Proj2(i) => {
            IdxTm::Proj2(subst_term_idxtm_rec(t,x,i))
        }
        IdxTm::Lam(y,g,i) => {
            if term_is_idxtm(&t) && x == &y {
                IdxTm::Lam(y,g,i)
            } else {
                IdxTm::Lam(y,g,subst_term_idxtm_rec(t,x,i))
            }
        }
        IdxTm::App(i, j) => {
            IdxTm::App(subst_term_idxtm_rec(t.clone(), x, i),
                       subst_term_idxtm_rec(t, x, j))
        }
        IdxTm::Map(n, j) => {
            IdxTm::Map(subst_term_nmtm_rec(t.clone(), x, n),
                       subst_term_idxtm_rec(t, x, j))
        }
        IdxTm::MapStar(n, j) => {
            IdxTm::MapStar(subst_term_nmtm_rec(t.clone(), x, n),
                           subst_term_idxtm_rec(t, x, j))
        }
        IdxTm::FlatMap(i, j) => {
            IdxTm::FlatMap(subst_term_idxtm_rec(t.clone(), x, i),
                           subst_term_idxtm_rec(t, x, j))
        }
        IdxTm::FlatMapStar(i, j) => {
            IdxTm::FlatMapStar(subst_term_idxtm_rec(t.clone(), x, i),
                        subst_term_idxtm_rec(t, x, j))
        }
        IdxTm::NoParse(s) =>
            IdxTm::NoParse(s)
    }}
}

/// Substitute terms into name terms
pub fn subst_term_nmtm_rec(t:Term, x:&String, m:Rc<NameTm>) -> Rc<NameTm> {
    Rc::new(subst_term_nmtm(t,x,(*m).clone()))
}


/// Substitute name terms into name terms
pub fn subst_term_nmtm(t:Term, x:&String, m:NameTm) -> NameTm {
    if ! term_is_nmtm(&t) { m.clone() } else { match m {
        NameTm::ValVar(y) => {
            NameTm::ValVar(y)
        }
        NameTm::Ident(y) => {
            NameTm::Ident(y)
        }
        NameTm::Var(y) => {
            if term_is_nmtm(&t) && x == &y {
                match t {
                    Term::NmTm(n) => n,
                    _ => unreachable!(),
                }
            } else {
                NameTm::Var(y)
            }
        }
        NameTm::WriteScope => {
            if x == nmtm_writescope_var_str() { match t {
                Term::NmTm(n) => n,
                _ => unreachable!(),
            }} else {
                NameTm::WriteScope
            }
        },
        NameTm::NoParse(s) => NameTm::NoParse(s),
        NameTm::Name(n) => NameTm::Name(n),
        NameTm::App(n1, n2) => {
            NameTm::App(
                subst_term_nmtm_rec(t.clone(), x, n1),
                subst_term_nmtm_rec(t        , x, n2),
            )                
        }
        NameTm::Bin(n1, n2) => {
            NameTm::Bin(
                subst_term_nmtm_rec(t.clone(), x, n1),
                subst_term_nmtm_rec(t        , x, n2),
            )                
        }
        NameTm::Lam(y,yg,m1) => {
            if term_is_nmtm(&t) && x == &y {
                NameTm::Lam(y,yg,m1)
            }
            else {
                NameTm::Lam(y,yg,
                            subst_term_nmtm_rec(t,x,m1))
            }
        }
    }}
}


/// Substitute name terms into name terms
pub fn subst_nmtm(t:NameTm, x:&String, m:NameTm) -> NameTm {
    subst_term_nmtm(Term::NmTm(t), x, m)
}

/// Substitute name terms into name terms
pub fn subst_nmtm_rec(t:NameTm, x:&String, m:Rc<NameTm>) -> Rc<NameTm> {
    Rc::new(subst_term_nmtm(Term::NmTm(t), x, (*m).clone()))
}