#lang scheme/base
(require test-engine/scheme-tests
"anormalize.ss"
"../../collects/moby/runtime/stx.ss")
(require (for-syntax scheme/base
"../../stx-helpers.ss"
"../../collects/moby/runtime/stx.ss"))
(define-syntax (check-expansion stx)
(syntax-case stx ()
[(_ original-form expanded-form)
(with-syntax ([a-stx-sexp (stx->sexp (syntax->stx #'original-form))])
(syntax/loc stx
(check-expect (stx->datum
(anormalize (sexp->stx (quote a-stx-sexp))))
(quote expanded-form))))]))
(check-expansion [(define (f x)
(* x x))]
[(define (d0_f a_x)
(* a_x a_x))])
(check-expansion [(define (g x)
x)]
[(define (d0_g a_x)
a_x)])
(test)
(
'((define (memoize f)
(local [(define-struct entry (key value))
(define table (box empty))]
(lambda (n)
(local [(define lookup
(filter (lambda (result) (equal? (entry-key result) n))
(unbox table)))]
(if (empty? lookup)
(local [(define ans (f n))]
(begin
(set-box! table (cons (make-entry n ans) (unbox table)))
ans))
(entry-value (first lookup))))))))
'((define (d0_memoize a_f)
(local [(define-struct s1_entry (key value))
(define d2_table (box empty))]
(lambda (a_n)
(local [(define d3_lookup
(filter (lambda (a_result) (equal? (s1_entry-key a_result) a_n))
(unbox d2_table)))]
(if (empty? d3_lookup)
(local [(define d4_ans (a_f a_n))]
(begin
(set-box! d2_table (cons (make-s1_entry a_n d4_ans) (unbox d2_table)))
d4_ans))
(s1_entry-value (first d3_lookup))))))))
'((define-struct s1_entry (key value))
(define (d0_memoize a_f)
(local [(define d2_table (box empty))]
(lambda (a_n)
(local [(define d3_lookup
(filter (lambda (a_result) (equal? (s1_entry-key a_result) a_n))
(unbox d2_table)))]
(if (empty? d3_lookup)
(local [(define d4_ans (a_f a_n))]
(begin
(set-box! d2_table (cons (make-s1_entry a_n d4_ans) (unbox d2_table)))
d4_ans))
(s1_entry-value (first d3_lookup))))))))
'((define-struct s1_entry (key value))
(define (d0_memoize a_f)
(local [(define d2_table (box empty))
(define (anon0 a_n)
(local [(define d3_lookup
(local [(define (anon1 a_result) (equal? (s1_entry-key a_result) a_n))]
(filter anon1 (unbox d2_table))))]
(if (empty? d3_lookup)
(local [(define d4_ans (a_f a_n))]
(begin
(set-box! d2_table (cons (make-s1_entry a_n d4_ans) (unbox d2_table)))
d4_ans))
(s1_entry-value (first d3_lookup)))))]
anon0)))
'((define-struct s1_entry (key value))
(define (d0_memoize a_f)
(local [(define (anon0 a_n)
(local [(define d3_lookup (box 'undefined))]
(begin
(set-box! d3_lookup
(local [(define (anon1 a_result) (equal? (s1_entry-key a_result) a_n))]
(filter anon1 (unbox (unbox d2_table)))))
(if (empty? (unbox d3_lookup))
(local [(define d4_ans (box 'undefined))]
(begin
(set-box! d4_ans (a_f a_n))
(begin
(set-box! (unbox d2_table) (cons (make-s1_entry a_n (unbox d4_ans))
(unbox (unbox d2_table))))
(unbox d4_ans))))
(s1_entry-value (first (unbox d3_lookup)))))))
(define d2_table (box 'undefined))]
(begin
(set-box! d2_table (box empty))
anon0))))
'((define-struct s1_entry (key value))
(define (d0_memoize a_f)
(local [(define (anon0 a_n)
(local [(define d3_lookup (box 'undefined))]
(begin
(local [(define temp0
(local [(define (anon1 a_result) (equal? (s1_entry-key a_result) a_n))]
(filter anon1 (unbox (unbox d2_table)))))]
(set-box! d3_lookup temp0))
(if (empty? (unbox d3_lookup))
(local [(define d4_ans (box 'undefined))]
(begin
(local [(define temp1 (a_f a_n))]
(set-box! d4_ans temp1))
(begin
(set-box! (unbox d2_table) (cons (make-s1_entry a_n (unbox d4_ans))
(unbox (unbox d2_table))))
(unbox d4_ans))))
(s1_entry-value (first (unbox d3_lookup)))))))
(define d2_table (box 'undefined))]
(begin
(set-box! d2_table (box empty))
anon0))))
'((define-struct s1_entry (key value))
(define (f0_d0_memoize anon0 d2_table a_f) anon0)
(define (d0_memoize a_f)
(local [(define (f4_anon0 d4_ans d3_lookup a_n) (unbox d4_ans))
(define (f3_anon0 d4_ans d3_lookup a_n)
(begin
(set-box! (unbox d2_table) (cons (make-s1_entry a_n (unbox d4_ans))
(unbox (unbox d2_table))))
(f4_anon0 d4_ans d3_lookup a_n)))
(define (f2_anon0 temp1 d4_ans d3_lookup a_n) (set-box! d4_ans temp1))
(define (f1_anon0 d3_lookup a_n)
(if (empty? (unbox d3_lookup))
(local [(define d4_ans (box 'undefined))]
(begin
(local [(define temp1 (a_f a_n))]
(f2_anon0 temp1 d4_ans d3_lookup a_n))
(f3_anon0 d4_ans d3_lookup a_n)))
(s1_entry-value (first (unbox d3_lookup)))))
(define (f0_anon0 temp0 d3_lookup a_n)
(set-box! d3_lookup temp0))
(define (anon0 a_n)
(local [(define d3_lookup (box 'undefined))]
(begin
(local [(define temp0
(local [(define (anon1 a_result) (equal? (s1_entry-key a_result) a_n))]
(filter anon1 (unbox (unbox d2_table)))))]
(f0_anon0 temp0 d3_lookup a_n))
(f1_anon0 d3_lookup a_n))))
(define d2_table (box 'undefined))]
(begin
(set-box! d2_table (box empty))
(f0_d0_memoize anon0 d2_table a_f)))))
'((define (my-foldr f base lst)
(if (empty? lst)
base
(f (first lst) (my-foldr f base (rest lst)))))
(define (my-map f lst)
(foldr (lambda (elt rst) (cons (f elt) rst))
empty
lst))
(define (transpose matrix)
(if (or (empty? matrix)
(empty? (first matrix)))
empty
(cons (my-map first matrix)
(transpose (my-map rest matrix))))))
'((define (f0_d0_my-foldr temp0 a_f a_base a_lst)
(a_f (first a_lst) temp0))
(define (d0_my-foldr a_f a_base a_lst)
(if (empty? a_lst)
a_base
(local [(define temp0 (d0_my-foldr a_f a_base (rest a_lst)))]
(f0_d0_my-foldr temp0 a_f a_base a_lst))))
(define (d1_my-map a_f a_lst)
(local [(define (f0_anon0 temp1 a_elt a_rst)
(cons temp1 a_rst))
(define (anon0 a_elt a_rst)
(local [(define temp1 (a_f a_elt))]
(f0_anon0 temp1 a_elt a_rst)))]
(foldr anon0 empty a_lst)))
(define (f3_d2_transpose temp4 temp3 temp2 temp5 a_matrix)
(cons temp3 temp4))
(define (f2_d2_transpose temp3 temp2 temp5 a_matrix)
(local [(define temp4 (d2_transpose temp2))]
(f3_d2_transpose temp4 temp3 temp2 temp5 a_matrix)))
(define (f1_d2_transpose temp2 temp5 a_matrix)
(local [(define temp3 (d1_my-map first a_matrix))]
(f2_d2_transpose temp3 temp2 temp5 a_matrix)))
(define (f0_d2_transpose temp5 a_matrix)
(if temp5
empty
(local [(define temp2 (d1_my-map rest a_matrix))]
(f1_d2_transpose temp2 temp5 a_matrix))))
(define (d2_transpose a_matrix)
(local [(define (f0_temp5) (empty? (first a_matrix)))
(define temp5 (or (empty? a_matrix) (f0_temp5)))]
(f0_d2_transpose temp5 a_matrix))))
'((define (max lst)
(if (empty? (rest lst))
(first lst)
(local [(define max-rest (max (rest lst)))]
(if (<= (first lst) max-rest)
(first lst)
max-rest)))))
'((define (f1_d0_max d1_max-rest a_lst)
(if (<= (first a_lst) (unbox d1_max-rest))
(first a_lst)
(unbox d1_max-rest)))
(define (f0_d0_max temp0 d1_max-rest a_lst)
(set-box! d1_max-rest temp0))
(define (d0_max a_lst)
(if (empty? (rest a_lst))
(first a_lst)
(local [(define d1_max-rest (box 'undefined))]
(begin
(local [(define temp0 (d0_max (rest a_lst)))]
(f0_d0_max temp0 d1_max-rest a_lst))
(f1_d0_max d1_max-rest a_lst))))))
)