Files
kraken/partial_eval_test.kp

160 lines
9.4 KiB
Plaintext
Raw Normal View History

(with_import "./partial_eval.kp"
(let (
test-case (lambda (code) (let (
_ (println "Code: " code)
; For right now we only support calling partial_eval in such a way that it partial evals against
; the root env, but this is could and really should be extended. We could at least check if the env we're called with
; is the root_env, or if what we look up in whatever env is passed in matches something in the root env
[comb_to_mark_map partially_evaled] (partial_eval code)
_ (println "Partially evaled: " partially_evaled)
stripped (strip partially_evaled)
_ (println "Stripped: " stripped)
fully_evaled (eval stripped root_env)
_ (println "Fully evaled: " fully_evaled)
2021-08-22 13:03:33 -04:00
fully_evaled_called (if (combiner? fully_evaled) (fully_evaled 1337))
_ (if (combiner? fully_evaled) (println "..and called " fully_evaled_called))
outer_eval (eval code root_env)
_ (println " outer-eval " outer_eval)
2021-08-22 13:03:33 -04:00
outer_called (if (combiner? outer_eval) (outer_eval 1337))
_ (if (combiner? outer_eval) (println "..and outer called " outer_called))
_ (cond (or (combiner? fully_evaled) (combiner? outer_eval))
(if (!= fully_evaled_called outer_called) (error (str "called versions unequal for " code " are " fully_evaled_called " vs " outer_called)))
(!= fully_evaled outer_eval) (error (str "partial-eval versions unequal for " code " are " fully_evaled " vs " outer_eval))
true nil)
_ (println)
) fully_evaled))
simple_add (read-string "(+ 1 2)")
2021-08-16 00:37:56 -04:00
vau_with_add (read-string "(vau (y) (+ 1 2))")
vau_with_add_called (read-string "((vau (y) (+ 1 2)) 4)")
vau_with_passthrough (read-string "((vau (y) y) 4)")
vau_with_no_eval_add (read-string "((vau (y) (+ 13 2 y)) 4)")
vau_with_wrap_add (read-string "((wrap (vau (y) (+ 13 2 y))) (+ 3 4))")
2021-08-16 00:37:56 -04:00
vau_with_add_p (read-string "(vau de (y) (+ (eval y de) (+ 1 2)))")
vau_with_add_p_called (read-string "((vau de (y) ((vau dde (z) (+ 1 (eval z dde))) y)) 17)")
2021-08-17 18:17:42 -04:00
cond_test (read-string "(cond false 1 false 2 (+ 1 2) 3 true 1337)")
cond_vau_test (read-string "(vau de (x) (cond false 1 false 2 x 3 true 42))")
cond_vau_test2 (read-string "(vau de (x) (cond false 1 false 2 3 x true 42))")
combiner_test (read-string "(combiner? true)")
combiner_test2 (read-string "(combiner? (vau de (x) x))")
combiner_test3 (read-string "(vau de (x) (combiner? x))")
symbol_test (read-string "((vau (x) x) a)")
env_test (read-string "(env? true)")
; this doesn't partially eval, but it could with a more percise if the marked values were more percise
env_test2 (read-string "(vau de (x) (env? de))")
env_test3 (read-string "(vau de (x) (env? x))")
env_test4 (read-string "((vau de (x) (env? de)) 1)")
2021-08-22 13:03:33 -04:00
; let1 test
; ((wrap (vau root_env (quote) ((wrap (vau (let1) ;HERE;)) (vau de (s v b) (eval (array (array vau (array s) b) (eval v de)) de))))) (vau (x) x))
;let1_test (read-string "((wrap (vau root_env (quote) ((wrap (vau (let1) (let1 a 12 (+ a 1)))) (vau de (s v b) (eval (array (array vau (array s) b) (eval v de)) de))))) (vau (x) x))")
let1_test (read-string "((wrap (vau (let1) (let1 a 12 (+ a 1)))) (vau de (s v b) (eval (array (array vau (array s) b) (eval v de)) de)))")
let2_test (read-string "((wrap (vau (let1) (let1 a 12 (vau (x) (+ a 1))))) (vau de (s v b) (eval (array (array vau (array s) b) (eval v de)) de)))")
let3_test (read-string "((wrap (vau (let1) (let1 a 12 (wrap (vau (x) (+ x a 1)))))) (vau de (s v b) (eval (array (array vau (array s) b) (eval v de)) de)))")
let4_test (read-string "((wrap (vau (let1) (let1 a 12 (wrap (vau (x) (let1 y (+ a 1) (+ y x a))))))) (vau de (s v b) (eval (array (array vau (array s) b) (eval v de)) de)))")
; I've broken this one with my parameter bailing I think
; The problem seems to be the second eval of let1, which is ( marked_array ( ( marked_array ( ( prim_comb combiner(wrap_level: 1) builtin_combiner_vau(wrap_level: 0) ) ( val ( y) ) ( val ( + y x a ) ) ) ) ( later ( builtin_combiner_+(wrap_level: 1) x a 1 ) ) ) )
; that is, ['ma ['ma vau ('val y) 'val (+ y x a) ] 'later [+ x a 1] ], and because of that later, eval_strip
; is returning not-ok, and so the whole thing can't be passed to partial_eval.
; To fix it, we'd need that strip-hack-thing to strip it out then sub it back in in the partial eval.
let5_test (read-string "((wrap (vau (let1)
(let1 a 12 (wrap (vau (x) (let1 y (+ x a 1) (+ y x a))))
))) (vau de (s v b) (eval (array (array vau (array s) b) (eval v de)) de)))")
lambda1_test (read-string "((wrap (vau (let1)
(let1 lambda (vau se (p b) (wrap (eval (array vau p b) se)))
(lambda (x) x)
))) (vau de (s v b) (eval (array (array vau (array s) b) (eval v de)) de)))")
lambda2_test (read-string "((wrap (vau (let1)
(let1 lambda (vau se (p b) (wrap (eval (array vau p b) se)))
(let1 a 12
(lambda (x) (+ a x)))
))) (vau de (s v b) (eval (array (array vau (array s) b) (eval v de)) de)))")
;!!!! Ditto to let5_test
lambda3_test (read-string "((wrap (vau (let1)
(let1 lambda (vau se (p b) (wrap (eval (array vau p b) se)))
(let1 a 12
(lambda (x) (let1 b (+ a x)
(+ a x b))))
))) (vau de (s v b) (eval (array (array vau (array s) b) (eval v de)) de)))")
array_test (read-string "(array 1 2 3 4 5)")
vararg_test (read-string "((wrap (vau (a & rest) rest)) 1 2 3 4 5)")
;do1_test (read-string "((wrap (vau (let1)
; (let1 lambda (vau se (p b) (wrap (eval (array vau p b) se)))
; (let1 do_helper (lambda (recurse s i se) (cond (= i (len s)) nil
; (= i (- (len s) 1)) (eval (idx s i) se)
; (eval (idx s i) se) (recurse recurse s (+ i 1) se)
; true (recurse recurse s (+ i 1) se)))
; (let1 do (vau se (& s) (do_helper do_helper s 0 se))
; (do (println 1 2 3)
; (println 4 5 6))
; ))))) (vau de (s v b) (eval (array (array vau (array s) b) (eval v de)) de)))")
;do2_test (read-string "((wrap (vau (let1)
; (let1 lambda (vau se (p b) (wrap (eval (array vau p b) se)))
; (let1 do_helper (lambda (recurse s i se) (cond (= i (len s)) nil
; (= i (- (len s) 1)) (eval (idx s i) se)
; (eval (idx s i) se) (recurse recurse s (+ i 1) se)
; true (recurse recurse s (+ i 1) se)))
; (let1 do (vau se (& s) (do_helper do_helper s 0 se))
; (do (println 1 2 3)
; (println 4 5 6))
; ))))) (vau de (s v b) (eval (array (array vau (array s) b) (eval v de)) de)))")
2021-08-22 13:03:33 -04:00
;big_test1 [[vau ['*ARGV*] (read-string (slurp "./prelude.kp"))] ["" "-C" "1339"]]
;big_test1 [[vau ['*ARGV*] (read-string (slurp "./prelude.kp"))] ["" "-C" "(let (a 17) (vau (x) a))"]]
big_test1 [[vau ['*ARGV*] (read-string (slurp "./prelude.kp"))] ["" "-C" "(let (a 17) a)"]]
;big_test1 [[vau ['*ARGV*] (read-string (slurp "./prelude.kp"))] []]
_ (test-case simple_add)
_ (test-case vau_with_add)
_ (test-case vau_with_add_called)
_ (test-case vau_with_passthrough)
_ (test-case vau_with_no_eval_add)
_ (test-case vau_with_wrap_add)
_ (test-case vau_with_add_p)
_ (test-case vau_with_add_p_called)
2021-08-17 18:17:42 -04:00
_ (test-case cond_test)
_ (test-case cond_vau_test)
_ (test-case cond_vau_test2)
_ (test-case combiner_test)
_ (test-case combiner_test2)
_ (test-case combiner_test3)
_ (test-case symbol_test)
_ (test-case env_test)
_ (test-case env_test2)
_ (test-case env_test3)
_ (test-case env_test4)
2021-08-22 13:03:33 -04:00
_ (test-case let1_test)
_ (test-case let2_test)
_ (test-case let3_test)
_ (test-case let4_test)
_ (test-case let5_test)
_ (test-case lambda1_test)
_ (test-case lambda2_test)
_ (test-case lambda3_test)
2021-08-22 13:03:33 -04:00
_ (test-case array_test)
_ (test-case vararg_test)
;_ (test-case do1_test)
;_ (test-case do2_test)
2021-08-22 13:03:33 -04:00
;_ (println "THE BIG SHOW")
;_ (println big_test1)
;_ (test-case big_test1)
) nil))