Move over much more code, including the tricky destructuring lambda which revealed bug in the need_for_progress system - a call that takes in the dynamic env that failed and had to be re-constructed would set attempted on the call to true, but would not note the dynamic environment as one of the needed-for-progress idxs. Often I think it would be anyway, so this didn't come up too often, and of course finally revealed itself when doing nested let/lambda destructuring stuff. Fixed by having attempted record not just true or false, but in the case where it's a call that takes in the dynamic env, makes it that env's id, which gets added to the for_progress_idxs.
This commit is contained in:
125
to_compile.kp
125
to_compile.kp
@@ -12,12 +12,11 @@
|
||||
(let1 let (vY (lambda (recurse) (vau de2 (vs b) (cond (= (len vs) 0) (eval b de2)
|
||||
true (vapply let1 (array (idx vs 0) (idx vs 1) (array recurse (slice vs 2 -1) b)) de2)))))
|
||||
(let (
|
||||
;a 1
|
||||
;lcompose (lambda (g f) (lambda (& args) (lapply g (array (lapply f args)))))
|
||||
lcompose (lambda (g f) (lambda (& args) (lapply g (array (lapply f args)))))
|
||||
rec-lambda (vau se (n p b) (eval (array Y (array lambda (array n) (array lambda p b))) se))
|
||||
;if (vau de (con than & else) (cond (eval con de) (eval than de)
|
||||
; (> (len else) 0) (eval (idx else 0) de)
|
||||
; true false))
|
||||
if (vau de (con than & else) (cond (eval con de) (eval than de)
|
||||
(> (len else) 0) (eval (idx else 0) de)
|
||||
true false))
|
||||
|
||||
map (lambda (f5 l5)
|
||||
; now maybe errors on can't find helper?
|
||||
@@ -31,16 +30,124 @@
|
||||
)) (+ i4 4))
|
||||
true (recurse f4 l4 (concat n4 (array (f4 (idx l4 i4)))) (+ i4 1)))))
|
||||
(helper f5 l5 (array) 0)))
|
||||
test (map (lambda (x) (+ x 1)) (array 1 2))
|
||||
;test ((rec-lambda recurse (n) (cond (= 0 n) 1
|
||||
; true (* n (recurse (- n 1))))) 5)
|
||||
|
||||
|
||||
map_i (lambda (f l)
|
||||
(let (helper (rec-lambda recurse (f l n i)
|
||||
(cond (= i (len l)) n
|
||||
(<= i (- (len l) 4)) (recurse f l (concat n (array
|
||||
(f (+ i 0) (idx l (+ i 0)))
|
||||
(f (+ i 1) (idx l (+ i 1)))
|
||||
(f (+ i 2) (idx l (+ i 2)))
|
||||
(f (+ i 3) (idx l (+ i 3)))
|
||||
)) (+ i 4))
|
||||
true (recurse f l (concat n (array (f i (idx l i)))) (+ i 1)))))
|
||||
(helper f l (array) 0)))
|
||||
|
||||
filter_i (lambda (f l)
|
||||
(let (helper (rec-lambda recurse (f l n i)
|
||||
(if (= i (len l))
|
||||
n
|
||||
(if (f i (idx l i)) (recurse f l (concat n (array (idx l i))) (+ i 1))
|
||||
(recurse f l n (+ i 1))))))
|
||||
(helper f l (array) 0)))
|
||||
filter (lambda (f l) (filter_i (lambda (i x) (f x)) l))
|
||||
|
||||
not (lambda (x) (if x false true))
|
||||
|
||||
; Huge thanks to Oleg Kiselyov for his fantastic website
|
||||
; http://okmij.org/ftp/Computation/fixed-point-combinators.html
|
||||
Y* (lambda (& l)
|
||||
((lambda (u) (u u))
|
||||
(lambda (p)
|
||||
(map (lambda (li) (lambda (& x) (lapply (lapply li (p p)) x))) l))))
|
||||
vY* (lambda (& l)
|
||||
((lambda (u) (u u))
|
||||
(lambda (p)
|
||||
(map (lambda (li) (vau ide (& x) (vapply (lapply li (p p)) x ide))) l))))
|
||||
|
||||
let-rec (vau de (name_func body)
|
||||
(let (names (filter_i (lambda (i x) (= 0 (% i 2))) name_func)
|
||||
funcs (filter_i (lambda (i x) (= 1 (% i 2))) name_func)
|
||||
overwrite_name (idx name_func (- (len name_func) 2)))
|
||||
(eval (array let (concat (array overwrite_name (concat (array Y*) (map (lambda (f) (array lambda names f)) funcs)))
|
||||
(lapply concat (map_i (lambda (i n) (array n (array idx overwrite_name i))) names)))
|
||||
body) de)))
|
||||
let-vrec (vau de (name_func body)
|
||||
(let (names (filter_i (lambda (i x) (= 0 (% i 2))) name_func)
|
||||
funcs (filter_i (lambda (i x) (= 1 (% i 2))) name_func)
|
||||
overwrite_name (idx name_func (- (len name_func) 2)))
|
||||
(eval (array let (concat (array overwrite_name (concat (array vY*) (map (lambda (f) (array lambda names f)) funcs)))
|
||||
(lapply concat (map_i (lambda (i n) (array n (array idx overwrite_name i))) names)))
|
||||
body) de)))
|
||||
|
||||
flat_map (lambda (f l)
|
||||
(let (helper (rec-lambda recurse (f l n i)
|
||||
(if (= i (len l))
|
||||
n
|
||||
(recurse f l (concat n (f (idx l i))) (+ i 1)))))
|
||||
(helper f l (array) 0)))
|
||||
flat_map_i (lambda (f l)
|
||||
(let (helper (rec-lambda recurse (f l n i)
|
||||
(if (= i (len l))
|
||||
n
|
||||
(recurse f l (concat n (f i (idx l i))) (+ i 1)))))
|
||||
(helper f l (array) 0)))
|
||||
|
||||
; with all this, we make a destrucutring-capable let
|
||||
let (let (
|
||||
destructure_helper (rec-lambda recurse (vs i r)
|
||||
(cond (= (len vs) i) r
|
||||
(array? (idx vs i)) (let (bad_sym (str-to-symbol (str (idx vs i)))
|
||||
;new_vs (flat_map_i (lambda (i x) (array x (array idx bad_sym i))) (slice (idx vs i) 1 -1))
|
||||
new_vs (flat_map_i (lambda (i x) (array x (array idx bad_sym i))) (idx vs i))
|
||||
)
|
||||
(recurse (concat new_vs (slice vs (+ i 2) -1)) 0 (concat r (array bad_sym (idx vs (+ i 1))))))
|
||||
true (recurse vs (+ i 2) (concat r (slice vs i (+ i 2))))
|
||||
))) (vau de (vs b) (vapply let (array (destructure_helper vs 0 (array)) b) de)))
|
||||
|
||||
; and a destructuring-capable lambda!
|
||||
only_symbols (rec-lambda recurse (a i) (cond (= i (len a)) true
|
||||
(symbol? (idx a i)) (recurse a (+ i 1))
|
||||
true false))
|
||||
lambda (vau se (p b) (if (only_symbols p 0) (vapply lambda (array p b) se)
|
||||
(let (
|
||||
sym_params (map (lambda (param) (if (symbol? param) param
|
||||
(str-to-symbol (str param)))) p)
|
||||
body (array let (flat_map_i (lambda (i x) (array (idx p i) x)) sym_params) b)
|
||||
) (wrap (eval (array vau sym_params body) se)))))
|
||||
|
||||
; and rec-lambda - yes it's the same definition again
|
||||
rec-lambda (vau se (n p b) (eval (array Y (array lambda (array n) (array lambda p b))) se))
|
||||
|
||||
|
||||
test0 (map (lambda (x) (+ x 1)) (array 1 2))
|
||||
test1 (map_i (lambda (i x) (+ x i 1)) (array 1 2))
|
||||
test2 (filter_i (lambda (i x) (> i 0)) (array 1 2))
|
||||
test2 (filter (lambda ( x) (> x 1)) (array 1 2))
|
||||
test3 (not 1)
|
||||
test4 (flat_map (lambda (x) (array 1 x 2)) (array 1 2))
|
||||
test5 (flat_map_i (lambda (i x) (array i x 2)) (array 1 2))
|
||||
test6 (let ( (a b) (array 1 2) c (+ a b) ) c)
|
||||
test7 ((rec-lambda recurse (n) (cond (= 0 n) 1
|
||||
true (* n (recurse (- n 1))))) 5)
|
||||
test8 ((lambda (a b c) (+ a b c)) 1 13 14)
|
||||
test9 ((lambda (a (b c)) (+ a b c)) 1 (array 13 14))
|
||||
;monad (array 'open 3 "test_self_out" (lambda (fd code)
|
||||
; (array 'write fd "wabcdefghijklmnopqrstuvwx" (lambda (written code)
|
||||
; (array 'exit (if (= 0 written) 12 14))))))
|
||||
;old 4
|
||||
;test (+ old 4)
|
||||
;test 4
|
||||
monad (array 'write 1 "test_self_out2" (vau (written code) (map (lambda (x) (+ x 133)) (array written code))))
|
||||
;monad (array 'write 1 "test_self_out2" (vau (written code) (map (lambda (x) (+ x 133)) (array written code))))
|
||||
;monad (array 'write 1 "test_self_out2" (vau (written code) (map_i (lambda (i x) (+ x i 133)) (array written code))))
|
||||
;monad (array 'write 1 "test_self_out2" (vau (written code) (filter_i (lambda (i x) (> i 0)) (array written code))))
|
||||
;monad (array 'write 1 "test_self_out2" (vau (written code) (filter (lambda (x) (> x 0)) (array written code))))
|
||||
;monad (array 'write 1 "test_self_out2" (vau (written code) (not (array written code))))
|
||||
;monad (array 'write 1 "test_self_out2" (vau (written code) (flat_map (lambda (x) (array 1 x 2)) (array written code))))
|
||||
;monad (array 'write 1 "test_self_out2" (vau (written code) (flat_map_i (lambda (i x) (array i x 2)) (array written code))))
|
||||
monad (array 'write 1 "test_self_out2" (vau (written code) (let ( (a b) (array written code) c (+ a b test8 test9)) c)))
|
||||
;monad (array 'write 1 "test_self_out2" (vau (written code) ((lambda (a (b c)) (+ a b c)) 1 (array written code))))
|
||||
)
|
||||
monad
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user