www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

okasaki.mlish (41295B)


      1 #lang s-exp "../../../mlish.rkt"
      2 (require "../../rackunit-typechecking.rkt")
      3 
      4 ;; -----------------------------------------------------------------------------
      5 
      6 (define-type (Option A)
      7   [None]
      8   [Some A])
      9 
     10 ;; -----------------------------------------------------------------------------
     11 
     12 (define (div (n1 : Int) (n2 : Int) → Int)
     13   (if (< n1 n2)
     14     0
     15     (+ 1 (div (- n1 n2) n2))))
     16 
     17 (define (mod (n1 : Int) (n2 : Int) → Int)
     18   (if (< n1 n2)
     19     n1
     20     (mod (- n1 n2) n2)))
     21 
     22 ;; -----------------------------------------------------------------------------
     23 
     24 (define-type (List a)
     25   [Nil]
     26   [∷ a (List a)])
     27 
     28 (define (foldl [f : (→ A B A)] [acc : A] [x* : (List B)] → A)
     29   (match x* with
     30    [Nil -> acc]
     31    [∷ h t -> (foldl f (f acc h) t)]))
     32 
     33 (define (reverse [x* : (List A)] → (List A))
     34   (foldl (λ ([acc : (List A)] [x : A]) (∷ x acc)) Nil x*))
     35 
     36 (define (append [x* : (List A)] [y* : (List A)] → (List A))
     37   (match x* with
     38    [Nil -> y*]
     39    [∷ x x* ->
     40     (∷ x (append x* y*))]))
     41 
     42 (define (take [i : Int] [x* : (List A)] → (List A))
     43   (if (<= i 0)
     44     Nil
     45     (match x* with
     46      [Nil -> Nil]
     47      [∷ h t -> (∷ h (take (- i 1) t))])))
     48 
     49 (define (drop [i : Int] [x* : (List A)] → (List A))
     50   (if (<= i 0)
     51     x*
     52     (match x* with
     53      [Nil -> Nil]
     54      [∷ h t -> (drop (- i 1) t)])))
     55 
     56 ;; =============================================================================
     57 ;; === BatchedQueue
     58 
     59 (define-type (BatchedQueue A)
     60   [BQ (List A) (List A)])
     61 
     62 (define (bq-check [f : (List A)] [r : (List A)] → (BatchedQueue A))
     63   (match f with
     64    [Nil -> (BQ (reverse r) Nil)]
     65    [∷ h t -> (BQ f r)]))
     66 
     67 (define (bq-empty → (BatchedQueue A))
     68   (BQ Nil Nil))
     69 
     70 (define (bq-isEmpty [bq : (BatchedQueue A)] → Bool)
     71   (match bq with
     72    [BQ f r ->
     73     (match f with
     74      [Nil -> #t]
     75      [∷ h t -> #f])]))
     76 
     77 (define (bq-snoc [bq : (BatchedQueue A)] [x : A] → (BatchedQueue A))
     78   (match bq with
     79    [BQ f r -> (bq-check f (∷ x r))]))
     80 
     81 (define (bq-head [bq : (BatchedQueue A)] → (Option A))
     82   (match bq with
     83    [BQ f r ->
     84     (match f with
     85      [Nil -> None]
     86      [∷ h t -> (Some h)])]))
     87 
     88 (define (bq-tail [bq : (BatchedQueue A)] → (Option (BatchedQueue A)))
     89   (match bq with
     90    [BQ f* r* ->
     91     (match f* with
     92      [Nil -> None]
     93      [∷ x f* ->
     94       (Some (bq-check f* r*))])]))
     95 
     96 (define (list->bq [x* : (List A)] → (BatchedQueue A))
     97   (foldl
     98     ;bq-snoc ;; TODO
     99     (λ ([q : (BatchedQueue A)] [x : A]) (bq-snoc q x))
    100     (bq-empty) x*))
    101 
    102 ;; -----------------------------------------------------------------------------
    103 
    104 (define digit*
    105   (∷ 1 (∷ 2 (∷ 3 (∷ 4 (∷ 5 (∷ 6 (∷ 7 (∷ 8 (∷ 9 Nil))))))))))
    106 
    107 (define abc
    108   (∷ "A" (∷ "B" (∷ "C" Nil))))
    109 
    110 (define def
    111   (∷ "D" (∷ "E" (∷ "F" Nil))))
    112 
    113 (define sample-bq (list->bq digit*))
    114 
    115 (check-type
    116   (bq-isEmpty (BQ (Nil {Bool}) (Nil {Bool})))
    117   ;(bq-isEmpty (bq-empty {Bool}))
    118   ;(bq-isEmpty (BQ (Nil {Bool}) (Nil {Bool}))) ;; TODO
    119   : Bool
    120   ⇒ #t)
    121 
    122 (check-type
    123   (bq-isEmpty sample-bq)
    124   : Bool
    125   ⇒ #f)
    126 
    127 (check-type
    128   (bq-head sample-bq)
    129   : (Option Int)
    130   ⇒ (Some 1))
    131 
    132 (check-type
    133   (bq-head (bq-snoc sample-bq 10))
    134   : (Option Int)
    135   ⇒ (Some 1))
    136 
    137 (define (>> [f : (→ A (Option A))] [x : (Option A)] → (Option A))
    138   (match x with
    139    [None -> None]
    140    [Some y -> (f y)]))
    141 
    142 (check-type
    143   (match (bq-tail sample-bq) with
    144    [None -> None]
    145    [Some bq -> (bq-head bq)])
    146   : (Option Int)
    147   ⇒ (Some 2))
    148 
    149 ;; TODO
    150 ;(check-type
    151 ;  (>> bq-head (>> bq-tail (>> bq-tail (>> bq-tail (Some sample-bq)))))
    152 ;  : (Option Int)
    153 ;  ⇒ (Some 4))
    154 
    155 ;; =============================================================================
    156 ;; === Bankers Queue
    157 
    158 (define-type (BankersQueue A)
    159   (Bank Int (List A) Int (List A)))
    160 
    161 (define (bank-check [lenf : Int] [f : (List A)] [lenr : Int] [r : (List A)] → (BankersQueue A))
    162   (if (<= lenr lenf)
    163     (Bank lenf f lenr r)
    164     (Bank (+ lenf lenr) (append f (reverse r)) 0 Nil)))
    165 
    166 (define (bank-empty → (BankersQueue A))
    167   (Bank 0 Nil 0 Nil))
    168 
    169 (define (bank-isEmpty [bq : (BankersQueue A)] → Bool)
    170   (match bq with
    171    [Bank lenf f lenr r -> (= 0 lenf)]))
    172 
    173 (define (bank-snoc [bq : (BankersQueue A)] [x : A] → (BankersQueue A))
    174   (match bq with
    175    [Bank lenf f lenr r -> (bank-check lenf f (+ 1 lenr) (∷ x r))]))
    176 
    177 (define (bank-head [bq : (BankersQueue A)] → (Option A))
    178   (match bq with
    179    [Bank lenf f lenr r ->
    180     (match f with
    181      [Nil -> None]
    182      [∷ h t -> (Some h)])]))
    183 
    184 (define (bank-tail [bq : (BankersQueue A)] → (Option (BankersQueue A)))
    185   (match bq with
    186    [Bank lenf f lenr r ->
    187     (match f with
    188      [Nil -> None]
    189      [∷ h t -> (Some (bank-check (- lenf 1) t lenr r))])]))
    190 
    191 ;; -----------------------------------------------------------------------------
    192 
    193 (define sample-bank
    194   (foldl (λ ([acc : (BankersQueue Int)] [x : Int]) (bank-snoc acc x)) (bank-empty) digit*))
    195 
    196 (check-type
    197   (bank-isEmpty (Bank 0 (Nil {Int}) 0 (Nil {Int})))
    198   : Bool
    199   ⇒ #t)
    200 
    201 (check-type
    202   (bank-isEmpty sample-bank)
    203   : Bool
    204   ⇒ #f)
    205 
    206 (check-type
    207   (bank-head sample-bank)
    208   : (Option Int)
    209   ⇒ (Some 1))
    210 
    211 (check-type
    212   (bank-head (bank-snoc sample-bank 10))
    213   : (Option Int)
    214   ⇒ (Some 1))
    215 
    216 (check-type
    217   (match (bank-tail sample-bank) with
    218    [None -> None]
    219    [Some bank -> (bank-head bank)])
    220   : (Option Int)
    221   ⇒ (Some 2))
    222 
    223 ;; =============================================================================
    224 ;; === Physicists Queue
    225 
    226 (define-type (PhysicistsQueue A)
    227   (PQ (List A) Int (List A) Int (List A)))
    228 
    229 (define (pq-check [w : (List A)] [lenf : Int] [f : (List A)] [lenr : Int] [r : (List A)] → (PhysicistsQueue A))
    230   (if (<= lenr lenf)
    231     (pq-checkw w lenf f lenr r)
    232     (pq-checkw f (+ lenf lenr) (append f (reverse r)) 0 Nil)))
    233 
    234 (define (pq-checkw [w : (List A)] [lenf : Int] [f : (List A)] [lenr : Int] [r : (List A)] → (PhysicistsQueue A))
    235   (match w with
    236    [Nil -> (PQ f lenf f lenr r)]
    237    [∷ h t -> (PQ w lenf f lenr r)]))
    238 
    239 (define (pq-empty → (PhysicistsQueue A))
    240   (PQ Nil 0 Nil 0 Nil))
    241 
    242 (define (pq-isEmpty [pq : (PhysicistsQueue A)] → Bool)
    243   (match pq with
    244    [PQ w lenf f lenr r ->
    245     (= lenf 0)]))
    246 
    247 (define (pq-snoc [pq : (PhysicistsQueue A)] [x : A] → (PhysicistsQueue A))
    248   (match pq with
    249    [PQ w lenf f lenr r -> (pq-check w lenf f (+ 1 lenr) (∷ x r))]))
    250 
    251 (define (pq-head [pq : (PhysicistsQueue A)] → (Option A))
    252   (match pq with
    253    [PQ w lenf f lenr r ->
    254     (match w with
    255      [Nil -> None]
    256      [∷ w w* -> (Some w)])]))
    257 
    258 (define (pq-tail [pq : (PhysicistsQueue A)] → (Option (PhysicistsQueue A)))
    259   (match pq with
    260    [PQ w lenf f lenr r ->
    261     (match w with
    262      [Nil -> None]
    263      [∷ x w* ->
    264       (match f with
    265        [Nil -> None] ;; Never happens
    266        [∷ f f* -> (Some (pq-check w* (- lenf 1) f* lenr r))])])]))
    267 
    268 ;; -----------------------------------------------------------------------------
    269 
    270 (define sample-pq
    271   (foldl (λ ([acc : (PhysicistsQueue Int)] [x : Int]) (pq-snoc acc x)) (pq-empty) digit*))
    272 
    273 (check-type
    274   (pq-isEmpty (PQ (Nil {Int}) 0 (Nil {Int}) 0 (Nil {Int})))
    275   : Bool
    276   ⇒ #t)
    277 
    278 (check-type
    279   (pq-isEmpty sample-pq)
    280   : Bool
    281   ⇒ #f)
    282 
    283 (check-type
    284   (pq-head sample-pq)
    285   : (Option Int)
    286   ⇒ (Some 1))
    287 
    288 (check-type
    289   (pq-head (pq-snoc sample-pq 10))
    290   : (Option Int)
    291   ⇒ (Some 1))
    292 
    293 (check-type
    294   (match (pq-tail sample-pq) with
    295    [None -> None]
    296    [Some pq -> (pq-head pq)])
    297   : (Option Int)
    298   ⇒ (Some 2))
    299 
    300 ;; =============================================================================
    301 ;; === Hood-Melville Queue
    302 
    303 (define-type (RotationState A)
    304   [Idle]
    305   [Reversing Int (List A) (List A) (List A) (List A)]
    306   [Appending Int (List A) (List A)]
    307   [Done (List A)])
    308 
    309 (define-type (HoodMelvilleQueue A)
    310   [HM Int (List A) (RotationState A) Int (List A)])
    311 
    312 (define (hm-exec [rs : (RotationState A)] → (RotationState A))
    313   (match rs with
    314    [Idle -> rs]
    315    [Done x -> rs]
    316    [Appending ok f* r* ->
    317     (if (= ok 0)
    318       (Done r*)
    319       (match f* with
    320        [Nil -> rs]
    321        [∷ x f* ->
    322         (Appending (- ok 1) f* (∷ x r*))]))]
    323    [Reversing ok f1* f2* r1* r2* ->
    324     (match f1* with
    325      [Nil ->
    326       (match r1* with
    327        [Nil -> rs]
    328        [∷ y r1* ->
    329         (match r1* with
    330          [Nil -> (Appending ok f2* (∷ y r2*))]
    331          [∷ a b -> rs])])]
    332      [∷ x f1* ->
    333       (match r1* with
    334        [Nil -> rs]
    335        [∷ y r1* ->
    336         (Reversing (+ ok 1) f1* (∷ x f2*) r1* (∷ y r2*))])])]))
    337 
    338 (define (hm-invalidate [rs : (RotationState A)] → (RotationState A))
    339   (match rs with
    340    [Reversing ok f1* f2* r1* r2* ->
    341     (Reversing (- ok 1) f1* f2* r1* r2*)]
    342    [Appending ok f* r* ->
    343      (if (= 0 ok)
    344        (match r* with
    345         [Nil -> rs]
    346         [∷ x r* -> (Done r*)])
    347        (Appending (- ok 1) f* r*))]
    348    [Done x -> rs]
    349    [Idle -> rs]))
    350 
    351 (define (hm-exec2 [lenf : Int] [f* : (List A)] [state : (RotationState A)] [lenr : Int] [r : (List A)] → (HoodMelvilleQueue A))
    352   ((λ ([newstate : (RotationState A)])
    353      (match newstate with
    354       [Done newf -> (HM lenf newf Idle lenr r)]
    355       [Idle                -> (HM lenf f* newstate lenr r)]
    356       [Appending a b c     -> (HM lenf f* newstate lenr r)]
    357       [Reversing a b c d e -> (HM lenf f* newstate lenr r)]))
    358   (hm-exec (hm-exec state))))
    359 
    360 (define (hm-check [lenf : Int] [f* : (List A)] [state : (RotationState A)] [lenr : Int] [r* : (List A)] → (HoodMelvilleQueue A))
    361   (if (<= lenr lenf)
    362     (hm-exec2 lenf f* state lenr r*)
    363     (hm-exec2 (+ lenf lenr) f* (Reversing 0 f* Nil r* Nil) 0 Nil)))
    364 
    365 (define (hm-empty → (HoodMelvilleQueue A))
    366   (HM 0 Nil Idle 0 Nil))
    367 
    368 (define (hm-isEmpty [hm : (HoodMelvilleQueue A)] → Bool)
    369   (match hm with
    370    [HM lenf b c d e ->
    371     (= lenf 0)]))
    372 
    373 (define (hm-snoc [hm : (HoodMelvilleQueue A)] [x : A] → (HoodMelvilleQueue A))
    374   (match hm with
    375    [HM lenf f state lenr r -> (hm-check lenf f state (+ lenr 1) (∷ x r))]))
    376 
    377 (define (hm-head [hm : (HoodMelvilleQueue A)] → (Option A))
    378   (match hm with
    379    [HM a f b c d ->
    380     (match f with
    381      [Nil -> None]
    382      [∷ x f* -> (Some x)])]))
    383 
    384 (define (hm-tail [hm : (HoodMelvilleQueue A)] → (Option (HoodMelvilleQueue A)))
    385   (match hm with
    386    [HM lenf f state lenr r ->
    387     (match f with
    388      [Nil -> None]
    389      [∷ x f* -> (Some (hm-check (- lenf 1) f* (hm-invalidate state) lenr r))])]))
    390 
    391 ;; -----------------------------------------------------------------------------
    392 
    393 (define sample-hm
    394   (foldl (λ ([acc : (HoodMelvilleQueue Int)] [x : Int]) (hm-snoc acc x)) (hm-empty) digit*))
    395 
    396 (check-type
    397   (hm-isEmpty (HM 0 (Nil {Int}) Idle 0 (Nil {Int})))
    398   : Bool
    399   ⇒ #t)
    400 
    401 (check-type
    402   (hm-isEmpty sample-hm)
    403   : Bool
    404   ⇒ #f)
    405 
    406 (check-type
    407   (hm-head sample-hm)
    408   : (Option Int)
    409   ⇒ (Some 1))
    410 
    411 (check-type
    412   (hm-head (hm-snoc sample-hm 10))
    413   : (Option Int)
    414   ⇒ (Some 1))
    415 
    416 (check-type
    417   (match (hm-tail sample-hm) with
    418    [None -> None]
    419    [Some hm -> (hm-head hm)])
    420   : (Option Int)
    421   ⇒ (Some 2))
    422 
    423 ;; =============================================================================
    424 ;; === Bootstrapped Queue
    425 
    426 (define-type (BootstrappedQueue a)
    427   [E]
    428   [Q Int (List a) (BootstrappedQueue (List a)) Int (List a)])
    429 
    430 (define (bs-checkQ [lenfm : Int] [f : (List A)] [m : (BootstrappedQueue (List A))] [lenr : Int] [r : (List A)] → (BootstrappedQueue A))
    431   (if (<= lenr lenfm)
    432     (bs-checkF lenfm f m lenr r)
    433     (bs-checkF (+ lenfm lenr) f (bs-snoc m (reverse r)) 0 Nil)))
    434 
    435 (define (bs-checkF [lenfm : Int] [f : (List A)] [m : (BootstrappedQueue (List A))] [lenr : Int] [r : (List A)] → (BootstrappedQueue A))
    436   (match f with
    437    [Nil ->
    438     (match m with
    439      [E -> E]
    440      [Q _a _b _c _d _e ->
    441       (match (bs-head m) with
    442        [None -> E]
    443        [Some hd ->
    444         (match (bs-tail m) with
    445          [None -> E]
    446          [Some tl ->
    447           (Q lenfm hd tl lenr r)])])])]
    448    [∷ _f _f* ->
    449     (Q lenfm f m lenr r)]))
    450 
    451 (define (bs-empty → (BootstrappedQueue A))
    452   (Q 0 Nil E 0 Nil))
    453 
    454 (define (bs-isEmpty [m : (BootstrappedQueue A)] → Bool)
    455   (match m with
    456    [E -> #t]
    457    [Q a b c d e -> #f]))
    458 
    459 (define (bs-snoc [m : (BootstrappedQueue A)] [x : A] → (BootstrappedQueue A))
    460   (match m with
    461    [E -> (Q 1 (∷ x Nil) E 0 Nil)]
    462    [Q lenfm f m lenr r -> (bs-checkQ lenfm f m (+ 1 lenr) (∷ x r))]))
    463 
    464 (define (bs-head [m : (BootstrappedQueue A)] → (Option A))
    465   (match m with
    466    [E -> None]
    467    [Q lenfm f m lenr r ->
    468     (match f with
    469      [Nil -> None]
    470      [∷ x f* -> (Some x)])]))
    471 
    472 (define (bs-tail [m : (BootstrappedQueue A)] → (Option (BootstrappedQueue A)))
    473   (match m with
    474    [E -> None]
    475    [Q lenfm f m lenr r ->
    476     (match f with
    477      [Nil -> None]
    478      [∷ _x f* -> (Some (bs-checkQ (- lenfm 1) f* m lenr r))])]))
    479 
    480 ;; -----------------------------------------------------------------------------
    481 
    482 (define sample-bs
    483   (foldl (λ ([acc : (BootstrappedQueue Int)] [x : Int]) (bs-snoc acc x)) (bs-empty) digit*))
    484 
    485 (check-type
    486   (bs-isEmpty (E {Int}))
    487   : Bool
    488   ⇒ #t)
    489 
    490 (check-type
    491   (bs-isEmpty sample-bs)
    492   : Bool
    493   ⇒ #f)
    494 
    495 (check-type
    496   (bs-head sample-bs)
    497   : (Option Int)
    498   ⇒ (Some 1))
    499 
    500 (check-type
    501   (bs-head (bs-snoc sample-bs 10))
    502   : (Option Int)
    503   ⇒ (Some 1))
    504 
    505 (check-type
    506   (match (bs-tail sample-bs) with
    507    [None -> None]
    508    [Some bs -> (bs-head bs)])
    509   : (Option Int)
    510   ⇒ (Some 2))
    511 
    512 ;; =============================================================================
    513 ;; === Implicit Queue
    514 
    515 (define-type (Digit A)
    516   [Zero]
    517   [One A]
    518   [Two A A])
    519 
    520 (define-type (ImplicitQueue A)
    521   [Shallow (Digit A)]
    522   [Deep (Digit A) (ImplicitQueue (× A A)) (Digit A)])
    523 
    524 (define (iq-empty → (ImplicitQueue A))
    525   (Shallow Zero))
    526 
    527 (define (iq-isEmpty [iq : (ImplicitQueue A)] → Bool)
    528   (match iq with
    529    [Shallow d ->
    530     (match d with
    531      [Zero -> #t]
    532      [One x -> #f]
    533      [Two x y -> #f])]
    534    [Deep a b c -> #f]))
    535 
    536 (define (iq-snoc [iq : (ImplicitQueue A)] [y : A] → (ImplicitQueue A))
    537   (match iq with
    538    [Shallow d ->
    539     (match d with
    540      [Zero -> (Shallow (One y))]
    541      [One x -> (Deep (Two x y) (Shallow Zero) Zero)]
    542      [Two x y -> ;; Error
    543       (Shallow Zero)])]
    544    [Deep f m d ->
    545     (match d with
    546      [Zero -> (Deep f m (One y))]
    547      [One x -> (Deep f (iq-snoc m (tup x y)) Zero)]
    548      [Two x y -> (Shallow Zero)])])) ;; Error
    549 
    550 (define (iq-head [iq : (ImplicitQueue A)] → (Option A))
    551   (match iq with
    552    [Shallow d ->
    553     (match d with
    554      [Zero -> None]
    555      [One x -> (Some x)]
    556      [Two x y -> (Some x)])] ;; Error
    557    [Deep d m r ->
    558     (match d with
    559      [Zero -> None] ;; Error
    560      [One x -> (Some x)]
    561      [Two x y -> (Some x)])]))
    562 
    563 (define (iq-tail [iq : (ImplicitQueue A)] → (Option (ImplicitQueue A)))
    564   (match iq with
    565    [Shallow d ->
    566     (match d with
    567      [Zero -> None]
    568      [One x -> (Some (Shallow Zero))]
    569      [Two x y -> None])] ;; Error
    570    [Deep d m r ->
    571     (match d with
    572      [Zero -> None] ;; Error
    573      [One x ->
    574       (match (iq-head m) with
    575        [None -> (Some (Shallow r))]
    576        [Some yz ->
    577         (match yz with
    578          [y z ->
    579           (match (iq-tail m) with
    580            [None -> None]
    581            [Some tl ->
    582             (Some (Deep (Two y z) tl r))])])])]
    583      [Two x y -> (Some (Deep (One y) m r))])]))
    584 
    585 ;; -----------------------------------------------------------------------------
    586 
    587 (define sample-iq
    588   (foldl (λ ([acc : (ImplicitQueue Int)] [x : Int]) (iq-snoc acc x)) (iq-empty) digit*))
    589 
    590 (check-type
    591   (iq-isEmpty (Shallow (Zero {Int})))
    592   : Bool
    593   ⇒ #t)
    594 
    595 (check-type
    596   (iq-isEmpty sample-iq)
    597   : Bool
    598   ⇒ #f)
    599 
    600 (check-type
    601   (iq-head sample-iq)
    602   : (Option Int)
    603   ⇒ (Some 1))
    604 
    605 (check-type
    606   (iq-head (iq-snoc sample-iq 10))
    607   : (Option Int)
    608   ⇒ (Some 1))
    609 
    610 (check-type
    611   (match (iq-tail sample-iq) with
    612    [None -> None]
    613    [Some iq -> (iq-head iq)])
    614   : (Option Int)
    615   ⇒ (Some 2))
    616 
    617 ;; =============================================================================
    618 ;; === Bankers Deque
    619 
    620 (define-type (BankersDeque A)
    621   [BD Int (List A) Int (List A)])
    622 
    623 (define c 3)
    624 
    625 (define (bd-check [lenf : Int] [f : (List A)] [lenr : Int] [r : (List A)] → (BankersDeque A))
    626   (if (> lenf (+ c (+ lenr 1)))
    627     (let* ([i (div (+ lenf lenr) 2)]
    628            [j (- (+ lenf lenr) i)]
    629            [r2 (take j r)]
    630            [f2 (append f (reverse (drop j r)))])
    631       (BD i f2 j r2))
    632     (if (> lenr (+ 1 (* c lenf)))
    633       (let* ([j (div (+ lenf lenr) 2)]
    634              [i (- (+ lenr lenf) j)]
    635              [r2 (take j r)]
    636              [f2 (append f (reverse (drop j r)))])
    637         (BD i f2 j r2))
    638       (BD lenf f lenr r))))
    639 
    640 (define (bd-empty → (BankersDeque A))
    641   (BD 0 Nil 0 Nil))
    642 
    643 (define (bd-isEmpty [bd : (BankersDeque A)] → Bool)
    644   (match bd with
    645    [BD lenf f lenr r -> (= 0 (+ lenf lenr))]))
    646 
    647 (define (bd-cons [x : A] [bd : (BankersDeque A)] → (BankersDeque A))
    648   (match bd with
    649    [BD lenf f lenr r -> (bd-check (+ lenf 1) (∷ x f) lenr r)]))
    650 
    651 (define (bd-head [bd : (BankersDeque A)] → (Option A))
    652   (match bd with
    653    [BD lenf f lenr r ->
    654     (match f with
    655      [Nil -> None]
    656      [∷ x f2 -> (Some x)])]))
    657 
    658 (define (bd-tail [bd : (BankersDeque A)] → (Option (BankersDeque A)))
    659   (match bd with
    660    [BD lenf f lenr r ->
    661     (match f with
    662      [Nil -> None]
    663      [∷ x f2 -> (Some (bd-check (- lenf 1) f2 lenr r))])]))
    664 
    665 (define (bd-snoc [bd : (BankersDeque A)] [x : A] → (BankersDeque A))
    666   (match bd with
    667    [BD lenf f lenr r -> (bd-check lenf f (+ lenr 1) (∷ x r))]))
    668 
    669 (define (bd-last [bd : (BankersDeque A)] → (Option A))
    670   (match bd with
    671    [BD lenf f lenr r ->
    672     (match r with
    673      [Nil -> None]
    674      [∷ x r2 -> (Some x)])]))
    675 
    676 (define (bd-init [bd : (BankersDeque A)] → (Option (BankersDeque A)))
    677   (match bd with
    678    [BD lenf f lenr r ->
    679     (match r with
    680      [Nil -> None]
    681      [∷ x r -> (Some (bd-check lenf f (- lenr 1) r))])]))
    682 
    683 ;; -----------------------------------------------------------------------------
    684 
    685 (define sample-bd
    686   (foldl (λ ([acc : (BankersDeque Int)] [x : Int]) (bd-snoc acc x)) (bd-empty) digit*))
    687 
    688 (check-type
    689   (bd-isEmpty (BD 0 (Nil {Int}) 0 (Nil {Int})))
    690   : Bool
    691   ⇒ #t)
    692 
    693 (check-type
    694   (bd-isEmpty sample-bd)
    695   : Bool
    696   ⇒ #f)
    697 
    698 (check-type
    699   (bd-head sample-bd)
    700   : (Option Int)
    701   ⇒ (Some 1))
    702 
    703 (check-type
    704   (bd-last sample-bd)
    705   : (Option Int)
    706   ⇒ (Some 9))
    707 
    708 (check-type
    709   (bd-head (bd-snoc sample-bd 10))
    710   : (Option Int)
    711   ⇒ (Some 1))
    712 
    713 (check-type
    714   (bd-head (bd-cons 10 sample-bd))
    715   : (Option Int)
    716   ⇒ (Some 10))
    717 
    718 (check-type
    719   (match (bd-tail sample-bd) with
    720    [None -> None]
    721    [Some bd -> (bd-head bd)])
    722   : (Option Int)
    723   ⇒ (Some 2))
    724 
    725 (check-type
    726   (match (bd-init sample-bd) with
    727    [None -> None]
    728    [Some bd -> (bd-last bd)])
    729   : (Option Int)
    730   ⇒ (Some 8))
    731 
    732 ;; =============================================================================
    733 ;; === Simple Catenable Deque
    734 
    735 (define-type (SimpleCatDeque a)
    736   [SShallow (BankersDeque a)]
    737   [SDeep (BankersDeque a) (SimpleCatDeque (BankersDeque a)) (BankersDeque a)])
    738 
    739 (define (bd-tooSmall [d : (BankersDeque A)] → Bool)
    740   (if (bd-isEmpty d)
    741     #t
    742     (match (bd-tail d) with
    743      [None -> #t]
    744      [Some d -> (bd-isEmpty d)])))
    745 
    746 (define (bd-dappendL [d1 : (BankersDeque A)] [d2 : (BankersDeque A)] → (BankersDeque A))
    747   (match (bd-head d1) with
    748    [None -> d2]
    749    [Some h -> (bd-cons h d2)]))
    750 
    751 (define (bd-dappendR [d1 : (BankersDeque A)] [d2 : (BankersDeque A)] → (BankersDeque A))
    752   (match (bd-head d2) with
    753    [None -> d1]
    754    [Some h -> (bd-snoc d1 h)]))
    755 
    756 (define (scd-empty → (SimpleCatDeque A))
    757   (SShallow (bd-empty)))
    758 
    759 (define (scd-isEmpty [scd : (SimpleCatDeque A)] → Bool)
    760   (match scd with
    761    [SShallow d -> (bd-isEmpty d)]
    762    [SDeep a b c -> #f]))
    763 
    764 (define (scd-cons [x : A] [scd : (SimpleCatDeque A)] → (SimpleCatDeque A))
    765   (match scd with
    766    [SShallow d -> (SShallow (bd-cons x d))]
    767    [SDeep f m r -> (SDeep (bd-cons x f) m r)]))
    768 
    769 (define (scd-snoc [scd : (SimpleCatDeque A)] [x : A] → (SimpleCatDeque A))
    770   (match scd with
    771    [SShallow d -> (SShallow (bd-snoc d x))]
    772    [SDeep f m r -> (SDeep f m (bd-snoc f x))]))
    773 
    774 (define (scd-head [scd : (SimpleCatDeque A)] → (Option A))
    775   (match scd with
    776    [SShallow d -> (bd-head d)]
    777    [SDeep f m r -> (bd-head f)]))
    778 
    779 (define (scd-last [scd : (SimpleCatDeque A)] → (Option A))
    780   (match scd with
    781    [SShallow d -> (bd-last d)]
    782    [SDeep f m r -> (bd-last r)]))
    783 
    784 (define (scd-tail [scd : (SimpleCatDeque A)] → (Option (SimpleCatDeque A)))
    785   (match scd with
    786    [SShallow d ->
    787     (match (bd-tail d) with
    788      [None -> None]
    789      [Some t -> (Some (SShallow t))])]
    790    [SDeep f m r ->
    791     (match (bd-tail f) with
    792      [None -> None]
    793      [Some f2 ->
    794       (if (not (bd-tooSmall f2))
    795         (Some (SDeep f2 m r))
    796         (if (scd-isEmpty m)
    797           (Some (SShallow (bd-dappendL f2 r)))
    798           (match (scd-head m) with
    799            [None -> None]
    800            [Some hm ->
    801             (match (scd-tail m) with
    802              [None -> None]
    803              [Some tm ->
    804               (Some (SDeep (bd-dappendL f2 hm) tm r))])])))])]))
    805 
    806 (define (scd-init [scd : (SimpleCatDeque A)] → (Option (SimpleCatDeque A)))
    807   (match scd with
    808    [SShallow d ->
    809     (match (bd-init d) with
    810      [None -> None]
    811      [Some t -> (Some (SShallow t))])]
    812    [SDeep f m r ->
    813     (match (bd-init r) with
    814      [None -> None]
    815      [Some r2 ->
    816       (if (not (bd-tooSmall r2))
    817         (Some (SDeep f m r2))
    818         (if (scd-isEmpty m)
    819           (Some (SShallow (bd-dappendR r r2)))
    820           (match (scd-last m) with
    821            [None -> None]
    822            [Some lm ->
    823             (match (scd-init m) with
    824              [None -> None]
    825              [Some im ->
    826               (Some (SDeep f im (bd-dappendR lm r2)))])])))])]))
    827 
    828 (define (scd-++ [scd1 : (SimpleCatDeque A)] [scd2 : (SimpleCatDeque A)] → (SimpleCatDeque A))
    829   (match scd1 with
    830    [SShallow d1 ->
    831     (match scd2 with
    832      [SShallow d2 ->
    833       (if (bd-tooSmall d1)
    834         (SShallow (bd-dappendL d1 d2))
    835         (if (bd-tooSmall d2)
    836           (SShallow (bd-dappendR d1 d2))
    837           (SDeep d1 (scd-empty) d2)))]
    838      [SDeep f m r ->
    839       (if (bd-tooSmall d1)
    840         (SDeep (bd-dappendL d1 f) m r)
    841         (SDeep d1 (scd-cons f m) r))])]
    842    [SDeep f1 m1 r1 ->
    843     (match scd2 with
    844      [SShallow d2 ->
    845       (if (bd-tooSmall d2)
    846         (SDeep f1 m1 (bd-dappendR r1 d2))
    847         (SDeep f1 (scd-snoc m1 r1) d2))]
    848      [SDeep f2 m2 r2 ->
    849       (SDeep f1 (scd-++ (scd-snoc m1 f1) (scd-cons r1 m2)) r2)])]))
    850 
    851 (define (scd->list [scd : (SimpleCatDeque A)] → (List A))
    852   (match (scd-head scd) with
    853    [None -> Nil]
    854    [Some hd ->
    855     (match (scd-tail scd) with
    856      [None -> (∷ hd Nil)]
    857      [Some tl -> (∷ hd (scd->list tl))])]))
    858 
    859 ;; -----------------------------------------------------------------------------
    860 
    861 (define sample-scd
    862   (foldl (λ ([acc : (SimpleCatDeque Int)] [x : Int]) (scd-snoc acc x)) (scd-empty) digit*))
    863 
    864 (define (empty-sample → (SimpleCatDeque Int))
    865   (scd-empty))
    866 
    867 (check-type
    868   (scd-isEmpty (SShallow (BD 0 (Nil {Int}) 0 (Nil {Int}))))
    869   : Bool
    870   ⇒ #t)
    871 
    872 (check-type
    873   (scd-isEmpty sample-scd)
    874   : Bool
    875   ⇒ #f)
    876 
    877 (check-type
    878   (scd-head sample-scd)
    879   : (Option Int)
    880   ⇒ (Some 1))
    881 
    882 (check-type
    883   (scd-head (empty-sample))
    884   : (Option Int)
    885   ⇒ None)
    886 
    887 (check-type
    888   (scd-last sample-scd)
    889   : (Option Int)
    890   ⇒ (Some 9))
    891 
    892 (check-type
    893   (scd-last (empty-sample))
    894   : (Option Int)
    895   ⇒ None)
    896 
    897 (check-type
    898   (scd-head (scd-snoc sample-scd 10))
    899   : (Option Int)
    900   ⇒ (Some 1))
    901 
    902 (check-type
    903   (scd-head (scd-cons 10 sample-scd))
    904   : (Option Int)
    905   ⇒ (Some 10))
    906 
    907 (check-type
    908   (match (scd-tail sample-scd) with
    909    [None -> None]
    910    [Some scd -> (scd-head scd)])
    911   : (Option Int)
    912   ⇒ (Some 2))
    913 
    914 (check-type
    915   (scd-tail (empty-sample))
    916   : (Option (SimpleCatDeque Int))
    917   ⇒ None)
    918 
    919 (check-type
    920   (match (scd-init sample-scd) with
    921    [None -> None]
    922    [Some scd -> (scd-last scd)])
    923   : (Option Int)
    924   ⇒ (Some 8))
    925 
    926 (check-type
    927   (scd-init (empty-sample))
    928   : (Option (SimpleCatDeque Int))
    929   ⇒ None)
    930 
    931 (check-type
    932   (match (scd-head (scd-++ (scd-cons 1 (scd-empty)) (empty-sample))) with
    933    [None -> None]
    934    [Some i -> (Some i)])
    935   : (Option Int)
    936   ⇒ (Some 1))
    937 
    938 (check-type
    939   (match (scd-head (scd-++ (empty-sample) (scd-cons 2 (scd-empty)))) with
    940    [None -> None]
    941    [Some scd -> (Some scd)])
    942   : (Option Int)
    943   ⇒ (Some 2))
    944 
    945 (check-type
    946   (match (scd-tail (scd-++ (scd-cons 1 (scd-empty)) (scd-cons 2 (scd-empty)))) with
    947    [None -> None]
    948    [Some scd -> (scd-head scd)])
    949   : (Option Int)
    950   ⇒ (Some 2))
    951 
    952 (define (scd-ref [n : Int] [scd : (SimpleCatDeque A)] → (Option A))
    953   (if (< n 1)
    954     (scd-head scd)
    955     (match (scd-tail scd) with
    956      [None -> None]
    957      [Some tl -> (scd-ref (- n 1) tl)])))
    958 
    959 (check-type
    960   (scd-ref 2
    961     (scd-++
    962       (scd-++ (scd-cons 1 (scd-empty)) (scd-cons 2 (scd-empty)))
    963       (scd-++ (scd-cons 3 (scd-empty)) (scd-cons 4 (scd-empty)))))
    964   : (Option Int)
    965   ⇒ (Some 3))
    966 
    967 (check-type
    968   (scd-ref 0
    969     (scd-++
    970       (scd-++ (scd-cons 1 (scd-empty)) (scd-cons 2 (scd-empty)))
    971       (scd-++ (scd-cons 3 (scd-empty)) (scd-cons 4 (scd-empty)))))
    972   : (Option Int)
    973   ⇒ (Some 1))
    974 
    975 (check-type
    976   (scd->list
    977     (scd-++
    978       (scd-++ (scd-cons 1 (scd-empty)) (scd-cons 2 (scd-empty)))
    979       (scd-++ (scd-cons 3 (scd-empty)) (scd-cons 4 (scd-empty)))))
    980   : (List Int)
    981   ⇒ (∷ 1 (∷ 2 (∷ 3 (∷ 4 Nil)))))
    982 
    983 (check-type
    984   (scd-ref 1
    985     (scd-++
    986       (scd-++
    987         (scd-++ (scd-cons 1 (scd-empty)) (scd-cons 2 (scd-empty)))
    988         (scd-++ (scd-cons 3 (scd-empty)) (scd-cons 4 (scd-empty))))
    989       (scd-++
    990         (scd-++ (scd-cons 5 (scd-empty)) (scd-cons 6 (scd-empty)))
    991         (scd-++ (scd-cons 7 (scd-empty)) (scd-cons 8 (scd-empty))))))
    992   : (Option Int)
    993   ⇒ (Some 2))
    994 
    995 (check-type
    996   (scd-ref 3
    997     (scd-++
    998       (scd-++
    999         (scd-++ (scd-cons 1 (scd-empty)) (scd-cons 2 (scd-empty)))
   1000         (scd-++ (scd-cons 3 (scd-empty)) (scd-cons 4 (scd-empty))))
   1001       (scd-++
   1002         (scd-++ (scd-cons 5 (scd-empty)) (scd-cons 6 (scd-empty)))
   1003         (scd-++ (scd-cons 7 (scd-empty)) (scd-cons 8 (scd-empty))))))
   1004   : (Option Int)
   1005   ⇒ (Some 4))
   1006 
   1007 ;; TODO this is a bug, but at least we have the right types in MLish
   1008 ;(check-type
   1009 ;  (scd->list
   1010 ;    (scd-++
   1011 ;      (scd-++
   1012 ;        (scd-++ (scd-cons 1 (scd-empty)) (scd-cons 2 (scd-empty)))
   1013 ;        (scd-++ (scd-cons 3 (scd-empty)) (scd-cons 4 (scd-empty))))
   1014 ;      (scd-++
   1015 ;        (scd-++ (scd-cons 5 (scd-empty)) (scd-cons 6 (scd-empty)))
   1016 ;        (scd-++ (scd-cons 7 (scd-empty)) (scd-cons 8 (scd-empty))))))
   1017 ;  : (List Int)
   1018 ;  ⇒ (∷ 1 (∷ 2 (∷ 3 (∷ 4 (∷ 5 (∷ 6 (∷ 7 (∷ 8 Nil)))))))))
   1019 
   1020 ;; =============================================================================
   1021 ;; === Binary Random Access List
   1022 
   1023 (define-type (Tree A)
   1024   (Leaf A)
   1025   (Node Int (Tree A) (Tree A)))
   1026 
   1027 (define-type (BLDigit A)
   1028   (BLZero)
   1029   (BLOne (Tree A)))
   1030 
   1031 (define-type (BinaryList A)
   1032   (BL (List (BLDigit A))))
   1033 
   1034 (define (size (t : (Tree A)) → Int)
   1035   (match t with
   1036    (Leaf x -> 1)
   1037    (Node w t1 t2 -> w)))
   1038 
   1039 (define (link (t1 : (Tree A)) (t2 : (Tree A)) → (Tree A))
   1040   (Node (+ (size t1) (size t2)) t1 t2))
   1041 
   1042 (define (consTree (t : (Tree A)) (x* : (List (BLDigit A))) → (List (BLDigit A)))
   1043   (match x* with
   1044    (Nil -> (∷ (BLOne t) Nil))
   1045    (∷ h ts ->
   1046     (match h with
   1047      (BLZero -> (∷ (BLOne t) ts))
   1048      (BLOne t2 -> (∷ BLZero (consTree (link t t2) ts)))))))
   1049 
   1050 
   1051 ;; TODO τ_e bad syntax when using `match2`
   1052 (define (unconsTree (d* : (List (BLDigit A))) → (Option (× (Tree A) (List (BLDigit A)))))
   1053   (match d* with
   1054    (Nil -> None)
   1055    (∷ d*-hd d*-tl ->
   1056     (match d*-hd with
   1057      (BLOne t ->
   1058       (match d*-tl with
   1059        (Nil -> (Some (tup t Nil)))
   1060        (∷ a b -> (Some (tup t (∷ BLZero d*-tl))))))
   1061      (BLZero ->
   1062       (match (unconsTree d*-tl) with
   1063        (None -> None)
   1064        (Some udt ->
   1065         (match udt with
   1066          (a ts ->
   1067           (match a with
   1068            (Leaf x -> None)
   1069            (Node x t1 t2 ->
   1070             (Some (tup t1 (∷ (BLOne t2) ts))))))))))))))
   1071 
   1072 (define (bl-empty → (BinaryList A))
   1073   (BL Nil))
   1074 
   1075 (define (bl-isEmpty (b : (BinaryList A)) → Bool)
   1076   (match b with
   1077    (BL x* ->
   1078     (match x* with
   1079      (Nil -> #t)
   1080      (∷ a b -> #f)))))
   1081 
   1082 (define (bl-cons (x : A) (b : (BinaryList A)) → (BinaryList A))
   1083   (match b with
   1084    (BL ts -> (BL (consTree (Leaf x) ts)))))
   1085 
   1086 (define (bl-head (b : (BinaryList A)) → (Option A))
   1087   (match b with
   1088    (BL ts ->
   1089     (match (unconsTree ts) with
   1090      (None -> None)
   1091      (Some xy ->
   1092       (match xy with
   1093        (x y ->
   1094         (match x with
   1095          (Leaf x -> (Some x))
   1096          (Node a b c -> None)))))))))
   1097 
   1098 (define (bl-tail (b : (BinaryList A)) → (Option (BinaryList A)))
   1099   (match b with
   1100    (BL ts ->
   1101     (match (unconsTree ts) with
   1102      (None -> None)
   1103      (Some xy ->
   1104       (match xy with
   1105        (x ts2 -> (Some (BL ts2)))))))))
   1106 
   1107 (define (bl-lookup (i : Int) (b : (BinaryList A)) → (Option A))
   1108   (match b with
   1109    [BL ts ->
   1110     (look i ts)]))
   1111 
   1112 (define (look [i : Int] [ts : (List (BLDigit A))] → (Option A))
   1113   (match ts with
   1114    [Nil -> None]
   1115    [∷ h ts ->
   1116     (match h with
   1117      [BLZero ->
   1118       (look i ts)]
   1119      [BLOne t ->
   1120       (let ((size-t (size t)))
   1121         (if (< i size-t)
   1122           (lookTree i t)
   1123           (look (- i size-t) ts)))])]))
   1124 
   1125 (define (lookTree (i : Int) (t : (Tree A)) → (Option A))
   1126   (match t with
   1127    [Leaf x ->
   1128     (if (= 0 i)
   1129       (Some x)
   1130       None)]
   1131    [Node w t1 t2 ->
   1132     (let ((w/2 (div w 2)))
   1133       (if (< i w/2)
   1134         (lookTree i t1)
   1135         (lookTree (- i w/2) t2)))]))
   1136 
   1137 (define (bl-update (i : Int) (y : A) (b : (BinaryList A)) → (Option (BinaryList A)))
   1138   (match b with
   1139    [BL ts ->
   1140     (match (upd i y ts) with
   1141      (None -> None)
   1142      (Some x -> (Some (BL x))))]))
   1143 
   1144 (define (upd (i : Int) (y : A) (b : (List (BLDigit A))) → (Option (List (BLDigit A))))
   1145   (match b with
   1146    [Nil -> None]
   1147    [∷ h ts ->
   1148     (match h with
   1149      [BLZero ->
   1150       (match (upd i y ts) with
   1151        (None -> None)
   1152        (Some x -> (Some (∷ BLZero x))))]
   1153      [BLOne t ->
   1154       (let ((size-t (size t)))
   1155         (if (< i size-t)
   1156           (match (updTree i y t) with
   1157            (None -> None)
   1158            (Some x -> (Some (∷ (BLOne x) ts))))
   1159           (match (upd (- i size-t) y ts) with
   1160            (None -> None)
   1161            (Some x -> (Some (∷ (BLOne t) x))))))])]))
   1162 
   1163 (define (updTree (i : Int) (y : A) (t : (Tree A)) → (Option (Tree A)))
   1164   (match t with
   1165    (Leaf x ->
   1166     (if (= 0 i)
   1167       (Some (Leaf y))
   1168       None))
   1169    (Node w t1 t2 ->
   1170     (let ((w/2 (div w 2)))
   1171       (if (< i w/2)
   1172         (match (updTree i y t1) with
   1173          (None -> None)
   1174          (Some x -> (Some (Node w x t2))))
   1175         (match (updTree (- i w/2) y t2) with
   1176          (None -> None)
   1177          (Some x -> (Some (Node w t1 x)))))))))
   1178 
   1179 (define (list->bl-list (x* : (List A)) → (BinaryList A))
   1180   (match x* with
   1181    (Nil -> (bl-empty))
   1182    (∷ x x* -> (bl-cons x (list->bl-list x*)))))
   1183 
   1184 ;; =============================================================================
   1185 
   1186 (define bl-digit*
   1187   (list->bl-list (∷ 1 (∷ 2 (∷ 3 (∷ 4 (∷ 5 (∷ 6 (∷ 7 (∷ 8 (∷ 9 Nil)))))))))))
   1188 
   1189 (define (bl-nil → (BinaryList Int))
   1190   (list->bl-list Nil))
   1191 
   1192 (check-type
   1193   (bl-isEmpty (bl-nil))
   1194   : Bool
   1195   ⇒ #t)
   1196 
   1197 (check-type
   1198   (bl-isEmpty bl-digit*)
   1199   : Bool
   1200   ⇒ #f)
   1201 
   1202 (check-type
   1203   (match (bl-head bl-digit*) with
   1204    (None -> 0)
   1205    (Some x -> x))
   1206   : Int
   1207   ⇒ 1)
   1208 
   1209 (check-type
   1210   (match (bl-tail bl-digit*) with
   1211    (None -> 0)
   1212    (Some x ->
   1213     (match (bl-head x) with
   1214      (None -> 0)
   1215      (Some y -> y))))
   1216   : Int
   1217   ⇒ 2)
   1218 
   1219 (check-type
   1220   (match (bl-lookup 7 bl-digit*) with
   1221    (None -> 0)
   1222    (Some x -> x))
   1223   : Int
   1224   ⇒ 8)
   1225 
   1226 (check-type
   1227   (match (bl-lookup 8 bl-digit*) with
   1228    (None -> 0)
   1229    (Some x -> x))
   1230   : Int
   1231   ⇒ 9)
   1232 
   1233 (check-type
   1234   (bl-lookup -111 bl-digit*)
   1235   : (Option Int)
   1236   ⇒ None)
   1237 
   1238 (check-type
   1239   (match (bl-update 3 99 bl-digit*) with
   1240    (None -> None)
   1241    (Some x -> (bl-lookup 3 x)))
   1242   : (Option Int)
   1243   ⇒ (Some 99))
   1244 
   1245 (check-type
   1246   (match (bl-update 0 222 bl-digit*) with
   1247    (None -> None)
   1248    (Some x -> (bl-head x)))
   1249   : (Option Int)
   1250   ⇒ (Some 222))
   1251 
   1252 (check-type
   1253   (bl-update 83 1 bl-digit*)
   1254   : (Option (BinaryList Int))
   1255   ⇒ None)
   1256 
   1257 ;; =============================================================================
   1258 ;; === Skew Binary Random Access Lists
   1259 
   1260 (define-type (ATree A)
   1261   (ALeaf A)
   1262   (ANode A (ATree A) (ATree A)))
   1263 
   1264 (define-type (SkewList A)
   1265   (SL (List (× Int (ATree A)))))
   1266 
   1267 (define (sb-empty → (SkewList A))
   1268   (SL Nil))
   1269 
   1270 (define (sb-isEmpty (sl : (SkewList A)) → Bool)
   1271   (match sl with
   1272    (SL xs ->
   1273     (match xs with
   1274      (Nil -> #t)
   1275      (∷ a b -> #f)))))
   1276 
   1277 (define (sb-cons (x : A) (sl : (SkewList A)) → (SkewList A))
   1278   (match sl with
   1279    (SL ts ->
   1280     (let ((base-case (SL (∷ (tup 1 (ALeaf x)) ts))))
   1281       (match ts with
   1282        (Nil -> base-case)
   1283        (∷ w1t1 ts ->
   1284         (match ts with
   1285          (Nil -> base-case)
   1286          (∷ w2t2 ts ->
   1287           (match w1t1 with
   1288            (w1 t1 ->
   1289             (match w2t2 with
   1290              (w2 t2 ->
   1291               (if (= w1 w2)
   1292                 (SL (∷ (tup (+ 1 (+ w1 w2)) (ANode x t1 t2)) ts))
   1293                 base-case)))))))))))))
   1294 
   1295 (define (sb-head (sl : (SkewList A)) → (Option A))
   1296   (match sl with
   1297    (SL ts ->
   1298     (match ts with
   1299      (Nil -> None)
   1300      (∷ w1t1 ts ->
   1301       (match w1t1 with
   1302        (w1 t1 ->
   1303         (match t1 with
   1304          (ALeaf x ->
   1305           (if (= w1 1)
   1306             (Some x)
   1307             None)) ;; Invariant error
   1308          (ANode x t1 t2 ->
   1309           (Some x))))))))))
   1310 
   1311 (define (sb-tail (sl : (SkewList A)) → (Option (SkewList A)))
   1312   (match sl with
   1313    (SL ts ->
   1314     (match ts with
   1315      (Nil -> None)
   1316      (∷ w1t1 ts ->
   1317       (match w1t1 with
   1318        (w1 t1 ->
   1319         (match t1 with
   1320          (ALeaf x ->
   1321           (if (= 1 w1)
   1322             (Some (SL ts))
   1323             None)) ;; Invariant
   1324          (ANode x t1 t2 ->
   1325            (let ((w1/2 (div w1 2)))
   1326              (Some (SL (∷ (tup w1/2 t1) (∷ (tup w1/2 t2) ts))))))))))))))
   1327 
   1328 (define (sb-lookup (i : Int) (sl : (SkewList A)) → (Option A))
   1329   (match sl with
   1330    (SL ts ->
   1331     (sb-look i ts))))
   1332 
   1333 (define (sb-look (i : Int) (ts : (List (× Int (ATree A)))) → (Option A))
   1334   (match ts with
   1335    (Nil -> None) ;; Bad subscript
   1336    (∷ wt ts ->
   1337     (match wt with
   1338      (w t ->
   1339       (if (< i w)
   1340         (sb-lookTree w i t)
   1341         (sb-look (- i w) ts)))))))
   1342 
   1343 (define (sb-lookTree (w : Int) (i : Int) (t : (ATree A)) → (Option A))
   1344   (match t with
   1345    (ALeaf x ->
   1346     (if (and (= w 1) (= i 0))
   1347       (Some x)
   1348       None))
   1349    (ANode x t1 t2 ->
   1350     (if (= 0 i)
   1351       (Some x)
   1352       (let ((w/2 (div w 2)))
   1353         (if (<= i w/2)
   1354           (sb-lookTree w/2 (- i 1) t1)
   1355           (sb-lookTree w/2 (- (- i 1) w/2) t2)))))))
   1356 
   1357 (define (sb-update (i : Int) (y : A) (sl : (SkewList A)) → (Option (SkewList A)))
   1358   (match sl with
   1359    (SL ts ->
   1360     (match (sb-upd i y ts) with
   1361      (None -> None)
   1362      (Some ts -> (Some (SL ts)))))))
   1363 
   1364 (define (sb-upd (i : Int) (y : A) (ts : (List (× Int (ATree A)))) → (Option (List (× Int (ATree A)))))
   1365   (match ts with
   1366    (Nil -> None)
   1367    (∷ wt ts ->
   1368     (match wt with
   1369      (w t ->
   1370       (if (< i w)
   1371         (match (sb-updTree w i y t) with
   1372          (None -> None)
   1373          (Some x -> (Some (∷ (tup w x) ts))))
   1374         (match (sb-upd (- i w) y ts) with
   1375          (None -> None)
   1376          (Some x -> (Some (∷ (tup w t) x))))))))))
   1377 
   1378 (define (sb-updTree (w : Int) (i : Int) (y : A) (t : (ATree A)) → (Option (ATree A)))
   1379   (match t with
   1380    (ALeaf x ->
   1381     (if (and (= 1 w) (= 0 i))
   1382       (Some (ALeaf y))
   1383       None)) ;; Invariant error
   1384    (ANode x t1 t2 ->
   1385     (if (= 0 i)
   1386       (Some (ANode y t1 t2))
   1387       (let ((w/2 (div w 2)))
   1388         (if (<= i w/2)
   1389           (match (sb-updTree w/2 (- i 1) y t1) with
   1390            (None -> None)
   1391            (Some z -> (Some (ANode x z t2))))
   1392           (match (sb-updTree w/2 (- (- i 1) w/2) y t2) with
   1393            (None -> None)
   1394            (Some z -> (Some (ANode x t1 z))))))))))
   1395 
   1396 (define (list->sb-list (x* : (List A)) → (SkewList A))
   1397   (match x* with
   1398    (Nil -> (sb-empty))
   1399    (∷ h t -> (sb-cons h (list->sb-list t)))))
   1400 
   1401 ;; =============================================================================
   1402 
   1403 (define sb-digit*
   1404   (list->sb-list (∷ 1 (∷ 2 (∷ 3 (∷ 4 (∷ 5 (∷ 6 (∷ 7 (∷ 8 (∷ 9 Nil)))))))))))
   1405 
   1406 (define (sb-nil → (SkewList Int))
   1407   (list->sb-list Nil))
   1408 
   1409 (check-type
   1410   (sb-isEmpty (sb-nil))
   1411   : Bool
   1412   ⇒ #t)
   1413 
   1414 (check-type
   1415   (sb-isEmpty sb-digit*)
   1416   : Bool
   1417   ⇒ #f)
   1418 
   1419 (check-type
   1420   (match (sb-head sb-digit*) with
   1421    (None -> 0)
   1422    (Some x -> x))
   1423   : Int
   1424   ⇒ 1)
   1425 
   1426 (check-type
   1427   (match (sb-tail sb-digit*) with
   1428    (None -> 0)
   1429    (Some x ->
   1430     (match (sb-head x) with
   1431      (None -> 0)
   1432      (Some y -> y))))
   1433   : Int
   1434   ⇒ 2)
   1435 
   1436 (check-type
   1437   (match (sb-lookup 7 sb-digit*) with
   1438    (None -> 0)
   1439    (Some x -> x))
   1440   : Int
   1441   ⇒ 8)
   1442 
   1443 (check-type
   1444   (match (sb-lookup 8 sb-digit*) with
   1445    (None -> 0)
   1446    (Some x -> x))
   1447   : Int
   1448   ⇒ 9)
   1449 
   1450 (check-type
   1451   (sb-lookup -111 sb-digit*)
   1452   : (Option Int)
   1453   ⇒ None)
   1454 
   1455 (check-type
   1456   (match (sb-update 3 99 sb-digit*) with
   1457    (None -> None)
   1458    (Some x -> (sb-lookup 3 x)))
   1459   : (Option Int)
   1460   ⇒ (Some 99))
   1461 
   1462 (check-type
   1463   (match (sb-update 0 222 sb-digit*) with
   1464    (None -> None)
   1465    (Some x -> (sb-head x)))
   1466   : (Option Int)
   1467   ⇒ (Some 222))
   1468 
   1469 (check-type
   1470   (sb-update 83 1 sb-digit*)
   1471   : (Option (SkewList Int))
   1472   ⇒ None)
   1473 
   1474 ;; =============================================================================
   1475 ;; === Alt Binary Random Access List
   1476 
   1477 (define-type (BinaryRAList A)
   1478   (ABLNil)
   1479   (ABLZero (BinaryRAList (× A A)))
   1480   (ABLOne A (BinaryRAList (× A A))))
   1481 
   1482 (define (abl-uncons (bl : (BinaryRAList A)) → (Option (× A (BinaryRAList A))))
   1483   (match bl with
   1484    (ABLNil -> None)
   1485    (ABLOne x ps ->
   1486     (Some (match ps with
   1487      (ABLNil -> (tup x ABLNil))
   1488      (ABLOne y ps2 -> (tup x (ABLZero ps)))
   1489      (ABLZero ps2 -> (tup x (ABLZero ps))))))
   1490    (ABLZero ps ->
   1491     (match (abl-uncons ps) with
   1492      (None -> None)
   1493      (Some xyps2 ->
   1494       (match xyps2 with
   1495        (xy ps2 ->
   1496         (match xy with
   1497          (x y -> (Some (tup x (ABLOne y ps2))))))))))))
   1498 
   1499 (define (abl-fupdate (f : (→ A A)) (i : Int) (bl : (BinaryRAList A)) → (Option (BinaryRAList A)))
   1500   (match bl with
   1501    (ABLNil -> None)
   1502    (ABLOne x ps ->
   1503     (if (= 0 i)
   1504       (Some (ABLOne (f x) ps))
   1505       (match (abl-fupdate f (- i 1) (ABLZero ps)) with
   1506        (None -> None)
   1507        (Some z -> (Some (abl-cons x z))))))
   1508    (ABLZero ps ->
   1509      (let ((f2 (if (= 0 (mod i 2))
   1510                  (λ ([xy : (× A A)])
   1511                    (match xy with (x y -> (tup (f x) y))))
   1512                  (λ ([xy : (× A A)])
   1513                    (match xy with (x y -> (tup x (f y))))))))
   1514       (match (abl-fupdate f2 (div i 2) ps) with
   1515         (None -> None)
   1516         (Some z -> (Some (ABLZero z))))))))
   1517 
   1518 (define (aabl-empty → (BinaryRAList A))
   1519   ABLNil)
   1520 
   1521 (define (abl-isEmpty (bl : (BinaryRAList A)) → Bool)
   1522   (match bl with
   1523    (ABLNil -> #t)
   1524    (ABLOne a b -> #f)
   1525    (ABLZero a -> #f)))
   1526 
   1527 (define (abl-cons (x : A) (bl : (BinaryRAList A)) → (BinaryRAList A))
   1528   (match bl with
   1529    (ABLNil -> (ABLOne x ABLNil))
   1530    (ABLZero ps -> (ABLOne x ps))
   1531    (ABLOne y ps -> (ABLZero (abl-cons (tup x y) ps)))))
   1532 
   1533 (define (abl-head (bl : (BinaryRAList A)) → (Option A))
   1534   (match (abl-uncons bl) with
   1535    (None -> None)
   1536    (Some xy ->
   1537     (match xy with
   1538      (x y -> (Some x))))))
   1539 
   1540 (define (abl-tail (bl : (BinaryRAList A)) → (Option (BinaryRAList A)))
   1541   (match (abl-uncons bl) with
   1542    (None -> None)
   1543    (Some xy ->
   1544     (match xy with
   1545      (x y -> (Some y))))))
   1546 
   1547 (define (abl-lookup (i : Int) (bl : (BinaryRAList A)) → (Option A))
   1548   (if (< i 0)
   1549     None
   1550     (abl-lookup/natural i bl)))
   1551 
   1552 (define (abl-lookup/natural (i : Int) (bl : (BinaryRAList A)) → (Option A))
   1553   (match bl with
   1554    (ABLNil -> None)
   1555    (ABLOne x ps ->
   1556     (if (= 0 i)
   1557       (Some x)
   1558       (abl-lookup/natural (- i 1) (ABLZero ps))))
   1559    (ABLZero ps ->
   1560     (match (abl-lookup/natural (div i 2) ps) with
   1561      (None -> None)
   1562      (Some xy ->
   1563       (match xy with
   1564        (x y ->
   1565         (if (= 0 (mod i 2))
   1566           (Some x)
   1567           (Some y)))))))))
   1568 
   1569 (define (abl-update (i : Int) (y : A) (bl : (BinaryRAList A)) → (Option (BinaryRAList A)))
   1570   (abl-fupdate (λ ([x : A]) y) i bl))
   1571 
   1572 (define (list->abl-list (xs : (List A)) → (BinaryRAList A))
   1573   (match xs with
   1574    (Nil -> (aabl-empty))
   1575    (∷ a b -> (abl-cons a (list->abl-list b)))))
   1576 
   1577 ;; =============================================================================
   1578 
   1579 (define abl-digit*
   1580   (list->abl-list (∷ 1 (∷ 2 (∷ 3 (∷ 4 (∷ 5 (∷ 6 (∷ 7 (∷ 8 (∷ 9 Nil)))))))))))
   1581 
   1582 (define (abl-nil → (BinaryRAList Int))
   1583   (list->abl-list Nil))
   1584 
   1585 (check-type
   1586   (abl-isEmpty (abl-nil))
   1587   : Bool
   1588   ⇒ #t)
   1589 
   1590 (check-type
   1591   (abl-isEmpty abl-digit*)
   1592   : Bool
   1593   ⇒ #f)
   1594 
   1595 (check-type
   1596   (match (abl-head abl-digit*) with
   1597    (None -> 0)
   1598    (Some x -> x))
   1599   : Int
   1600   ⇒ 1)
   1601 
   1602 (check-type
   1603   (match (abl-tail abl-digit*) with
   1604    (None -> 0)
   1605    (Some x ->
   1606     (match (abl-head x) with
   1607      (None -> 0)
   1608      (Some y -> y))))
   1609   : Int
   1610   ⇒ 2)
   1611 
   1612 (check-type
   1613   (match (abl-lookup 7 abl-digit*) with
   1614    (None -> 0)
   1615    (Some x -> x))
   1616   : Int
   1617   ⇒ 8)
   1618 
   1619 (check-type
   1620   (match (abl-lookup 8 abl-digit*) with
   1621    (None -> 0)
   1622    (Some x -> x))
   1623   : Int
   1624   ⇒ 9)
   1625 
   1626 (check-type
   1627   (abl-lookup -111 abl-digit*)
   1628   : (Option Int)
   1629   ⇒ None)
   1630 
   1631 (check-type
   1632   (match (abl-update 3 99 abl-digit*) with
   1633    (None -> None)
   1634    (Some x -> (abl-lookup 3 x)))
   1635   : (Option Int)
   1636   ⇒ (Some 99))
   1637 
   1638 (check-type
   1639   (match (abl-update 0 222 abl-digit*) with
   1640    (None -> None)
   1641    (Some x -> (abl-head x)))
   1642   : (Option Int)
   1643   ⇒ (Some 222))
   1644 
   1645 (check-type
   1646   (abl-update 83 1 abl-digit*)
   1647   : (Option (BinaryRAList Int))
   1648   ⇒ None)
   1649