2021-08-09 23:58:40 -04:00
|
|
|
(with_import "./collections.kp"
|
|
|
|
|
(let (
|
|
|
|
|
; This partial eval only works via the root env,
|
|
|
|
|
; since we can't tell from outside if something takes an env
|
|
|
|
|
; or not
|
|
|
|
|
|
|
|
|
|
; Here is every form in k'
|
|
|
|
|
; True
|
|
|
|
|
; False
|
|
|
|
|
; Env: *KPEnv
|
|
|
|
|
; Combiner: KPCombiner / BuiltinCombiner: KPBuiltinCombiner
|
|
|
|
|
; String: str
|
|
|
|
|
; Symbol: str
|
|
|
|
|
; Int: int
|
|
|
|
|
; Array: rc<vec<KPValue>>
|
|
|
|
|
; Nil
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
; Ok, some more things we need / need to change
|
|
|
|
|
|
|
|
|
|
; meta...
|
|
|
|
|
|
2021-09-05 00:25:33 -04:00
|
|
|
; !!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
|
|
|
; ! To avoid exponential blowup due to evaluating function, then params, then function with params, etc etc
|
|
|
|
|
; ! Should probabally implement some form of evaluating to head-normal form controlled by boolean
|
|
|
|
|
; ! that quits as soon as it has any sort of value (I suppose the real change is only to combinators and arrays)
|
|
|
|
|
; ! so that we don't waste time re-trying etc. Anything in parameter position would be fully evaluated, so I don't think
|
|
|
|
|
; ! we'd waste overmuch, but it could make things less efficient I suppose...
|
|
|
|
|
; ! Maybe it's a bad idea - food for thought! Might need a better cacheing strategy
|
|
|
|
|
; !!!!!!!!!!!!!!!!!!!!!!!!!
|
|
|
|
|
|
2021-08-10 22:56:12 -04:00
|
|
|
; Ok, instead of just ['now v] and ['later v], we have these marked values
|
|
|
|
|
; ['val v] (v can be an array)
|
|
|
|
|
; ['later c]
|
|
|
|
|
; ['marked_array a] (a contains marked values)
|
2021-08-26 01:03:36 -04:00
|
|
|
; ['comb wrap_level de? se variadic params body <actual_function>]
|
2021-08-10 22:56:12 -04:00
|
|
|
; ['prim_comb <handler_function> <actual_function>]
|
2021-08-11 23:30:49 -04:00
|
|
|
; ['env [ ['symbol marked_value ]... ] <actual_env>]
|
2021-08-09 23:58:40 -04:00
|
|
|
|
2021-08-10 22:56:12 -04:00
|
|
|
|
|
|
|
|
val? (lambda (x) (= 'val (idx x 0)))
|
|
|
|
|
.val (lambda (x) (idx x 1))
|
|
|
|
|
later? (lambda (x) (= 'later (idx x 0)))
|
|
|
|
|
.later (lambda (x) (idx x 1))
|
|
|
|
|
marked_array? (lambda (x) (= 'marked_array (idx x 0)))
|
2021-08-17 23:09:42 -04:00
|
|
|
.marked_array (lambda (x) (idx x 1))
|
2021-08-10 22:56:12 -04:00
|
|
|
comb? (lambda (x) (= 'comb (idx x 0)))
|
|
|
|
|
.comb (lambda (x) (slice x 1 -1))
|
|
|
|
|
prim_comb? (lambda (x) (= 'prim_comb (idx x 0)))
|
|
|
|
|
.prim_comb (lambda (x) (idx x 1))
|
|
|
|
|
marked_env? (lambda (x) (= 'env (idx x 0)))
|
2021-08-11 23:30:49 -04:00
|
|
|
.env_marked (lambda (x) (idx x 1))
|
|
|
|
|
.env_real (lambda (x) (idx x 2))
|
2021-08-10 22:56:12 -04:00
|
|
|
|
2021-08-16 00:37:56 -04:00
|
|
|
env-lookup-helper (rec-lambda recurse (dict key i fail success) (cond (and (= i (- (len dict) 1)) (= nil (idx dict i))) (fail)
|
2021-08-15 01:27:53 -04:00
|
|
|
(= i (- (len dict) 1)) (recurse (idx (idx dict i) 1) key 0 fail success)
|
|
|
|
|
(= key (idx (idx dict i) 0)) (success (idx (idx dict i) 1))
|
|
|
|
|
true (recurse dict key (+ i 1) fail success)))
|
2021-08-17 01:19:38 -04:00
|
|
|
env-lookup (lambda (env key) (env-lookup-helper (idx env 1) key 0 (lambda () (error (str key " not found in env " (idx env 1)))) (lambda (x) x)))
|
2021-08-15 01:27:53 -04:00
|
|
|
|
2021-08-23 23:43:47 -04:00
|
|
|
|
|
|
|
|
; This is a conservative analysis, since we can't always tell what constructs introduce
|
|
|
|
|
; a new binding scope & would be shadowing... we should at least be able to implement it for
|
|
|
|
|
; vau/lambda, but we won't at first
|
|
|
|
|
closes_over_var_from_this_env_val (rec-lambda recurse (env body) (cond
|
|
|
|
|
(symbol? body) (env-lookup-helper (concat (slice (idx env 1) 0 -2) [nil]) body 0 (lambda () false) (lambda (x) [body]))
|
|
|
|
|
(array? body) (foldl (lambda (a x) (or a (recurse env x))) false body)
|
|
|
|
|
true false))
|
|
|
|
|
closes_over_var_from_this_env_marked (rec-lambda recurse (env x) (cond
|
|
|
|
|
(val? x) (closes_over_var_from_this_env_val env (.val x))
|
|
|
|
|
(later? x) (closes_over_var_from_this_env_val env (.later x))
|
|
|
|
|
(marked_array? x) (foldl (lambda (a x) (or a (recurse env x))) false (.marked_array x))
|
|
|
|
|
; This is where we'd be smart and remove shadowed stuff
|
|
|
|
|
(comb? x) (let (
|
2021-08-26 01:03:36 -04:00
|
|
|
[wrap_level de? se variadic params body actual_function] (.comb x)
|
2021-08-23 23:43:47 -04:00
|
|
|
; Also this won't work yet because env
|
|
|
|
|
(error (str "haven't handled comb yet really either for closes_over_var_from_this_env_marked " x))
|
|
|
|
|
) (or (recurse env se) (recurse env body)))
|
|
|
|
|
|
|
|
|
|
(prim_comb? x) false
|
|
|
|
|
; Should check to see if the env has anything form env in it, somehow? See if it has this env itself in it?
|
|
|
|
|
(marked_env? x) (error "haven't handled env in closes_over_var_from_this_env_marked!")
|
|
|
|
|
true (error (str "Something odd passed to closes_over_var_from_this_env_marked " x))
|
|
|
|
|
))
|
|
|
|
|
|
2021-08-22 13:03:33 -04:00
|
|
|
indent_str (rec-lambda recurse (i) (if (= i 0) ""
|
|
|
|
|
(str " " (recurse (- i 1)))))
|
|
|
|
|
|
2021-08-10 22:56:12 -04:00
|
|
|
strip (rec-lambda recurse (x)
|
2021-09-05 00:25:33 -04:00
|
|
|
(do (println "calling strip with " x)
|
|
|
|
|
(cond (val? x) ;(.val x)
|
|
|
|
|
(let (v (.val x)) (if (array? v) (cons array (map recurse (map (lambda (x) ['val x]) v))) v))
|
2021-08-10 22:56:12 -04:00
|
|
|
(later? x) (.later x)
|
2021-08-22 13:03:33 -04:00
|
|
|
(marked_array? x) (cons array (map recurse (idx x 1)))
|
2021-08-26 01:03:36 -04:00
|
|
|
(comb? x) (let (c (idx x 7))
|
2021-08-23 23:43:47 -04:00
|
|
|
(if (= nil c) (error (str "partial eval failed: regular stripping a combinator without a real combinator (due to nil enviornment, no doubt)" x))
|
2021-08-22 20:27:48 -04:00
|
|
|
c))
|
2021-08-10 22:56:12 -04:00
|
|
|
(prim_comb? x) (idx x 2)
|
2021-08-17 01:19:38 -04:00
|
|
|
(marked_env? x) (error "Env escaped to strip!")
|
|
|
|
|
true (error (str "some other strip? " x))
|
2021-08-16 01:20:10 -04:00
|
|
|
))
|
2021-08-10 22:56:12 -04:00
|
|
|
)
|
|
|
|
|
|
2021-09-05 00:25:33 -04:00
|
|
|
eval_strip (rec-lambda recurse (x)
|
|
|
|
|
(cond (val? x) [true (.val x)]
|
|
|
|
|
(later? x) [false (.later x)]
|
|
|
|
|
(marked_array? x) (foldl (lambda ([ok a] x) (let ([nok p] (recurse x))
|
|
|
|
|
[(and ok nok) (concat a [p])]))
|
|
|
|
|
[true []]
|
|
|
|
|
(idx x 1))
|
|
|
|
|
(comb? x) (let (c (idx x 7))
|
|
|
|
|
(if (= nil c) (error (str "partial eval failed: eval_stripping a combinator without a real combinator (due to nil enviornment, no doubt)" x))
|
|
|
|
|
[true c]))
|
|
|
|
|
(prim_comb? x) [true (idx x 2)]
|
|
|
|
|
(marked_env? x) (error "Env escaped to eval_strip!")
|
|
|
|
|
true (error (str "some other eval_strip? " x))
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
2021-08-11 23:30:49 -04:00
|
|
|
; GAH ok additionally
|
|
|
|
|
; partial_eval_helper will have to deal with combinator values (at least, primitives, I suspect all)
|
|
|
|
|
; as it might have to use them to reconstruct an expression on strip,
|
|
|
|
|
; and we will have to partially-eval previously strip'ped expressions when
|
|
|
|
|
; calling functions whose definition we partially-evaled, etc.
|
2021-08-10 22:56:12 -04:00
|
|
|
; partial_eval_helper always takes in unmarked expressions and makes marked ones, maintaining marked envs
|
2021-08-11 23:30:49 -04:00
|
|
|
;
|
|
|
|
|
; If indeed we have to keep track of non-primitive combinator values (which I think makes sense for stripping),
|
2021-08-15 01:27:53 -04:00
|
|
|
; we'll have to continually keep a map of values to their definition (we do this now!).
|
2021-08-11 23:30:49 -04:00
|
|
|
|
2021-08-23 23:43:47 -04:00
|
|
|
partial_eval_helper (rec-lambda recurse (x env comb_to_mark_map imm_eval indent)
|
2021-08-11 23:30:49 -04:00
|
|
|
(cond (= x true) [comb_to_mark_map ['val true ]]
|
|
|
|
|
(= x false) [comb_to_mark_map ['val false]]
|
2021-08-16 00:37:56 -04:00
|
|
|
(env? x) (error (str "called partial_eval with an env " x))
|
|
|
|
|
(combiner? x) [comb_to_mark_map (get-value comb_to_mark_map x)]
|
2021-08-11 23:30:49 -04:00
|
|
|
(string? x) [comb_to_mark_map ['val x]]
|
2021-08-15 01:27:53 -04:00
|
|
|
(symbol? x) [comb_to_mark_map (env-lookup env x)]
|
2021-08-11 23:30:49 -04:00
|
|
|
(int? x) [comb_to_mark_map ['val x]]
|
2021-08-09 23:58:40 -04:00
|
|
|
(and (array? x) (= 0 (len x))) (error "Partial eval on empty array")
|
2021-08-23 23:43:47 -04:00
|
|
|
(array? x) (let ( [comb_to_mark_map comb] (recurse (idx x 0) env comb_to_mark_map true (+ 1 indent))
|
|
|
|
|
_ (println (indent_str indent) "Going to do an array call!")
|
|
|
|
|
_ (println (indent_str indent) " total is " x)
|
|
|
|
|
_ (println (indent_str indent) " evaled comb is " comb)
|
2021-08-24 00:33:29 -04:00
|
|
|
comb (if (val? comb) (get-value comb_to_mark_map (.val comb))
|
|
|
|
|
comb)
|
2021-08-23 23:43:47 -04:00
|
|
|
)
|
2021-08-15 01:27:53 -04:00
|
|
|
; it seems like even if it's later we should be able to eval some?
|
|
|
|
|
; Maybe there should be something between 'later and 'comb made in vau
|
|
|
|
|
; for those sorts of cases, but maybe it doesn't matter?
|
2021-08-17 01:19:38 -04:00
|
|
|
; NOTE: it does matter, and we def need an in between.
|
|
|
|
|
; Consider a nested vau "((vau de (y) ((vau dde (z) (+ 1 (eval z dde))) y)) 17)"
|
|
|
|
|
; This won't partially evaluate much at all (besides resolving global functions)
|
|
|
|
|
; because the inner vau will never evaluate to a comb, since it didn't have a real env
|
|
|
|
|
; (because of a bug, actually, MAYYYYYBE we still don't need it?), and thus couldn't then be called
|
|
|
|
|
; even though that call would do the evaluation without any real env and would have succeded.
|
|
|
|
|
(cond (later? comb) [comb_to_mark_map ['later (cons (strip comb) (slice x 1 -1))]]
|
2021-08-23 23:43:47 -04:00
|
|
|
(prim_comb? comb) ((.prim_comb comb) env comb_to_mark_map (slice x 1 -1) imm_eval (+ 1 indent))
|
2021-08-10 22:56:12 -04:00
|
|
|
(comb? comb) (let (
|
2021-08-26 01:03:36 -04:00
|
|
|
[wrap_level de? se variadic params body actual_function] (.comb comb)
|
2021-08-17 01:19:38 -04:00
|
|
|
literal_params (slice x 1 -1)
|
2021-09-05 00:25:33 -04:00
|
|
|
[ok comb_to_mark_map appropriatly_evaled_params] ((rec-lambda param-recurse (wrap params comb_to_mark_map)
|
2021-08-15 01:27:53 -04:00
|
|
|
(if (!= 0 wrap)
|
2021-09-05 00:25:33 -04:00
|
|
|
(let ([ok eval_strip_params] (eval_strip ['marked_array params]))
|
|
|
|
|
(if (not ok) [ok comb_to_mark_map nil]
|
2021-08-15 01:27:53 -04:00
|
|
|
(let ([comb_to_mark_map evaled_params]
|
|
|
|
|
(foldl (lambda ([comb_to_mark_map ac] p)
|
2021-08-23 23:43:47 -04:00
|
|
|
(let ([comb_to_mark_map p] (recurse p env comb_to_mark_map false (+ 1 indent)))
|
2021-08-15 01:27:53 -04:00
|
|
|
[comb_to_mark_map (concat ac [p])]))
|
2021-09-05 00:25:33 -04:00
|
|
|
[comb_to_mark_map []]
|
|
|
|
|
eval_strip_params))
|
|
|
|
|
(param-recurse (- wrap 1) evaled_params comb_to_mark_map))))
|
|
|
|
|
[true comb_to_mark_map params])
|
2021-08-17 01:19:38 -04:00
|
|
|
) wrap_level (map (lambda (p) ['val p]) literal_params) comb_to_mark_map)
|
2021-09-05 00:25:33 -04:00
|
|
|
) (if (not ok) [comb_to_mark_map ['later (cons (strip comb) (slice x 1 -1))]]
|
|
|
|
|
(let (
|
2021-08-26 01:03:36 -04:00
|
|
|
final_params (if variadic (concat (slice appropriatly_evaled_params 0 (- (len params) 1))
|
|
|
|
|
[['marked_array (slice appropriatly_evaled_params (- (len params) 1) -1)]])
|
|
|
|
|
appropriatly_evaled_params)
|
2021-08-17 01:19:38 -04:00
|
|
|
de_entry (if (!= nil de?) [ [de? env] ] [])
|
2021-08-26 01:03:36 -04:00
|
|
|
_ (println (indent_str indent) "final_params params " final_params)
|
2021-08-22 13:03:33 -04:00
|
|
|
de_real_entry (if (!= nil de?) [ [de? (.env_real env)] ] [])
|
2021-08-17 01:19:38 -04:00
|
|
|
se_real_env (.env_real se)
|
2021-08-22 13:03:33 -04:00
|
|
|
inner_real_env (if (and se_real_env (or (not de?) (.env_real env)))
|
2021-08-17 01:19:38 -04:00
|
|
|
(add-dict-to-env se_real_env
|
2021-08-26 01:03:36 -04:00
|
|
|
(concat (zip params (map strip final_params))
|
2021-08-17 01:19:38 -04:00
|
|
|
de_real_entry))
|
|
|
|
|
nil)
|
2021-08-22 13:03:33 -04:00
|
|
|
_ (println (indent_str indent) "Inner_real_env is " inner_real_env " because de_real " de_real_entry " se_real_env " se_real_env)
|
2021-08-26 01:03:36 -04:00
|
|
|
inner_env ['env (concat (zip params final_params) de_entry [se]) inner_real_env]
|
2021-08-22 13:03:33 -04:00
|
|
|
_ (println (indent_str indent) "going to eval " body " with inner_env is " inner_env)
|
2021-08-23 23:43:47 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
; Ok, this might be a later with un-evaled references to parameter symbols,
|
|
|
|
|
; in which case we need to re-wrap up in a vau, since
|
|
|
|
|
; if they're used as parameters to a 'later value that might be a vau,
|
|
|
|
|
; since we don't know if they have to be evaluated and thus
|
2021-08-17 01:19:38 -04:00
|
|
|
; can't partially evaluate them.
|
2021-08-23 23:43:47 -04:00
|
|
|
; We check with closes_over_var_from_this_env_marked, which can be made more
|
|
|
|
|
; sophisticated, see its definition
|
|
|
|
|
|
|
|
|
|
[comb_to_mark_map func_result] (recurse body inner_env comb_to_mark_map imm_eval (+ 1 indent))
|
|
|
|
|
|
|
|
|
|
_ (println (indent_str indent) "evaled result of function call (imm_eval was " imm_eval ") is " func_result)
|
|
|
|
|
result (if (and (later? func_result)
|
|
|
|
|
(closes_over_var_from_this_env_marked inner_env func_result))
|
|
|
|
|
(if (= nil actual_function)
|
|
|
|
|
; this is exponential-y - we retry without imm to see if we can
|
|
|
|
|
; have a better partial eval'd later instead of giving up entirely
|
|
|
|
|
(let ([comb_to_mark_map comb] (recurse (idx x 0) env comb_to_mark_map false (+ 1 indent)))
|
|
|
|
|
[comb_to_mark_map ['later (cons (strip comb) literal_params)]])
|
|
|
|
|
[comb_to_mark_map ['later (cons actual_function literal_params)]])
|
2021-08-17 01:19:38 -04:00
|
|
|
[comb_to_mark_map func_result])
|
2021-09-05 00:25:33 -04:00
|
|
|
) result)))
|
2021-08-24 00:33:29 -04:00
|
|
|
true (error (str "Partial eval noticed that you will likely call not a function " comb " total is " x))))
|
2021-08-11 23:30:49 -04:00
|
|
|
(nil? x) [comb_to_mark_map ['val x]]
|
2021-08-16 00:37:56 -04:00
|
|
|
true (error (str "impossible partial_eval value " x))
|
2021-08-09 23:58:40 -04:00
|
|
|
)
|
|
|
|
|
)
|
2021-08-17 23:09:42 -04:00
|
|
|
is_all_vals (lambda (evaled_params) (foldl (lambda (a x) (and a (val? x))) true evaled_params))
|
2021-08-10 22:56:12 -04:00
|
|
|
needs_params_val_lambda (vau de (f_sym) (let (
|
|
|
|
|
actual_function (eval f_sym de)
|
2021-08-23 23:43:47 -04:00
|
|
|
handler (lambda (de comb_to_mark_map params imm_eval indent) (let (
|
2021-08-11 23:30:49 -04:00
|
|
|
[comb_to_mark_map evaled_params] (foldl (lambda ([comb_to_mark_map evaleds] x) (let (
|
2021-08-23 23:43:47 -04:00
|
|
|
[comb_to_mark_map evaled] (partial_eval_helper x de comb_to_mark_map false (+ 1 indent))
|
|
|
|
|
) [comb_to_mark_map (concat evaleds [evaled])])) [comb_to_mark_map []] params)
|
2021-08-11 23:30:49 -04:00
|
|
|
)
|
2021-08-17 23:09:42 -04:00
|
|
|
(if (is_all_vals evaled_params) [comb_to_mark_map ['val (lapply actual_function (map .val evaled_params))]]
|
2021-08-23 23:43:47 -04:00
|
|
|
[comb_to_mark_map ['later (cons actual_function (map strip evaled_params))]])))
|
2021-08-10 22:56:12 -04:00
|
|
|
) [f_sym ['prim_comb handler actual_function]]))
|
2021-08-10 23:26:22 -04:00
|
|
|
give_up (vau de (f_sym) (let (
|
|
|
|
|
actual_function (eval f_sym de)
|
2021-08-23 23:43:47 -04:00
|
|
|
handler (lambda (de comb_to_mark_map params imm_eval indent) [comb_to_mark_map ['later (cons actual_function params)]])
|
2021-08-10 23:26:22 -04:00
|
|
|
) [f_sym ['prim_comb handler actual_function]]))
|
2021-08-10 22:56:12 -04:00
|
|
|
|
2021-08-15 01:27:53 -04:00
|
|
|
|
2021-08-23 23:43:47 -04:00
|
|
|
parameters_evaled_proxy (lambda (pasthr_ie inner_f) (lambda (de comb_to_mark_map params imm_eval indent) (let (
|
|
|
|
|
[comb_to_mark_map evaled_params l] (foldl (lambda ([comb_to_mark_map ac i] p)
|
|
|
|
|
(let ([comb_to_mark_map p] (partial_eval_helper p de comb_to_mark_map (if (= i pasthr_ie) imm_eval false) (+ 1 indent)))
|
|
|
|
|
[comb_to_mark_map (concat ac [p]) (+ i 1)]))
|
|
|
|
|
[comb_to_mark_map [] 0]
|
2021-08-17 20:31:07 -04:00
|
|
|
params)
|
2021-08-23 23:43:47 -04:00
|
|
|
) (inner_f de comb_to_mark_map evaled_params imm_eval indent))))
|
2021-08-17 20:31:07 -04:00
|
|
|
|
2021-08-11 23:30:49 -04:00
|
|
|
root_marked_env ['env [
|
|
|
|
|
; Ok, so for combinators, it should partial eval the body.
|
|
|
|
|
; It should then check to see if the partial-evaled body has closed over
|
|
|
|
|
; any 'later values from above the combinator. If so, the combinator should
|
|
|
|
|
; evaluate to a ['later [vau de? params (strip partially_evaled_body)]], otherwise it can evaluate to a 'comb.
|
|
|
|
|
; Note that this 'later may be re-evaluated later if the parent function is called.
|
2021-08-23 23:43:47 -04:00
|
|
|
['vau ['prim_comb (lambda (de comb_to_mark_map params imm_eval indent) (let (
|
2021-08-15 01:27:53 -04:00
|
|
|
de? (if (= 3 (len params)) (idx params 0))
|
2021-08-16 11:54:05 -04:00
|
|
|
vau_de? (if (= nil de?) [] [de?])
|
2021-08-26 01:03:36 -04:00
|
|
|
[variadic vau_params] (foldl (lambda ([v a] x) (if (= x '&) [true a] [v (concat a [x])])) [false []] (if (= nil de?) (idx params 0) (idx params 1)))
|
2021-08-15 01:27:53 -04:00
|
|
|
body (if (= nil de?) (idx params 1) (idx params 2))
|
|
|
|
|
inner_env ['env (concat (map (lambda (p) [p ['later p]]) vau_params) (if (= nil de?) [] [ [de? ['later de?]] ]) [de]) nil]
|
2021-08-23 23:43:47 -04:00
|
|
|
_ (println (indent_str indent) "in vau, evaluating body with 'later params - " body)
|
|
|
|
|
[comb_to_mark_map pe_body] (partial_eval_helper body inner_env comb_to_mark_map false (+ 1 indent))
|
|
|
|
|
_ (println (indent_str indent) "in vau, result of evaluating body was " pe_body " stripping")
|
2021-08-15 01:27:53 -04:00
|
|
|
spe_body (strip pe_body)
|
2021-08-23 23:43:47 -04:00
|
|
|
_ (println (indent_str indent) "in vau, result of stripping evaled body was " spe_body)
|
2021-08-22 13:03:33 -04:00
|
|
|
for_later (= nil (.env_real de))
|
2021-08-23 23:43:47 -04:00
|
|
|
_ (println (indent_str indent) "imm_eval is " imm_eval " and for_later is " for_later " for " params " because of env being null " de)
|
|
|
|
|
) (if for_later (if (not imm_eval) [comb_to_mark_map ['later (concat [vau] vau_de? [vau_params spe_body])]]
|
2021-08-26 01:03:36 -04:00
|
|
|
[comb_to_mark_map ['comb 0 de? de variadic vau_params spe_body nil]])
|
2021-08-22 20:27:48 -04:00
|
|
|
(let (real_func (eval (concat [vau] vau_de? [vau_params spe_body]) (.env_real de))
|
2021-08-26 01:03:36 -04:00
|
|
|
marked_func ['comb 0 de? de variadic vau_params spe_body real_func]
|
2021-08-22 20:27:48 -04:00
|
|
|
_ (println (indent_str indent) "Marked func is " marked_func)
|
|
|
|
|
) [(put comb_to_mark_map real_func marked_func) marked_func])))
|
2021-08-15 01:27:53 -04:00
|
|
|
) vau]]
|
2021-08-23 23:43:47 -04:00
|
|
|
|
|
|
|
|
['wrap ['prim_comb (parameters_evaled_proxy 0 (lambda (de comb_to_mark_map [evaled] imm_eval indent)
|
2021-08-26 01:03:36 -04:00
|
|
|
(if (comb? evaled) (let ([wrap_level de? se variadic params body actual_function] (.comb evaled)
|
2021-08-23 23:43:47 -04:00
|
|
|
wrapped_actual_fun (if (= nil actual_function) nil (wrap actual_function))
|
2021-08-26 01:03:36 -04:00
|
|
|
wrapped_marked_fun ['comb (+ 1 wrap_level) de? se variadic params body wrapped_actual_fun]
|
2021-08-16 01:20:10 -04:00
|
|
|
) [(put comb_to_mark_map wrapped_actual_fun wrapped_marked_fun) wrapped_marked_fun])
|
|
|
|
|
[comb_to_mark_map ['later [wrap (strip evaled)]]]))
|
|
|
|
|
) wrap]]
|
2021-08-23 23:43:47 -04:00
|
|
|
['unwrap ['prim_comb (parameters_evaled_proxy 0 (lambda (de comb_to_mark_map [evaled] imm_eval indent)
|
2021-08-26 01:03:36 -04:00
|
|
|
(if (comb? evaled) (let ([wrap_level de? se variadic params body actual_function] (.comb evaled)
|
2021-08-23 23:43:47 -04:00
|
|
|
unwrapped_actual_fun (if (= nil actual_function) nil (unwrap actual_function))
|
2021-08-26 01:03:36 -04:00
|
|
|
unwrapped_marked_fun ['comb (- wrap_level 1) de? se variadic params body unwrapped_actual_fun]
|
2021-08-16 01:20:10 -04:00
|
|
|
) [(put comb_to_mark_map unwrapped_actual_fun unwrapped_marked_fun) unwrapped_marked_fun])
|
|
|
|
|
[comb_to_mark_map ['later [unwrap (strip evaled)]]]))
|
|
|
|
|
) unwrap]]
|
2021-08-11 23:30:49 -04:00
|
|
|
|
2021-08-23 23:43:47 -04:00
|
|
|
['eval ['prim_comb (lambda (de comb_to_mark_map params imm_eval indent) (let (
|
|
|
|
|
[comb_to_mark_map eval_env] (if (= 2 (len params)) (partial_eval_helper (idx params 1) de comb_to_mark_map false (+ 1 indent))
|
2021-08-17 01:19:38 -04:00
|
|
|
[comb_to_mark_map de])
|
2021-08-23 23:43:47 -04:00
|
|
|
) (if (not (marked_env? eval_env)) [comb_to_mark_map ['later (cons eval params)]]
|
2021-08-17 01:19:38 -04:00
|
|
|
(let (
|
2021-08-22 13:03:33 -04:00
|
|
|
_ (println (indent_str indent) "ok, env was " eval_env)
|
2021-08-23 23:43:47 -04:00
|
|
|
[comb_to_mark_map body1] (partial_eval_helper (idx params 0) de comb_to_mark_map imm_eval (+ 1 indent))
|
|
|
|
|
_ (println (indent_str indent) "after first eval of param " body1)
|
2021-08-22 13:03:33 -04:00
|
|
|
|
2021-08-23 23:43:47 -04:00
|
|
|
c_body2 (cond (val? body1) (partial_eval_helper (.val body1) eval_env comb_to_mark_map imm_eval (+ 1 indent))
|
2021-09-05 00:25:33 -04:00
|
|
|
(later? body1) [comb_to_mark_map ['later [eval (.later body1) (if (= nil (.env_real eval_env)) (slice params 1 -1) (.env_real eval_env))]]]
|
2021-08-25 01:32:24 -04:00
|
|
|
; In the case of a later value internal to marked_array, it must finish being evaluated, or at least kept as a later
|
2021-09-05 00:25:33 -04:00
|
|
|
(marked_array? body1) (let ([ok sbody1] (eval_strip body1))
|
2021-08-25 01:32:24 -04:00
|
|
|
(if ok (partial_eval_helper sbody1 eval_env comb_to_mark_map imm_eval (+ 1 indent))
|
2021-09-05 00:25:33 -04:00
|
|
|
[comb_to_mark_map ['later [eval (strip body1) (if (= nil (.env_real eval_env)) (slice params 1 -1) (.env_real eval_env))]]]))
|
2021-08-23 23:43:47 -04:00
|
|
|
(comb? body1) [comb_to_mark_map body1]
|
|
|
|
|
(prim_comb? body1) [comb_to_mark_map body1]
|
|
|
|
|
(marked_env? body1) (error "Env escaped to eval_strip!")
|
|
|
|
|
true (error (str "some other eval_strip? " body1))
|
|
|
|
|
)
|
|
|
|
|
_ (println (indent_str indent) "and body2 is " (idx c_body2 1))
|
|
|
|
|
) c_body2)
|
|
|
|
|
))) eval]]
|
|
|
|
|
|
|
|
|
|
;TODO: This could go a lot farther, not stopping after the first 'later, etc
|
|
|
|
|
; Also, GAH on odd params - but only one by one - a later odd param can't be imm_eval cuz it will
|
|
|
|
|
; be frozen if an earlier cond is 'later....
|
|
|
|
|
['cond ['prim_comb (parameters_evaled_proxy nil (lambda (de comb_to_mark_map evaled_params imm_eval indent)
|
2021-08-17 20:31:07 -04:00
|
|
|
(if (!= 0 (% (len evaled_params) 2)) (error (str "partial eval cond with odd evaled_params " evaled_params))
|
|
|
|
|
((rec-lambda recurse (i)
|
2021-08-17 23:09:42 -04:00
|
|
|
(cond (later? (idx evaled_params i)) [comb_to_mark_map ['later (cons cond (slice (map strip evaled_params) i -1))]]
|
2021-08-17 18:17:42 -04:00
|
|
|
(and (val? (idx evaled_params i))
|
2021-08-17 23:09:42 -04:00
|
|
|
(not (.val (idx evaled_params i)))) (recurse (+ 2 i))
|
|
|
|
|
true [comb_to_mark_map (idx evaled_params (+ 1 i))])
|
2021-08-17 20:31:07 -04:00
|
|
|
) 0)
|
|
|
|
|
)
|
|
|
|
|
)) cond]]
|
2021-08-10 22:56:12 -04:00
|
|
|
(needs_params_val_lambda symbol?)
|
|
|
|
|
(needs_params_val_lambda int?)
|
|
|
|
|
(needs_params_val_lambda string?)
|
2021-08-23 23:43:47 -04:00
|
|
|
; not even a gah, but kinda!
|
|
|
|
|
['combiner? ['prim_comb (parameters_evaled_proxy nil (lambda (de comb_to_mark_map [evaled_param] imm_eval indent)
|
2021-08-17 20:31:07 -04:00
|
|
|
(cond (val? evaled_param) [comb_to_mark_map ['val (combiner? (.val evaled_param))]]
|
|
|
|
|
(comb? evaled_param) [comb_to_mark_map ['val true]]
|
|
|
|
|
(prim_comb? evaled_param) [comb_to_mark_map ['val true]]
|
|
|
|
|
(later? evaled_param) [comb_to_mark_map ['later [combiner? (strip evaled_param)]]]
|
|
|
|
|
true [comb_to_mark_map ['val false]]
|
|
|
|
|
)
|
|
|
|
|
)) combiner?]]
|
2021-08-23 23:43:47 -04:00
|
|
|
; not even a gah, but kinda!
|
|
|
|
|
['env? ['prim_comb (parameters_evaled_proxy nil (lambda (de comb_to_mark_map [evaled_param] imm_eval indent)
|
2021-08-17 20:31:07 -04:00
|
|
|
(cond (val? evaled_param) [comb_to_mark_map ['val (env? (.val evaled_param))]]
|
|
|
|
|
(marked_env? evaled_param) [comb_to_mark_map ['val true]]
|
|
|
|
|
(later? evaled_param) [comb_to_mark_map ['later [env? (strip evaled_param)]]]
|
|
|
|
|
true [comb_to_mark_map ['val false]]
|
|
|
|
|
)
|
|
|
|
|
)) env?]]
|
2021-08-10 22:56:12 -04:00
|
|
|
(needs_params_val_lambda nil?)
|
|
|
|
|
(needs_params_val_lambda bool?)
|
|
|
|
|
(needs_params_val_lambda str-to-symbol)
|
|
|
|
|
(needs_params_val_lambda get-text)
|
2021-08-23 23:43:47 -04:00
|
|
|
['array? ['prim_comb (parameters_evaled_proxy nil (lambda (de comb_to_mark_map [evaled_param] imm_eval indent)
|
2021-08-17 23:09:42 -04:00
|
|
|
(cond (val? evaled_param) [comb_to_mark_map ['val (array? (.val evaled_param))]]
|
|
|
|
|
(marked_array? evaled_param) [comb_to_mark_map ['val true]]
|
|
|
|
|
(later? evaled_param) [comb_to_mark_map ['later [array? (strip evaled_param)]]]
|
|
|
|
|
true [comb_to_mark_map ['val false]]
|
|
|
|
|
)
|
|
|
|
|
)) array?]]
|
2021-08-23 23:43:47 -04:00
|
|
|
['array ['prim_comb (parameters_evaled_proxy nil (lambda (de comb_to_mark_map evaled_params imm_eval indent)
|
2021-08-17 23:09:42 -04:00
|
|
|
(if (is_all_vals evaled_params) [comb_to_mark_map ['val (map strip evaled_params)]]
|
|
|
|
|
[comb_to_mark_map ['marked_array evaled_params]]
|
|
|
|
|
)
|
|
|
|
|
)) array]]
|
2021-08-23 23:43:47 -04:00
|
|
|
['len ['prim_comb (parameters_evaled_proxy nil (lambda (de comb_to_mark_map [evaled_param] imm_eval indent)
|
2021-08-17 23:09:42 -04:00
|
|
|
(cond (val? evaled_param) [comb_to_mark_map ['val (len (.val evaled_param))]]
|
|
|
|
|
(marked_array? evaled_param) [comb_to_mark_map ['val (len (.marked_array evaled_param))]]
|
|
|
|
|
true [comb_to_mark_map ['later [len (strip evaled_param)]]]
|
|
|
|
|
)
|
|
|
|
|
)) len]]
|
2021-08-23 23:43:47 -04:00
|
|
|
['idx ['prim_comb (parameters_evaled_proxy nil (lambda (de comb_to_mark_map [evaled_array evaled_idx] imm_eval indent)
|
2021-08-17 23:09:42 -04:00
|
|
|
(cond (and (val? evaled_idx) (val? evaled_array)) [comb_to_mark_map ['val (idx (.val evaled_array) (.val evaled_idx))]]
|
|
|
|
|
(and (val? evaled_idx) (marked_array? evaled_array)) [comb_to_mark_map (idx (.marked_array evaled_array) (.val evaled_idx))]
|
|
|
|
|
true [comb_to_mark_map ['later [idx (strip evaled_array) (strip evaled_idx)]]]
|
|
|
|
|
)
|
|
|
|
|
)) idx]]
|
2021-08-23 23:43:47 -04:00
|
|
|
['slice ['prim_comb (parameters_evaled_proxy nil (lambda (de comb_to_mark_map [evaled_array evaled_begin evaled_end] imm_eval indent)
|
2021-08-17 23:09:42 -04:00
|
|
|
(cond (and (val? evaled_begin) (val? evaled_end) (val? evaled_array)) [comb_to_mark_map ['val (slice (.val evaled_array) (.val evaled_begin) (.val evaled_end))]]
|
|
|
|
|
(and (val? evaled_begin) (val? evaled_end) (marked_array? evaled_array)) [comb_to_mark_map ['marked_array (slice (.marked_array evaled_array)
|
|
|
|
|
(.val evaled_begin)
|
|
|
|
|
(.val evaled_end))]]
|
|
|
|
|
true [comb_to_mark_map ['later [slice (strip evaled_array)
|
|
|
|
|
(strip evaled_begin)
|
|
|
|
|
(strip evaled_end)]]]
|
|
|
|
|
)
|
|
|
|
|
)) slice]]
|
2021-08-23 23:43:47 -04:00
|
|
|
['concat ['prim_comb (parameters_evaled_proxy nil (lambda (de comb_to_mark_map evaled_params imm_eval indent)
|
2021-08-17 23:09:42 -04:00
|
|
|
(cond (foldl (lambda (a x) (and a (val? x))) true evaled_params) [comb_to_mark_map ['val (lapply concat (map strip evaled_params))]]
|
|
|
|
|
(foldl (lambda (a x) (and a (or (val? x) (marked_array? x)))) true evaled_params) [comb_to_mark_map ['marked_array (lapply concat (map (lambda (x)
|
|
|
|
|
(if (val? x) (map (lambda (y) ['val y]) (.val x))
|
|
|
|
|
(.marked_array x))
|
|
|
|
|
) evaled_params))]]
|
|
|
|
|
true [comb_to_mark_map ['later (cons concat (map strip evaled_params))]]
|
|
|
|
|
)
|
|
|
|
|
)) concat]]
|
2021-08-10 22:56:12 -04:00
|
|
|
(needs_params_val_lambda +)
|
|
|
|
|
(needs_params_val_lambda -)
|
|
|
|
|
(needs_params_val_lambda *)
|
|
|
|
|
(needs_params_val_lambda /)
|
|
|
|
|
(needs_params_val_lambda %)
|
|
|
|
|
(needs_params_val_lambda &)
|
|
|
|
|
(needs_params_val_lambda |)
|
|
|
|
|
(needs_params_val_lambda <<)
|
|
|
|
|
(needs_params_val_lambda >>)
|
|
|
|
|
(needs_params_val_lambda =)
|
|
|
|
|
(needs_params_val_lambda !=)
|
|
|
|
|
(needs_params_val_lambda <)
|
|
|
|
|
(needs_params_val_lambda <=)
|
|
|
|
|
(needs_params_val_lambda >)
|
|
|
|
|
(needs_params_val_lambda >=)
|
2021-08-09 23:58:40 -04:00
|
|
|
|
2021-08-23 23:43:47 -04:00
|
|
|
['and ['prim_comb (parameters_evaled_proxy nil (lambda (de comb_to_mark_map evaled_params imm_eval indent)
|
2021-08-17 23:09:42 -04:00
|
|
|
((rec-lambda recurse (i)
|
|
|
|
|
(cond (= i (- (len evaled_params) 1)) [comb_to_mark_map (idx evaled_params i)]
|
|
|
|
|
(later? (idx evaled_params i)) [comb_to_mark_map ['later (cons and (slice (map strip evaled_params) i -1))]]
|
|
|
|
|
(and (val? (idx evaled_params i))
|
|
|
|
|
(not (.val (idx evaled_params i)))) [comb_to_mark_map (idx evaled_params i)]
|
|
|
|
|
true (recurse (+ 1 i)))
|
|
|
|
|
) 0)
|
|
|
|
|
)) and]]
|
2021-08-23 23:43:47 -04:00
|
|
|
['or ['prim_comb (parameters_evaled_proxy nil (lambda (de comb_to_mark_map evaled_params imm_eval indent)
|
2021-08-17 23:09:42 -04:00
|
|
|
((rec-lambda recurse (i)
|
|
|
|
|
(cond (= i (- (len evaled_params) 1)) [comb_to_mark_map (idx evaled_params i)]
|
|
|
|
|
(later? (idx evaled_params i)) [comb_to_mark_map ['later (cons or (slice (map strip evaled_params) i -1))]]
|
|
|
|
|
(and (val? (idx evaled_params i))
|
|
|
|
|
(not (.val (idx evaled_params i)))) (recurse (+ 1 i))
|
|
|
|
|
true [comb_to_mark_map (idx evaled_params i)])
|
|
|
|
|
) 0)
|
|
|
|
|
)) or]]
|
2021-08-17 20:31:07 -04:00
|
|
|
; should make not a built in and then do here
|
2021-08-17 23:09:42 -04:00
|
|
|
; OR not - I think it will actually lower correctly partially evaled
|
2021-08-09 23:58:40 -04:00
|
|
|
|
2021-08-17 23:09:42 -04:00
|
|
|
(needs_params_val_lambda pr-str)
|
2021-08-10 22:56:12 -04:00
|
|
|
(needs_params_val_lambda str)
|
|
|
|
|
(needs_params_val_lambda prn)
|
2021-08-10 23:26:22 -04:00
|
|
|
(give_up println)
|
2021-08-17 23:09:42 -04:00
|
|
|
; really do need to figure out if we want to keep meta, and add it if so
|
2021-08-10 23:26:22 -04:00
|
|
|
(give_up meta)
|
|
|
|
|
(give_up with-meta)
|
2021-08-17 23:09:42 -04:00
|
|
|
; if we want to get fancy, we could do error/recover too
|
2021-08-10 23:26:22 -04:00
|
|
|
(give_up error)
|
|
|
|
|
(give_up recover)
|
2021-08-17 23:09:42 -04:00
|
|
|
(needs_params_val_lambda read-string)
|
2021-08-10 23:26:22 -04:00
|
|
|
(give_up slurp)
|
|
|
|
|
(give_up get_line)
|
|
|
|
|
(give_up write_file)
|
2021-08-11 23:30:49 -04:00
|
|
|
['empty_env ['env [] empty_env]]
|
2021-08-16 00:37:56 -04:00
|
|
|
nil
|
2021-08-11 23:30:49 -04:00
|
|
|
] root_env]
|
|
|
|
|
|
2021-08-16 00:37:56 -04:00
|
|
|
comb_to_mark_map (foldl (lambda (a x) (cond (= nil x) a
|
|
|
|
|
(comb? (idx x 1)) (put a (idx (idx x 1) 6) (idx x 1))
|
2021-08-11 23:30:49 -04:00
|
|
|
(prim_comb? (idx x 1)) (put a (idx (idx x 1) 2) (idx x 1))
|
|
|
|
|
true a
|
|
|
|
|
) ) empty_dict (idx root_marked_env 1))
|
2021-08-23 23:43:47 -04:00
|
|
|
partial_eval (lambda (x) (partial_eval_helper x root_marked_env comb_to_mark_map false 0))
|
2021-08-09 23:58:40 -04:00
|
|
|
)
|
2021-08-10 22:56:12 -04:00
|
|
|
(provide partial_eval strip)
|
2021-08-09 23:58:40 -04:00
|
|
|
))
|