artdag: Phase 7 opt-reduce bridges maude normal form back to a runnable DAG + 8 tests
artdag/opt-reduce: encode a DAG cone -> opt-term, mau/creduce against the optimisation module, decode the normal form back to build-entries and rebuild. Result-preserving: a 5-node blur;blur;id;bright0 chain collapses to 2 nodes and an over(I,I) dedup 3->2, both executing identically to the original; non-optimisable DAGs round-trip their radius faithfully (unary 1+1+1 -> 3). Completes Phase 7's bridge-back + equivalence boxes. maude-optimize 33/33, total 191/191. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -167,3 +167,104 @@
|
||||
"distinct pipelines stay distinct"
|
||||
(artdag/opt-same-form? "blur(src, 1)" "bright(src, 1)")
|
||||
false)
|
||||
|
||||
; ---- bridge the normal form back to a runnable DAG (opt-reduce) ----
|
||||
; result-preserving: the maude-optimised DAG executes to the same result as the
|
||||
; original, with fewer nodes. Runner is a numeric op model (blur/bright additive in
|
||||
; radius, id pass-through, over idempotent) so the pipeline algebra holds concretely.
|
||||
|
||||
(define
|
||||
mo-eq-runner
|
||||
(artdag/op-table-runner
|
||||
{:src (fn (params inputs) 0)
|
||||
:blur (fn (params inputs) (+ (first inputs) (get params :radius)))
|
||||
:bright (fn (params inputs) (+ (first inputs) (* 100 (get params :radius))))
|
||||
:id (fn (params inputs) (first inputs))
|
||||
:over (fn (params inputs) (if (= (nth inputs 0) (nth inputs 1)) (nth inputs 0) (+ (nth inputs 0) (nth inputs 1))))}))
|
||||
|
||||
(define
|
||||
mo-eq-result
|
||||
(fn (dag id) (artdag/result-of (artdag/run dag mo-eq-runner (persist/open)) id)))
|
||||
|
||||
(define
|
||||
mo-eq-opt-result
|
||||
(fn
|
||||
(dag id)
|
||||
(let
|
||||
((o (artdag/opt-reduce dag id)))
|
||||
(artdag/result-of (artdag/run o mo-eq-runner (persist/open)) (artdag/opt-last (artdag/dag-order o))))))
|
||||
|
||||
; fixture: blur;blur chain + id + zero-radius bright (all collapse to one blur)
|
||||
(define
|
||||
mo-chain5
|
||||
(artdag/build
|
||||
(list
|
||||
(list "s" "src" (list) {})
|
||||
(list "b1" "blur" (list "s") {:radius 1})
|
||||
(list "b2" "blur" (list "b1") {:radius 1})
|
||||
(list "i" "id" (list "b2") {})
|
||||
(list "z" "bright" (list "i") {:radius 0}))))
|
||||
(define mo-chain5-id (artdag/dag-id mo-chain5 "z"))
|
||||
(define mo-chain5-opt (artdag/opt-reduce mo-chain5 mo-chain5-id))
|
||||
(define mo-chain5-sink (artdag/opt-last (artdag/dag-order mo-chain5-opt)))
|
||||
|
||||
(artdag-test
|
||||
"opt-reduce: 5-node chain collapses to 2 nodes"
|
||||
(artdag/node-count mo-chain5-opt)
|
||||
2)
|
||||
|
||||
(artdag-test
|
||||
"opt-reduce: fused sink op is blur"
|
||||
(artdag/node-op (artdag/dag-get mo-chain5-opt mo-chain5-sink))
|
||||
"blur")
|
||||
|
||||
(artdag-test
|
||||
"opt-reduce: fused sink radius is the sum"
|
||||
(artdag/node-params (artdag/dag-get mo-chain5-opt mo-chain5-sink))
|
||||
{:radius 2})
|
||||
|
||||
(artdag-test
|
||||
"opt-reduce: result-preserving on chain"
|
||||
(= (mo-eq-result mo-chain5 mo-chain5-id) (mo-eq-opt-result mo-chain5 mo-chain5-id))
|
||||
true)
|
||||
|
||||
; fixture: over of identical subpipelines (idempotent dedup)
|
||||
(define
|
||||
mo-dedup
|
||||
(artdag/build
|
||||
(list
|
||||
(list "s" "src" (list) {})
|
||||
(list "b" "blur" (list "s") {:radius 2})
|
||||
(list "o" "over" (list "b" "b") {} true))))
|
||||
(define mo-dedup-id (artdag/dag-id mo-dedup "o"))
|
||||
|
||||
(artdag-test
|
||||
"opt-reduce: over dedup collapses to 2 nodes"
|
||||
(artdag/node-count (artdag/opt-reduce mo-dedup mo-dedup-id))
|
||||
2)
|
||||
|
||||
(artdag-test
|
||||
"opt-reduce: result-preserving on dedup"
|
||||
(= (mo-eq-result mo-dedup mo-dedup-id) (mo-eq-opt-result mo-dedup mo-dedup-id))
|
||||
true)
|
||||
|
||||
; non-optimisable DAG: opt-reduce is a faithful round-trip (no laws fire)
|
||||
(define
|
||||
mo-plain
|
||||
(artdag/build
|
||||
(list
|
||||
(list "s" "src" (list) {})
|
||||
(list "b" "blur" (list "s") {:radius 3}))))
|
||||
(define mo-plain-id (artdag/dag-id mo-plain "b"))
|
||||
(define mo-plain-opt (artdag/opt-reduce mo-plain mo-plain-id))
|
||||
|
||||
(artdag-test
|
||||
"opt-reduce: untouched DAG keeps its node count"
|
||||
(artdag/node-count mo-plain-opt)
|
||||
2)
|
||||
|
||||
(artdag-test
|
||||
"opt-reduce: untouched DAG keeps its radius (unary round-trip)"
|
||||
(artdag/node-params
|
||||
(artdag/dag-get mo-plain-opt (artdag/opt-last (artdag/dag-order mo-plain-opt))))
|
||||
{:radius 3})
|
||||
|
||||
Reference in New Issue
Block a user