#lang scheme
(require scheme/provide)
(provide (filtered-out (lambda (name)
(and (regexp-match? "ra:" name)
(regexp-replace "ra:" name "")))
(all-defined-out)))
(define-struct kons (size tree rest)
#:property prop:equal+hash
(list
(lambda (ra1 ra2 equal?)
(and (= (kons-size ra1) (kons-size ra2))
(tree-equal? (kons-tree ra1) (kons-tree ra2) equal?)
(equal? (kons-rest ra1) (kons-rest ra2))))
(lambda (ra equal-hash-code)
(+ (bitwise-bit-field (+ (kons-size ra)
(equal-hash-code (kons-tree ra)))
0 14)
(arithmetic-shift
(bitwise-bit-field (equal-hash-code (kons-tree ra)) 0 14) 14)))
(lambda (ra equal-hash-code)
(+ (bitwise-bit-field (equal-hash-code (kons-tree ra)) 14 28)
(kons-size ra)
(arithmetic-shift
(bitwise-bit-field (equal-hash-code (ra:rest ra)) 14 28) 14))))
#:property prop:custom-write
(lambda (ra p write?)
(let ((print (if write? write display)))
(let ((curly? (print-pair-curly-braces)))
(display (if curly? "{" "(") p)
(let loop ((ls ra))
(cond [(ra:empty? ls) 'done]
[(ra:cons? ls)
(print (ra:car ls) p)
(unless (ra:empty? (ra:cdr ls))
(display " " p)
(loop (ra:cdr ls)))]
[else
(display ". " p)
(print ls p)]))
(display (if curly? "}" ")") p))))
#:property prop:sequence
(lambda (ra)
(let ((init (list->forest ra)))
(make-do-sequence
(lambda ()
(values
(lambda (x) (tree-val (car x)))
(lambda (p)
(let ((tr (car p)))
(cond [(node? tr)
(cons (node-left tr)
(cons (node-right tr)
(cdr p)))]
[else (cdr p)])))
init cons? void void))))))
(define-struct node (val left right) #:prefab)
(define (tree-equal? t1 t2 equal?)
(if (node? t1)
(and (node? t2)
(equal? (node-val t1) (node-val t2))
(tree-equal? (node-left t1) (node-left t2) equal?)
(tree-equal? (node-right t1) (node-right t2) equal?))
(equal? t1 t2)))
(define (tree-val t)
(match t
[(struct node (x _ _)) x]
[x x]))
(define (tree-map f t)
(match t
[(struct node (x l r))
(make-node (f x) (tree-map f l) (tree-map f r))]
[x (f x)]))
(define (tree-map/n f ts)
(let recr ((ts ts))
(match ts
[(list (struct node (vs ls rs)) ...)
(make-node (apply f vs) (recr ls) (recr rs))]
[xs (apply f xs)])))
(define (build-tree i f) (let rec ((i i) (o 0))
(cond [(= 1 i) (f o)]
[else
(let ((i/2 (half i)))
(make-node (f o)
(rec i/2 (add1 o))
(rec i/2 (+ 1 o i/2))))])))
(define (tr:make-tree i x) (let recr ((i i))
(if (= 1 i)
x
(let ((n (recr (half i))))
(make-node x n n)))))
(define (tree-ref/update mid t i f)
(cond [(zero? i)
(match t
[(struct node (x l r))
(values x (make-node (f x) l r))]
[else (values t (f t))])]
[(<= i mid)
(let-values ([(v* t*) (tree-ref/update (half (sub1 mid))
(node-left t)
(sub1 i)
f)])
(values v* (make-node (node-val t) t* (node-right t))))]
[else
(let-values ([(v* t*) (tree-ref/update (half (sub1 mid))
(node-right t)
(sub1 (- i mid))
f)])
(values v* (make-node (node-val t) (node-left t) t*)))]))
(define (tree-ref/a t i mid)
(cond [(zero? i) (tree-val t)]
[(<= i mid)
(tree-ref/a (node-left t)
(sub1 i)
(half (sub1 mid)))]
[else
(tree-ref/a (node-right t)
(sub1 (- i mid))
(half (sub1 mid)))]))
(define (tree-ref size t i)
(if (zero? i)
(tree-val t)
(tree-ref/a t i (half (sub1 size)))))
(define (tree-update size t i f)
(let recr ((mid (half (sub1 size))) (t t) (i i))
(cond [(zero? i)
(if (node? t)
(make-node (f (node-val t))
(node-left t)
(node-right t))
(f t))]
[(<= i mid)
(make-node (node-val t)
(recr (half (sub1 mid))
(node-left t)
(sub1 i))
(node-right t))]
[else
(make-node (node-val t)
(node-left t)
(recr (half (sub1 mid))
(node-right t)
(sub1 (- i mid))))])))
(define indx-msg "index ~a too large for: ~a")
(define ra:empty empty)
(define ra:cons? kons?)
(define ra:empty? empty?)
(define (ra:list? x)
(or (ra:empty? x)
(and (ra:cons? x)
(ra:list? (ra:cdr x)))))
(define (ra:cons x ls)
(match ls
[(struct kons (s t1 (struct kons (s t2 r))))
(make-kons (+ 1 s s) (make-node x t1 t2) r)]
[else
(make-kons 1 x ls)]))
(define (get-car+cdr name p)
(match p
[(struct kons (s (struct node (x t1 t2)) r))
(let ((s* (half s)))
(values x (make-kons s* t1 (make-kons s* t2 r))))]
[(struct kons (s x r))
(values x r)]
[else
(error name "expected cons, given: ~a" p)]))
(define (get-first+rest name p)
(get-car+cdr name p)
(let-values ([(f r) (get-car+cdr name p)])
(if (or (ra:cons? r) (ra:empty? r))
(values f r)
(error name "expected proper cons, given: ~a" p))))
(define (ra:car+cdr p)
(get-car+cdr 'ra:car+cdr p))
(define (ra:first+rest ls)
(get-first+rest 'ra:first+rest ls))
(define (ra:first ls)
(let-values ([(f r) (get-first+rest 'ra:first ls)])
f))
(define (ra:rest ls)
(let-values ([(f r) (get-first+rest 'ra:rest ls)])
r))
(define (ra:car p)
(let-values ([(x y) (get-car+cdr 'ra:car p)])
x))
(define (ra:cdr p)
(let-values ([(x y) (get-car+cdr 'ra:cdr p)])
y))
(define (half n)
(arithmetic-shift n -1))
(define (ra:list-ref/update ls i f)
(let recr ((xs ls) (j i))
(match xs
[(struct kons (s t r))
(cond [(< j s)
(let-values ([(v* t*) (tree-ref/update (half (sub1 s)) t j f)])
(values v* (make-kons s t* r)))]
[else
(let-values ([(v* r*) (recr r (- j s))])
(values v* (make-kons s t r*)))])]
[else (error 'ra:list-ref/update indx-msg i ls)])))
(define (ra:list-update ls i f)
(let recr ((xs ls) (j i))
(let ((s (kons-size xs)))
(if (< j s)
(make-kons s (tree-update s (kons-tree xs) j f) (kons-rest xs))
(make-kons s (kons-tree xs) (recr (kons-rest xs) (- j s)))))
(match xs
[(struct kons (s t r))
(if (< j s)
(make-kons s (tree-update s t j f) r)
(make-kons s t (recr r (- j s))))]
[else (error 'ra:list-update indx-msg i ls)])))
(define (ra:list-ref ls i)
(let loop ((xs ls) (j i))
(match xs
[(struct kons (s t r))
(cond [(< j s) (tree-ref s t j)]
[else (loop r (- j s))])]
[else (error 'ra:list-ref indx-msg i ls)])))
(define (ra:list-ref/set ls i v)
(ra:list-ref/update ls i (lambda (_) v)))
(define (ra:list-set ls i v)
(let-values ([(_ l*) (ra:list-ref/set ls i v)]) l*))
(define (ra:foldl/1 f a ls)
(for/fold ([a a]) ([x (ra:in-list ls)])
(f x a)))
(define (ra:foldr/1 f b ls)
(let recr ((ls ls))
(cond [(ra:empty? ls) b]
[else (let-values ([(fst rst) (ra:first+rest ls)])
(f fst (recr rst)))])))
(define ra:foldl
(case-lambda
[(f a ls) (ra:foldl/1 f a ls)]
[(f a . lss)
(check-nary-loop-args 'ra:foldl add1 f lss)
(let loop ((lss lss) (a a))
(match lss
[(cons (match:ra:list) _) a]
[(list (match:ra:cons xs rs) ...)
(loop rs
(apply f (append xs (list a))))]))]))
(define ra:foldr
(case-lambda
[(f b ls) (ra:foldr/1 f b ls)]
[(f b . lss)
(check-nary-loop-args 'ra:foldr add1 f lss)
(let recr ((lss lss))
(match lss
[(cons (match:ra:list) _) b]
[(list (match:ra:cons xs rs) ...)
(apply f (append xs (list (recr rs))))]))]))
(define ra:andmap
(case-lambda
[(f ls) (for/and ([x (ra:in-list ls)]) (f x))]
[(f . lss)
(check-nary-loop-args 'ra:andmap (lambda (x) x) f lss)
(match lss
[(cons (match:ra:list) _) true]
[else
(let loop ((lss lss))
(match lss
[(list (match:ra:cons xs (match:ra:list)) ...)
(apply f xs)]
[(list (match:ra:cons xs rs) ...)
(and (apply f xs)
(loop rs))]))])]))
(define ra:ormap
(case-lambda
[(f ls) (for/or ([x (ra:in-list ls)]) (f x))]
[(f . lss)
(check-nary-loop-args 'ra:ormap (lambda (x) x) f lss)
(match lss
[(cons (match:ra:list) _) false]
[else
(let loop ((lss lss))
(match lss
[(list (match:ra:cons xs (match:ra:list)) ...)
(apply f xs)]
[(list (match:ra:cons xs rs) ...)
(or (apply f xs)
(loop rs))]))])]))
(define (check-nary-loop-args name mod f lss) (void))
(define (check-nary-loop-args name mod f lss)
(let ((n (ra:length (car lss)))
(m (mod (length lss))))
(let loop ((l (cdr lss)))
(unless (empty? l)
(unless (= n (ra:length (car l)))
(error name
"given lists of un-equal size: ~a" lss))
(loop (cdr l))))
(unless (procedure-arity-includes? f m)
(error name
"arity mismatch for ~a, expects ~a arguments, given ~a"
f (procedure-arity f) m))))
(define (ra:list . xs)
(foldr ra:cons ra:empty xs))
(define (ra:list* x . r+t)
(let loop ((xs+t (cons x r+t)))
(match xs+t
[(list t) t]
[(cons x xs+t)
(ra:cons x (loop xs+t))])))
(define (ra:build-list n f)
(let loop ((n n) (a ra:empty))
(cond [(zero? n) a]
[else
(let ((t (largest-skew-binary n)))
(let ((n* (- n t)))
(loop n*
(make-kons
t (build-tree t (lambda (i) (f (+ i n*))))
a))))])))
(define (ra:make-list n x)
(let loop ((n n) (a ra:empty))
(cond [(zero? n) a]
[else
(let ((t (largest-skew-binary n)))
(loop (- n t)
(make-kons t (tr:make-tree t x) a)))])))
(define (skew-succ t) (add1 (arithmetic-shift t 1)))
(define (largest-skew-binary n)
(if (= 1 n)
1
(let* ((t (largest-skew-binary (half n)))
(s (skew-succ t)))
(if (> s n) t s))))
(define ra:map
(case-lambda
[(f ls)
(let recr ((ls ls))
(match ls
[(struct kons (s t r))
(make-kons s (tree-map f t) (recr r))]
[else ra:empty]))]
[(f . lss)
(check-nary-loop-args 'ra:map (lambda (x) x) f lss)
(let recr ((lss lss))
(cond [(ra:empty? (car lss)) ra:empty]
[else
(make-kons (kons-size (car lss))
(tree-map/n f (map kons-tree lss))
(recr (map kons-rest lss)))]))]))
(define (ra:count ls)
(let recr ((ls ls))
(match ls
[(struct kons (s _ r)) (+ s (recr r))]
[else 0])))
(define ra:length ra:count)
(define (ra:second ls) (ra:list-ref ls 1))
(define (ra:third ls) (ra:list-ref ls 2))
(define (ra:fourth ls) (ra:list-ref ls 3))
(define (ra:fifth ls) (ra:list-ref ls 4))
(define (ra:sixth ls) (ra:list-ref ls 5))
(define (ra:seventh ls) (ra:list-ref ls 6))
(define (ra:eighth ls) (ra:list-ref ls 7))
(define (ra:ninth ls) (ra:list-ref ls 8))
(define (ra:tenth ls) (ra:list-ref ls 9))
(define (ra:last ls) (ra:list-ref ls (sub1 (ra:length ls))))
(define (ra:list-tail ls i)
(let loop ((xs ls) (j i))
(cond [(zero? j) xs]
[else (loop (ra:cdr xs) (sub1 j))])))
(define (ra:append . lss)
(cond [(empty? lss) ra:empty]
[lss (let recr ((lss lss))
(cond [(empty? (cdr lss)) (car lss)]
[else (ra:foldr/1 ra:cons
(recr (cdr lss))
(car lss))]))]))
(define (ra:reverse ls)
(ra:foldl/1 ra:cons ra:empty ls))
(define-match-expander match:ra:list
(syntax-rules ()
[(match:ra:list) (quote ())]
[(match:ra:list x y ...)
(match:ra:cons x (match:ra:list y ...))])
(syntax-rules ()
[(_) ra:list]))
(define-match-expander match:ra:cons
(syntax-rules (match:ra:list)
[(match:ra:cons fst (match:ra:list))
(struct kons (1 fst (match:ra:list)))]
[(match:ra:cons fst rst)
(or (and (struct kons (_ (struct node (fst _ _)) _))
(app ra:rest rst))
(struct kons (_ fst rst)))])
(syntax-rules ()
[(_) ra:cons]))
(define (list->forest ra)
(cond [(ra:empty? ra) empty]
[else (cons (kons-tree ra)
(list->forest (kons-rest ra)))]))
(define (sequence-init ra)
(match ra
[(quote ())
(values false false false)]
[(struct kons
(size
(struct node (x (and (struct node _) left-node) right-node))
rest))
(values x left-node (cons right-node (list->forest rest)))]
[(struct kons
(size (struct node (x left-leaf right-leaf)) rest))
(values x false (cons left-leaf (cons right-leaf (list->forest rest))))]
[(struct kons (size leaf (quote ())))
(values leaf false empty)]
[(struct kons (size leaf (struct kons (_ (and (struct node _) n) rest))))
(values leaf n (list->forest rest))]
[(struct kons (size leaf rest))
(values leaf false (list->forest rest))]))
(define (advance n f)
(if n
(let ((l (node-left n)))
(cond [(node? l)
(values (node-val n) l (cons (node-right n) f))]
[else
(values (node-val n) false (cons l (cons (node-right n) f)))]))
(match f
[(quote ()) (values false false false)]
[(cons (and (struct node _) n) f)
(match n
[(struct node (x (and (struct node _) left) right))
(values x left (cons right f))]
[(struct node (x leaf-left leaf-right))
(values x false (cons leaf-left (cons leaf-right f)))])]
[(cons leaf f)
(values leaf false f)])))
(define-sequence-syntax ra:in-list
(lambda () #'(lambda (x) x))
(lambda (stx)
(syntax-case stx ()
[((id) (_ ra-list-exp))
#'[(id)
(:do-in
([(v t f) (sequence-init ra-list-exp)])
'outer-check
([v v] [t t] [f f])
f
([(id) v]
[(next tree forest) (advance t f)])
#t #t (next tree forest))]])))