c12f3a09eaeb74d51f44f1e46345a019c77b4c56725be49a18936ce8c9265d66.tri (5120B)
1 -- Guarded Boolean-binding elimination before stable abstraction. 2 -- Trusted well-formed resolved core only; no general beta or eta rewrite. 3 !import source_binding_analysis as A 4 !import native_math as N 5 !import source_binding_support as K 6 !export bindingTransform 7 !export bindingInspect 8 !export bindingAnalyze 9 head = K.head 10 tail = K.tail 11 kind = A.kind 12 payload = A.payload 13 lazy = K.lazy 14 choose = K.choose 15 rebuild original value = t (kind original) (t value (tail (tail original))) 16 withSpan original value = t (kind value) (t (payload value) (tail (tail original))) 17 -- Positive de Bruijn shift. Local variables below cutoff are unchanged. 18 shift amount cutoff node = lazy (_ : node) 19 (_ : case kind node of 20 2 = lazy (_ : rebuild node (N.add amount (payload node))) (_ : node) (N.gte (payload node) cutoff) 21 3 = let ab = payload node in rebuild node 22 (t (shift amount cutoff (head ab)) (shift amount cutoff (tail ab))) 23 4 = rebuild node (shift amount (N.successor cutoff) (payload node)) 24 8 = rebuild node (K.map (child : shift amount cutoff child) (payload node)) 25 _ = node) (N.zero amount) 26 -- Remove one surrounding binder. RHS is in the outer scope, not that binder's. 27 substitute rhs depth node = case kind node of 28 2 = lazy (_ : shift depth 0 rhs) 29 (_ : lazy (_ : rebuild node (N.predecessor (payload node))) (_ : node) 30 (N.gt (payload node) depth)) (N.equal (payload node) depth) 31 3 = let ab = payload node in rebuild node 32 (t (substitute rhs depth (head ab)) (substitute rhs depth (tail ab))) 33 4 = rebuild node (substitute rhs (N.successor depth) (payload node)) 34 8 = rebuild node (K.map (child : substitute rhs depth child) (payload node)) 35 _ = node 36 containsLambda node = case kind node of 37 4 = t t 38 3 = let ab = payload node in K.either (containsLambda (head ab)) (containsLambda (tail ab)) 39 8 = (let rec walk xs = K.triage t (_ : t t) 40 (a b : K.either (containsLambda a) (walk b)) xs in walk (payload node)) 41 _ = t 42 boolTree value = K.triage (t t) (child : N.zero child) (_ _ : t) value 43 leafLiteral node = lazy (_ : t t) 44 (_ : lazy (_ : N.zero (payload node)) (_ : t) (N.equal (kind node) 5)) (N.equal (kind node) 1) 45 -- Literal Boolean trees, including the core constructor spelling `t t`. 46 boolLiteral node = case kind node of 47 1 = t t 48 5 = boolTree (payload node) 49 6 = boolTree (payload node) 50 3 = let ab = payload node in K.both (leafLiteral (head ab)) (leafLiteral (tail ab)) 51 _ = t 52 -- The literal identity is matched only when saturated with one argument. 53 -- The harness authenticates the certificate's Boolean-result contract. 54 booleanRhs negation node = lazy (_ : t t) 55 (_ : lazy 56 (_ : let ab = payload node in let fun = head ab in let arg = tail ab in 57 K.both (K.either (N.equal (kind arg) 2) (boolLiteral arg)) 58 (K.both (N.equal (kind fun) 5) (A.equal (payload fun) negation))) 59 (_ : t) (N.equal (kind node) 3)) (boolLiteral node) 60 -- Input node must be Apply (Lambda body) rhs. The screening certificate is 61 -- still experimental, not a general proof for arbitrary reflective contexts. 62 -- 0..6: analysis decisions; 7: uncertified RHS; 8: any nested body lambda; 63 -- 9: not an immediate binding. Only decision 1 performs substitution. 64 rewrite selector negation node = lazy 65 (_ : let ab = payload node in let fun = head ab in 66 lazy 67 (_ : let body = payload fun in let rhs = tail ab in 68 let decision = A.classify (A.analyze selector body) in 69 lazy 70 (_ : lazy (_ : t 7 node) 71 (_ : lazy (_ : t 8 node) 72 (_ : t 1 (withSpan node (substitute rhs 0 body))) (containsLambda body)) 73 (K.not (booleanRhs negation rhs))) 74 (_ : t decision node) (N.equal decision 1)) 75 (_ : t 9 node) (N.equal (kind fun) 4)) 76 (_ : t 9 node) (N.equal (kind node) 3) 77 -- Batch transport helpers for independent host oracles. 78 shiftBatch inputs = K.map (x : let rest = tail x in shift (head x) (head rest) (tail rest)) inputs 79 substituteBatch inputs = K.map (x : substitute (head x) 0 (tail x)) inputs 80 rewriteBatch certificates nodes = K.map (node : rewrite (head certificates) (tail certificates) node) nodes 81 -- Conservative whole-core traversal: an immediate binding is a decision unit. 82 -- Rejected bindings are returned exactly, without rewriting their descendants. 83 -- Accepted bindings are substituted once; no fixed-point rewrite loop. 84 transform selector negation node = case kind node of 85 3 = let ab = payload node in 86 lazy (_ : tail (rewrite selector negation node)) 87 (_ : rebuild node (t (transform selector negation (head ab)) 88 (transform selector negation (tail ab)))) (N.equal (kind (head ab)) 4) 89 4 = rebuild node (transform selector negation (payload node)) 90 8 = rebuild node (K.map (child : transform selector negation child) (payload node)) 91 _ = node 92 -- Public compiler entrypoints close over compiler-owned literal identities. 93 -- Callers supply trusted located core, never selector/negation certificates. 94 bindingTransform core = transform K.choose K.not core 95 bindingInspect node = rewrite K.choose K.not node 96 bindingAnalyze body = A.inspect (t K.choose body)