diff --git a/src/v4/compiler/06_translate.dag b/src/v4/compiler/06_translate.dag index 112514e74a2..bf5374f6c3f 100644 --- a/src/v4/compiler/06_translate.dag +++ b/src/v4/compiler/06_translate.dag @@ -169,7 +169,9 @@ import v4.std.target_model { target_type_expr_emitted_kind, target_type_expr_record_emitted, target_type_expr_sum_emitted, - type_expr_projection_row_closedness_from_target + type_expr_projection_graft_substrate_in_row_slot, + type_expr_projection_row_closedness_from_target, + type_expr_projection_graft_witness_gate } import v4.std.target_model { TargetAtomRealization, @@ -2413,14 +2415,43 @@ fn project_type_expression_connective_bounded( ), f: fn(original_split) { bind_outcome( - o: project_type_expression_arrow_emitted_from_original_split( + o: translate_project_arrow_split_types( original_split: original_split, - anchor: node, target: target, - projection: projection + projection: projection, + anchor: node ), - f: fn(emitted) { - Accepted { value: emitted, diagnostics: None } + f: fn(split) { + bind_outcome( + o: type_expr_projection_graft_substrate_in_row_slot( + projection: projection, + connective: Arrow, + node: node, + projected_positional: list_append( + left: split.inputs, + right: Cons { head: split.output, tail: Empty } + ) + ), + f: fn(grafted) { + bind_outcome( + o: type_expr_projection_graft_witness_gate( + source_facts: grafted, + projection: projection, + connective: Arrow + ), + f: fn(_) { + Accepted { + value: target_type_expr_arrow_emitted( + form: projection.arrow_form, + inputs: split.inputs, + output: split.output + ), + diagnostics: None + } + } + ) + } + ) } ) } diff --git a/src/v4/std/find_witness.dag b/src/v4/std/find_witness.dag index 53a7b5d5bc7..3a614d840bc 100644 --- a/src/v4/std/find_witness.dag +++ b/src/v4/std/find_witness.dag @@ -30,6 +30,9 @@ import v4.std.project_to_core_predicate { project_to_core_preservation_holds } import v4.std.refinement_widening_predicate { refinement_widening_preservation_holds } +import v4.std.type_expr_projection_discipline_zip_fold_predicate { + type_expr_projection_discipline_zip_fold_preservation_holds +} import v4.std.node { Node, Symbol, well_formed } import v4.std.witness { StructuralPropertyWitness } data find_witness_reason_no_candidate: Symbol = find_witness_reason_no_candidate @@ -40,6 +43,7 @@ data preservation_rule_constraint_satisfaction: Symbol = preservation_rule_const data preservation_rule_exact_structural_equality_zip_fold: Symbol = preservation_rule_exact_structural_equality_zip_fold data preservation_rule_refinement_widening: Symbol = preservation_rule_refinement_widening data preservation_rule_project_to_core: Symbol = preservation_rule_project_to_core +data preservation_rule_type_expr_projection_discipline_zip_fold: Symbol = preservation_rule_type_expr_projection_discipline_zip_fold type CandidateSet { candidates: List closedness: StructuralPropertyWitness @@ -78,7 +82,8 @@ fn realized_preservation_rules() -> List { preservation_rule_constraint_satisfaction, preservation_rule_exact_structural_equality_zip_fold, preservation_rule_refinement_widening, - preservation_rule_project_to_core + preservation_rule_project_to_core, + preservation_rule_type_expr_projection_discipline_zip_fold ] } fn find_witness_no_candidate_diagnostic(source_facts: Node) -> Diagnostic { @@ -147,6 +152,12 @@ fn preservation_predicate_holds( candidate: candidate, algebra: predicate.algebra ) + } else if predicate.preservation_rule == preservation_rule_type_expr_projection_discipline_zip_fold { + type_expr_projection_discipline_zip_fold_preservation_holds( + source_facts: source_facts, + candidate: candidate, + algebra: predicate.algebra + ) } else { false } diff --git a/src/v4/std/node_query.dag b/src/v4/std/node_query.dag index 27457639614..04024d0d395 100644 --- a/src/v4/std/node_query.dag +++ b/src/v4/std/node_query.dag @@ -19,7 +19,16 @@ import v4.std.diagnostic { outcome_accepted, outcome_rejected } -import v4.std.algebra { Empty, list_snoc_item } +import v4.std.algebra { Cons, Empty, FreeMonoid, list_snoc_item } +import v4.std.collection { + List, + Optional, + optional_absent, + optional_present +} +import v4.std.type_expr_projection_row_schema { + target_type_expr_field_child_slots +} import v4.std.node { Ambiguous, Arrow, @@ -54,6 +63,59 @@ fn node_labeled_child_edges(node: Node) -> List { }) } +// 🟡 gated — predicate-dissolution interim (docs/modeling-discipline.md Practice 10) — feature: node-envelope-without-named-coordinate — bind node://adhoc-sg2-envelope-peel — dissolve-on-arrival: substrate canonical peel of ≤1 named coordinate from a Conj envelope edge list; forbidden: parallel edge-label Bool filters outside std/node_query. +fn node_envelope_without_named_coordinate_go( + xs: FreeMonoid, + coordinate: Symbol, + acc: FreeMonoid, + saw_coordinate: Bool +) -> Optional> { + match xs { + Empty => optional_present(value: acc) + Cons { head: h, tail: t } => + match h.label { + Named { name: n } => + if n == coordinate { + if saw_coordinate { + optional_absent() + } else { + node_envelope_without_named_coordinate_go( + xs: t, + coordinate: coordinate, + acc: acc, + saw_coordinate: true + ) + } + } else { + node_envelope_without_named_coordinate_go( + xs: t, + coordinate: coordinate, + acc: list_snoc_item(xs: acc, item: h), + saw_coordinate: saw_coordinate + ) + } + Positional => + node_envelope_without_named_coordinate_go( + xs: t, + coordinate: coordinate, + acc: list_snoc_item(xs: acc, item: h), + saw_coordinate: saw_coordinate + ) + } + } +} + +fn node_envelope_without_named_coordinate( + children: FreeMonoid, + coordinate: Symbol +) -> Optional> { + node_envelope_without_named_coordinate_go( + xs: children, + coordinate: coordinate, + acc: Empty, + saw_coordinate: false + ) +} // 🟢 grounded — canonical single-named-edge-target lookup via name_occurrences uniqueness gate // (Practice 10: no parallel EdgeLabel walker). Misses and ambiguities fail closed via Outcome. diff --git a/src/v4/std/target_model.dag b/src/v4/std/target_model.dag index 68e1c373504..fab945bd1cd 100644 --- a/src/v4/std/target_model.dag +++ b/src/v4/std/target_model.dag @@ -47,7 +47,8 @@ import v4.std.find_witness { PreservationPredicate, UniqueOnly, find_witness, - preservation_rule_exact_structural_equality_zip_fold + preservation_rule_exact_structural_equality_zip_fold, + preservation_rule_type_expr_projection_discipline_zip_fold } import v4.std.node { Arrow, @@ -79,7 +80,8 @@ import v4.std.type_expr_projection_row_schema { target_type_expr_discipline_labeled, target_type_expr_discipline_no_edges, target_type_expr_discipline_positional, - target_type_expr_field_child_slot_discipline + target_type_expr_field_child_slot_discipline, + target_type_expr_field_child_slots } import v4.std.node_query { find_named_child, node_labeled_child_edges, node_positional_child_targets } import v4.std.logic { Bool } @@ -3590,6 +3592,26 @@ fn type_expr_projection_row_shape_envelope( } } +// SG-2 fold — substrate child slots as named coordinate (substrate TypeNode authority; not flat Conj mix). +fn type_expr_projection_graft_child_slots_coordinate( + envelope: Node, + facts: Node +) -> Node { + Node { + kind: envelope.kind, + children: list_append( + left: envelope.children, + right: Cons { + head: target_model_named_edge( + name: target_type_expr_field_child_slots, + target: facts + ), + tail: Empty + } + ) + } +} + // P2 row-shape authority — consumed by *type_expression_projection_bundle_node in language targets. fn type_expr_projection_step_algebra(projection: TargetTypeExpressionProjection) -> Node { Node { @@ -3714,6 +3736,67 @@ fn type_expr_projection_candidate_for_connective( ) } +// SG-2 fold — anchored substrate TypeNode with projected positional children (checkpoint 3). +// Labeled Conj/Disj substrates keep node.children; positional connectives pass projected list. +fn type_expr_projection_substrate_facts( + node: Node, + projected_positional: List +) -> Node { + match node.kind { + TypeNode { connective: Conj } => + Node { + kind: node.kind, + children: node.children + } + TypeNode { connective: Disj } => + Node { + kind: node.kind, + children: node.children + } + _ => + Node { + kind: node.kind, + children: fold_list( + xs: projected_positional, + empty: Empty, + cons: fn(acc, child) { + list_snoc_item( + xs: acc, + item: Edge { label: Positional, target: child } + ) + } + ) + } + } +} + +// SG-2 fold — graft substrate child slots into row envelope (discipline + delimiter metadata). +fn type_expr_projection_graft_substrate_in_row_slot( + projection: TargetTypeExpressionProjection, + connective: Connective, + node: Node, + projected_positional: List +) -> Outcome { + let facts = type_expr_projection_substrate_facts( + node: node, + projected_positional: projected_positional + ) + bind_outcome( + o: type_expr_projection_shape_for_connective( + projection: projection, + connective: connective + ), + f: fn(envelope) { + outcome_accepted( + type_expr_projection_graft_child_slots_coordinate( + envelope: envelope, + facts: facts + ) + ) + } + ) +} + fn type_expr_projection_row_witness_gate_with_rule( source_facts: Node, projection: TargetTypeExpressionProjection, @@ -3757,7 +3840,21 @@ fn type_expr_projection_row_shape_witness_gate( ) } -// SG-2 fold — connective → step_algebra row slot field (uniform key for bundle decode). +// SG-2 fold — grafted substrate witness gate (requires child_slots coordinate; discipline zip-fold). +fn type_expr_projection_graft_witness_gate( + source_facts: Node, + projection: TargetTypeExpressionProjection, + connective: Connective +) -> Outcome { + type_expr_projection_row_witness_gate_with_rule( + source_facts: source_facts, + projection: projection, + connective: connective, + preservation_rule: preservation_rule_type_expr_projection_discipline_zip_fold + ) +} + +// SG-2 fold — connective → step_algebra row slot field (uniform key for graft + bundle decode). fn type_expr_projection_row_field_for_connective(connective: Connective) -> Symbol { match connective { Atom { identity: _ } => target_type_expr_field_atom diff --git a/src/v4/std/type_expr_projection_discipline_zip_fold_predicate.dag b/src/v4/std/type_expr_projection_discipline_zip_fold_predicate.dag new file mode 100644 index 00000000000..ac219019eb3 --- /dev/null +++ b/src/v4/std/type_expr_projection_discipline_zip_fold_predicate.dag @@ -0,0 +1,138 @@ +// src/v4/std/type_expr_projection_discipline_zip_fold_predicate.dag +// Scope: SG-2 fold — discipline-aware zip-fold over row envelope + child slots. +// Anchor: connective_edges_conform (node.dag); type_expr_projection row child-slot schema. +// Status: one uniform predicate — positional=by-index, labeled=by-name; no per-connective arms. + + +module v4.std.type_expr_projection_discipline_zip_fold_predicate + +import v4.std.algebra { zip_eq } +import v4.std.collection { + Absent, + List, + Optional, + Present, + optional_absent, + optional_present +} +import v4.std.node { + Atom, + Edge, + EdgeDiscipline, + LabeledEdges, + Named, + NoEdges, + Node, + PositionalEdges, + Symbol, + TypeNode, + edges_conform, + well_formed +} +import v4.std.type_expr_projection_row_schema { + target_type_expr_discipline_labeled, + target_type_expr_discipline_no_edges, + target_type_expr_discipline_positional, + target_type_expr_field_child_slot_discipline, + target_type_expr_field_child_slots +} +import v4.std.node_query { + find_named_child, + node_envelope_without_named_coordinate +} +import v4.std.diagnostic { Accepted, Rejected } +import v4.std.exact_structural_equality_zip_fold_predicate { + exact_structural_equality_zip_fold +} + +fn type_expr_projection_discipline_from_atom(id: Symbol) -> Optional { + if id == target_type_expr_discipline_no_edges { + optional_present(value: NoEdges) + } else if id == target_type_expr_discipline_labeled { + optional_present(value: LabeledEdges) + } else if id == target_type_expr_discipline_positional { + optional_present(value: PositionalEdges) + } else { + optional_absent() + } +} + +fn type_expr_projection_child_slot_discipline_from_envelope(node: Node) -> Optional { + match find_named_child( + root: node, + name: target_type_expr_field_child_slot_discipline + ) { + Accepted { value: discipline_node, diagnostics: _ } => + match discipline_node.kind { + TypeNode { connective: Atom { identity: id } } => + type_expr_projection_discipline_from_atom(id: id) + _ => optional_absent() + } + Rejected { diagnostics: _ } => optional_absent() + } +} + +fn type_expr_projection_edge_equal(left: Edge, right: Edge) -> Bool { + (left.label == right.label) + && exact_structural_equality_zip_fold( + source_facts: left.target, + candidate: right.target + ) +} + +fn type_expr_projection_edges_exact_equal( + left: List, + right: List +) -> Bool { + zip_eq( + a: left, + b: right, + eq: fn(l, r) { type_expr_projection_edge_equal(left: l, right: r) } + ) +} + +fn type_expr_projection_row_envelope_exact_equal( + candidate: Node, + source_facts: Node +) -> Bool { + match node_envelope_without_named_coordinate( + children: source_facts.children, + coordinate: target_type_expr_field_child_slots + ) { + Present { value: envelope } => + type_expr_projection_edges_exact_equal( + left: candidate.children, + right: envelope + ) + Absent => false + } +} + +fn type_expr_projection_discipline_zip_fold_preservation_holds( + source_facts: Node, + candidate: Node, + algebra: Node, +) -> Bool { + match type_expr_projection_child_slot_discipline_from_envelope(node: candidate) { + Present { value: discipline } => + match find_named_child( + root: source_facts, + name: target_type_expr_field_child_slots + ) { + Accepted { value: child_slots_node, diagnostics: _ } => + let child_slots = child_slots_node.children + well_formed(n: source_facts) + && well_formed(n: candidate) + && well_formed(n: algebra) + && well_formed(n: child_slots_node) + && (algebra == source_facts) + && type_expr_projection_row_envelope_exact_equal( + candidate: candidate, + source_facts: source_facts + ) + && edges_conform(children: child_slots, d: discipline) + Rejected { diagnostics: _ } => false + } + Absent => false + } +} diff --git a/src/v4/std/type_expr_projection_row_schema.dag b/src/v4/std/type_expr_projection_row_schema.dag index 36a72b88a1c..f8686622fdb 100644 --- a/src/v4/std/type_expr_projection_row_schema.dag +++ b/src/v4/std/type_expr_projection_row_schema.dag @@ -1,12 +1,13 @@ // src/v4/std/type_expr_projection_row_schema.dag // Scope: SG-2 row child-slot discipline symbols (leaf — no find_witness / target_model import). -// Anchor: connective_edge_discipline (node.dag); consumed by target_model row envelope. +// Anchor: connective_edge_discipline (node.dag); consumed by target_model row envelope + zip-fold predicate. module v4.std.type_expr_projection_row_schema import v4.std.node { Symbol } data target_type_expr_field_child_slot_discipline: Symbol = target_type_expr_field_child_slot_discipline +data target_type_expr_field_child_slots: Symbol = target_type_expr_field_child_slots data target_type_expr_discipline_no_edges: Symbol = target_type_expr_discipline_no_edges data target_type_expr_discipline_labeled: Symbol = target_type_expr_discipline_labeled data target_type_expr_discipline_positional: Symbol = target_type_expr_discipline_positional