summaryrefslogtreecommitdiff
path: root/Source/Core/PureCollections.ssc
blob: 971af388c4be6b8063a29179f3cc1c8b2074b7c4 (plain)
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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
//-----------------------------------------------------------------------------
//
// Copyright (C) Microsoft Corporation.  All Rights Reserved.
//
//-----------------------------------------------------------------------------

//---------------------------------------------------------------------
// PureCollections.cs
//       - Mostly pure functions for tuples, sets, maps, Sequences
// Version 1.0, WS, 3/23/2002 
//---------------------------------------------------------------------


using System;
using System.Collections;
using Microsoft.Contracts;

namespace PureCollections {
  //-------------------------------------------------------------------
  // General types
  //-------------------------------------------------------------------

    public class MissingCase :Exception{} 

  public struct Capacity{
    public int capacity;
    public Capacity (int i) {capacity = i;}
  }

  abstract public class Coll {
    public object[] elems;      // null is used to show empty spots!
    protected int card;
        protected Coll() {}
        protected Coll(object[] elems, int card) {this.elems = elems; this.card = card; }
        protected Coll(Coll! c) 
          requires c.elems != null;
        {
          this.elems = (object[])c.elems.Clone();
          this.card = c.card;
        }
  }  


  // ------------------------------------------------------------------
  // Tuple
  // ------------------------------------------------------------------

  public class Tuple : Coll, IComparable
  {
    //public object[] elems;
    
    //invariant this.elems != null;

    // Constructor - - - - - - - - - - - - - - - - - - - - - - - - - - 
    public Tuple(params object []! ts) {
            elems = ts;
            card = ts.Length;}
    public Tuple(Capacity c) {
            elems = new object[c.capacity];
            card = c.capacity;
        }

    //Equality - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
    
    [Pure][Reads(ReadsAttribute.Reads.Nothing)]
    public override bool Equals (object o){
      assert this.elems != null;
      if (o == null || !(o is Tuple) || elems.Length != ((!)((Tuple)o).elems).Length)
        return false;

      Tuple s = (Tuple) o;  
      for(int i = 0; i < elems.Length; i ++)
        if ( ! Equals(this.elems[i], s.elems[i]))
          return false;
      return true;
    }

    [Pure][Reads(ReadsAttribute.Reads.Nothing)]  // ugh, is this right?  --KRML
    public static bool operator == (Tuple s, Tuple t) {return s == null ? t == null : s.Equals(t);}
    [Pure][Reads(ReadsAttribute.Reads.Nothing)]  // ugh, is this right?  --KRML
    public static bool operator != (Tuple s, Tuple t) { return ! (t == s); }

    [Pure]
    public override int GetHashCode (){
      int h =0;
      assume this.elems != null;
      for(int i = 0; i < elems.Length; i++)
      {
        object elem = elems[i];
        if (elem != null) 
          h += elem.GetHashCode();
      }
      return h;  
    }

    //Compare - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    int IComparable.CompareTo(object o)  {
      assert this.elems != null;
      if (o == null || !(o is Tuple) || elems.Length != ((!)((Tuple)o).elems).Length)
        throw new MissingCase();
     
      Tuple t = (Tuple) o;
      for(int i = 0; i < elems.Length; i ++) {
        int c = ((IComparable!) elems[i]).CompareTo(t.elems[i]);
        if (c < 0) return -1;
        else if (c > 0) return +1;
      }
      return 0;
    }

    public static bool operator <= (Tuple s, Tuple t) {return s == null ? t == null : ((IComparable) s).CompareTo(t) <= 0;}
    public static bool operator < (Tuple s, Tuple t) {return s == null ? false : ((IComparable) s).CompareTo(t) < 0;}
    public static bool operator >= (Tuple s, Tuple t) {return t <= s;  }    
    public static bool operator > (Tuple s, Tuple t) { return t < s; }

    //Select and Update - - - - - - - - - - - - - - - - - - - - - - - -
    public object this[int index]{
      get{assert this.elems != null; return elems[index];}
      set{assert this.elems != null; elems[index] = value;}
    }
    
    //ToString - - - -  - - - - - - - - - - - - - - - - - - - - - - - -
    [Pure]
    public override string! ToString() {
      assert this.elems != null; 
      if (elems.Length==0)
        return "()";
      
      string s = "(";
      for (int i= 0; i< elems.Length-1; i++)
        s += ((!)elems[i]).ToString() + ", ";
      return s + ((!)elems[elems.Length-1]).ToString() + ")";
    }   
  }

  // ------------------------------------------------------------------
  // Pair
  public class Pair : Tuple{
    protected Pair(){}
    public Pair(object first, object second) {
      elems = new object[]{first,second};
    }
    public object First{
      get{assert this.elems != null; return elems[0];}
      set{assert this.elems != null; elems[0]=value;}
    }
    public object Second{
      get{assert this.elems != null; return elems[1];}
      set{assert this.elems != null; elems[1]=value;}
    }
  }

  // --------------------------------------------------------------------
  // Map
  // --------------------------------------------------------------------

  public class MapEnumerator: IEnumerator{
    private Map! map;
    private int index = -1;
    public MapEnumerator(Map! m) { map = m;}
    public bool MoveNext() {
      do{
        index++;
        assert map.elems != null;
      } while (index < map.elems.Length && map.elems[index] == null);
      return index < map.elems.Length;
    }
    public object Current{ get { assert map.elems != null; return new Pair(map.elems[index],map.vals[index]); }}
    public void Reset() {index = -1; }
  }
  
  public class Map:Coll, IEnumerable, IComparable
  {  
    public Object[]! vals;
  
    //invariant this.elems != null;
     
    // constructors - - - - - - - - - - - - - - - - - - - - - - - - - - - 
    public Map(Capacity c){
      elems = new Object[c.capacity*2];
      vals = new Object[c.capacity*2];
      card = 0;
    }
 
    [NotDelayed]
    public Map(params Pair []! ps){
      elems = new Object[ps.Length*2];
      vals = new Object[ps.Length*2];
      base();
      card = 0;
      for(int i = 0; i < ps.Length; i++)
        Insert( ((!)ps[i]).First, ((!)ps[i]).Second);
    }

    // iterators - - - - - - - - - - - - - - - - - - - - - - - - - - - 
    [Pure] [GlobalAccess(false)] [Escapes(true,false)]
    public IEnumerator! GetEnumerator() { 
      return new MapEnumerator(this);
    }
    public Pair[] ToArray() {
      Pair [] n = new Pair[card];
      int ct = 0;
      assert this.elems != null;
      for(int i =0; i < elems.Length ; i++)
        if (elems[i] != null)
          n[ct++] = new Pair(elems[i], vals[i]);
      return n;
    }
  
    //(ASM) Update- - - - - - - - - - - - - - - - - - - - - - - - - - -
    public Map Update(object k, object v) {
      Map n = new Map(new Capacity(card+1));
      assert this.elems != null;
      for (int i = 0; i < elems.Length; i++ )
        if (elems[i] != null && ! Equals(elems[i], k))
          n.Insert(elems[i], vals[i]);
      n.Insert(k,v);
      return n;
    }

    //In place Update (and Remove)- - - - - - - - - - - - - - - - - - -
    public object this[object index]{
      get{return this.Apply(index);}
      set{this.Insert(index,value);}
    }

        public void Remove(object! o) {
            assert this.elems != null;
            int h = Math.Abs(o.GetHashCode()) % elems.Length;
            for (int i = 0; i < elems.Length; i++ ) {
                int j = (i+ h) % elems.Length;
                if (elems[j] == null) {
                    break;
                } else if (Equals(elems[j], o)){
                    elems[j] = null;
                    vals[j] = null;
                    break;
                } 
            }
        }
   
    public void Insert(Object key, Object val) {
      if (key == null)
        throw new MissingCase();      
      
      assert this.elems != null;
      if (elems.Length == 0 || 2*card >= elems.Length){
        int m = card*2; if (m < 4) m = 4;
        object [] newElems = new object [m];
        object [] newVals = new object [m];
        for (int k = 0; k < elems.Length; k++) {
          object elem = elems[k];
          if (elem != null) {
            int newHash = Math.Abs(elem.GetHashCode()) % newElems.Length;
            for (int i = 0; i < newElems.Length; i++ ) {
              int j = (i+ newHash) % newElems.Length;
              if (newElems[j] == null) {
                newElems[j] = elem;
                newVals[j] = vals[k];
                break;
              }
            }
          }
        }
        elems = newElems;
        vals = newVals;
      }        
      int h = Math.Abs(key.GetHashCode()) % elems.Length;
      for (int i = 0; i < elems.Length; i++ ) {
        int j = (i+ h) % elems.Length;
        if (elems[j] == null) {
          elems[j] = key;
          vals[j] = val;
          card ++;
          return;
        } else if (key.Equals(elems[j])) {
          vals[j] = val; 
          return;
        }
      }
    }

    //ToString - - - - - - - - - - - - - - - - - - - - - - - - -
    [Pure]
    public override string! ToString() {
      if (card ==0)
        return "{|->}";
      else {
        string s = "{"; int ct = 0;
        assert this.elems != null;
        for(int i =0; i < elems.Length ; i++) {
          object elem = elems[i];
          if (elem != null){
            s += elem.ToString() +"|->" + ((!)vals[i]).ToString() ;
            s +=(ct!=card-1) ? ", " : "";
            ct ++;
          }
        }  
        return s+"}";
      }
    }
 
    // Subset operations - - - - - - - - - - - - - - - - - - - - - - -
    // View Map as Set of Pairs

    int IComparable.CompareTo(object o) {
      if (o == null || !(o is Map))
        throw new MissingCase();
      // WS Improve performance!
      Map t = (Map) o;
      if (this < t) return -1;
      else if(this > t) return +1;
      else return 0;
    }
    public static bool operator <= (Map s, Map t){
      if (s==null) return t==null;
      if (t==null) return false;
      assert s.elems != null;
      for(int i = 0; i < s.elems.Length; i++)
        if (s.elems[i]!= null) {
          object o = t.Apply(s.elems[i]);
          if (o == null || !o.Equals(s.vals[i]))
            return false;
        }
      return true; 
    }
    public static bool operator < (Map s, Map t){
      return s == null || t == null ? false : s.card < t.card && s <= t; 
    }
    public static bool operator >= (Map s, Map t){
      return t <= s;  
    }    
    public static bool operator > (Map s, Map t){ 
      return t < s; 
    }

    // Equality - - - - - - - - - - - - - - - - - - - - - - -
    [Pure][Reads(ReadsAttribute.Reads.Nothing)]
    public override bool Equals (Object t){
      return t != null && t is Map && card == ((Map) t).card && this<= ((Map) t);
    }
    [Pure][Reads(ReadsAttribute.Reads.Nothing)]  // ugh, is this right?  --KRML
    public static bool operator == (Map s, Map t){
      if ((object)s==null)
        if ((object)t==null) return true;
        else return t.Equals(s);
      else
        return s.Equals(t);
    }
    [Pure][Reads(ReadsAttribute.Reads.Nothing)]  // ugh, is this right?  --KRML
    public static bool operator != (Map s, Map t){ 
      return ! (t == s); 
    }
    [Pure]
    public override int GetHashCode (){
      int h =0;
      assert this.elems != null;
      for(int i = 0; i < elems.Length; i++)
      {
        object elem = elems[i];
        if (elem != null)
        {
          h += elem.GetHashCode() + ((!)vals[i]).GetHashCode();
        }
      }
      return h;  
    }
 
    //Ordinary map operations- - - - - - - - - - - - - - - - - - - - - - - - 
   
    [Pure]
    public bool Has(Object x) {      
      if (x == null) 
        throw new MissingCase();      

      assert this.elems != null;
      if (elems.Length == 0) 
        return false;
      int h = Math.Abs(x.GetHashCode()) % elems.Length;
      for (int i = 0; i < elems.Length; i++ ) {
        int j = (i+ h) % elems.Length;
        if (x.Equals(elems[j]))
          return true;
      }
      return false;
    }

    public object Apply(object x) {  
      if (x == null)
        throw new MissingCase();
      assert this.elems != null;
      if (elems.Length == 0) 
        return null;
      int h = Math.Abs(x.GetHashCode()) % elems.Length;
      for (int i = 0; i < elems.Length; i++ ) {
        int j = (i+ h) % elems.Length;
        if (elems[j] != null && x.Equals(elems[j]))
          return vals[j];
      }
      return null;     
    }

    public static Map Override(Map! s, Map! t) {      
      Map m = new Map(new Capacity(s.card+t.card));
      assert s.elems != null;
      for(int i = 0; i< s.elems.Length; i++)
        if (s.elems[i] != null) 
          m.Insert(s.elems[i], s.vals[i]);
      assert t.elems != null;
      for(int i = 0; i< t.elems.Length; i++)
        if (t.elems[i] != null) 
          m.Insert(t.elems[i], t.vals[i]);
      return m;    
    }   
  }

  // --------------------------------------------------------------------
  // Sequence

  public class SequenceEnumerator: IEnumerator{
    [Peer] private Sequence! seq;
    private int index = -1;
    [Captured]
    public SequenceEnumerator(Sequence! s) { seq = s;}
    public bool MoveNext() {
      index++;
      //while (index < seq.elems.Length); // Sequences allow nils ... && seq.elems[index] == null);
      return index < seq.Length;
    }
    public object Current{ get { assert seq.elems != null; return seq.elems[index]; }}
    public void Reset() {index = -1; }
  }
  
  public  class Sequence:Coll, IEnumerable, IComparable
  {
    public Sequence(){elems = new object [4];}
    
    //invariant this.elems != null;

    //constructors - - - - - - - - - - - - - - - - - - - - - - - - - - - 
    public Sequence(params object []! ds){card = ds.Length; elems = ds; }
    public Sequence(Sequence! seq) {
      base(seq);
    }
    public Sequence(Capacity c){elems = new object [c.capacity];}

    // Iterators - - - - - - - - - - - - - - - - - - - - - - - - - - - 
    [Pure] [GlobalAccess(false)] [Escapes(true,false)]
    public IEnumerator! GetEnumerator()
      ensures Owner.Same(result, this);
      ensures result.IsNew;
    { 
      return new SequenceEnumerator(this);
    }
      
    public object[] ToArray() {
      object [] n = new object[card];
      int ct = 0;
      assert this.elems != null;
      for(int i =0; i < elems.Length ; i++)
        n[ct++] = elems[i];
      return n;
    }
  
    //ASM Update - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    public Sequence Update(int i, object v) {
      Sequence n = new Sequence(new Capacity(card+1));
      assert this.elems != null;
      assert n.elems != null;
      for (int j = 0; j < elems.Length; j++ )
        n.elems[j] = elems[j];
      if (i >= 0 && i < card){
        n.elems[i] = v;
        n.card = card;
        return n;
      } else if (i == card) {
        n.elems[i] = v;
        n.card = card+1;
        return n;
      } else
        throw new Exception("Sequence Update out of range");
    }

    //In place Update (and Remove) and Length - - - - - - - - - - - - - - - 
    public int Length {
      get{return this.card;}
    }

    public object this[int index]{
      get{assert this.elems != null; return this.elems[index];}
      set{assert this.elems != null; this.elems[index] = value;}
    }

    public void Add(object o){
      assert this.elems != null; 
      int n = this.elems.Length;
      int i = this.card++;
      if (i == n){
        int m = n*2; if (m < 4) m = 4;
        object [] newElems = new object [m];
        for (int j = 0; j < n; j++) newElems[j] = elems[j];
        elems = newElems;
      }
      elems[i] = o;
    }
  
    public void AddRange(Sequence! seq){
      foreach (object o in seq) {
        Add(o);
      }
    }
  
    public void Remove(){
      if (card == 0)
        return;
      card--;
      }
  
    // remove the first occurrence of o from this sequence
    public void Remove(Object x) {
      if (x == null)
        throw new MissingCase();
      assert this.elems != null; 
      for (int i = 0; i < card; i++) {
        if (x.Equals(elems[i])) {
          ++i;
          while (i < card) {
            elems[i-1] = elems[i];
            ++i;
          }
          card--;
          elems[card] = null;
          return;
        }
      }
    }
    
    public void Truncate(int newLen)
      requires 0 <= newLen && newLen <= Length;
    {
      assert elems != null;
      for (int i = newLen; i < card; i++) {
        elems[i] = null;
      }
      card = newLen;
    }

    //ToString - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    [Pure]
    public override string! ToString() {
      string s ="";
      assert this.elems != null; 
      if (card > 0 && elems[0] is Char) {
        for(int i =0; i < card ; i++)
        {
          object elem = elems[i];
          if (elem != null) { s +=elem.ToString(); }
        }
        return s;
      } else {
        s = "[";
        for(int i =0; i < card-1; i++) {
          object elem = elems[i];
          if (elem != null) { s += elem.ToString()+", "; }
        }
        if (card > 0)
        {
          object last = elems[card-1];
          if (last != null) { s += last.ToString(); }
        }
        s += "]";
        return s;
      }
    }
    //Equality- - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
    [Pure][Reads(ReadsAttribute.Reads.Nothing)]
    public override bool Equals (object that){    
      return that != null && that is Sequence && ((Sequence) this == (Sequence) that);
    }
    [Pure][Reads(ReadsAttribute.Reads.Nothing)]  // ugh, is this right?  --KRML
    public static bool operator == (Sequence s, Sequence t){ 
        if ((object)s == (object)t) {
            return true;
        } else if ((object)s == null || (object)t == null) {
            return false;
        }
      if (s.card != t.card) return false;
      assert s.elems != null; 
      assert t.elems != null; 
      for(int i = 0; i < s.card; i++)
        if (! Equals(s.elems[i], t.elems[i])) 
          return false;
      return true; 
    }
    [Pure][Reads(ReadsAttribute.Reads.Nothing)]  // ugh, is this right?  --KRML
    public static bool operator != (Sequence s, Sequence t){ 
      return !(s == t); 
    }
    [Pure]
    public override int GetHashCode (){
      int h = 0;
      for(int i = 0; i < card; i++)
      {
        assert this.elems != null; 
        object elem = elems[i];
        if (elem != null) { h += elem.GetHashCode(); }
      }
      return h; 
    }
    //Subset- - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
    // View Sequence of T as Set of (Integer,T) 
    int IComparable.CompareTo(object o) {
      if (o == null || !(o is Sequence))
        throw new MissingCase();
      // WS Improve performance!
      Sequence t = (Sequence) o;
      if (this < t) return -1;
      else if(this > t) return +1;
      else return 0;
    }
    
    public static bool operator < (Sequence s, Sequence t){ 
      if (s==null) throw new ArgumentNullException("s");
      if (t==null) throw new ArgumentNullException("t");
      if (s.card >= t.card) return false;
      assert s.elems != null; 
      assert t.elems != null; 
      for(int i = 0; i < s.card; i++)
        if ( ! Equals(s.elems[i], t.elems[i])) 
          return false;
      return true; 
    }
    public static bool operator <= (Sequence s, Sequence t){ 
      if (s==null) throw new ArgumentNullException("s");
      if (t==null) throw new ArgumentNullException("t");
      if (s.card > t.card) return false;
      assert s.elems != null; 
      assert t.elems != null; 
      for(int i = 0; i < s.card; i++)
        if ( ! Equals(s.elems[i], t.elems[i])) 
          return false;
      return true; 
    }
    public static bool operator > (Sequence s, Sequence t){ return t < s;}
    public static bool operator >= (Sequence s, Sequence t){ return t <= s;}

    //pure---------------------------------------------------------------
    [Pure]
    public bool Has(object x) {         // WS translate to tailrecursion 
      if (x == null)
        throw new MissingCase();
      assert this.elems != null; 
      for (int i = 0; i< card; i++)
        if (x.Equals(elems[i]))
          return true;
      return false;
    }

    // the index of the first occurrence of x in this sequence,
    // or -1 if x does not occur
    [Pure]
    public int IndexOf(object x) {
      if (x == null)
        throw new MissingCase();
      assert this.elems != null; 
      for (int i = 0; i< card; i++)
        if (x.Equals(elems[i]))
          return i;
      return -1;
    }

    // the index of the last occurrence of x in this sequence,
    // or -1 if x does not occur
    [Pure]
    public int LastIndexOf(object x) {
      if (x == null)
        throw new MissingCase();
      assert this.elems != null; 
      for (int i = card - 1; i >= 0; i--)
        if (x.Equals(elems[i]))
          return i;
      return -1;
    }

    public object Head() {assert this.elems != null;  return elems[0]; }
    public object Last() {assert this.elems != null;  return elems[card-1]; }

    public static Sequence Tail(Sequence! s) { 
      Sequence n = new Sequence(new Capacity(s.card-1));
      assert n.elems != null; 
      assert s.elems != null; 
      for (int i = 1; i< s.card; i++) n.elems[n.card++] = s.elems[i];
      return n;
    }

    public static Sequence Front(Sequence! s) { 
      Sequence n = new Sequence(new Capacity(s.card-1));
      assert n.elems != null; 
      assert s.elems != null; 
      for (int i = 0; i< s.card-1; i++) n.elems[n.card++] = s.elems[i];
      return n;
    }
    public static Sequence Concat(Sequence! s) { 
      Sequence n = new Sequence(new Capacity(s.card));
      assert n.elems != null; 
      assert s.elems != null; 
      for (int i = 0; i< s.card; i++) {
        Sequence t = (Sequence!) s.elems[i];
        assert t.elems != null; 
        for (int j = 0; j < t.card; j ++)
          n.Add(t.elems[j]);
      }
      return n;
    }

    public static Sequence Reverse(Sequence! s) { 
      Sequence n = new Sequence(new Capacity(s.card));
      assert n.elems != null; 
      assert s.elems != null; 
      for (int i = s.card-1; i>=0; i--) n.elems[n.card++] = s.elems[i];
      return n;
    }

    public static Sequence operator + (Sequence s, Sequence t)
    {
        if (s==null) throw new ArgumentNullException("s");
        if (t == null) throw new ArgumentNullException("t");
         return Append(t,s);
    }
    
    public static Sequence! Append(Sequence! s, Sequence! t) {       
      Sequence! n = new Sequence(new Capacity(s.card + t.card));
      assert n.elems != null; 
      assert s.elems != null; 
      assert t.elems != null; 
      for (int i = 0; i< s.card; i++) n.elems[n.card++] = s.elems[i];
      for (int i = 0; i< t.card; i++) n.elems[n.card++] = t.elems[i];
      return n;
    }
    public static Sequence Zip(Sequence! s, Sequence! t) { 
      int min = s.card<t.card ? s.card : t.card;
      Sequence n = new Sequence(new Capacity(min));
      assert n.elems != null; 
      assert s.elems != null; 
      assert t.elems != null; 
      for (int i = 0; i< min; i++) n.elems[n.card++] = new Tuple(s.elems[i], t.elems[i]);
      return n;
    }
    public static Tuple Unzip(Sequence! s) { 
      Sequence n0 = new Sequence(new Capacity(s.card));
      Sequence n1 = new Sequence(new Capacity(s.card));
      assert s.elems != null; 
      assert n0.elems != null; 
      assert n1.elems != null; 
      for (int i = 0; i< s.card; i++) {
        n0.elems[n0.card++] = ((!)((Tuple!)s.elems[i]).elems)[0];
        n1.elems[n1.card++] = ((!)((Tuple!)s.elems[i]).elems)[1];
      }
      return new Tuple(n0,n1);
    }

    public static Sequence FromTo(int from, int to) {       //WS hash the result! 
      if (from > to) return new Sequence();
      Sequence n = new Sequence(new Capacity(to-from+1));
      assert n.elems != null; 
      for (int i = from; i<= to; i++) 
        n.elems[n.card++] = i;
      return n;
    }
  
    public static Sequence FromStepTo(int from, int step, int to) {      
      Sequence n = new Sequence();
      int incr = step-from;
      if (incr >0)
        for (int i = from; i<= to; i+=incr) 
          n.Add(i); 
      else if (incr < 0)
        for (int i = to; i>= from; i-=incr) 
          n.Add(i); 
      return n;
    }
   
  }

}