Repository navigation
CX: soundness fixes + PERF-6 + complexity enabled #336
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
4c2a6d6
c814bb5
dc4bc83
d1eee0f
9384e98
d771788
b8fbe73
e49740c
6353d1c
60d2032
0db3e82
609ae0e
e4f1fef
8f38a23
2ba1b24
32127a3
ae3301e
eb04987
9d06549
4c2e61c
1f7b6b0
058a290
98f5ddf
ffd494e
be8c10e
61c2fb3
fbe9382
825f9b4
0107368
438dae6
9da9018
7c74d15
fcbd7f6
5c7056e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| // Directed graph algebra: adjacency representation, DFS, SCC detection. | ||
| // | ||
| // Provides Kosaraju's algorithm for strongly connected component detection | ||
| // on string-labeled directed graphs. Used by the complexity analyzer for | ||
| // termination proof validation and parser mutual recursion analysis. | ||
|
|
||
| module std.graph | ||
|
|
||
| import std.termination { | ||
| ProofEdge, | ||
| DescentEvidence, Strict, NonIncreasing, DescentUnknown, | ||
| TerminationProof | ||
| } | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Invariant violation: |
||
|
|
||
| // Bidirectional adjacency representation: forward (caller→callee) and | ||
| // reverse (callee→caller) edges, keyed by node name. | ||
| // M4 note: String keys are a textual identity proxy. Post-M4, vertices | ||
| // should be structural Node references, not names. Pre-existing pattern | ||
| // (moved from complexity.dag, not introduced here). | ||
| type CallGraph { | ||
| forward: Map<String, List<String>> | ||
| reverse: Map<String, List<String>> | ||
| } | ||
|
|
||
| type CallGraphAcc { | ||
| forward: Map<String, List<String>> | ||
| reverse: Map<String, List<String>> | ||
| } | ||
|
|
||
| // DFS accumulator for Kosaraju pass 1: finish order computation. | ||
| type DfsFinishAcc { | ||
| visited: Map<String, Bool> | ||
| order: List<String> | ||
| } | ||
|
|
||
| // DFS accumulator for Kosaraju pass 2: component collection. | ||
| type SccComponentAcc { | ||
| visited: Map<String, Bool> | ||
| members: List<String> | ||
| } | ||
|
|
||
| // SCC cycle detection result. | ||
| type SccCycleAcc { | ||
| visited: Map<String, Bool> | ||
| has_cycle: Bool | ||
| } | ||
|
|
||
| fn set_has(m: Map<String, Bool>, key: String) -> Bool { | ||
| map_get(m, key) != none | ||
| } | ||
|
|
||
| // Initialize an adjacency map with empty neighbor lists for all names. | ||
| fn seed_adjacency_map(names: List<String>) -> Map<String, List<String>> { | ||
| names |> fold(init: empty_map(), f: (acc, name) => | ||
| map_insert(acc, name, []) | ||
| ) | ||
| } | ||
|
|
||
| // Build a call graph from ProofEdge values. Self-loops are excluded | ||
| // (they are handled separately by cycle detection callers). | ||
| fn build_call_graph_from_proof_edges(names: List<String>, edges: List<ProofEdge>) -> CallGraph { | ||
| let initial_forward = seed_adjacency_map(names: names) | ||
| let initial_reverse = seed_adjacency_map(names: names) | ||
| let graph_acc = edges |> filter(e => e.caller != e.callee) |> fold(init: CallGraphAcc { forward: initial_forward, reverse: initial_reverse }, f: (acc, edge) => | ||
| let forward_neighbors = match map_get(acc.forward, edge.caller) { | ||
| Some { value: ns } => concat(ns, [edge.callee]) | ||
| None => [edge.callee] | ||
| } | ||
| let reverse_neighbors = match map_get(acc.reverse, edge.callee) { | ||
| Some { value: ns } => concat(ns, [edge.caller]) | ||
| None => [edge.caller] | ||
| } | ||
| CallGraphAcc { | ||
| forward: map_insert(acc.forward, edge.caller, forward_neighbors), | ||
| reverse: map_insert(acc.reverse, edge.callee, reverse_neighbors) | ||
| } | ||
| ) | ||
| CallGraph { forward: graph_acc.forward, reverse: graph_acc.reverse } | ||
| } | ||
|
|
||
| // Kosaraju pass 1: DFS on forward graph to compute finish order. | ||
| fn dfs_finish_order(node: String, adjacency: Map<String, List<String>>, acc: DfsFinishAcc) -> DfsFinishAcc { | ||
| if set_has(m: acc.visited, key: node) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Invariant violation: |
||
| acc | ||
| } else { | ||
| let next_visited = map_insert(acc.visited, node, true) | ||
| let neighbors = match map_get(adjacency, node) { | ||
| Some { value: ns } => ns | ||
| None => [] | ||
| } | ||
| let explored = neighbors |> fold(init: DfsFinishAcc { visited: next_visited, order: acc.order }, f: (inner, neighbor) => | ||
| dfs_finish_order(node: neighbor, adjacency: adjacency, acc: inner) | ||
| ) | ||
| DfsFinishAcc { visited: explored.visited, order: list_push(explored.order, node) } | ||
| } | ||
| } | ||
|
|
||
| // Kosaraju pass 2: DFS on reverse graph to collect component members. | ||
| fn dfs_collect_component(node: String, adjacency: Map<String, List<String>>, acc: SccComponentAcc) -> SccComponentAcc { | ||
| if set_has(m: acc.visited, key: node) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Invariant violation: |
||
| acc | ||
| } else { | ||
| let next_visited = map_insert(acc.visited, node, true) | ||
| let next_members = list_push(acc.members, node) | ||
| let neighbors = match map_get(adjacency, node) { | ||
| Some { value: ns } => ns | ||
| None => [] | ||
| } | ||
| neighbors |> fold(init: SccComponentAcc { visited: next_visited, members: next_members }, f: (inner, neighbor) => | ||
| dfs_collect_component(node: neighbor, adjacency: adjacency, acc: inner) | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| // Detect whether the graph contains a strongly connected component with | ||
| // more than one node (Kosaraju's algorithm: 2-pass DFS). | ||
| fn graph_has_multi_node_scc(names: List<String>, graph: CallGraph) -> Bool { | ||
| let finish = names |> fold(init: DfsFinishAcc { visited: empty_map(), order: [] }, f: (acc, name) => | ||
| dfs_finish_order(node: name, adjacency: graph.forward, acc: acc) | ||
| ) | ||
| let result = finish.order |> reverse |> fold(init: SccCycleAcc { visited: empty_map(), has_cycle: false }, f: (acc, name) => | ||
| if acc.has_cycle || set_has(m: acc.visited, key: name) { | ||
| acc | ||
| } else { | ||
| let component = dfs_collect_component(node: name, adjacency: graph.reverse, acc: SccComponentAcc { visited: acc.visited, members: [] }) | ||
| SccCycleAcc { | ||
| visited: component.visited, | ||
| has_cycle: (component.members |> count) > 1 | ||
| } | ||
| } | ||
| ) | ||
| result.has_cycle | ||
| } | ||
|
|
||
| // ========================================================================= | ||
| // Termination proof validation | ||
| // ========================================================================= | ||
|
|
||
| // Check if a lexicographic proof edge is descending: | ||
| // first Strict dimension wins; all higher dimensions must be NonIncreasing. | ||
| fn is_lexicographic_descent(evidence: List<DescentEvidence>) -> Bool { | ||
| match evidence |> first { | ||
| None => false // no dimensions = no proof | ||
| Some { value: e } => | ||
| match e { | ||
| Strict => true | ||
| NonIncreasing => is_lexicographic_descent(evidence: evidence |> skip(1)) | ||
| DescentUnknown => false | ||
| } | ||
| } | ||
This comment was marked as resolved.
Sorry, something went wrong. |
||
| } | ||
|
|
||
| // Verify a termination proof: every cycle must have lexicographic descent. | ||
| // Algorithm: the subgraph of non-descending edges must be acyclic. | ||
| // A non-descending self-loop (caller == callee) IS a cycle. | ||
| fn is_valid_proof(proof: TerminationProof, edges: List<ProofEdge>) -> Bool { | ||
| let non_descending = edges |> filter(e => is_lexicographic_descent(evidence: e.evidence) == false) | ||
| let has_self_cycle = non_descending |> any(e => e.caller == e.callee) | ||
| if has_self_cycle { false } | ||
| else { | ||
| let members = edges |> flat_map(e => [e.caller, e.callee]) | ||
| let nd_graph = build_call_graph_from_proof_edges(names: members, edges: non_descending) | ||
| graph_has_multi_node_scc(names: members, graph: nd_graph) == false | ||
| } | ||
| } | ||
This comment was marked as resolved.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.