omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / aienux / crates / aienos-explain / explain.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note aienux/crates/aienos-explain/explain.om: Pure-Omega AIENOS Explanation Bridge. |
| 3 | note Contract validation, bounded causal backtracking, cycle cutting, Kahn linearization, |
| 4 | note canonical SHA-256 bundle digests, Cortex/branch adapters, deterministic rendering, |
| 5 | note and clarification sessions. |
| 6 | note Ported from aienux crates/aienos-explain/src/{contract,bundle,build,adapters,render,session,main,lib}.rs. |
| 7 | note Compose with: si/directives/om2.om om/tls/sha2.om aienux/crates/aienos-explain/explain.om ... |
| 8 | |
| 9 | note ============================================================================ |
| 10 | note Section 1: Contract Constants, Error Codes, Enums, and Limits |
| 11 | note ============================================================================ |
| 12 | |
| 13 | const EXP_VERSION ulong 1 |
| 14 | const EXP_MAX_GRAPH_NODES ulong 65536 |
| 15 | const EXP_MAX_PARENTS_PER_NODE ulong 1024 |
| 16 | |
| 17 | const EXP_HARD_MAX_DEPTH ulong 256 |
| 18 | const EXP_HARD_MAX_NODES ulong 4096 |
| 19 | const EXP_DEFAULT_MAX_DEPTH ulong 32 |
| 20 | const EXP_DEFAULT_MAX_NODES ulong 256 |
| 21 | |
| 22 | note Stable contract error codes (1..10; 0 = ok) |
| 23 | const EXP_OK int 0 |
| 24 | const EXP_ERR_BAD_FORMAT int 1 |
| 25 | const EXP_ERR_UNSUPPORTED_VERSION int 2 |
| 26 | const EXP_ERR_UNKNOWN_CODE int 3 |
| 27 | const EXP_ERR_DUPLICATE_NODE_ID int 4 |
| 28 | const EXP_ERR_TARGET_NOT_FOUND int 5 |
| 29 | const EXP_ERR_GRAPH_TOO_LARGE int 6 |
| 30 | const EXP_ERR_STEP_OUT_OF_RANGE int 7 |
| 31 | const EXP_ERR_NOT_IN_BRANCH_SCOPE int 8 |
| 32 | const EXP_ERR_JOURNAL int 9 |
| 33 | const EXP_ERR_INVALID_LIMITS int 10 |
| 34 | |
| 35 | note Epistemic status codes (0..6) |
| 36 | const EXP_ST_UNCLASSIFIED ulong 0 |
| 37 | const EXP_ST_DIRECT_OBS ulong 1 |
| 38 | const EXP_ST_VERIFIED_FACT ulong 2 |
| 39 | const EXP_ST_INFERENCE ulong 3 |
| 40 | const EXP_ST_HYPOTHESIS ulong 4 |
| 41 | const EXP_ST_CONTRADICTION ulong 5 |
| 42 | const EXP_ST_OPERATOR_DECISION ulong 6 |
| 43 | |
| 44 | note NodeKind codes (1..13) |
| 45 | const EXP_KD_CORTEX_RECORD ulong 1 |
| 46 | const EXP_KD_CAP_METRIC ulong 2 |
| 47 | const EXP_KD_CAP_ANALYSIS ulong 3 |
| 48 | const EXP_KD_DIAGNOSTIC ulong 4 |
| 49 | const EXP_KD_HYPOTHESIS ulong 5 |
| 50 | const EXP_KD_PREDICTION ulong 6 |
| 51 | const EXP_KD_FALSIFICATION ulong 7 |
| 52 | const EXP_KD_EVAL_RECEIPT ulong 8 |
| 53 | const EXP_KD_PROMOTION_STATE ulong 9 |
| 54 | const EXP_KD_OPERATOR_DECISION ulong 10 |
| 55 | const EXP_KD_AGENT_BRANCH ulong 11 |
| 56 | const EXP_KD_CONCLUSION ulong 12 |
| 57 | const EXP_KD_ALTERNATIVE ulong 13 |
| 58 | |
| 59 | note Relation codes (1..6) |
| 60 | const EXP_REL_SUPPORTED_BY ulong 1 |
| 61 | const EXP_REL_EVALUATES ulong 2 |
| 62 | const EXP_REL_PART_OF ulong 3 |
| 63 | const EXP_REL_CONTRADICTS ulong 4 |
| 64 | const EXP_REL_ALTERNATIVE_TO ulong 5 |
| 65 | const EXP_REL_REFERENCES ulong 6 |
| 66 | |
| 67 | note Narrative Role codes (0..7, matching causal order) |
| 68 | const EXP_ROLE_OBSERVED ulong 0 |
| 69 | const EXP_ROLE_ANALYSIS ulong 1 |
| 70 | const EXP_ROLE_HYPOTHESIS ulong 2 |
| 71 | const EXP_ROLE_PREDICTION ulong 3 |
| 72 | const EXP_ROLE_FALSIFICATION ulong 4 |
| 73 | const EXP_ROLE_EVALUATION ulong 5 |
| 74 | const EXP_ROLE_DECISION ulong 6 |
| 75 | const EXP_ROLE_CONCLUSION ulong 7 |
| 76 | |
| 77 | note UncertaintyKind codes (1..10) |
| 78 | const EXP_UK_CYCLE ulong 1 |
| 79 | const EXP_UK_CONFLICT ulong 2 |
| 80 | const EXP_UK_MISSING_PARENT ulong 3 |
| 81 | const EXP_UK_MISSING_LINK ulong 4 |
| 82 | const EXP_UK_UNSUPPORTED ulong 5 |
| 83 | const EXP_UK_HYPO_NOT_FALSIFIED ulong 6 |
| 84 | const EXP_UK_INFERRED ulong 7 |
| 85 | const EXP_UK_UNKNOWN ulong 8 |
| 86 | const EXP_UK_TRUNCATED ulong 9 |
| 87 | const EXP_UK_PRODUCER_NOTE ulong 10 |
| 88 | |
| 89 | note Presentation Mode and Audience codes |
| 90 | const EXP_MODE_SUMMARY ulong 0 |
| 91 | const EXP_MODE_STEP_BY_STEP ulong 1 |
| 92 | const EXP_MODE_TECHNICAL ulong 2 |
| 93 | const EXP_MODE_ANALOGY ulong 3 |
| 94 | |
| 95 | const EXP_AUD_GENERAL ulong 0 |
| 96 | const EXP_AUD_OPERATOR ulong 1 |
| 97 | const EXP_AUD_DEVELOPER ulong 2 |
| 98 | const EXP_AUD_SYSTEMS_ENG ulong 3 |
| 99 | const EXP_AUD_RESEARCHER ulong 4 |
| 100 | const EXP_AUD_INVALID ulong 255 |
| 101 | |
| 102 | const EXP_MAX_LEVEL ulong 4 |
| 103 | const EXP_LEVEL_DEFAULT ulong 255 |
| 104 | |
| 105 | note Internal table capacities |
| 106 | const EXP_STR_CAP ulong 131072 |
| 107 | const EXP_OUT_CAP ulong 32768 |
| 108 | const EXP_CAP_NODES ulong 320 |
| 109 | const EXP_CAP_EDGES ulong 1024 |
| 110 | const EXP_CAP_MEAS ulong 256 |
| 111 | const EXP_CAP_MISS ulong 128 |
| 112 | const EXP_CAP_NOTES ulong 128 |
| 113 | const EXP_CAP_TRUNC ulong 128 |
| 114 | const EXP_CAP_SCOPE ulong 128 |
| 115 | const EXP_CAP_UNCS ulong 512 |
| 116 | const EXP_CAP_CONTRAS ulong 128 |
| 117 | const EXP_CAP_CON_INV ulong 256 |
| 118 | const EXP_CAP_ALTS ulong 128 |
| 119 | const EXP_CX_CAP ulong 64 |
| 120 | const EXP_CX_PAR_CAP ulong 256 |
| 121 | const EXP_BR_CAP ulong 16 |
| 122 | const EXP_BR_REF_CAP ulong 64 |
| 123 | const EXP_BR_TOK_CAP ulong 128 |
| 124 | const EXP_NONE ulong 999999 |
| 125 | |
| 126 | note ============================================================================ |
| 127 | note Section 2: Constant Texts and Global State Tables |
| 128 | note ============================================================================ |
| 129 | |
| 130 | text exp_txt_format "aien.explain.evidence-graph" |
| 131 | text exp_txt_builder "aienos-explain/1" |
| 132 | text exp_txt_hex "0123456789abcdef" |
| 133 | text exp_txt_cx_prod "aienos-explain/cortex-adapter" |
| 134 | text exp_txt_br_prod "aienos-explain/branch-adapter" |
| 135 | text exp_txt_cx_sys "aienos-cortex" |
| 136 | text exp_txt_cx_art "EpistemicRecord" |
| 137 | text exp_txt_cx_cbas "Cortex record confidence" |
| 138 | text exp_txt_cx_pfx "cortex:" |
| 139 | text exp_txt_br_sys "aienos-agent-state" |
| 140 | text exp_txt_br_art "LogicalBranch" |
| 141 | text exp_txt_br_pfx "branch:" |
| 142 | text exp_txt_vby_pfx "verified by " |
| 143 | text exp_txt_jmagic "ACXJ" |
| 144 | |
| 145 | note Status technical names |
| 146 | text exp_sn_0 "Unclassified" |
| 147 | text exp_sn_1 "DirectObservation" |
| 148 | text exp_sn_2 "VerifiedFact" |
| 149 | text exp_sn_3 "Inference" |
| 150 | text exp_sn_4 "Hypothesis" |
| 151 | text exp_sn_5 "Contradiction" |
| 152 | text exp_sn_6 "OperatorDecision" |
| 153 | |
| 154 | note Status plain words (render.rs status_words) |
| 155 | text exp_sw_0 "status unknown" |
| 156 | text exp_sw_1 "observed" |
| 157 | text exp_sw_2 "checked fact" |
| 158 | text exp_sw_3 "inferred, not checked" |
| 159 | text exp_sw_4 "hypothesis, not yet established" |
| 160 | text exp_sw_5 "conflict between records" |
| 161 | text exp_sw_6 "operator decision" |
| 162 | |
| 163 | note NodeKind technical names |
| 164 | text exp_kn_1 "CortexRecord" |
| 165 | text exp_kn_2 "CapabilityMetric" |
| 166 | text exp_kn_3 "CapabilityAnalysis" |
| 167 | text exp_kn_4 "Diagnostic" |
| 168 | text exp_kn_5 "Hypothesis" |
| 169 | text exp_kn_6 "Prediction" |
| 170 | text exp_kn_7 "Falsification" |
| 171 | text exp_kn_8 "EvaluationReceipt" |
| 172 | text exp_kn_9 "PromotionState" |
| 173 | text exp_kn_10 "OperatorDecision" |
| 174 | text exp_kn_11 "AgentBranch" |
| 175 | text exp_kn_12 "Conclusion" |
| 176 | text exp_kn_13 "Alternative" |
| 177 | |
| 178 | note Relation technical names |
| 179 | text exp_rn_1 "SupportedBy" |
| 180 | text exp_rn_2 "Evaluates" |
| 181 | text exp_rn_3 "PartOf" |
| 182 | text exp_rn_4 "Contradicts" |
| 183 | text exp_rn_5 "AlternativeTo" |
| 184 | text exp_rn_6 "References" |
| 185 | |
| 186 | note Role headings |
| 187 | text exp_rh_0 "Observed" |
| 188 | text exp_rh_1 "System analysis" |
| 189 | text exp_rh_2 "Hypothesis" |
| 190 | text exp_rh_3 "Prediction" |
| 191 | text exp_rh_4 "Falsification" |
| 192 | text exp_rh_5 "Evaluation" |
| 193 | text exp_rh_6 "Decision" |
| 194 | text exp_rh_7 "Conclusion" |
| 195 | |
| 196 | note Role debug names (technical mode) |
| 197 | text exp_rd_0 "Observed" |
| 198 | text exp_rd_1 "Analysis" |
| 199 | text exp_rd_2 "Hypothesis" |
| 200 | text exp_rd_3 "Prediction" |
| 201 | text exp_rd_4 "Falsification" |
| 202 | text exp_rd_5 "Evaluation" |
| 203 | text exp_rd_6 "Decision" |
| 204 | text exp_rd_7 "Conclusion" |
| 205 | |
| 206 | note Uncertainty labels |
| 207 | text exp_ul_1 "circular support" |
| 208 | text exp_ul_2 "conflicting evidence" |
| 209 | text exp_ul_3 "missing evidence" |
| 210 | text exp_ul_4 "missing link" |
| 211 | text exp_ul_5 "unsupported claim" |
| 212 | text exp_ul_6 "hypothesis not yet tested" |
| 213 | text exp_ul_7 "inferred step" |
| 214 | text exp_ul_8 "unknown status" |
| 215 | text exp_ul_9 "explanation cut short" |
| 216 | text exp_ul_10 "caveat" |
| 217 | |
| 218 | note Uncertainty technical names |
| 219 | text exp_un_1 "Cycle" |
| 220 | text exp_un_2 "ConflictingEvidence" |
| 221 | text exp_un_3 "MissingParent" |
| 222 | text exp_un_4 "MissingLink" |
| 223 | text exp_un_5 "Unsupported" |
| 224 | text exp_un_6 "HypothesisNotFalsified" |
| 225 | text exp_un_7 "Inferred" |
| 226 | text exp_un_8 "Unknown" |
| 227 | text exp_un_9 "Truncated" |
| 228 | text exp_un_10 "ProducerNote" |
| 229 | |
| 230 | note Audience names |
| 231 | text exp_an_0 "general" |
| 232 | text exp_an_1 "operator" |
| 233 | text exp_an_2 "developer" |
| 234 | text exp_an_3 "systems-engineer" |
| 235 | text exp_an_4 "researcher" |
| 236 | |
| 237 | note Presentation-only analogy catalog (never stored in bundle) |
| 238 | text exp_ana_cap "The flagged part acts like a toll booth on a busy road: every request has to pass through it, so when it slows down, everything behind it queues up." |
| 239 | text exp_ana_hyp "A hypothesis is like a mechanic's first guess about a noise in the engine: useful for deciding what to check, but not a finding until the check is done." |
| 240 | text exp_ana_fal "A falsification test is like agreeing in advance which gauge reading would prove the repair did not work." |
| 241 | text exp_ana_evl "The evaluation receipt is like an inspection sheet signed after a test drive: it records what was measured, not what was hoped." |
| 242 | text exp_ana_brn "A branch is like one line of investigation in a case file: it lists the documents it actually relied on, not every document in the archive." |
| 243 | text exp_ana_con "A contradiction is like two witnesses whose accounts do not match: the system keeps both and flags the conflict instead of picking one quietly." |
| 244 | |
| 245 | note Reusable rendering strings |
| 246 | text exp_s_concl "Conclusion: " |
| 247 | text exp_s_mreason "Main reason: " |
| 248 | text exp_s_no_reason "Main reason: none recorded. The system has no evidence supporting this conclusion.\n" |
| 249 | text exp_s_sev "Strongest evidence: " |
| 250 | text exp_s_no_ev "Strongest evidence: no observed or checked evidence on this path.\n" |
| 251 | text exp_s_munc "Main uncertainty: " |
| 252 | text exp_s_no_munc "Main uncertainty: none recorded on this path.\n" |
| 253 | text exp_s_therefore ". Therefore: " |
| 254 | text exp_s_rests " (rests on step " |
| 255 | text exp_s_refers " (refers to step " |
| 256 | text exp_s_weak "; a weak link, not a recorded one)" |
| 257 | text exp_s_from " [from " |
| 258 | text exp_s_br_ref " (referenced by the branch)" |
| 259 | text exp_s_meas " measurements: " |
| 260 | text exp_s_meas_ni "measurements: " |
| 261 | text exp_s_src " source: " |
| 262 | text exp_s_dig " digest " |
| 263 | text exp_s_unc_hdr "Uncertainty:\n" |
| 264 | text exp_s_no_gaps "- no gaps recorded on this path (inferred steps are marked as such)\n" |
| 265 | text exp_s_conf_hdr "Conflicts:\n" |
| 266 | text exp_s_inv_a " (involves " |
| 267 | text exp_s_inv_b " step(s))\n" |
| 268 | text exp_s_alt_hdr "Other explanations the system recorded:\n" |
| 269 | text exp_s_ana_hdr "Analogy (illustration only, not evidence; about step " |
| 270 | text exp_s_ana_none "Analogy: none available for this explanation.\n" |
| 271 | text exp_s_ev_itself "\nThe evidence itself:\n" |
| 272 | text exp_s_step_hdr "Step " |
| 273 | text exp_s_start_pt "It is a starting point: recorded directly, not derived.\n" |
| 274 | text exp_s_no_sup "Nothing earlier on the path supports it.\n" |
| 275 | text exp_s_it_rests "It rests on step " |
| 276 | text exp_s_it_refs "It refers to (weak link, not a recorded one) step " |
| 277 | text exp_s_rests_on_it " rests on it.\n" |
| 278 | text exp_s_caveat "Caveat: " |
| 279 | text exp_s_comes " comes from " |
| 280 | text exp_s_ref_lbl "), reference " |
| 281 | text exp_s_dig_c ", digest " |
| 282 | text exp_s_node_sc "; node " |
| 283 | text exp_s_st_sc "; status " |
| 284 | text exp_s_no_ev_att "No digests, receipts or measurements are attached to this path.\n" |
| 285 | text exp_s_no_unc_rec "No uncertainty is recorded on this path.\n" |
| 286 | text exp_s_no_alt_rec "No alternative explanations are recorded in the evidence.\n" |
| 287 | text exp_s_conflict_pfx "- conflict: " |
| 288 | text exp_s_t_expl "explanation target=" |
| 289 | text exp_s_t_prod " producer=" |
| 290 | text exp_s_t_bldr " builder=" |
| 291 | text exp_s_t_cont " contract=" |
| 292 | text exp_s_t_concl "conclusion step=" |
| 293 | text exp_s_t_step "step " |
| 294 | text exp_s_t_id " id=" |
| 295 | text exp_s_t_kind " kind=" |
| 296 | text exp_s_t_status " status=" |
| 297 | text exp_s_t_role " role=" |
| 298 | text exp_s_t_depth " depth=" |
| 299 | text exp_s_t_gnd " grounding=" |
| 300 | text exp_s_t_sup " supported_by=[" |
| 301 | text exp_s_t_stmt " statement: " |
| 302 | text exp_s_t_conf " confidence_permille=" |
| 303 | text exp_s_t_basis " basis=" |
| 304 | text exp_s_t_unspec "unspecified" |
| 305 | text exp_s_t_src " source system=" |
| 306 | text exp_s_t_art " artifact=" |
| 307 | text exp_s_t_ref " reference=" |
| 308 | text exp_s_t_dig " digest=" |
| 309 | text exp_s_t_rec " recorded_at_utc=" |
| 310 | text exp_s_t_rec_n " (informational, not used for order)\n" |
| 311 | text exp_s_t_note " note: " |
| 312 | text exp_s_t_unc "uncertainty code=" |
| 313 | text exp_s_t_node " node=" |
| 314 | text exp_s_t_rel " related=" |
| 315 | text exp_s_t_det " detail=" |
| 316 | text exp_s_t_contra "contradiction id=" |
| 317 | text exp_s_t_invs " involves=[" |
| 318 | text exp_s_t_stmteq "] statement=" |
| 319 | text exp_s_t_alt "alternative id=" |
| 320 | text exp_s_t_to " to=" |
| 321 | text exp_s_t_stmts " statement=" |
| 322 | text exp_s_t_idig "input_digest=" |
| 323 | text exp_s_t_bdig " bundle_digest=" |
| 324 | text exp_s_t_vis " visited=" |
| 325 | text exp_s_t_lim " limits=depth:" |
| 326 | text exp_s_t_nod ",nodes:" |
| 327 | text exp_s_true "true" |
| 328 | text exp_s_false "false" |
| 329 | text exp_s_miss_a " lists " |
| 330 | text exp_s_miss_b " as support, but no such artifact is available; the system has no evidence for that link" |
| 331 | text exp_s_trunc_a "support of " |
| 332 | text exp_s_trunc_b " was not followed further (depth limit " |
| 333 | text exp_s_trunc_c ", node limit " |
| 334 | text exp_s_ptrunc_a "the producer of this graph stopped collecting evidence around " |
| 335 | text exp_s_ptrunc_b " at its own limits; more support or contradictions may exist" |
| 336 | text exp_s_cyc_a " rests on " |
| 337 | text exp_s_cyc_b ", which already rests on " |
| 338 | text exp_s_cyc_c "; the loop was cut and this support is circular, not grounding" |
| 339 | text exp_s_mlink_a "no evidence for: " |
| 340 | text exp_s_unk_a "the epistemic status of step " |
| 341 | text exp_s_unk_b " is not known; it is neither observed nor verified" |
| 342 | text exp_s_unsup_a "step " |
| 343 | text exp_s_unsup_b " is a " |
| 344 | text exp_s_unsup_c " with no recorded support" |
| 345 | text exp_s_inf_a " is inferred from earlier steps, not observed directly" |
| 346 | text exp_s_hyp_a "hypothesis at step " |
| 347 | text exp_s_hyp_b " has no evaluation that tests it directly; it remains a hypothesis" |
| 348 | text exp_s_contra_a " contradicts " |
| 349 | text exp_s_contra_b " is involved in a recorded contradiction: " |
| 350 | text exp_s_br_a "Agent branch " |
| 351 | text exp_s_br_b " (lineage depth " |
| 352 | text exp_s_br_c ", parent " |
| 353 | text exp_s_br_d ") depends on " |
| 354 | text exp_s_br_e " referenced Cortex record(s)" |
| 355 | text exp_s_br_root "none (root branch)" |
| 356 | text exp_s_json_fmt "\"format\"" |
| 357 | text exp_s_json_ver "\"version\"" |
| 358 | |
| 359 | note String arena |
| 360 | global exp_str uchar 131072 |
| 361 | global exp_str_len ulong 1 |
| 362 | |
| 363 | note Output buffer for rendering and session responses |
| 364 | global exp_out uchar 32768 |
| 365 | global exp_out_len ulong 1 |
| 366 | |
| 367 | note EvidenceGraph header |
| 368 | global exp_g_fmt_off ulong 1 |
| 369 | global exp_g_fmt_len ulong 1 |
| 370 | global exp_g_ver ulong 1 |
| 371 | global exp_g_prod_off ulong 1 |
| 372 | global exp_g_prod_len ulong 1 |
| 373 | global exp_g_ncnt ulong 1 |
| 374 | global exp_g_decl_ncnt ulong 1 |
| 375 | global exp_g_ecnt ulong 1 |
| 376 | global exp_g_mcnt ulong 1 |
| 377 | global exp_g_xcnt ulong 1 |
| 378 | global exp_g_notecnt ulong 1 |
| 379 | global exp_g_tcnt ulong 1 |
| 380 | global exp_g_has_scope ulong 1 |
| 381 | global exp_g_sbr_off ulong 1 |
| 382 | global exp_g_sbr_len ulong 1 |
| 383 | global exp_g_scnt ulong 1 |
| 384 | |
| 385 | note EvidenceGraph nodes (0..EXP_CAP_NODES-1) |
| 386 | global exp_gn_id_off ulong 320 |
| 387 | global exp_gn_id_len ulong 320 |
| 388 | global exp_gn_kind ulong 320 |
| 389 | global exp_gn_status ulong 320 |
| 390 | global exp_gn_stmt_off ulong 320 |
| 391 | global exp_gn_stmt_len ulong 320 |
| 392 | global exp_gn_sys_off ulong 320 |
| 393 | global exp_gn_sys_len ulong 320 |
| 394 | global exp_gn_art_off ulong 320 |
| 395 | global exp_gn_art_len ulong 320 |
| 396 | global exp_gn_ref_off ulong 320 |
| 397 | global exp_gn_ref_len ulong 320 |
| 398 | global exp_gn_has_dig ulong 320 |
| 399 | global exp_gn_dig_off ulong 320 |
| 400 | global exp_gn_dig_len ulong 320 |
| 401 | global exp_gn_has_conf ulong 320 |
| 402 | global exp_gn_conf ulong 320 |
| 403 | global exp_gn_has_cbas ulong 320 |
| 404 | global exp_gn_cbas_off ulong 320 |
| 405 | global exp_gn_cbas_len ulong 320 |
| 406 | global exp_gn_has_ts ulong 320 |
| 407 | global exp_gn_ts ulong 320 |
| 408 | global exp_gn_decl_par ulong 320 |
| 409 | |
| 410 | note EvidenceGraph raw edges (owner node idx -> target id string, relation) |
| 411 | global exp_ge_owner ulong 1024 |
| 412 | global exp_ge_to_off ulong 1024 |
| 413 | global exp_ge_to_len ulong 1024 |
| 414 | global exp_ge_rel ulong 1024 |
| 415 | |
| 416 | note EvidenceGraph measurements |
| 417 | global exp_gm_owner ulong 256 |
| 418 | global exp_gm_name_off ulong 256 |
| 419 | global exp_gm_name_len ulong 256 |
| 420 | global exp_gm_val_off ulong 256 |
| 421 | global exp_gm_val_len ulong 256 |
| 422 | global exp_gm_unit_off ulong 256 |
| 423 | global exp_gm_unit_len ulong 256 |
| 424 | |
| 425 | note EvidenceGraph missing links |
| 426 | global exp_gx_owner ulong 128 |
| 427 | global exp_gx_exp_off ulong 128 |
| 428 | global exp_gx_exp_len ulong 128 |
| 429 | global exp_gx_rsn_off ulong 128 |
| 430 | global exp_gx_rsn_len ulong 128 |
| 431 | |
| 432 | note EvidenceGraph notes |
| 433 | global exp_go_owner ulong 128 |
| 434 | global exp_go_off ulong 128 |
| 435 | global exp_go_len ulong 128 |
| 436 | |
| 437 | note EvidenceGraph truncated_at and scope.referenced |
| 438 | global exp_gt_off ulong 128 |
| 439 | global exp_gt_len ulong 128 |
| 440 | global exp_gs_off ulong 128 |
| 441 | global exp_gs_len ulong 128 |
| 442 | |
| 443 | note Builder scratch arrays |
| 444 | global exp_w_sup_off ulong 320 |
| 445 | global exp_w_sup_cnt ulong 320 |
| 446 | global exp_w_sup_rel ulong 1024 |
| 447 | global exp_w_sup_to_off ulong 1024 |
| 448 | global exp_w_sup_to_len ulong 1024 |
| 449 | global exp_w_sup_total ulong 1 |
| 450 | global exp_w_depth ulong 320 |
| 451 | global exp_w_vis ulong 320 |
| 452 | global exp_w_trunc ulong 320 |
| 453 | global exp_w_queue ulong 320 |
| 454 | global exp_w_color ulong 320 |
| 455 | global exp_w_ve_off ulong 320 |
| 456 | global exp_w_ve_cnt ulong 320 |
| 457 | global exp_w_ve_to ulong 1024 |
| 458 | global exp_w_ve_rel ulong 1024 |
| 459 | global exp_w_ve_cut ulong 1024 |
| 460 | global exp_w_ve_total ulong 1 |
| 461 | global exp_w_stk_n ulong 640 |
| 462 | global exp_w_stk_i ulong 640 |
| 463 | global exp_w_rem ulong 320 |
| 464 | global exp_w_placed ulong 320 |
| 465 | global exp_w_pos ulong 320 |
| 466 | global exp_w_sorted_n ulong 320 |
| 467 | global exp_w_tmp_off ulong 128 |
| 468 | global exp_w_tmp_len ulong 128 |
| 469 | global exp_w_inv_off ulong 128 |
| 470 | global exp_w_inv_len ulong 128 |
| 471 | global exp_w_bfs_st ulong 2 |
| 472 | |
| 473 | note Built ExplanationBundle |
| 474 | global exp_b_scnt ulong 1 |
| 475 | global exp_b_concl_step ulong 1 |
| 476 | global exp_bs_nid ulong 320 |
| 477 | global exp_bs_role ulong 320 |
| 478 | global exp_bs_depth ulong 320 |
| 479 | global exp_bs_gnd ulong 320 |
| 480 | global exp_bs_bref ulong 320 |
| 481 | global exp_bs_sup_off ulong 320 |
| 482 | global exp_bs_sup_cnt ulong 320 |
| 483 | global exp_bs_sup_step ulong 1024 |
| 484 | global exp_bs_sup_rel ulong 1024 |
| 485 | global exp_bs_sup_total ulong 1 |
| 486 | |
| 487 | global exp_be_cnt ulong 1 |
| 488 | global exp_be_step ulong 320 |
| 489 | |
| 490 | global exp_bu_cnt ulong 1 |
| 491 | global exp_bu_kind ulong 512 |
| 492 | global exp_bu_nid_off ulong 512 |
| 493 | global exp_bu_nid_len ulong 512 |
| 494 | global exp_bu_has_rel ulong 512 |
| 495 | global exp_bu_rel_off ulong 512 |
| 496 | global exp_bu_rel_len ulong 512 |
| 497 | global exp_bu_det_off ulong 512 |
| 498 | global exp_bu_det_len ulong 512 |
| 499 | |
| 500 | global exp_bc_cnt ulong 1 |
| 501 | global exp_bc_nid ulong 128 |
| 502 | global exp_bc_stmt_off ulong 128 |
| 503 | global exp_bc_stmt_len ulong 128 |
| 504 | global exp_bc_inv_off ulong 128 |
| 505 | global exp_bc_inv_cnt ulong 128 |
| 506 | global exp_bc_inv_total ulong 1 |
| 507 | global exp_bc_inv_id_off ulong 256 |
| 508 | global exp_bc_inv_id_len ulong 256 |
| 509 | |
| 510 | global exp_ba_cnt ulong 1 |
| 511 | global exp_ba_nid ulong 128 |
| 512 | global exp_ba_to_off ulong 128 |
| 513 | global exp_ba_to_len ulong 128 |
| 514 | |
| 515 | global exp_bp_tgt_off ulong 1 |
| 516 | global exp_bp_tgt_len ulong 1 |
| 517 | global exp_bp_max_d ulong 1 |
| 518 | global exp_bp_max_n ulong 1 |
| 519 | global exp_bp_vis ulong 1 |
| 520 | global exp_bp_gnodes ulong 1 |
| 521 | global exp_bp_in_dig uchar 32 |
| 522 | global exp_bp_bun_dig uchar 32 |
| 523 | |
| 524 | note Cortex adapter store |
| 525 | global exp_cx_str uchar 16384 |
| 526 | global exp_cx_str_len ulong 1 |
| 527 | global exp_cx_cnt ulong 1 |
| 528 | global exp_cx_seq ulong 1 |
| 529 | global exp_cx_id uchar 1024 |
| 530 | global exp_cx_status ulong 64 |
| 531 | global exp_cx_stmt_off ulong 64 |
| 532 | global exp_cx_stmt_len ulong 64 |
| 533 | global exp_cx_prov_off ulong 64 |
| 534 | global exp_cx_prov_len ulong 64 |
| 535 | global exp_cx_evhash uchar 2048 |
| 536 | global exp_cx_ts ulong 64 |
| 537 | global exp_cx_has_conf ulong 64 |
| 538 | global exp_cx_conf ulong 64 |
| 539 | global exp_cx_has_vby ulong 64 |
| 540 | global exp_cx_vby_off ulong 64 |
| 541 | global exp_cx_vby_len ulong 64 |
| 542 | global exp_cx_pcnt ulong 1 |
| 543 | global exp_cx_p_own ulong 256 |
| 544 | global exp_cx_p_id uchar 4096 |
| 545 | |
| 546 | note Cortex adapter scratch |
| 547 | global exp_cxw_depth ulong 64 |
| 548 | global exp_cxw_vis ulong 64 |
| 549 | global exp_cxw_trunc ulong 64 |
| 550 | global exp_cxw_isroot ulong 64 |
| 551 | global exp_cxw_q ulong 64 |
| 552 | global exp_cxw_ord ulong 64 |
| 553 | global exp_cxw_emit ulong 64 |
| 554 | global exp_cxw_con ulong 64 |
| 555 | |
| 556 | note Agent branch adapter store |
| 557 | global exp_br_cnt ulong 1 |
| 558 | global exp_br_id uchar 256 |
| 559 | global exp_br_has_par ulong 16 |
| 560 | global exp_br_par_id uchar 256 |
| 561 | global exp_br_depth ulong 16 |
| 562 | global exp_br_ts ulong 16 |
| 563 | global exp_br_rcnt ulong 1 |
| 564 | global exp_br_r_own ulong 64 |
| 565 | global exp_br_r_id uchar 1024 |
| 566 | global exp_br_tcnt ulong 1 |
| 567 | global exp_br_t_own ulong 128 |
| 568 | global exp_br_t_val ulong 128 |
| 569 | |
| 570 | note ExplanationSession state (0=level, 1=audience) |
| 571 | global exp_sess ulong 2 |
| 572 | |
| 573 | note ============================================================================ |
| 574 | note Section 3: Contract Predicates and Enum Helpers |
| 575 | note ============================================================================ |
| 576 | |
| 577 | fn exp_status_valid code ulong to bool |
| 578 | ret le code EXP_ST_OPERATOR_DECISION |
| 579 | end |
| 580 | |
| 581 | fn exp_status_is_grounding code ulong to bool |
| 582 | if eq code EXP_ST_DIRECT_OBS |
| 583 | ret true |
| 584 | end |
| 585 | if eq code EXP_ST_VERIFIED_FACT |
| 586 | ret true |
| 587 | end |
| 588 | if eq code EXP_ST_OPERATOR_DECISION |
| 589 | ret true |
| 590 | end |
| 591 | ret false |
| 592 | end |
| 593 | |
| 594 | fn exp_kind_valid code ulong to bool |
| 595 | if lt code EXP_KD_CORTEX_RECORD |
| 596 | ret false |
| 597 | end |
| 598 | if gt code EXP_KD_ALTERNATIVE |
| 599 | ret false |
| 600 | end |
| 601 | ret true |
| 602 | end |
| 603 | |
| 604 | fn exp_rel_valid code ulong to bool |
| 605 | if lt code EXP_REL_SUPPORTED_BY |
| 606 | ret false |
| 607 | end |
| 608 | if gt code EXP_REL_REFERENCES |
| 609 | ret false |
| 610 | end |
| 611 | ret true |
| 612 | end |
| 613 | |
| 614 | fn exp_rel_is_support code ulong to bool |
| 615 | if eq code EXP_REL_SUPPORTED_BY |
| 616 | ret true |
| 617 | end |
| 618 | if eq code EXP_REL_EVALUATES |
| 619 | ret true |
| 620 | end |
| 621 | if eq code EXP_REL_PART_OF |
| 622 | ret true |
| 623 | end |
| 624 | if eq code EXP_REL_REFERENCES |
| 625 | ret true |
| 626 | end |
| 627 | ret false |
| 628 | end |
| 629 | |
| 630 | fn exp_role_of kd ulong st ulong to ulong |
| 631 | if eq kd EXP_KD_CAP_METRIC |
| 632 | ret EXP_ROLE_OBSERVED |
| 633 | end |
| 634 | if eq kd EXP_KD_CAP_ANALYSIS |
| 635 | ret EXP_ROLE_ANALYSIS |
| 636 | end |
| 637 | if eq kd EXP_KD_DIAGNOSTIC |
| 638 | ret EXP_ROLE_ANALYSIS |
| 639 | end |
| 640 | if eq kd EXP_KD_HYPOTHESIS |
| 641 | ret EXP_ROLE_HYPOTHESIS |
| 642 | end |
| 643 | if eq kd EXP_KD_PREDICTION |
| 644 | ret EXP_ROLE_PREDICTION |
| 645 | end |
| 646 | if eq kd EXP_KD_FALSIFICATION |
| 647 | ret EXP_ROLE_FALSIFICATION |
| 648 | end |
| 649 | if eq kd EXP_KD_EVAL_RECEIPT |
| 650 | ret EXP_ROLE_EVALUATION |
| 651 | end |
| 652 | if eq kd EXP_KD_PROMOTION_STATE |
| 653 | ret EXP_ROLE_DECISION |
| 654 | end |
| 655 | if eq kd EXP_KD_OPERATOR_DECISION |
| 656 | ret EXP_ROLE_DECISION |
| 657 | end |
| 658 | if eq kd EXP_KD_CONCLUSION |
| 659 | ret EXP_ROLE_CONCLUSION |
| 660 | end |
| 661 | if eq kd EXP_KD_AGENT_BRANCH |
| 662 | ret EXP_ROLE_CONCLUSION |
| 663 | end |
| 664 | if eq kd EXP_KD_ALTERNATIVE |
| 665 | ret EXP_ROLE_ANALYSIS |
| 666 | end |
| 667 | note CortexRecord maps by epistemic status |
| 668 | if eq st EXP_ST_DIRECT_OBS |
| 669 | ret EXP_ROLE_OBSERVED |
| 670 | end |
| 671 | if eq st EXP_ST_VERIFIED_FACT |
| 672 | ret EXP_ROLE_OBSERVED |
| 673 | end |
| 674 | if eq st EXP_ST_OPERATOR_DECISION |
| 675 | ret EXP_ROLE_DECISION |
| 676 | end |
| 677 | if eq st EXP_ST_HYPOTHESIS |
| 678 | ret EXP_ROLE_HYPOTHESIS |
| 679 | end |
| 680 | ret EXP_ROLE_ANALYSIS |
| 681 | end |
| 682 | |
| 683 | fn exp_uk_severity uk ulong to ulong |
| 684 | if eq uk EXP_UK_CYCLE |
| 685 | ret 0 |
| 686 | end |
| 687 | if eq uk EXP_UK_CONFLICT |
| 688 | ret 1 |
| 689 | end |
| 690 | if eq uk EXP_UK_MISSING_PARENT |
| 691 | ret 2 |
| 692 | end |
| 693 | if eq uk EXP_UK_MISSING_LINK |
| 694 | ret 3 |
| 695 | end |
| 696 | if eq uk EXP_UK_UNSUPPORTED |
| 697 | ret 4 |
| 698 | end |
| 699 | if eq uk EXP_UK_HYPO_NOT_FALSIFIED |
| 700 | ret 5 |
| 701 | end |
| 702 | if eq uk EXP_UK_UNKNOWN |
| 703 | ret 6 |
| 704 | end |
| 705 | if eq uk EXP_UK_TRUNCATED |
| 706 | ret 7 |
| 707 | end |
| 708 | if eq uk EXP_UK_PRODUCER_NOTE |
| 709 | ret 8 |
| 710 | end |
| 711 | ret 9 |
| 712 | end |
| 713 | |
| 714 | fn exp_check_limits max_d ulong max_n ulong to int |
| 715 | if eq max_d 0 |
| 716 | ret EXP_ERR_INVALID_LIMITS |
| 717 | end |
| 718 | if eq max_n 0 |
| 719 | ret EXP_ERR_INVALID_LIMITS |
| 720 | end |
| 721 | if gt max_d EXP_HARD_MAX_DEPTH |
| 722 | ret EXP_ERR_INVALID_LIMITS |
| 723 | end |
| 724 | if gt max_n EXP_HARD_MAX_NODES |
| 725 | ret EXP_ERR_INVALID_LIMITS |
| 726 | end |
| 727 | ret EXP_OK |
| 728 | end |
| 729 | |
| 730 | note ============================================================================ |
| 731 | note Section 4: String Arena, Output Buffer, Hex & Decimal Helpers |
| 732 | note ============================================================================ |
| 733 | |
| 734 | fn exp_str_reset to void |
| 735 | put exp_str_len 0 0 |
| 736 | end |
| 737 | |
| 738 | fn exp_str_mark to ulong |
| 739 | ret get exp_str_len 0 |
| 740 | end |
| 741 | |
| 742 | fn exp_str_put_b b uchar to void |
| 743 | p = get exp_str_len 0 |
| 744 | if lt p EXP_STR_CAP |
| 745 | put exp_str p b |
| 746 | put exp_str_len 0 add p 1 |
| 747 | end |
| 748 | end |
| 749 | |
| 750 | fn exp_str_add_view s view uchar * to ulong |
| 751 | start = get exp_str_len 0 |
| 752 | n = len s |
| 753 | for i ulong n bound 1024 |
| 754 | exp_str_put_b get s i |
| 755 | end |
| 756 | ret start |
| 757 | end |
| 758 | |
| 759 | fn exp_str_add_arena off ulong ln ulong to ulong |
| 760 | start = get exp_str_len 0 |
| 761 | for i ulong ln bound 1024 |
| 762 | exp_str_put_b get exp_str add off i |
| 763 | end |
| 764 | ret start |
| 765 | end |
| 766 | |
| 767 | fn exp_str_add_u64 v ulong to void |
| 768 | buf = new uchar 24 0 |
| 769 | if eq v 0 |
| 770 | exp_str_put_b uchar 48 |
| 771 | ret |
| 772 | end |
| 773 | k = ulong 0 |
| 774 | cur = v |
| 775 | while gt cur 0 bound 24 |
| 776 | put buf k uchar add 48 rem cur 10 |
| 777 | cur = div cur 10 |
| 778 | k = add k 1 |
| 779 | end |
| 780 | for i ulong k bound 24 |
| 781 | exp_str_put_b get buf sub sub k 1 i |
| 782 | end |
| 783 | end |
| 784 | |
| 785 | fn exp_str_add_hex16 src_base ulong is_cx ulong to void |
| 786 | for i ulong 16 |
| 787 | b = ulong 0 |
| 788 | if eq is_cx 1 |
| 789 | b = ulong get exp_cx_id add src_base i |
| 790 | else |
| 791 | if eq is_cx 2 |
| 792 | b = ulong get exp_br_id add src_base i |
| 793 | else |
| 794 | b = ulong get exp_br_par_id add src_base i |
| 795 | end |
| 796 | end |
| 797 | exp_str_put_b get exp_txt_hex shr b 4 |
| 798 | exp_str_put_b get exp_txt_hex and b 15 |
| 799 | end |
| 800 | end |
| 801 | |
| 802 | fn exp_str_add_hex16_view id view uchar * to void |
| 803 | for i ulong 16 |
| 804 | b = ulong get id i |
| 805 | exp_str_put_b get exp_txt_hex shr b 4 |
| 806 | exp_str_put_b get exp_txt_hex and b 15 |
| 807 | end |
| 808 | end |
| 809 | |
| 810 | fn exp_str_add_hex32_cx base ulong to void |
| 811 | for i ulong 32 |
| 812 | b = ulong get exp_cx_evhash add base i |
| 813 | exp_str_put_b get exp_txt_hex shr b 4 |
| 814 | exp_str_put_b get exp_txt_hex and b 15 |
| 815 | end |
| 816 | end |
| 817 | |
| 818 | fn exp_str_cmp a_off ulong a_ln ulong b_off ulong b_ln ulong to int |
| 819 | ml = a_ln |
| 820 | if lt b_ln ml |
| 821 | ml = b_ln |
| 822 | end |
| 823 | for i ulong ml bound 1024 |
| 824 | ca = get exp_str add a_off i |
| 825 | cb = get exp_str add b_off i |
| 826 | if lt ca cb |
| 827 | ret -1 |
| 828 | end |
| 829 | if gt ca cb |
| 830 | ret 1 |
| 831 | end |
| 832 | end |
| 833 | if lt a_ln b_ln |
| 834 | ret -1 |
| 835 | end |
| 836 | if gt a_ln b_ln |
| 837 | ret 1 |
| 838 | end |
| 839 | ret 0 |
| 840 | end |
| 841 | |
| 842 | fn exp_str_eq a_off ulong a_ln ulong b_off ulong b_ln ulong to bool |
| 843 | if ne a_ln b_ln |
| 844 | ret false |
| 845 | end |
| 846 | for i ulong a_ln bound 1024 |
| 847 | if ne get exp_str add a_off i get exp_str add b_off i |
| 848 | ret false |
| 849 | end |
| 850 | end |
| 851 | ret true |
| 852 | end |
| 853 | |
| 854 | fn exp_str_eq_view a_off ulong a_ln ulong s view uchar * to bool |
| 855 | if ne a_ln len s |
| 856 | ret false |
| 857 | end |
| 858 | for i ulong a_ln bound 1024 |
| 859 | if ne get exp_str add a_off i get s i |
| 860 | ret false |
| 861 | end |
| 862 | end |
| 863 | ret true |
| 864 | end |
| 865 | |
| 866 | fn exp_view_eq a view uchar * b view uchar * to bool |
| 867 | if ne len a len b |
| 868 | ret false |
| 869 | end |
| 870 | for i ulong len a bound 1024 |
| 871 | if ne get a i get b i |
| 872 | ret false |
| 873 | end |
| 874 | end |
| 875 | ret true |
| 876 | end |
| 877 | |
| 878 | fn exp_out_reset to void |
| 879 | put exp_out_len 0 0 |
| 880 | end |
| 881 | |
| 882 | fn exp_out_b b uchar to void |
| 883 | p = get exp_out_len 0 |
| 884 | if lt p EXP_OUT_CAP |
| 885 | put exp_out p b |
| 886 | put exp_out_len 0 add p 1 |
| 887 | end |
| 888 | end |
| 889 | |
| 890 | fn exp_out_view s view uchar * to void |
| 891 | n = len s |
| 892 | for i ulong n bound 1024 |
| 893 | exp_out_b get s i |
| 894 | end |
| 895 | end |
| 896 | |
| 897 | fn exp_out_arena off ulong ln ulong to void |
| 898 | for i ulong ln bound 1024 |
| 899 | exp_out_b get exp_str add off i |
| 900 | end |
| 901 | end |
| 902 | |
| 903 | fn exp_out_u64 v ulong to void |
| 904 | buf = new uchar 24 0 |
| 905 | if eq v 0 |
| 906 | exp_out_b uchar 48 |
| 907 | ret |
| 908 | end |
| 909 | k = ulong 0 |
| 910 | cur = v |
| 911 | while gt cur 0 bound 24 |
| 912 | put buf k uchar add 48 rem cur 10 |
| 913 | cur = div cur 10 |
| 914 | k = add k 1 |
| 915 | end |
| 916 | for i ulong k bound 24 |
| 917 | exp_out_b get buf sub sub k 1 i |
| 918 | end |
| 919 | end |
| 920 | |
| 921 | fn exp_out_dig32 is_bun ulong to void |
| 922 | for i ulong 32 |
| 923 | b = ulong 0 |
| 924 | if eq is_bun 1 |
| 925 | b = ulong get exp_bp_bun_dig i |
| 926 | else |
| 927 | b = ulong get exp_bp_in_dig i |
| 928 | end |
| 929 | exp_out_b get exp_txt_hex shr b 4 |
| 930 | exp_out_b get exp_txt_hex and b 15 |
| 931 | end |
| 932 | end |
| 933 | |
| 934 | fn exp_out_copy dst edit uchar * to ulong |
| 935 | n = get exp_out_len 0 |
| 936 | if gt n len dst |
| 937 | n = len dst |
| 938 | end |
| 939 | for i ulong n bound 32768 |
| 940 | put dst i get exp_out i |
| 941 | end |
| 942 | ret n |
| 943 | end |
| 944 | |
| 945 | fn exp_out_match_at pos ulong needle view uchar * to bool |
| 946 | nlen = len needle |
| 947 | for j ulong nlen bound 256 |
| 948 | if ne get exp_out add pos j get needle j |
| 949 | ret false |
| 950 | end |
| 951 | end |
| 952 | ret true |
| 953 | end |
| 954 | |
| 955 | fn exp_out_contains needle view uchar * to bool |
| 956 | nlen = len needle |
| 957 | olen = get exp_out_len 0 |
| 958 | if eq nlen 0 |
| 959 | ret true |
| 960 | end |
| 961 | if gt nlen olen |
| 962 | ret false |
| 963 | end |
| 964 | lim = add sub olen nlen 1 |
| 965 | for i ulong lim bound 32768 |
| 966 | if exp_out_match_at i needle |
| 967 | ret true |
| 968 | end |
| 969 | end |
| 970 | ret false |
| 971 | end |
| 972 | |
| 973 | fn exp_slice_contains_out start ulong endp ulong needle view uchar * to bool |
| 974 | nlen = len needle |
| 975 | slen = sub endp start |
| 976 | if eq nlen 0 |
| 977 | ret true |
| 978 | end |
| 979 | if gt nlen slen |
| 980 | ret false |
| 981 | end |
| 982 | lim = add sub slen nlen 1 |
| 983 | for i ulong lim bound 2048 |
| 984 | if exp_out_match_at add start i needle |
| 985 | ret true |
| 986 | end |
| 987 | end |
| 988 | ret false |
| 989 | end |
| 990 | |
| 991 | fn exp_out_any_line_has_both a view uchar * b view uchar * to bool |
| 992 | olen = get exp_out_len 0 |
| 993 | pos = ulong 0 |
| 994 | while lt pos olen bound 4096 |
| 995 | st = pos |
| 996 | while lt pos olen bound 2048 |
| 997 | if eq get exp_out pos 10 |
| 998 | break |
| 999 | end |
| 1000 | pos = add pos 1 |
| 1001 | end |
| 1002 | if exp_slice_contains_out st pos a |
| 1003 | if exp_slice_contains_out st pos b |
| 1004 | ret true |
| 1005 | end |
| 1006 | end |
| 1007 | if lt pos olen |
| 1008 | pos = add pos 1 |
| 1009 | end |
| 1010 | end |
| 1011 | ret false |
| 1012 | end |
| 1013 | |
| 1014 | note ============================================================================ |
| 1015 | note Section 5: Streaming SHA-256 Helpers for Canonical Digests |
| 1016 | note ============================================================================ |
| 1017 | |
| 1018 | fn exp_sha_u64 ctx edit ulong * v ulong to void |
| 1019 | buf = new uchar 8 0 |
| 1020 | for i ulong 8 |
| 1021 | sh = mul sub 7 i 8 |
| 1022 | put buf i uchar and shr v sh 0xff |
| 1023 | end |
| 1024 | sha256_update ctx buf |
| 1025 | end |
| 1026 | |
| 1027 | fn exp_sha_arena ctx edit ulong * off ulong ln ulong to void |
| 1028 | exp_sha_u64 ctx ln |
| 1029 | buf = new uchar 64 0 |
| 1030 | pos = ulong 0 |
| 1031 | while lt pos ln bound 2048 |
| 1032 | k = sub ln pos |
| 1033 | if gt k 64 |
| 1034 | k = 64 |
| 1035 | end |
| 1036 | for i ulong k bound 64 |
| 1037 | put buf i get exp_str add add off pos i |
| 1038 | end |
| 1039 | sha256_update ctx part buf 0 k |
| 1040 | pos = add pos k |
| 1041 | end |
| 1042 | end |
| 1043 | |
| 1044 | fn exp_sha_tag ctx edit ulong * tag ulong to void |
| 1045 | buf = new uchar 1 0 |
| 1046 | put buf 0 uchar and tag 0xff |
| 1047 | sha256_update ctx buf |
| 1048 | end |
| 1049 | |
| 1050 | note ============================================================================ |
| 1051 | note Section 6: EvidenceGraph Construction & Contract Validation (contract.rs) |
| 1052 | note ============================================================================ |
| 1053 | |
| 1054 | fn exp_g_reset_prod prod view uchar * to void |
| 1055 | exp_str_reset |
| 1056 | fo = exp_str_add_view exp_txt_format |
| 1057 | put exp_g_fmt_off 0 fo |
| 1058 | put exp_g_fmt_len 0 len exp_txt_format |
| 1059 | put exp_g_ver 0 EXP_VERSION |
| 1060 | po = exp_str_add_view prod |
| 1061 | put exp_g_prod_off 0 po |
| 1062 | put exp_g_prod_len 0 len prod |
| 1063 | put exp_g_ncnt 0 0 |
| 1064 | put exp_g_decl_ncnt 0 0 |
| 1065 | put exp_g_ecnt 0 0 |
| 1066 | put exp_g_mcnt 0 0 |
| 1067 | put exp_g_xcnt 0 0 |
| 1068 | put exp_g_notecnt 0 0 |
| 1069 | put exp_g_tcnt 0 0 |
| 1070 | put exp_g_has_scope 0 0 |
| 1071 | put exp_g_sbr_off 0 0 |
| 1072 | put exp_g_sbr_len 0 0 |
| 1073 | put exp_g_scnt 0 0 |
| 1074 | end |
| 1075 | |
| 1076 | fn exp_g_set_format fmt view uchar * to void |
| 1077 | fo = exp_str_add_view fmt |
| 1078 | put exp_g_fmt_off 0 fo |
| 1079 | put exp_g_fmt_len 0 len fmt |
| 1080 | end |
| 1081 | |
| 1082 | fn exp_g_set_version ver ulong to void |
| 1083 | put exp_g_ver 0 ver |
| 1084 | end |
| 1085 | |
| 1086 | fn exp_g_set_producer prod view uchar * to void |
| 1087 | po = exp_str_add_view prod |
| 1088 | put exp_g_prod_off 0 po |
| 1089 | put exp_g_prod_len 0 len prod |
| 1090 | end |
| 1091 | |
| 1092 | fn exp_g_add_node_raw id_o ulong id_l ulong kd ulong st ulong to ulong |
| 1093 | idx = get exp_g_ncnt 0 |
| 1094 | if ge idx EXP_CAP_NODES |
| 1095 | ret EXP_NONE |
| 1096 | end |
| 1097 | put exp_gn_id_off idx id_o |
| 1098 | put exp_gn_id_len idx id_l |
| 1099 | put exp_gn_kind idx kd |
| 1100 | put exp_gn_status idx st |
| 1101 | put exp_gn_stmt_off idx 0 |
| 1102 | put exp_gn_stmt_len idx 0 |
| 1103 | put exp_gn_sys_off idx 0 |
| 1104 | put exp_gn_sys_len idx 0 |
| 1105 | put exp_gn_art_off idx 0 |
| 1106 | put exp_gn_art_len idx 0 |
| 1107 | put exp_gn_ref_off idx 0 |
| 1108 | put exp_gn_ref_len idx 0 |
| 1109 | put exp_gn_has_dig idx 0 |
| 1110 | put exp_gn_dig_off idx 0 |
| 1111 | put exp_gn_dig_len idx 0 |
| 1112 | put exp_gn_has_conf idx 0 |
| 1113 | put exp_gn_conf idx 0 |
| 1114 | put exp_gn_has_cbas idx 0 |
| 1115 | put exp_gn_cbas_off idx 0 |
| 1116 | put exp_gn_cbas_len idx 0 |
| 1117 | put exp_gn_has_ts idx 0 |
| 1118 | put exp_gn_ts idx 0 |
| 1119 | put exp_gn_decl_par idx 0 |
| 1120 | put exp_g_ncnt 0 add idx 1 |
| 1121 | put exp_g_decl_ncnt 0 add idx 1 |
| 1122 | ret idx |
| 1123 | end |
| 1124 | |
| 1125 | fn exp_g_add_node id view uchar * kd ulong st ulong stmt view uchar * to ulong |
| 1126 | id_o = exp_str_add_view id |
| 1127 | nid = exp_g_add_node_raw id_o len id kd st |
| 1128 | if eq nid EXP_NONE |
| 1129 | ret EXP_NONE |
| 1130 | end |
| 1131 | st_o = exp_str_add_view stmt |
| 1132 | put exp_gn_stmt_off nid st_o |
| 1133 | put exp_gn_stmt_len nid len stmt |
| 1134 | ret nid |
| 1135 | end |
| 1136 | |
| 1137 | fn exp_g_set_source_sa nid ulong sys view uchar * art view uchar * to void |
| 1138 | if ge nid get exp_g_ncnt 0 |
| 1139 | ret |
| 1140 | end |
| 1141 | so = exp_str_add_view sys |
| 1142 | ao = exp_str_add_view art |
| 1143 | put exp_gn_sys_off nid so |
| 1144 | put exp_gn_sys_len nid len sys |
| 1145 | put exp_gn_art_off nid ao |
| 1146 | put exp_gn_art_len nid len art |
| 1147 | end |
| 1148 | |
| 1149 | fn exp_g_set_source_ref nid ulong rf view uchar * to void |
| 1150 | if ge nid get exp_g_ncnt 0 |
| 1151 | ret |
| 1152 | end |
| 1153 | ro = exp_str_add_view rf |
| 1154 | put exp_gn_ref_off nid ro |
| 1155 | put exp_gn_ref_len nid len rf |
| 1156 | end |
| 1157 | |
| 1158 | fn exp_g_set_digest nid ulong dig view uchar * to void |
| 1159 | if ge nid get exp_g_ncnt 0 |
| 1160 | ret |
| 1161 | end |
| 1162 | dig_o = exp_str_add_view dig |
| 1163 | put exp_gn_has_dig nid 1 |
| 1164 | put exp_gn_dig_off nid dig_o |
| 1165 | put exp_gn_dig_len nid len dig |
| 1166 | end |
| 1167 | |
| 1168 | fn exp_g_set_conf nid ulong pm ulong bas view uchar * to void |
| 1169 | if ge nid get exp_g_ncnt 0 |
| 1170 | ret |
| 1171 | end |
| 1172 | put exp_gn_has_conf nid 1 |
| 1173 | put exp_gn_conf nid pm |
| 1174 | bo = exp_str_add_view bas |
| 1175 | put exp_gn_has_cbas nid 1 |
| 1176 | put exp_gn_cbas_off nid bo |
| 1177 | put exp_gn_cbas_len nid len bas |
| 1178 | end |
| 1179 | |
| 1180 | fn exp_g_set_ts nid ulong ts ulong to void |
| 1181 | if ge nid get exp_g_ncnt 0 |
| 1182 | ret |
| 1183 | end |
| 1184 | put exp_gn_has_ts nid 1 |
| 1185 | put exp_gn_ts nid ts |
| 1186 | end |
| 1187 | |
| 1188 | fn exp_g_add_edge_raw nid ulong to_o ulong to_l ulong rl ulong to void |
| 1189 | ec = get exp_g_ecnt 0 |
| 1190 | if ge ec EXP_CAP_EDGES |
| 1191 | ret |
| 1192 | end |
| 1193 | put exp_ge_owner ec nid |
| 1194 | put exp_ge_to_off ec to_o |
| 1195 | put exp_ge_to_len ec to_l |
| 1196 | put exp_ge_rel ec rl |
| 1197 | put exp_g_ecnt 0 add ec 1 |
| 1198 | end |
| 1199 | |
| 1200 | fn exp_g_add_edge nid ulong target view uchar * rl ulong to void |
| 1201 | to_o = exp_str_add_view target |
| 1202 | exp_g_add_edge_raw nid to_o len target rl |
| 1203 | end |
| 1204 | |
| 1205 | fn exp_g_add_meas nid ulong nm view uchar * vl view uchar * to ulong |
| 1206 | mc = get exp_g_mcnt 0 |
| 1207 | if ge mc EXP_CAP_MEAS |
| 1208 | ret EXP_NONE |
| 1209 | end |
| 1210 | put exp_gm_owner mc nid |
| 1211 | no = exp_str_add_view nm |
| 1212 | vo = exp_str_add_view vl |
| 1213 | put exp_gm_name_off mc no |
| 1214 | put exp_gm_name_len mc len nm |
| 1215 | put exp_gm_val_off mc vo |
| 1216 | put exp_gm_val_len mc len vl |
| 1217 | put exp_gm_unit_off mc 0 |
| 1218 | put exp_gm_unit_len mc 0 |
| 1219 | put exp_g_mcnt 0 add mc 1 |
| 1220 | ret mc |
| 1221 | end |
| 1222 | |
| 1223 | fn exp_g_meas_set_unit mid ulong un view uchar * to void |
| 1224 | if ge mid get exp_g_mcnt 0 |
| 1225 | ret |
| 1226 | end |
| 1227 | uo = exp_str_add_view un |
| 1228 | put exp_gm_unit_off mid uo |
| 1229 | put exp_gm_unit_len mid len un |
| 1230 | end |
| 1231 | |
| 1232 | fn exp_g_add_missing nid ulong ex view uchar * rsn view uchar * to void |
| 1233 | xc = get exp_g_xcnt 0 |
| 1234 | if ge xc EXP_CAP_MISS |
| 1235 | ret |
| 1236 | end |
| 1237 | put exp_gx_owner xc nid |
| 1238 | eo = exp_str_add_view ex |
| 1239 | ro = exp_str_add_view rsn |
| 1240 | put exp_gx_exp_off xc eo |
| 1241 | put exp_gx_exp_len xc len ex |
| 1242 | put exp_gx_rsn_off xc ro |
| 1243 | put exp_gx_rsn_len xc len rsn |
| 1244 | put exp_g_xcnt 0 add xc 1 |
| 1245 | end |
| 1246 | |
| 1247 | fn exp_g_add_note nid ulong nt view uchar * to void |
| 1248 | nc = get exp_g_notecnt 0 |
| 1249 | if ge nc EXP_CAP_NOTES |
| 1250 | ret |
| 1251 | end |
| 1252 | put exp_go_owner nc nid |
| 1253 | no = exp_str_add_view nt |
| 1254 | put exp_go_off nc no |
| 1255 | put exp_go_len nc len nt |
| 1256 | put exp_g_notecnt 0 add nc 1 |
| 1257 | end |
| 1258 | |
| 1259 | fn exp_g_add_truncated_raw id_o ulong id_l ulong to void |
| 1260 | tc = get exp_g_tcnt 0 |
| 1261 | if ge tc EXP_CAP_TRUNC |
| 1262 | ret |
| 1263 | end |
| 1264 | put exp_gt_off tc id_o |
| 1265 | put exp_gt_len tc id_l |
| 1266 | put exp_g_tcnt 0 add tc 1 |
| 1267 | end |
| 1268 | |
| 1269 | fn exp_g_add_truncated id view uchar * to void |
| 1270 | io = exp_str_add_view id |
| 1271 | exp_g_add_truncated_raw io len id |
| 1272 | end |
| 1273 | |
| 1274 | fn exp_g_set_scope_branch br_o ulong br_l ulong to void |
| 1275 | put exp_g_has_scope 0 1 |
| 1276 | put exp_g_sbr_off 0 br_o |
| 1277 | put exp_g_sbr_len 0 br_l |
| 1278 | end |
| 1279 | |
| 1280 | fn exp_g_add_scope_ref_raw id_o ulong id_l ulong to void |
| 1281 | sc = get exp_g_scnt 0 |
| 1282 | if ge sc EXP_CAP_SCOPE |
| 1283 | ret |
| 1284 | end |
| 1285 | put exp_gs_off sc id_o |
| 1286 | put exp_gs_len sc id_l |
| 1287 | put exp_g_scnt 0 add sc 1 |
| 1288 | end |
| 1289 | |
| 1290 | fn exp_g_find_node id_o ulong id_l ulong to ulong |
| 1291 | nc = get exp_g_ncnt 0 |
| 1292 | for i ulong nc bound 320 |
| 1293 | if exp_str_eq get exp_gn_id_off i get exp_gn_id_len i id_o id_l |
| 1294 | ret i |
| 1295 | end |
| 1296 | end |
| 1297 | ret EXP_NONE |
| 1298 | end |
| 1299 | |
| 1300 | fn exp_g_find_node_view id view uchar * to ulong |
| 1301 | nc = get exp_g_ncnt 0 |
| 1302 | for i ulong nc bound 320 |
| 1303 | if exp_str_eq_view get exp_gn_id_off i get exp_gn_id_len i id |
| 1304 | ret i |
| 1305 | end |
| 1306 | end |
| 1307 | ret EXP_NONE |
| 1308 | end |
| 1309 | |
| 1310 | fn exp_g_is_prod_truncated id_o ulong id_l ulong to bool |
| 1311 | tc = get exp_g_tcnt 0 |
| 1312 | for i ulong tc bound 128 |
| 1313 | if exp_str_eq get exp_gt_off i get exp_gt_len i id_o id_l |
| 1314 | ret true |
| 1315 | end |
| 1316 | end |
| 1317 | ret false |
| 1318 | end |
| 1319 | |
| 1320 | fn exp_g_is_scope_ref id_o ulong id_l ulong to bool |
| 1321 | sc = get exp_g_scnt 0 |
| 1322 | for i ulong sc bound 128 |
| 1323 | if exp_str_eq get exp_gs_off i get exp_gs_len i id_o id_l |
| 1324 | ret true |
| 1325 | end |
| 1326 | end |
| 1327 | ret false |
| 1328 | end |
| 1329 | |
| 1330 | fn exp_g_validate to int |
| 1331 | if not exp_str_eq_view get exp_g_fmt_off 0 get exp_g_fmt_len 0 exp_txt_format |
| 1332 | ret EXP_ERR_UNSUPPORTED_VERSION |
| 1333 | end |
| 1334 | if ne get exp_g_ver 0 EXP_VERSION |
| 1335 | ret EXP_ERR_UNSUPPORTED_VERSION |
| 1336 | end |
| 1337 | if gt get exp_g_decl_ncnt 0 EXP_MAX_GRAPH_NODES |
| 1338 | ret EXP_ERR_GRAPH_TOO_LARGE |
| 1339 | end |
| 1340 | nc = get exp_g_ncnt 0 |
| 1341 | if gt nc EXP_MAX_GRAPH_NODES |
| 1342 | ret EXP_ERR_GRAPH_TOO_LARGE |
| 1343 | end |
| 1344 | for i ulong nc bound 320 |
| 1345 | if not exp_kind_valid get exp_gn_kind i |
| 1346 | ret EXP_ERR_UNKNOWN_CODE |
| 1347 | end |
| 1348 | if not exp_status_valid get exp_gn_status i |
| 1349 | ret EXP_ERR_UNKNOWN_CODE |
| 1350 | end |
| 1351 | if ne get exp_gn_has_conf i 0 |
| 1352 | if gt get exp_gn_conf i 1000 |
| 1353 | ret EXP_ERR_UNKNOWN_CODE |
| 1354 | end |
| 1355 | end |
| 1356 | if gt get exp_gn_decl_par i EXP_MAX_PARENTS_PER_NODE |
| 1357 | ret EXP_ERR_GRAPH_TOO_LARGE |
| 1358 | end |
| 1359 | for j ulong i bound 320 |
| 1360 | if exp_str_eq get exp_gn_id_off i get exp_gn_id_len i get exp_gn_id_off j get exp_gn_id_len j |
| 1361 | ret EXP_ERR_DUPLICATE_NODE_ID |
| 1362 | end |
| 1363 | end |
| 1364 | end |
| 1365 | ec = get exp_g_ecnt 0 |
| 1366 | for i ulong ec bound 1024 |
| 1367 | if not exp_rel_valid get exp_ge_rel i |
| 1368 | ret EXP_ERR_UNKNOWN_CODE |
| 1369 | end |
| 1370 | end |
| 1371 | for i ulong nc bound 320 |
| 1372 | pc = ulong 0 |
| 1373 | for e ulong ec bound 1024 |
| 1374 | if eq get exp_ge_owner e i |
| 1375 | pc = add pc 1 |
| 1376 | end |
| 1377 | end |
| 1378 | if gt pc EXP_MAX_PARENTS_PER_NODE |
| 1379 | ret EXP_ERR_GRAPH_TOO_LARGE |
| 1380 | end |
| 1381 | end |
| 1382 | ret EXP_OK |
| 1383 | end |
| 1384 | |
| 1385 | note Structural JSON envelope check (rejects malformed JSON with E1) |
| 1386 | fn exp_json_check raw view uchar * to int |
| 1387 | n = len raw |
| 1388 | if lt n 2 |
| 1389 | ret EXP_ERR_BAD_FORMAT |
| 1390 | end |
| 1391 | if ne get raw 0 123 |
| 1392 | ret EXP_ERR_BAD_FORMAT |
| 1393 | end |
| 1394 | if ne get raw sub n 1 125 |
| 1395 | ret EXP_ERR_BAD_FORMAT |
| 1396 | end |
| 1397 | depth = ulong 0 |
| 1398 | in_str = false |
| 1399 | esc = false |
| 1400 | for i ulong n bound 8192 |
| 1401 | ch = get raw i |
| 1402 | if in_str |
| 1403 | if esc |
| 1404 | esc = false |
| 1405 | else |
| 1406 | if eq ch 92 |
| 1407 | esc = true |
| 1408 | else |
| 1409 | if eq ch 34 |
| 1410 | in_str = false |
| 1411 | end |
| 1412 | end |
| 1413 | end |
| 1414 | else |
| 1415 | if eq ch 34 |
| 1416 | in_str = true |
| 1417 | else |
| 1418 | if or eq ch 123 eq ch 91 |
| 1419 | depth = add depth 1 |
| 1420 | else |
| 1421 | if or eq ch 125 eq ch 93 |
| 1422 | if eq depth 0 |
| 1423 | ret EXP_ERR_BAD_FORMAT |
| 1424 | end |
| 1425 | depth = sub depth 1 |
| 1426 | end |
| 1427 | end |
| 1428 | end |
| 1429 | end |
| 1430 | end |
| 1431 | if in_str |
| 1432 | ret EXP_ERR_BAD_FORMAT |
| 1433 | end |
| 1434 | if ne depth 0 |
| 1435 | ret EXP_ERR_BAD_FORMAT |
| 1436 | end |
| 1437 | has_colon = false |
| 1438 | for i ulong n bound 8192 |
| 1439 | if eq get raw i 58 |
| 1440 | has_colon = true |
| 1441 | break |
| 1442 | end |
| 1443 | end |
| 1444 | if not has_colon |
| 1445 | ret EXP_ERR_BAD_FORMAT |
| 1446 | end |
| 1447 | ret EXP_OK |
| 1448 | end |
| 1449 | |
| 1450 | note ============================================================================ |
| 1451 | note Section 7: Canonical Input & Bundle SHA-256 Digests (bundle.rs) |
| 1452 | note ============================================================================ |
| 1453 | |
| 1454 | fn exp_hash_graph_node ctx edit ulong * nid ulong to void |
| 1455 | exp_sha_tag ctx 1 |
| 1456 | exp_sha_arena ctx get exp_gn_id_off nid get exp_gn_id_len nid |
| 1457 | exp_sha_u64 ctx get exp_gn_kind nid |
| 1458 | exp_sha_u64 ctx get exp_gn_status nid |
| 1459 | exp_sha_arena ctx get exp_gn_stmt_off nid get exp_gn_stmt_len nid |
| 1460 | exp_sha_arena ctx get exp_gn_sys_off nid get exp_gn_sys_len nid |
| 1461 | exp_sha_arena ctx get exp_gn_art_off nid get exp_gn_art_len nid |
| 1462 | exp_sha_arena ctx get exp_gn_ref_off nid get exp_gn_ref_len nid |
| 1463 | exp_sha_u64 ctx get exp_gn_has_dig nid |
| 1464 | if ne get exp_gn_has_dig nid 0 |
| 1465 | exp_sha_arena ctx get exp_gn_dig_off nid get exp_gn_dig_len nid |
| 1466 | end |
| 1467 | exp_sha_u64 ctx get exp_gn_has_conf nid |
| 1468 | if ne get exp_gn_has_conf nid 0 |
| 1469 | exp_sha_u64 ctx get exp_gn_conf nid |
| 1470 | end |
| 1471 | exp_sha_u64 ctx get exp_gn_has_cbas nid |
| 1472 | if ne get exp_gn_has_cbas nid 0 |
| 1473 | exp_sha_arena ctx get exp_gn_cbas_off nid get exp_gn_cbas_len nid |
| 1474 | end |
| 1475 | exp_sha_u64 ctx get exp_gn_has_ts nid |
| 1476 | if ne get exp_gn_has_ts nid 0 |
| 1477 | exp_sha_u64 ctx get exp_gn_ts nid |
| 1478 | end |
| 1479 | end |
| 1480 | |
| 1481 | fn exp_hash_graph_node_lists ctx edit ulong * nid ulong to void |
| 1482 | ec = get exp_g_ecnt 0 |
| 1483 | for e ulong ec bound 1024 |
| 1484 | if eq get exp_ge_owner e nid |
| 1485 | exp_sha_tag ctx 2 |
| 1486 | exp_sha_arena ctx get exp_ge_to_off e get exp_ge_to_len e |
| 1487 | exp_sha_u64 ctx get exp_ge_rel e |
| 1488 | end |
| 1489 | end |
| 1490 | mc = get exp_g_mcnt 0 |
| 1491 | for m ulong mc bound 256 |
| 1492 | if eq get exp_gm_owner m nid |
| 1493 | exp_sha_tag ctx 3 |
| 1494 | exp_sha_arena ctx get exp_gm_name_off m get exp_gm_name_len m |
| 1495 | exp_sha_arena ctx get exp_gm_val_off m get exp_gm_val_len m |
| 1496 | exp_sha_arena ctx get exp_gm_unit_off m get exp_gm_unit_len m |
| 1497 | end |
| 1498 | end |
| 1499 | xc = get exp_g_xcnt 0 |
| 1500 | for x ulong xc bound 128 |
| 1501 | if eq get exp_gx_owner x nid |
| 1502 | exp_sha_tag ctx 4 |
| 1503 | exp_sha_arena ctx get exp_gx_exp_off x get exp_gx_exp_len x |
| 1504 | exp_sha_arena ctx get exp_gx_rsn_off x get exp_gx_rsn_len x |
| 1505 | end |
| 1506 | end |
| 1507 | noc = get exp_g_notecnt 0 |
| 1508 | for o ulong noc bound 128 |
| 1509 | if eq get exp_go_owner o nid |
| 1510 | exp_sha_tag ctx 5 |
| 1511 | exp_sha_arena ctx get exp_go_off o get exp_go_len o |
| 1512 | end |
| 1513 | end |
| 1514 | end |
| 1515 | |
| 1516 | fn exp_compute_input_digest dst edit uchar * to void |
| 1517 | ctx = new ulong 40 0 |
| 1518 | sha256_init ctx |
| 1519 | exp_sha_arena ctx get exp_g_fmt_off 0 get exp_g_fmt_len 0 |
| 1520 | exp_sha_u64 ctx get exp_g_ver 0 |
| 1521 | exp_sha_arena ctx get exp_g_prod_off 0 get exp_g_prod_len 0 |
| 1522 | nc = get exp_g_ncnt 0 |
| 1523 | exp_sha_u64 ctx nc |
| 1524 | for i ulong nc bound 320 |
| 1525 | exp_hash_graph_node ctx i |
| 1526 | exp_hash_graph_node_lists ctx i |
| 1527 | end |
| 1528 | tc = get exp_g_tcnt 0 |
| 1529 | exp_sha_u64 ctx tc |
| 1530 | for i ulong tc bound 128 |
| 1531 | exp_sha_arena ctx get exp_gt_off i get exp_gt_len i |
| 1532 | end |
| 1533 | exp_sha_u64 ctx get exp_g_has_scope 0 |
| 1534 | if ne get exp_g_has_scope 0 0 |
| 1535 | exp_sha_arena ctx get exp_g_sbr_off 0 get exp_g_sbr_len 0 |
| 1536 | sc = get exp_g_scnt 0 |
| 1537 | exp_sha_u64 ctx sc |
| 1538 | for i ulong sc bound 128 |
| 1539 | exp_sha_arena ctx get exp_gs_off i get exp_gs_len i |
| 1540 | end |
| 1541 | end |
| 1542 | sha256_final ctx dst |
| 1543 | end |
| 1544 | |
| 1545 | fn exp_hash_bundle_steps ctx edit ulong * to void |
| 1546 | sc = get exp_b_scnt 0 |
| 1547 | exp_sha_u64 ctx sc |
| 1548 | for i ulong sc bound 320 |
| 1549 | nid = get exp_bs_nid i |
| 1550 | exp_sha_tag ctx 10 |
| 1551 | exp_sha_u64 ctx add i 1 |
| 1552 | exp_hash_graph_node ctx nid |
| 1553 | exp_sha_u64 ctx get exp_bs_role i |
| 1554 | exp_sha_u64 ctx get exp_bs_depth i |
| 1555 | exp_sha_u64 ctx get exp_bs_gnd i |
| 1556 | exp_sha_u64 ctx get exp_bs_bref i |
| 1557 | soff = get exp_bs_sup_off i |
| 1558 | scnt = get exp_bs_sup_cnt i |
| 1559 | exp_sha_u64 ctx scnt |
| 1560 | for j ulong scnt bound 1024 |
| 1561 | exp_sha_u64 ctx get exp_bs_sup_step add soff j |
| 1562 | exp_sha_u64 ctx get exp_bs_sup_rel add soff j |
| 1563 | end |
| 1564 | end |
| 1565 | end |
| 1566 | |
| 1567 | fn exp_hash_bundle_ev_unc ctx edit ulong * to void |
| 1568 | evc = get exp_be_cnt 0 |
| 1569 | exp_sha_u64 ctx evc |
| 1570 | for i ulong evc bound 320 |
| 1571 | exp_sha_tag ctx 11 |
| 1572 | exp_sha_u64 ctx get exp_be_step i |
| 1573 | end |
| 1574 | uc = get exp_bu_cnt 0 |
| 1575 | exp_sha_u64 ctx uc |
| 1576 | for i ulong uc bound 512 |
| 1577 | exp_sha_tag ctx 12 |
| 1578 | exp_sha_u64 ctx get exp_bu_kind i |
| 1579 | exp_sha_arena ctx get exp_bu_nid_off i get exp_bu_nid_len i |
| 1580 | exp_sha_u64 ctx get exp_bu_has_rel i |
| 1581 | if ne get exp_bu_has_rel i 0 |
| 1582 | exp_sha_arena ctx get exp_bu_rel_off i get exp_bu_rel_len i |
| 1583 | end |
| 1584 | exp_sha_arena ctx get exp_bu_det_off i get exp_bu_det_len i |
| 1585 | end |
| 1586 | end |
| 1587 | |
| 1588 | fn exp_hash_contra_one ctx edit ulong * i ulong to void |
| 1589 | exp_sha_tag ctx 13 |
| 1590 | nid = get exp_bc_nid i |
| 1591 | exp_sha_arena ctx get exp_gn_id_off nid get exp_gn_id_len nid |
| 1592 | exp_sha_arena ctx get exp_bc_stmt_off i get exp_bc_stmt_len i |
| 1593 | io = get exp_bc_inv_off i |
| 1594 | ic = get exp_bc_inv_cnt i |
| 1595 | exp_sha_u64 ctx ic |
| 1596 | for j ulong ic bound 256 |
| 1597 | exp_sha_arena ctx get exp_bc_inv_id_off add io j get exp_bc_inv_id_len add io j |
| 1598 | end |
| 1599 | end |
| 1600 | |
| 1601 | fn exp_hash_bundle_extras ctx edit ulong * to void |
| 1602 | exp_hash_bundle_ev_unc ctx |
| 1603 | cc = get exp_bc_cnt 0 |
| 1604 | exp_sha_u64 ctx cc |
| 1605 | for i ulong cc bound 128 |
| 1606 | exp_hash_contra_one ctx i |
| 1607 | end |
| 1608 | ac = get exp_ba_cnt 0 |
| 1609 | exp_sha_u64 ctx ac |
| 1610 | for i ulong ac bound 128 |
| 1611 | exp_sha_tag ctx 14 |
| 1612 | nid = get exp_ba_nid i |
| 1613 | exp_sha_arena ctx get exp_gn_id_off nid get exp_gn_id_len nid |
| 1614 | exp_sha_arena ctx get exp_ba_to_off i get exp_ba_to_len i |
| 1615 | end |
| 1616 | end |
| 1617 | |
| 1618 | fn exp_compute_bundle_digest dst edit uchar * to void |
| 1619 | ctx = new ulong 40 0 |
| 1620 | sha256_init ctx |
| 1621 | exp_sha_u64 ctx get exp_b_concl_step 0 |
| 1622 | exp_hash_bundle_steps ctx |
| 1623 | exp_hash_bundle_extras ctx |
| 1624 | exp_sha_tag ctx 15 |
| 1625 | exp_sha_arena ctx get exp_g_fmt_off 0 get exp_g_fmt_len 0 |
| 1626 | exp_sha_u64 ctx get exp_g_ver 0 |
| 1627 | exp_sha_arena ctx get exp_g_prod_off 0 get exp_g_prod_len 0 |
| 1628 | exp_sha_arena ctx get exp_bp_tgt_off 0 get exp_bp_tgt_len 0 |
| 1629 | exp_sha_u64 ctx get exp_bp_max_d 0 |
| 1630 | exp_sha_u64 ctx get exp_bp_max_n 0 |
| 1631 | exp_sha_u64 ctx get exp_bp_vis 0 |
| 1632 | exp_sha_u64 ctx get exp_bp_gnodes 0 |
| 1633 | indig = new uchar 32 0 |
| 1634 | for i ulong 32 |
| 1635 | put indig i get exp_bp_in_dig i |
| 1636 | end |
| 1637 | sha256_update ctx indig |
| 1638 | sha256_final ctx dst |
| 1639 | end |
| 1640 | |
| 1641 | fn exp_bundle_verify_digest to bool |
| 1642 | dig = new uchar 32 0 |
| 1643 | exp_compute_bundle_digest dig |
| 1644 | for i ulong 32 |
| 1645 | if ne get dig i get exp_bp_bun_dig i |
| 1646 | ret false |
| 1647 | end |
| 1648 | end |
| 1649 | ret true |
| 1650 | end |
| 1651 | |
| 1652 | fn exp_bundle_get_digest dst edit uchar * to void |
| 1653 | for i ulong 32 |
| 1654 | put dst i get exp_bp_bun_dig i |
| 1655 | end |
| 1656 | end |
| 1657 | |
| 1658 | fn exp_bundle_get_input_digest dst edit uchar * to void |
| 1659 | for i ulong 32 |
| 1660 | put dst i get exp_bp_in_dig i |
| 1661 | end |
| 1662 | end |
| 1663 | |
| 1664 | note ============================================================================ |
| 1665 | note Section 8: Bounded Backtracking, Cycle Cutting & Kahn Linearization (build.rs) |
| 1666 | note ============================================================================ |
| 1667 | |
| 1668 | fn exp_add_unc uk ulong no ulong nl ulong det_o ulong det_l ulong to ulong |
| 1669 | uc = get exp_bu_cnt 0 |
| 1670 | if ge uc EXP_CAP_UNCS |
| 1671 | ret EXP_NONE |
| 1672 | end |
| 1673 | put exp_bu_kind uc uk |
| 1674 | put exp_bu_nid_off uc no |
| 1675 | put exp_bu_nid_len uc nl |
| 1676 | put exp_bu_has_rel uc 0 |
| 1677 | put exp_bu_rel_off uc 0 |
| 1678 | put exp_bu_rel_len uc 0 |
| 1679 | put exp_bu_det_off uc det_o |
| 1680 | put exp_bu_det_len uc det_l |
| 1681 | put exp_bu_cnt 0 add uc 1 |
| 1682 | ret uc |
| 1683 | end |
| 1684 | |
| 1685 | fn exp_unc_set_rel idx ulong ro ulong rl ulong to void |
| 1686 | if eq idx EXP_NONE |
| 1687 | ret |
| 1688 | end |
| 1689 | put exp_bu_has_rel idx 1 |
| 1690 | put exp_bu_rel_off idx ro |
| 1691 | put exp_bu_rel_len idx rl |
| 1692 | end |
| 1693 | |
| 1694 | fn exp_sup_gt p1 ulong p2 ulong to bool |
| 1695 | r1 = get exp_w_sup_rel p1 |
| 1696 | r2 = get exp_w_sup_rel p2 |
| 1697 | if gt r1 r2 |
| 1698 | ret true |
| 1699 | end |
| 1700 | if eq r1 r2 |
| 1701 | ret gt exp_str_cmp get exp_w_sup_to_off p1 get exp_w_sup_to_len p1 get exp_w_sup_to_off p2 get exp_w_sup_to_len p2 0 |
| 1702 | end |
| 1703 | ret false |
| 1704 | end |
| 1705 | |
| 1706 | fn exp_sup_swap p1 ulong p2 ulong to void |
| 1707 | t = get exp_w_sup_rel p1 |
| 1708 | put exp_w_sup_rel p1 get exp_w_sup_rel p2 |
| 1709 | put exp_w_sup_rel p2 t |
| 1710 | t = get exp_w_sup_to_off p1 |
| 1711 | put exp_w_sup_to_off p1 get exp_w_sup_to_off p2 |
| 1712 | put exp_w_sup_to_off p2 t |
| 1713 | t = get exp_w_sup_to_len p1 |
| 1714 | put exp_w_sup_to_len p1 get exp_w_sup_to_len p2 |
| 1715 | put exp_w_sup_to_len p2 t |
| 1716 | end |
| 1717 | |
| 1718 | fn exp_sup_sort base ulong cnt ulong to void |
| 1719 | for a ulong cnt bound 1024 |
| 1720 | j = a |
| 1721 | while gt j 0 bound 1024 |
| 1722 | p1 = add base sub j 1 |
| 1723 | p2 = add base j |
| 1724 | if exp_sup_gt p1 p2 |
| 1725 | exp_sup_swap p1 p2 |
| 1726 | j = sub j 1 |
| 1727 | else |
| 1728 | break |
| 1729 | end |
| 1730 | end |
| 1731 | end |
| 1732 | end |
| 1733 | |
| 1734 | fn exp_sup_dedup base ulong cnt ulong to ulong |
| 1735 | ucnt = ulong 0 |
| 1736 | for a ulong cnt bound 1024 |
| 1737 | cur = add base a |
| 1738 | dup = false |
| 1739 | if gt ucnt 0 |
| 1740 | prv = add base sub ucnt 1 |
| 1741 | if eq get exp_w_sup_rel prv get exp_w_sup_rel cur |
| 1742 | if exp_str_eq get exp_w_sup_to_off prv get exp_w_sup_to_len prv get exp_w_sup_to_off cur get exp_w_sup_to_len cur |
| 1743 | dup = true |
| 1744 | end |
| 1745 | end |
| 1746 | end |
| 1747 | if not dup |
| 1748 | dst = add base ucnt |
| 1749 | put exp_w_sup_rel dst get exp_w_sup_rel cur |
| 1750 | put exp_w_sup_to_off dst get exp_w_sup_to_off cur |
| 1751 | put exp_w_sup_to_len dst get exp_w_sup_to_len cur |
| 1752 | ucnt = add ucnt 1 |
| 1753 | end |
| 1754 | end |
| 1755 | ret ucnt |
| 1756 | end |
| 1757 | |
| 1758 | fn exp_build_support_one nid ulong to void |
| 1759 | base = get exp_w_sup_total 0 |
| 1760 | cnt = ulong 0 |
| 1761 | ec = get exp_g_ecnt 0 |
| 1762 | for e ulong ec bound 1024 |
| 1763 | if eq get exp_ge_owner e nid |
| 1764 | rl = get exp_ge_rel e |
| 1765 | if exp_rel_is_support rl |
| 1766 | pos = add base cnt |
| 1767 | if lt pos EXP_CAP_EDGES |
| 1768 | put exp_w_sup_rel pos rl |
| 1769 | put exp_w_sup_to_off pos get exp_ge_to_off e |
| 1770 | put exp_w_sup_to_len pos get exp_ge_to_len e |
| 1771 | cnt = add cnt 1 |
| 1772 | end |
| 1773 | end |
| 1774 | end |
| 1775 | end |
| 1776 | exp_sup_sort base cnt |
| 1777 | ucnt = exp_sup_dedup base cnt |
| 1778 | put exp_w_sup_off nid base |
| 1779 | put exp_w_sup_cnt nid ucnt |
| 1780 | put exp_w_sup_total 0 add base ucnt |
| 1781 | end |
| 1782 | |
| 1783 | note Build sorted, deduplicated support edge list for each graph node |
| 1784 | fn exp_build_support_index to void |
| 1785 | put exp_w_sup_total 0 0 |
| 1786 | nc = get exp_g_ncnt 0 |
| 1787 | for i ulong nc bound 320 |
| 1788 | exp_build_support_one i |
| 1789 | end |
| 1790 | end |
| 1791 | |
| 1792 | fn exp_emit_missing_parent nid ulong po ulong pl ulong to void |
| 1793 | no = get exp_gn_id_off nid |
| 1794 | nl = get exp_gn_id_len nid |
| 1795 | st = exp_str_mark |
| 1796 | t = exp_str_add_arena no nl |
| 1797 | t = exp_str_add_view exp_s_miss_a |
| 1798 | t = exp_str_add_arena po pl |
| 1799 | t = exp_str_add_view exp_s_miss_b |
| 1800 | ui = exp_add_unc EXP_UK_MISSING_PARENT no nl st sub exp_str_mark st |
| 1801 | exp_unc_set_rel ui po pl |
| 1802 | end |
| 1803 | |
| 1804 | fn exp_emit_trunc_builder nid ulong max_d ulong max_n ulong to void |
| 1805 | no = get exp_gn_id_off nid |
| 1806 | nl = get exp_gn_id_len nid |
| 1807 | st = exp_str_mark |
| 1808 | t = exp_str_add_view exp_s_trunc_a |
| 1809 | t = exp_str_add_arena no nl |
| 1810 | t = exp_str_add_view exp_s_trunc_b |
| 1811 | exp_str_add_u64 max_d |
| 1812 | t = exp_str_add_view exp_s_trunc_c |
| 1813 | exp_str_add_u64 max_n |
| 1814 | exp_str_put_b uchar 41 |
| 1815 | ui = exp_add_unc EXP_UK_TRUNCATED no nl st sub exp_str_mark st |
| 1816 | end |
| 1817 | |
| 1818 | fn exp_emit_trunc_prod nid ulong to void |
| 1819 | no = get exp_gn_id_off nid |
| 1820 | nl = get exp_gn_id_len nid |
| 1821 | st = exp_str_mark |
| 1822 | t = exp_str_add_view exp_s_ptrunc_a |
| 1823 | t = exp_str_add_arena no nl |
| 1824 | t = exp_str_add_view exp_s_ptrunc_b |
| 1825 | ui = exp_add_unc EXP_UK_TRUNCATED no nl st sub exp_str_mark st |
| 1826 | end |
| 1827 | |
| 1828 | fn exp_bfs_expand u ulong max_d ulong max_n ulong to void |
| 1829 | d = get exp_w_depth u |
| 1830 | so = get exp_w_sup_off u |
| 1831 | sc = get exp_w_sup_cnt u |
| 1832 | for k ulong sc bound 1024 |
| 1833 | po = get exp_w_sup_to_off add so k |
| 1834 | pl = get exp_w_sup_to_len add so k |
| 1835 | pid = exp_g_find_node po pl |
| 1836 | if eq pid EXP_NONE |
| 1837 | if not exp_g_is_prod_truncated get exp_gn_id_off u get exp_gn_id_len u |
| 1838 | exp_emit_missing_parent u po pl |
| 1839 | end |
| 1840 | else |
| 1841 | if eq get exp_w_vis pid 0 |
| 1842 | vcnt = get exp_w_bfs_st 1 |
| 1843 | if or gt add d 1 max_d ge vcnt max_n |
| 1844 | put exp_w_trunc u 1 |
| 1845 | else |
| 1846 | qt = get exp_w_bfs_st 0 |
| 1847 | put exp_w_vis pid 1 |
| 1848 | put exp_w_depth pid add d 1 |
| 1849 | put exp_w_queue qt pid |
| 1850 | put exp_w_bfs_st 0 add qt 1 |
| 1851 | put exp_w_bfs_st 1 add vcnt 1 |
| 1852 | end |
| 1853 | end |
| 1854 | end |
| 1855 | end |
| 1856 | end |
| 1857 | |
| 1858 | fn exp_build_bfs tgt ulong max_d ulong max_n ulong to ulong |
| 1859 | nc = get exp_g_ncnt 0 |
| 1860 | for i ulong nc bound 320 |
| 1861 | put exp_w_vis i 0 |
| 1862 | put exp_w_depth i EXP_NONE |
| 1863 | put exp_w_trunc i 0 |
| 1864 | end |
| 1865 | put exp_w_vis tgt 1 |
| 1866 | put exp_w_depth tgt 0 |
| 1867 | put exp_w_queue 0 tgt |
| 1868 | put exp_w_bfs_st 0 1 |
| 1869 | put exp_w_bfs_st 1 1 |
| 1870 | qh = ulong 0 |
| 1871 | while lt qh get exp_w_bfs_st 0 bound 320 |
| 1872 | u = get exp_w_queue qh |
| 1873 | qh = add qh 1 |
| 1874 | skip = false |
| 1875 | if ne u tgt |
| 1876 | if exp_status_is_grounding get exp_gn_status u |
| 1877 | skip = true |
| 1878 | end |
| 1879 | end |
| 1880 | if not skip |
| 1881 | exp_bfs_expand u max_d max_n |
| 1882 | end |
| 1883 | end |
| 1884 | for i ulong nc bound 320 |
| 1885 | if ne get exp_w_trunc i 0 |
| 1886 | exp_emit_trunc_builder i max_d max_n |
| 1887 | else |
| 1888 | if ne get exp_w_vis i 0 |
| 1889 | if exp_g_is_prod_truncated get exp_gn_id_off i get exp_gn_id_len i |
| 1890 | exp_emit_trunc_prod i |
| 1891 | end |
| 1892 | end |
| 1893 | end |
| 1894 | end |
| 1895 | ret get exp_w_bfs_st 1 |
| 1896 | end |
| 1897 | |
| 1898 | fn exp_build_vis_edges_one nid ulong tgt ulong to void |
| 1899 | put exp_w_color nid 0 |
| 1900 | put exp_w_ve_off nid 0 |
| 1901 | put exp_w_ve_cnt nid 0 |
| 1902 | if eq get exp_w_vis nid 0 |
| 1903 | ret |
| 1904 | end |
| 1905 | base = get exp_w_ve_total 0 |
| 1906 | cnt = ulong 0 |
| 1907 | skip = false |
| 1908 | if ne nid tgt |
| 1909 | if exp_status_is_grounding get exp_gn_status nid |
| 1910 | skip = true |
| 1911 | end |
| 1912 | end |
| 1913 | if not skip |
| 1914 | so = get exp_w_sup_off nid |
| 1915 | sc = get exp_w_sup_cnt nid |
| 1916 | for k ulong sc bound 1024 |
| 1917 | pid = exp_g_find_node get exp_w_sup_to_off add so k get exp_w_sup_to_len add so k |
| 1918 | if ne pid EXP_NONE |
| 1919 | if ne get exp_w_vis pid 0 |
| 1920 | pos = add base cnt |
| 1921 | put exp_w_ve_to pos pid |
| 1922 | put exp_w_ve_rel pos get exp_w_sup_rel add so k |
| 1923 | put exp_w_ve_cut pos 0 |
| 1924 | cnt = add cnt 1 |
| 1925 | end |
| 1926 | end |
| 1927 | end |
| 1928 | end |
| 1929 | put exp_w_ve_off nid base |
| 1930 | put exp_w_ve_cnt nid cnt |
| 1931 | put exp_w_ve_total 0 add base cnt |
| 1932 | end |
| 1933 | |
| 1934 | fn exp_emit_cycle_edge from_n ulong to_n ulong to void |
| 1935 | fo = get exp_gn_id_off from_n |
| 1936 | fl = get exp_gn_id_len from_n |
| 1937 | t_o = get exp_gn_id_off to_n |
| 1938 | t_l = get exp_gn_id_len to_n |
| 1939 | st = exp_str_mark |
| 1940 | t = exp_str_add_arena fo fl |
| 1941 | t = exp_str_add_view exp_s_cyc_a |
| 1942 | t = exp_str_add_arena t_o t_l |
| 1943 | t = exp_str_add_view exp_s_cyc_b |
| 1944 | t = exp_str_add_arena fo fl |
| 1945 | t = exp_str_add_view exp_s_cyc_c |
| 1946 | ui = exp_add_unc EXP_UK_CYCLE fo fl st sub exp_str_mark st |
| 1947 | exp_unc_set_rel ui t_o t_l |
| 1948 | end |
| 1949 | |
| 1950 | fn exp_dfs_cut tgt ulong to void |
| 1951 | sp = ulong 1 |
| 1952 | put exp_w_stk_n 0 tgt |
| 1953 | put exp_w_stk_i 0 0 |
| 1954 | put exp_w_color tgt 1 |
| 1955 | while gt sp 0 bound 4096 |
| 1956 | sp = sub sp 1 |
| 1957 | u = get exp_w_stk_n sp |
| 1958 | ei = get exp_w_stk_i sp |
| 1959 | vc = get exp_w_ve_cnt u |
| 1960 | if lt ei vc |
| 1961 | put exp_w_stk_n sp u |
| 1962 | put exp_w_stk_i sp add ei 1 |
| 1963 | sp = add sp 1 |
| 1964 | epos = add get exp_w_ve_off u ei |
| 1965 | p = get exp_w_ve_to epos |
| 1966 | col = get exp_w_color p |
| 1967 | if eq col 0 |
| 1968 | put exp_w_color p 1 |
| 1969 | put exp_w_stk_n sp p |
| 1970 | put exp_w_stk_i sp 0 |
| 1971 | sp = add sp 1 |
| 1972 | else |
| 1973 | if eq col 1 |
| 1974 | put exp_w_ve_cut epos 1 |
| 1975 | end |
| 1976 | end |
| 1977 | else |
| 1978 | put exp_w_color u 2 |
| 1979 | end |
| 1980 | end |
| 1981 | end |
| 1982 | |
| 1983 | note Build visited-only support edges and run 3-color iterative DFS to cut back edges |
| 1984 | fn exp_build_dag_cut_cycles tgt ulong to void |
| 1985 | nc = get exp_g_ncnt 0 |
| 1986 | put exp_w_ve_total 0 0 |
| 1987 | for i ulong nc bound 320 |
| 1988 | exp_build_vis_edges_one i tgt |
| 1989 | end |
| 1990 | exp_dfs_cut tgt |
| 1991 | for i ulong nc bound 320 |
| 1992 | if ne get exp_w_vis i 0 |
| 1993 | vo = get exp_w_ve_off i |
| 1994 | vc = get exp_w_ve_cnt i |
| 1995 | for k ulong vc bound 1024 |
| 1996 | epos = add vo k |
| 1997 | if ne get exp_w_ve_cut epos 0 |
| 1998 | exp_emit_cycle_edge i get exp_w_ve_to epos |
| 1999 | end |
| 2000 | end |
| 2001 | end |
| 2002 | end |
| 2003 | end |
| 2004 | |
| 2005 | fn exp_node_less a ulong b ulong to bool |
| 2006 | ra = exp_role_of get exp_gn_kind a get exp_gn_status a |
| 2007 | rb = exp_role_of get exp_gn_kind b get exp_gn_status b |
| 2008 | if lt ra rb |
| 2009 | ret true |
| 2010 | end |
| 2011 | if gt ra rb |
| 2012 | ret false |
| 2013 | end |
| 2014 | ret lt exp_str_cmp get exp_gn_id_off a get exp_gn_id_len a get exp_gn_id_off b get exp_gn_id_len b 0 |
| 2015 | end |
| 2016 | |
| 2017 | fn exp_kahn_init to void |
| 2018 | nc = get exp_g_ncnt 0 |
| 2019 | for i ulong nc bound 320 |
| 2020 | put exp_w_placed i 0 |
| 2021 | put exp_w_pos i 0 |
| 2022 | rc = ulong 0 |
| 2023 | if ne get exp_w_vis i 0 |
| 2024 | vo = get exp_w_ve_off i |
| 2025 | vc = get exp_w_ve_cnt i |
| 2026 | for k ulong vc bound 1024 |
| 2027 | if eq get exp_w_ve_cut add vo k 0 |
| 2028 | rc = add rc 1 |
| 2029 | end |
| 2030 | end |
| 2031 | end |
| 2032 | put exp_w_rem i rc |
| 2033 | end |
| 2034 | end |
| 2035 | |
| 2036 | fn exp_kahn_pick_ready tgt ulong to ulong |
| 2037 | nc = get exp_g_ncnt 0 |
| 2038 | best = EXP_NONE |
| 2039 | for i ulong nc bound 320 |
| 2040 | if ne get exp_w_vis i 0 |
| 2041 | if eq get exp_w_placed i 0 |
| 2042 | if ne i tgt |
| 2043 | if eq get exp_w_rem i 0 |
| 2044 | if eq best EXP_NONE |
| 2045 | best = i |
| 2046 | else |
| 2047 | if exp_node_less i best |
| 2048 | best = i |
| 2049 | end |
| 2050 | end |
| 2051 | end |
| 2052 | end |
| 2053 | end |
| 2054 | end |
| 2055 | end |
| 2056 | ret best |
| 2057 | end |
| 2058 | |
| 2059 | fn exp_kahn_release best ulong to void |
| 2060 | nc = get exp_g_ncnt 0 |
| 2061 | for c ulong nc bound 320 |
| 2062 | if ne get exp_w_vis c 0 |
| 2063 | if eq get exp_w_placed c 0 |
| 2064 | vo = get exp_w_ve_off c |
| 2065 | vc = get exp_w_ve_cnt c |
| 2066 | for k ulong vc bound 1024 |
| 2067 | if eq get exp_w_ve_cut add vo k 0 |
| 2068 | if eq get exp_w_ve_to add vo k best |
| 2069 | put exp_w_rem c sub get exp_w_rem c 1 |
| 2070 | end |
| 2071 | end |
| 2072 | end |
| 2073 | end |
| 2074 | end |
| 2075 | end |
| 2076 | end |
| 2077 | |
| 2078 | note Kahn topological sort: tie-break by (Role, node_id), target always last |
| 2079 | fn exp_build_kahn tgt ulong vcnt ulong to void |
| 2080 | exp_kahn_init |
| 2081 | ocnt = ulong 0 |
| 2082 | need = sub vcnt 1 |
| 2083 | for iter ulong need bound 320 |
| 2084 | best = exp_kahn_pick_ready tgt |
| 2085 | if eq best EXP_NONE |
| 2086 | break |
| 2087 | end |
| 2088 | put exp_w_placed best 1 |
| 2089 | put exp_bs_nid ocnt best |
| 2090 | put exp_w_pos best add ocnt 1 |
| 2091 | ocnt = add ocnt 1 |
| 2092 | exp_kahn_release best |
| 2093 | end |
| 2094 | put exp_bs_nid ocnt tgt |
| 2095 | put exp_w_pos tgt add ocnt 1 |
| 2096 | ocnt = add ocnt 1 |
| 2097 | put exp_b_scnt 0 ocnt |
| 2098 | put exp_b_concl_step 0 ocnt |
| 2099 | end |
| 2100 | |
| 2101 | fn exp_step_sup_gt p1 ulong p2 ulong to bool |
| 2102 | s1 = get exp_bs_sup_step p1 |
| 2103 | s2 = get exp_bs_sup_step p2 |
| 2104 | if gt s1 s2 |
| 2105 | ret true |
| 2106 | end |
| 2107 | if eq s1 s2 |
| 2108 | ret gt get exp_bs_sup_rel p1 get exp_bs_sup_rel p2 |
| 2109 | end |
| 2110 | ret false |
| 2111 | end |
| 2112 | |
| 2113 | fn exp_sort_step_sups base ulong cnt ulong to void |
| 2114 | for a ulong cnt bound 1024 |
| 2115 | j = a |
| 2116 | while gt j 0 bound 1024 |
| 2117 | p1 = add base sub j 1 |
| 2118 | p2 = add base j |
| 2119 | if exp_step_sup_gt p1 p2 |
| 2120 | t = get exp_bs_sup_step p1 |
| 2121 | put exp_bs_sup_step p1 get exp_bs_sup_step p2 |
| 2122 | put exp_bs_sup_step p2 t |
| 2123 | t = get exp_bs_sup_rel p1 |
| 2124 | put exp_bs_sup_rel p1 get exp_bs_sup_rel p2 |
| 2125 | put exp_bs_sup_rel p2 t |
| 2126 | j = sub j 1 |
| 2127 | else |
| 2128 | break |
| 2129 | end |
| 2130 | end |
| 2131 | end |
| 2132 | end |
| 2133 | |
| 2134 | fn exp_build_step_one si ulong to void |
| 2135 | nid = get exp_bs_nid si |
| 2136 | put exp_bs_role si exp_role_of get exp_gn_kind nid get exp_gn_status nid |
| 2137 | put exp_bs_depth si get exp_w_depth nid |
| 2138 | if exp_status_is_grounding get exp_gn_status nid |
| 2139 | put exp_bs_gnd si 1 |
| 2140 | else |
| 2141 | put exp_bs_gnd si 0 |
| 2142 | end |
| 2143 | if exp_g_is_scope_ref get exp_gn_id_off nid get exp_gn_id_len nid |
| 2144 | put exp_bs_bref si 1 |
| 2145 | else |
| 2146 | put exp_bs_bref si 0 |
| 2147 | end |
| 2148 | base = get exp_bs_sup_total 0 |
| 2149 | cnt = ulong 0 |
| 2150 | vo = get exp_w_ve_off nid |
| 2151 | vc = get exp_w_ve_cnt nid |
| 2152 | for k ulong vc bound 1024 |
| 2153 | if eq get exp_w_ve_cut add vo k 0 |
| 2154 | p = get exp_w_ve_to add vo k |
| 2155 | pos = add base cnt |
| 2156 | put exp_bs_sup_step pos get exp_w_pos p |
| 2157 | put exp_bs_sup_rel pos get exp_w_ve_rel add vo k |
| 2158 | cnt = add cnt 1 |
| 2159 | end |
| 2160 | end |
| 2161 | exp_sort_step_sups base cnt |
| 2162 | put exp_bs_sup_off si base |
| 2163 | put exp_bs_sup_cnt si cnt |
| 2164 | put exp_bs_sup_total 0 add base cnt |
| 2165 | end |
| 2166 | |
| 2167 | fn exp_build_steps to void |
| 2168 | sc = get exp_b_scnt 0 |
| 2169 | put exp_bs_sup_total 0 0 |
| 2170 | for i ulong sc bound 320 |
| 2171 | exp_build_step_one i |
| 2172 | end |
| 2173 | end |
| 2174 | |
| 2175 | fn exp_str_add_status_name st ulong to void |
| 2176 | if eq st EXP_ST_UNCLASSIFIED |
| 2177 | t = exp_str_add_view exp_sn_0 |
| 2178 | ret |
| 2179 | end |
| 2180 | if eq st EXP_ST_DIRECT_OBS |
| 2181 | t = exp_str_add_view exp_sn_1 |
| 2182 | ret |
| 2183 | end |
| 2184 | if eq st EXP_ST_VERIFIED_FACT |
| 2185 | t = exp_str_add_view exp_sn_2 |
| 2186 | ret |
| 2187 | end |
| 2188 | if eq st EXP_ST_INFERENCE |
| 2189 | t = exp_str_add_view exp_sn_3 |
| 2190 | ret |
| 2191 | end |
| 2192 | if eq st EXP_ST_HYPOTHESIS |
| 2193 | t = exp_str_add_view exp_sn_4 |
| 2194 | ret |
| 2195 | end |
| 2196 | if eq st EXP_ST_CONTRADICTION |
| 2197 | t = exp_str_add_view exp_sn_5 |
| 2198 | ret |
| 2199 | end |
| 2200 | t = exp_str_add_view exp_sn_6 |
| 2201 | end |
| 2202 | |
| 2203 | fn exp_step_tests_hypo onid ulong ho ulong hl ulong to bool |
| 2204 | okd = get exp_gn_kind onid |
| 2205 | ost = get exp_gn_status onid |
| 2206 | so = get exp_w_sup_off onid |
| 2207 | scnt = get exp_w_sup_cnt onid |
| 2208 | if eq okd EXP_KD_EVAL_RECEIPT |
| 2209 | for k ulong scnt bound 1024 |
| 2210 | if eq get exp_w_sup_rel add so k EXP_REL_EVALUATES |
| 2211 | if exp_str_eq get exp_w_sup_to_off add so k get exp_w_sup_to_len add so k ho hl |
| 2212 | ret true |
| 2213 | end |
| 2214 | end |
| 2215 | end |
| 2216 | end |
| 2217 | if eq okd EXP_KD_CORTEX_RECORD |
| 2218 | if eq ost EXP_ST_VERIFIED_FACT |
| 2219 | for k ulong scnt bound 1024 |
| 2220 | if exp_str_eq get exp_w_sup_to_off add so k get exp_w_sup_to_len add so k ho hl |
| 2221 | ret true |
| 2222 | end |
| 2223 | end |
| 2224 | end |
| 2225 | end |
| 2226 | ret false |
| 2227 | end |
| 2228 | |
| 2229 | fn exp_step_has_hypo_test h_nid ulong to bool |
| 2230 | ho = get exp_gn_id_off h_nid |
| 2231 | hl = get exp_gn_id_len h_nid |
| 2232 | sc = get exp_b_scnt 0 |
| 2233 | for j ulong sc bound 320 |
| 2234 | if exp_step_tests_hypo get exp_bs_nid j ho hl |
| 2235 | ret true |
| 2236 | end |
| 2237 | end |
| 2238 | ret false |
| 2239 | end |
| 2240 | |
| 2241 | fn exp_step_emit_mn nid ulong to bool |
| 2242 | no = get exp_gn_id_off nid |
| 2243 | nl = get exp_gn_id_len nid |
| 2244 | has_miss = false |
| 2245 | xc = get exp_g_xcnt 0 |
| 2246 | for x ulong xc bound 128 |
| 2247 | if eq get exp_gx_owner x nid |
| 2248 | has_miss = true |
| 2249 | eo = get exp_gx_exp_off x |
| 2250 | el = get exp_gx_exp_len x |
| 2251 | st = exp_str_mark |
| 2252 | t = exp_str_add_view exp_s_mlink_a |
| 2253 | t = exp_str_add_arena eo el |
| 2254 | exp_str_put_b uchar 32 |
| 2255 | exp_str_put_b uchar 40 |
| 2256 | t = exp_str_add_arena get exp_gx_rsn_off x get exp_gx_rsn_len x |
| 2257 | exp_str_put_b uchar 41 |
| 2258 | ui = exp_add_unc EXP_UK_MISSING_LINK no nl st sub exp_str_mark st |
| 2259 | exp_unc_set_rel ui eo el |
| 2260 | end |
| 2261 | end |
| 2262 | noc = get exp_g_notecnt 0 |
| 2263 | for o ulong noc bound 128 |
| 2264 | if eq get exp_go_owner o nid |
| 2265 | ui = exp_add_unc EXP_UK_PRODUCER_NOTE no nl get exp_go_off o get exp_go_len o |
| 2266 | end |
| 2267 | end |
| 2268 | ret has_miss |
| 2269 | end |
| 2270 | |
| 2271 | fn exp_step_emit_st sidx ulong nid ulong has_sup bool to void |
| 2272 | no = get exp_gn_id_off nid |
| 2273 | nl = get exp_gn_id_len nid |
| 2274 | kd = get exp_gn_kind nid |
| 2275 | if eq kd EXP_KD_AGENT_BRANCH |
| 2276 | ret |
| 2277 | end |
| 2278 | sts = get exp_gn_status nid |
| 2279 | if eq sts EXP_ST_UNCLASSIFIED |
| 2280 | st = exp_str_mark |
| 2281 | t = exp_str_add_view exp_s_unk_a |
| 2282 | exp_str_add_u64 sidx |
| 2283 | t = exp_str_add_view exp_s_unk_b |
| 2284 | ui = exp_add_unc EXP_UK_UNKNOWN no nl st sub exp_str_mark st |
| 2285 | else |
| 2286 | if or eq sts EXP_ST_INFERENCE eq sts EXP_ST_HYPOTHESIS |
| 2287 | if not has_sup |
| 2288 | st = exp_str_mark |
| 2289 | t = exp_str_add_view exp_s_unsup_a |
| 2290 | exp_str_add_u64 sidx |
| 2291 | t = exp_str_add_view exp_s_unsup_b |
| 2292 | exp_str_add_status_name sts |
| 2293 | t = exp_str_add_view exp_s_unsup_c |
| 2294 | ui = exp_add_unc EXP_UK_UNSUPPORTED no nl st sub exp_str_mark st |
| 2295 | else |
| 2296 | if eq sts EXP_ST_INFERENCE |
| 2297 | st = exp_str_mark |
| 2298 | t = exp_str_add_view exp_s_unsup_a |
| 2299 | exp_str_add_u64 sidx |
| 2300 | t = exp_str_add_view exp_s_inf_a |
| 2301 | ui = exp_add_unc EXP_UK_INFERRED no nl st sub exp_str_mark st |
| 2302 | end |
| 2303 | end |
| 2304 | end |
| 2305 | end |
| 2306 | is_hyp = false |
| 2307 | if eq kd EXP_KD_HYPOTHESIS |
| 2308 | is_hyp = true |
| 2309 | else |
| 2310 | if eq kd EXP_KD_CORTEX_RECORD |
| 2311 | if eq sts EXP_ST_HYPOTHESIS |
| 2312 | is_hyp = true |
| 2313 | end |
| 2314 | end |
| 2315 | end |
| 2316 | if is_hyp |
| 2317 | if not exp_step_has_hypo_test nid |
| 2318 | st = exp_str_mark |
| 2319 | t = exp_str_add_view exp_s_hyp_a |
| 2320 | exp_str_add_u64 sidx |
| 2321 | t = exp_str_add_view exp_s_hyp_b |
| 2322 | ui = exp_add_unc EXP_UK_HYPO_NOT_FALSIFIED no nl st sub exp_str_mark st |
| 2323 | end |
| 2324 | end |
| 2325 | end |
| 2326 | |
| 2327 | fn exp_build_step_uncs to void |
| 2328 | sc = get exp_b_scnt 0 |
| 2329 | for i ulong sc bound 320 |
| 2330 | nid = get exp_bs_nid i |
| 2331 | has_sup = exp_step_emit_mn nid |
| 2332 | if gt get exp_w_sup_cnt nid 0 |
| 2333 | has_sup = true |
| 2334 | end |
| 2335 | exp_step_emit_st add i 1 nid has_sup |
| 2336 | end |
| 2337 | end |
| 2338 | |
| 2339 | fn exp_sort_dedup_tmp cnt ulong to ulong |
| 2340 | for a ulong cnt bound 128 |
| 2341 | j = a |
| 2342 | while gt j 0 bound 128 |
| 2343 | p1 = sub j 1 |
| 2344 | if gt exp_str_cmp get exp_w_tmp_off p1 get exp_w_tmp_len p1 get exp_w_tmp_off j get exp_w_tmp_len j 0 |
| 2345 | o1 = get exp_w_tmp_off p1 |
| 2346 | l1 = get exp_w_tmp_len p1 |
| 2347 | put exp_w_tmp_off p1 get exp_w_tmp_off j |
| 2348 | put exp_w_tmp_len p1 get exp_w_tmp_len j |
| 2349 | put exp_w_tmp_off j o1 |
| 2350 | put exp_w_tmp_len j l1 |
| 2351 | j = sub j 1 |
| 2352 | else |
| 2353 | break |
| 2354 | end |
| 2355 | end |
| 2356 | end |
| 2357 | ucnt = ulong 0 |
| 2358 | for a ulong cnt bound 128 |
| 2359 | dup = false |
| 2360 | if gt ucnt 0 |
| 2361 | prv = sub ucnt 1 |
| 2362 | if exp_str_eq get exp_w_tmp_off prv get exp_w_tmp_len prv get exp_w_tmp_off a get exp_w_tmp_len a |
| 2363 | dup = true |
| 2364 | end |
| 2365 | end |
| 2366 | if not dup |
| 2367 | put exp_w_tmp_off ucnt get exp_w_tmp_off a |
| 2368 | put exp_w_tmp_len ucnt get exp_w_tmp_len a |
| 2369 | ucnt = add ucnt 1 |
| 2370 | end |
| 2371 | end |
| 2372 | ret ucnt |
| 2373 | end |
| 2374 | |
| 2375 | fn exp_sort_dedup_inv cnt ulong to ulong |
| 2376 | for a ulong cnt bound 128 |
| 2377 | j = a |
| 2378 | while gt j 0 bound 128 |
| 2379 | p1 = sub j 1 |
| 2380 | if gt exp_str_cmp get exp_w_inv_off p1 get exp_w_inv_len p1 get exp_w_inv_off j get exp_w_inv_len j 0 |
| 2381 | o1 = get exp_w_inv_off p1 |
| 2382 | l1 = get exp_w_inv_len p1 |
| 2383 | put exp_w_inv_off p1 get exp_w_inv_off j |
| 2384 | put exp_w_inv_len p1 get exp_w_inv_len j |
| 2385 | put exp_w_inv_off j o1 |
| 2386 | put exp_w_inv_len j l1 |
| 2387 | j = sub j 1 |
| 2388 | else |
| 2389 | break |
| 2390 | end |
| 2391 | end |
| 2392 | end |
| 2393 | ucnt = ulong 0 |
| 2394 | for a ulong cnt bound 128 |
| 2395 | dup = false |
| 2396 | if gt ucnt 0 |
| 2397 | prv = sub ucnt 1 |
| 2398 | if exp_str_eq get exp_w_inv_off prv get exp_w_inv_len prv get exp_w_inv_off a get exp_w_inv_len a |
| 2399 | dup = true |
| 2400 | end |
| 2401 | end |
| 2402 | if not dup |
| 2403 | put exp_w_inv_off ucnt get exp_w_inv_off a |
| 2404 | put exp_w_inv_len ucnt get exp_w_inv_len a |
| 2405 | ucnt = add ucnt 1 |
| 2406 | end |
| 2407 | end |
| 2408 | ret ucnt |
| 2409 | end |
| 2410 | |
| 2411 | fn exp_sort_nodes_by_id to void |
| 2412 | nc = get exp_g_ncnt 0 |
| 2413 | for i ulong nc bound 320 |
| 2414 | put exp_w_sorted_n i i |
| 2415 | end |
| 2416 | for a ulong nc bound 320 |
| 2417 | j = a |
| 2418 | while gt j 0 bound 320 |
| 2419 | n1 = get exp_w_sorted_n sub j 1 |
| 2420 | n2 = get exp_w_sorted_n j |
| 2421 | if gt exp_str_cmp get exp_gn_id_off n1 get exp_gn_id_len n1 get exp_gn_id_off n2 get exp_gn_id_len n2 0 |
| 2422 | put exp_w_sorted_n sub j 1 n2 |
| 2423 | put exp_w_sorted_n j n1 |
| 2424 | j = sub j 1 |
| 2425 | else |
| 2426 | break |
| 2427 | end |
| 2428 | end |
| 2429 | end |
| 2430 | end |
| 2431 | |
| 2432 | fn exp_contra_emit nid ulong tcnt ulong icnt ulong to void |
| 2433 | stmt_o = get exp_gn_stmt_off nid |
| 2434 | stmt_l = get exp_gn_stmt_len nid |
| 2435 | if ne get exp_gn_status nid EXP_ST_CONTRADICTION |
| 2436 | st = exp_str_mark |
| 2437 | t = exp_str_add_arena get exp_gn_id_off nid get exp_gn_id_len nid |
| 2438 | t = exp_str_add_view exp_s_contra_a |
| 2439 | for k ulong tcnt bound 128 |
| 2440 | if gt k 0 |
| 2441 | exp_str_put_b uchar 44 |
| 2442 | exp_str_put_b uchar 32 |
| 2443 | end |
| 2444 | t = exp_str_add_arena get exp_w_tmp_off k get exp_w_tmp_len k |
| 2445 | end |
| 2446 | exp_str_put_b uchar 58 |
| 2447 | exp_str_put_b uchar 32 |
| 2448 | t = exp_str_add_arena stmt_o stmt_l |
| 2449 | stmt_o = st |
| 2450 | stmt_l = sub exp_str_mark st |
| 2451 | end |
| 2452 | for k ulong icnt bound 128 |
| 2453 | io = get exp_w_inv_off k |
| 2454 | il = get exp_w_inv_len k |
| 2455 | oo = get exp_gn_id_off nid |
| 2456 | ol = get exp_gn_id_len nid |
| 2457 | if exp_str_eq io il oo ol |
| 2458 | oo = get exp_w_tmp_off 0 |
| 2459 | ol = get exp_w_tmp_len 0 |
| 2460 | end |
| 2461 | st = exp_str_mark |
| 2462 | t = exp_str_add_arena io il |
| 2463 | t = exp_str_add_view exp_s_contra_b |
| 2464 | t = exp_str_add_arena stmt_o stmt_l |
| 2465 | ui = exp_add_unc EXP_UK_CONFLICT io il st sub exp_str_mark st |
| 2466 | exp_unc_set_rel ui oo ol |
| 2467 | end |
| 2468 | cc = get exp_bc_cnt 0 |
| 2469 | if lt cc EXP_CAP_CONTRAS |
| 2470 | base = get exp_bc_inv_total 0 |
| 2471 | put exp_bc_nid cc nid |
| 2472 | put exp_bc_stmt_off cc stmt_o |
| 2473 | put exp_bc_stmt_len cc stmt_l |
| 2474 | put exp_bc_inv_off cc base |
| 2475 | put exp_bc_inv_cnt cc icnt |
| 2476 | for k ulong icnt bound 128 |
| 2477 | if lt add base k EXP_CAP_CON_INV |
| 2478 | put exp_bc_inv_id_off add base k get exp_w_inv_off k |
| 2479 | put exp_bc_inv_id_len add base k get exp_w_inv_len k |
| 2480 | end |
| 2481 | end |
| 2482 | put exp_bc_inv_total 0 add base icnt |
| 2483 | put exp_bc_cnt 0 add cc 1 |
| 2484 | end |
| 2485 | end |
| 2486 | |
| 2487 | fn exp_build_contra_node nid ulong to void |
| 2488 | ec = get exp_g_ecnt 0 |
| 2489 | tcnt = ulong 0 |
| 2490 | has_alt = false |
| 2491 | alt_o = ulong 0 |
| 2492 | alt_l = ulong 0 |
| 2493 | for e ulong ec bound 1024 |
| 2494 | if eq get exp_ge_owner e nid |
| 2495 | rl = get exp_ge_rel e |
| 2496 | eo = get exp_ge_to_off e |
| 2497 | el = get exp_ge_to_len e |
| 2498 | if eq rl EXP_REL_CONTRADICTS |
| 2499 | if lt tcnt 128 |
| 2500 | put exp_w_tmp_off tcnt eo |
| 2501 | put exp_w_tmp_len tcnt el |
| 2502 | tcnt = add tcnt 1 |
| 2503 | end |
| 2504 | else |
| 2505 | if eq rl EXP_REL_ALTERNATIVE_TO |
| 2506 | if not has_alt |
| 2507 | tnid = exp_g_find_node eo el |
| 2508 | if ne tnid EXP_NONE |
| 2509 | if ne get exp_w_vis tnid 0 |
| 2510 | has_alt = true |
| 2511 | alt_o = eo |
| 2512 | alt_l = el |
| 2513 | end |
| 2514 | end |
| 2515 | end |
| 2516 | end |
| 2517 | end |
| 2518 | end |
| 2519 | end |
| 2520 | icnt = ulong 0 |
| 2521 | for k ulong tcnt bound 128 |
| 2522 | tnid = exp_g_find_node get exp_w_tmp_off k get exp_w_tmp_len k |
| 2523 | if ne tnid EXP_NONE |
| 2524 | if ne get exp_w_vis tnid 0 |
| 2525 | put exp_w_inv_off icnt get exp_w_tmp_off k |
| 2526 | put exp_w_inv_len icnt get exp_w_tmp_len k |
| 2527 | icnt = add icnt 1 |
| 2528 | end |
| 2529 | end |
| 2530 | end |
| 2531 | if gt tcnt 0 |
| 2532 | if ne get exp_w_vis nid 0 |
| 2533 | put exp_w_inv_off icnt get exp_gn_id_off nid |
| 2534 | put exp_w_inv_len icnt get exp_gn_id_len nid |
| 2535 | icnt = add icnt 1 |
| 2536 | end |
| 2537 | end |
| 2538 | icnt = exp_sort_dedup_inv icnt |
| 2539 | if gt icnt 0 |
| 2540 | tcnt = exp_sort_dedup_tmp tcnt |
| 2541 | exp_contra_emit nid tcnt icnt |
| 2542 | end |
| 2543 | if has_alt |
| 2544 | if eq get exp_gn_kind nid EXP_KD_ALTERNATIVE |
| 2545 | ac = get exp_ba_cnt 0 |
| 2546 | if lt ac EXP_CAP_ALTS |
| 2547 | put exp_ba_nid ac nid |
| 2548 | put exp_ba_to_off ac alt_o |
| 2549 | put exp_ba_to_len ac alt_l |
| 2550 | put exp_ba_cnt 0 add ac 1 |
| 2551 | end |
| 2552 | end |
| 2553 | end |
| 2554 | end |
| 2555 | |
| 2556 | fn exp_build_contras_and_alts to void |
| 2557 | put exp_bc_cnt 0 0 |
| 2558 | put exp_bc_inv_total 0 0 |
| 2559 | put exp_ba_cnt 0 0 |
| 2560 | exp_sort_nodes_by_id |
| 2561 | nc = get exp_g_ncnt 0 |
| 2562 | for i ulong nc bound 320 |
| 2563 | nid = get exp_w_sorted_n i |
| 2564 | exp_build_contra_node nid |
| 2565 | end |
| 2566 | end |
| 2567 | |
| 2568 | fn exp_unc_cmp a ulong b ulong to int |
| 2569 | ka = get exp_bu_kind a |
| 2570 | kb = get exp_bu_kind b |
| 2571 | if lt ka kb |
| 2572 | ret -1 |
| 2573 | end |
| 2574 | if gt ka kb |
| 2575 | ret 1 |
| 2576 | end |
| 2577 | c1 = exp_str_cmp get exp_bu_nid_off a get exp_bu_nid_len a get exp_bu_nid_off b get exp_bu_nid_len b |
| 2578 | if ne c1 0 |
| 2579 | ret c1 |
| 2580 | end |
| 2581 | ha = get exp_bu_has_rel a |
| 2582 | hb = get exp_bu_has_rel b |
| 2583 | if lt ha hb |
| 2584 | ret -1 |
| 2585 | end |
| 2586 | if gt ha hb |
| 2587 | ret 1 |
| 2588 | end |
| 2589 | if ne ha 0 |
| 2590 | c2 = exp_str_cmp get exp_bu_rel_off a get exp_bu_rel_len a get exp_bu_rel_off b get exp_bu_rel_len b |
| 2591 | if ne c2 0 |
| 2592 | ret c2 |
| 2593 | end |
| 2594 | end |
| 2595 | ret exp_str_cmp get exp_bu_det_off a get exp_bu_det_len a get exp_bu_det_off b get exp_bu_det_len b |
| 2596 | end |
| 2597 | |
| 2598 | fn exp_unc_swap a ulong b ulong to void |
| 2599 | t0 = get exp_bu_kind a |
| 2600 | put exp_bu_kind a get exp_bu_kind b |
| 2601 | put exp_bu_kind b t0 |
| 2602 | t0 = get exp_bu_nid_off a |
| 2603 | put exp_bu_nid_off a get exp_bu_nid_off b |
| 2604 | put exp_bu_nid_off b t0 |
| 2605 | t0 = get exp_bu_nid_len a |
| 2606 | put exp_bu_nid_len a get exp_bu_nid_len b |
| 2607 | put exp_bu_nid_len b t0 |
| 2608 | t0 = get exp_bu_has_rel a |
| 2609 | put exp_bu_has_rel a get exp_bu_has_rel b |
| 2610 | put exp_bu_has_rel b t0 |
| 2611 | t0 = get exp_bu_rel_off a |
| 2612 | put exp_bu_rel_off a get exp_bu_rel_off b |
| 2613 | put exp_bu_rel_off b t0 |
| 2614 | t0 = get exp_bu_rel_len a |
| 2615 | put exp_bu_rel_len a get exp_bu_rel_len b |
| 2616 | put exp_bu_rel_len b t0 |
| 2617 | t0 = get exp_bu_det_off a |
| 2618 | put exp_bu_det_off a get exp_bu_det_off b |
| 2619 | put exp_bu_det_off b t0 |
| 2620 | t0 = get exp_bu_det_len a |
| 2621 | put exp_bu_det_len a get exp_bu_det_len b |
| 2622 | put exp_bu_det_len b t0 |
| 2623 | end |
| 2624 | |
| 2625 | fn exp_unc_copy dst ulong src ulong to void |
| 2626 | put exp_bu_kind dst get exp_bu_kind src |
| 2627 | put exp_bu_nid_off dst get exp_bu_nid_off src |
| 2628 | put exp_bu_nid_len dst get exp_bu_nid_len src |
| 2629 | put exp_bu_has_rel dst get exp_bu_has_rel src |
| 2630 | put exp_bu_rel_off dst get exp_bu_rel_off src |
| 2631 | put exp_bu_rel_len dst get exp_bu_rel_len src |
| 2632 | put exp_bu_det_off dst get exp_bu_det_off src |
| 2633 | put exp_bu_det_len dst get exp_bu_det_len src |
| 2634 | end |
| 2635 | |
| 2636 | fn exp_sort_dedup_uncs to void |
| 2637 | uc = get exp_bu_cnt 0 |
| 2638 | for a ulong uc bound 512 |
| 2639 | j = a |
| 2640 | while gt j 0 bound 512 |
| 2641 | p1 = sub j 1 |
| 2642 | if gt exp_unc_cmp p1 j 0 |
| 2643 | exp_unc_swap p1 j |
| 2644 | j = sub j 1 |
| 2645 | else |
| 2646 | break |
| 2647 | end |
| 2648 | end |
| 2649 | end |
| 2650 | outc = ulong 0 |
| 2651 | for a ulong uc bound 512 |
| 2652 | dup = false |
| 2653 | if gt outc 0 |
| 2654 | if eq exp_unc_cmp sub outc 1 a 0 |
| 2655 | dup = true |
| 2656 | end |
| 2657 | end |
| 2658 | if not dup |
| 2659 | if ne outc a |
| 2660 | exp_unc_copy outc a |
| 2661 | end |
| 2662 | outc = add outc 1 |
| 2663 | end |
| 2664 | end |
| 2665 | put exp_bu_cnt 0 outc |
| 2666 | end |
| 2667 | |
| 2668 | fn exp_node_has_meas nid ulong to bool |
| 2669 | mc = get exp_g_mcnt 0 |
| 2670 | for m ulong mc bound 256 |
| 2671 | if eq get exp_gm_owner m nid |
| 2672 | ret true |
| 2673 | end |
| 2674 | end |
| 2675 | ret false |
| 2676 | end |
| 2677 | |
| 2678 | fn exp_build_evidence to void |
| 2679 | put exp_be_cnt 0 0 |
| 2680 | sc = get exp_b_scnt 0 |
| 2681 | for i ulong sc bound 320 |
| 2682 | nid = get exp_bs_nid i |
| 2683 | keep = false |
| 2684 | if ne get exp_gn_has_dig nid 0 |
| 2685 | keep = true |
| 2686 | else |
| 2687 | if exp_node_has_meas nid |
| 2688 | keep = true |
| 2689 | end |
| 2690 | end |
| 2691 | if keep |
| 2692 | ec = get exp_be_cnt 0 |
| 2693 | put exp_be_step ec add i 1 |
| 2694 | put exp_be_cnt 0 add ec 1 |
| 2695 | end |
| 2696 | end |
| 2697 | end |
| 2698 | |
| 2699 | fn exp_build_bundle_raw tgt_o ulong tgt_l ulong max_d ulong max_n ulong to int |
| 2700 | rc = exp_check_limits max_d max_n |
| 2701 | if ne rc EXP_OK |
| 2702 | ret rc |
| 2703 | end |
| 2704 | rc = exp_g_validate |
| 2705 | if ne rc EXP_OK |
| 2706 | ret rc |
| 2707 | end |
| 2708 | tgt = exp_g_find_node tgt_o tgt_l |
| 2709 | if eq tgt EXP_NONE |
| 2710 | ret EXP_ERR_TARGET_NOT_FOUND |
| 2711 | end |
| 2712 | put exp_bu_cnt 0 0 |
| 2713 | exp_build_support_index |
| 2714 | vcnt = exp_build_bfs tgt max_d max_n |
| 2715 | exp_build_dag_cut_cycles tgt |
| 2716 | exp_build_kahn tgt vcnt |
| 2717 | exp_build_steps |
| 2718 | exp_build_step_uncs |
| 2719 | exp_build_contras_and_alts |
| 2720 | exp_build_evidence |
| 2721 | exp_sort_dedup_uncs |
| 2722 | put exp_bp_tgt_off 0 tgt_o |
| 2723 | put exp_bp_tgt_len 0 tgt_l |
| 2724 | put exp_bp_max_d 0 max_d |
| 2725 | put exp_bp_max_n 0 max_n |
| 2726 | put exp_bp_vis 0 vcnt |
| 2727 | put exp_bp_gnodes 0 get exp_g_ncnt 0 |
| 2728 | idig = new uchar 32 0 |
| 2729 | exp_compute_input_digest idig |
| 2730 | for i ulong 32 |
| 2731 | put exp_bp_in_dig i get idig i |
| 2732 | end |
| 2733 | bdig = new uchar 32 0 |
| 2734 | exp_compute_bundle_digest bdig |
| 2735 | for i ulong 32 |
| 2736 | put exp_bp_bun_dig i get bdig i |
| 2737 | end |
| 2738 | ret EXP_OK |
| 2739 | end |
| 2740 | |
| 2741 | fn exp_build_bundle target view uchar * max_d ulong max_n ulong to int |
| 2742 | tgt_o = exp_str_add_view target |
| 2743 | ret exp_build_bundle_raw tgt_o len target max_d max_n |
| 2744 | end |
| 2745 | |
| 2746 | note Bundle query helpers |
| 2747 | fn exp_bundle_has_unc uk ulong to bool |
| 2748 | uc = get exp_bu_cnt 0 |
| 2749 | for i ulong uc bound 512 |
| 2750 | if eq get exp_bu_kind i uk |
| 2751 | ret true |
| 2752 | end |
| 2753 | end |
| 2754 | ret false |
| 2755 | end |
| 2756 | |
| 2757 | fn exp_bundle_step_eq idx1 ulong want view uchar * to bool |
| 2758 | if eq idx1 0 |
| 2759 | ret false |
| 2760 | end |
| 2761 | if gt idx1 get exp_b_scnt 0 |
| 2762 | ret false |
| 2763 | end |
| 2764 | nid = get exp_bs_nid sub idx1 1 |
| 2765 | ret exp_str_eq_view get exp_gn_id_off nid get exp_gn_id_len nid want |
| 2766 | end |
| 2767 | |
| 2768 | fn exp_bundle_find_step id view uchar * to ulong |
| 2769 | sc = get exp_b_scnt 0 |
| 2770 | for i ulong sc bound 320 |
| 2771 | nid = get exp_bs_nid i |
| 2772 | if exp_str_eq_view get exp_gn_id_off nid get exp_gn_id_len nid id |
| 2773 | ret add i 1 |
| 2774 | end |
| 2775 | end |
| 2776 | ret 0 |
| 2777 | end |
| 2778 | |
| 2779 | note ============================================================================ |
| 2780 | note Section 9: Cortex Store, Sealed Journal & Agent Branch Adapters (adapters.rs) |
| 2781 | note ============================================================================ |
| 2782 | |
| 2783 | fn exp_cx_reset to void |
| 2784 | put exp_cx_str_len 0 0 |
| 2785 | put exp_cx_cnt 0 0 |
| 2786 | put exp_cx_seq 0 1 |
| 2787 | put exp_cx_pcnt 0 0 |
| 2788 | end |
| 2789 | |
| 2790 | fn exp_cx_str_add_view s view uchar * to ulong |
| 2791 | start = get exp_cx_str_len 0 |
| 2792 | n = len s |
| 2793 | for i ulong n bound 1024 |
| 2794 | p = get exp_cx_str_len 0 |
| 2795 | if lt p 16384 |
| 2796 | put exp_cx_str p get s i |
| 2797 | put exp_cx_str_len 0 add p 1 |
| 2798 | end |
| 2799 | end |
| 2800 | ret start |
| 2801 | end |
| 2802 | |
| 2803 | fn exp_str_add_cx_arena off ulong ln ulong to ulong |
| 2804 | start = get exp_str_len 0 |
| 2805 | for i ulong ln bound 1024 |
| 2806 | exp_str_put_b get exp_cx_str add off i |
| 2807 | end |
| 2808 | ret start |
| 2809 | end |
| 2810 | |
| 2811 | fn exp_cx_id_eq a_base ulong b_base ulong to bool |
| 2812 | for i ulong 16 |
| 2813 | if ne get exp_cx_id add a_base i get exp_cx_id add b_base i |
| 2814 | ret false |
| 2815 | end |
| 2816 | end |
| 2817 | ret true |
| 2818 | end |
| 2819 | |
| 2820 | fn exp_cx_id_eq_view base ulong v view uchar * to bool |
| 2821 | for i ulong 16 |
| 2822 | if ne get exp_cx_id add base i get v i |
| 2823 | ret false |
| 2824 | end |
| 2825 | end |
| 2826 | ret true |
| 2827 | end |
| 2828 | |
| 2829 | fn exp_cx_id_cmp_view a view uchar * b view uchar * to int |
| 2830 | for i ulong 16 |
| 2831 | ca = get a i |
| 2832 | cb = get b i |
| 2833 | if lt ca cb |
| 2834 | ret -1 |
| 2835 | end |
| 2836 | if gt ca cb |
| 2837 | ret 1 |
| 2838 | end |
| 2839 | end |
| 2840 | ret 0 |
| 2841 | end |
| 2842 | |
| 2843 | fn exp_cx_find id view uchar * to ulong |
| 2844 | cnt = get exp_cx_cnt 0 |
| 2845 | for i ulong cnt bound 64 |
| 2846 | if exp_cx_id_eq_view mul i 16 id |
| 2847 | ret i |
| 2848 | end |
| 2849 | end |
| 2850 | ret EXP_NONE |
| 2851 | end |
| 2852 | |
| 2853 | fn exp_cx_make_id seq ulong stmt view uchar * dst edit uchar * to void |
| 2854 | ctx = new ulong 40 0 |
| 2855 | dig = new uchar 32 0 |
| 2856 | sha256_init ctx |
| 2857 | exp_sha_u64 ctx seq |
| 2858 | sha256_update ctx stmt |
| 2859 | sha256_final ctx dig |
| 2860 | for i ulong 16 |
| 2861 | put dst i get dig i |
| 2862 | end |
| 2863 | end |
| 2864 | |
| 2865 | fn exp_cx_add_rec status ulong stmt view uchar * prov view uchar * to ulong |
| 2866 | idx = get exp_cx_cnt 0 |
| 2867 | if ge idx EXP_CX_CAP |
| 2868 | ret EXP_NONE |
| 2869 | end |
| 2870 | seq = get exp_cx_seq 0 |
| 2871 | put exp_cx_seq 0 add seq 1 |
| 2872 | id = new uchar 16 0 |
| 2873 | exp_cx_make_id seq stmt id |
| 2874 | base16 = mul idx 16 |
| 2875 | for i ulong 16 |
| 2876 | put exp_cx_id add base16 i get id i |
| 2877 | end |
| 2878 | base32 = mul idx 32 |
| 2879 | for i ulong 32 |
| 2880 | put exp_cx_evhash add base32 i 0 |
| 2881 | end |
| 2882 | put exp_cx_status idx status |
| 2883 | so = exp_cx_str_add_view stmt |
| 2884 | po = exp_cx_str_add_view prov |
| 2885 | put exp_cx_stmt_off idx so |
| 2886 | put exp_cx_stmt_len idx len stmt |
| 2887 | put exp_cx_prov_off idx po |
| 2888 | put exp_cx_prov_len idx len prov |
| 2889 | put exp_cx_ts idx seq |
| 2890 | put exp_cx_has_conf idx 0 |
| 2891 | put exp_cx_conf idx 0 |
| 2892 | put exp_cx_has_vby idx 0 |
| 2893 | put exp_cx_vby_off idx 0 |
| 2894 | put exp_cx_vby_len idx 0 |
| 2895 | put exp_cx_cnt 0 add idx 1 |
| 2896 | ret idx |
| 2897 | end |
| 2898 | |
| 2899 | fn exp_cx_set_ev idx ulong ev view uchar * to void |
| 2900 | if ge idx get exp_cx_cnt 0 |
| 2901 | ret |
| 2902 | end |
| 2903 | evh = new uchar 32 0 |
| 2904 | dl = sha2_digest 256 ev evh |
| 2905 | base32 = mul idx 32 |
| 2906 | for i ulong 32 |
| 2907 | put exp_cx_evhash add base32 i get evh i |
| 2908 | end |
| 2909 | end |
| 2910 | |
| 2911 | fn exp_cx_set_id idx ulong id view uchar * to void |
| 2912 | if ge idx get exp_cx_cnt 0 |
| 2913 | ret |
| 2914 | end |
| 2915 | base16 = mul idx 16 |
| 2916 | for i ulong 16 |
| 2917 | put exp_cx_id add base16 i get id i |
| 2918 | end |
| 2919 | end |
| 2920 | |
| 2921 | fn exp_cx_get_id idx ulong dst edit uchar * to void |
| 2922 | base16 = mul idx 16 |
| 2923 | for i ulong 16 |
| 2924 | put dst i get exp_cx_id add base16 i |
| 2925 | end |
| 2926 | end |
| 2927 | |
| 2928 | fn exp_cx_set_meta idx ulong ts ulong has_c ulong conf ulong to void |
| 2929 | if ge idx get exp_cx_cnt 0 |
| 2930 | ret |
| 2931 | end |
| 2932 | put exp_cx_ts idx ts |
| 2933 | put exp_cx_has_conf idx has_c |
| 2934 | put exp_cx_conf idx conf |
| 2935 | end |
| 2936 | |
| 2937 | fn exp_cx_set_verified_by idx ulong vby view uchar * to void |
| 2938 | if ge idx get exp_cx_cnt 0 |
| 2939 | ret |
| 2940 | end |
| 2941 | vo = exp_cx_str_add_view vby |
| 2942 | put exp_cx_has_vby idx 1 |
| 2943 | put exp_cx_vby_off idx vo |
| 2944 | put exp_cx_vby_len idx len vby |
| 2945 | end |
| 2946 | |
| 2947 | fn exp_cx_add_parent idx ulong pid view uchar * to void |
| 2948 | pc = get exp_cx_pcnt 0 |
| 2949 | if ge pc EXP_CX_PAR_CAP |
| 2950 | ret |
| 2951 | end |
| 2952 | put exp_cx_p_own pc idx |
| 2953 | base = mul pc 16 |
| 2954 | for i ulong 16 |
| 2955 | put exp_cx_p_id add base i get pid i |
| 2956 | end |
| 2957 | put exp_cx_pcnt 0 add pc 1 |
| 2958 | end |
| 2959 | |
| 2960 | fn exp_cx_node_id_str id view uchar * to ulong |
| 2961 | st = exp_str_mark |
| 2962 | t0 = exp_str_add_view exp_txt_cx_pfx |
| 2963 | exp_str_add_hex16_view id |
| 2964 | ret st |
| 2965 | end |
| 2966 | |
| 2967 | fn exp_cx_emit_edges ridx ulong nid ulong sts ulong to void |
| 2968 | rel = EXP_REL_SUPPORTED_BY |
| 2969 | if eq sts EXP_ST_CONTRADICTION |
| 2970 | rel = EXP_REL_CONTRADICTS |
| 2971 | end |
| 2972 | pc = get exp_cx_pcnt 0 |
| 2973 | pid = new uchar 16 0 |
| 2974 | for p ulong pc bound 256 |
| 2975 | if eq get exp_cx_p_own p ridx |
| 2976 | base = mul p 16 |
| 2977 | for k ulong 16 |
| 2978 | put pid k get exp_cx_p_id add base k |
| 2979 | end |
| 2980 | po = exp_cx_node_id_str pid |
| 2981 | exp_g_add_edge_raw nid po 39 rel |
| 2982 | end |
| 2983 | end |
| 2984 | end |
| 2985 | |
| 2986 | fn exp_cx_emit_node ridx ulong to ulong |
| 2987 | id = new uchar 16 0 |
| 2988 | exp_cx_get_id ridx id |
| 2989 | id_o = exp_cx_node_id_str id |
| 2990 | sts = get exp_cx_status ridx |
| 2991 | nid = exp_g_add_node_raw id_o 39 EXP_KD_CORTEX_RECORD sts |
| 2992 | if eq nid EXP_NONE |
| 2993 | ret EXP_NONE |
| 2994 | end |
| 2995 | t = exp_str_add_cx_arena get exp_cx_stmt_off ridx get exp_cx_stmt_len ridx |
| 2996 | put exp_gn_stmt_off nid t |
| 2997 | put exp_gn_stmt_len nid get exp_cx_stmt_len ridx |
| 2998 | t = exp_str_add_view exp_txt_cx_sys |
| 2999 | put exp_gn_sys_off nid t |
| 3000 | put exp_gn_sys_len nid len exp_txt_cx_sys |
| 3001 | t = exp_str_add_view exp_txt_cx_art |
| 3002 | put exp_gn_art_off nid t |
| 3003 | put exp_gn_art_len nid len exp_txt_cx_art |
| 3004 | t = exp_str_add_cx_arena get exp_cx_prov_off ridx get exp_cx_prov_len ridx |
| 3005 | put exp_gn_ref_off nid t |
| 3006 | put exp_gn_ref_len nid get exp_cx_prov_len ridx |
| 3007 | t = exp_str_mark |
| 3008 | exp_str_add_hex32_cx mul ridx 32 |
| 3009 | put exp_gn_has_dig nid 1 |
| 3010 | put exp_gn_dig_off nid t |
| 3011 | put exp_gn_dig_len nid 64 |
| 3012 | if ne get exp_cx_has_conf ridx 0 |
| 3013 | put exp_gn_has_conf nid 1 |
| 3014 | put exp_gn_conf nid get exp_cx_conf ridx |
| 3015 | t = exp_str_add_view exp_txt_cx_cbas |
| 3016 | put exp_gn_has_cbas nid 1 |
| 3017 | put exp_gn_cbas_off nid t |
| 3018 | put exp_gn_cbas_len nid len exp_txt_cx_cbas |
| 3019 | end |
| 3020 | put exp_gn_has_ts nid 1 |
| 3021 | put exp_gn_ts nid get exp_cx_ts ridx |
| 3022 | if ne get exp_cx_has_vby ridx 0 |
| 3023 | ns = exp_str_mark |
| 3024 | t = exp_str_add_view exp_txt_vby_pfx |
| 3025 | t = exp_str_add_cx_arena get exp_cx_vby_off ridx get exp_cx_vby_len ridx |
| 3026 | nc = get exp_g_notecnt 0 |
| 3027 | if lt nc EXP_CAP_NOTES |
| 3028 | put exp_go_owner nc nid |
| 3029 | put exp_go_off nc ns |
| 3030 | put exp_go_len nc sub exp_str_mark ns |
| 3031 | put exp_g_notecnt 0 add nc 1 |
| 3032 | end |
| 3033 | end |
| 3034 | exp_cx_emit_edges ridx nid sts |
| 3035 | ret nid |
| 3036 | end |
| 3037 | |
| 3038 | fn exp_cx_contra_touches ci ulong to bool |
| 3039 | pc = get exp_cx_pcnt 0 |
| 3040 | pid = new uchar 16 0 |
| 3041 | for p ulong pc bound 256 |
| 3042 | if eq get exp_cx_p_own p ci |
| 3043 | base = mul p 16 |
| 3044 | for j ulong 16 |
| 3045 | put pid j get exp_cx_p_id add base j |
| 3046 | end |
| 3047 | pi = exp_cx_find pid |
| 3048 | if ne pi EXP_NONE |
| 3049 | if ne get exp_cxw_vis pi 0 |
| 3050 | ret true |
| 3051 | end |
| 3052 | end |
| 3053 | end |
| 3054 | end |
| 3055 | ret false |
| 3056 | end |
| 3057 | |
| 3058 | fn exp_cx_mark_contra_trunc ci ulong to void |
| 3059 | pc = get exp_cx_pcnt 0 |
| 3060 | pid = new uchar 16 0 |
| 3061 | for p ulong pc bound 256 |
| 3062 | if eq get exp_cx_p_own p ci |
| 3063 | base = mul p 16 |
| 3064 | for j ulong 16 |
| 3065 | put pid j get exp_cx_p_id add base j |
| 3066 | end |
| 3067 | pi = exp_cx_find pid |
| 3068 | if ne pi EXP_NONE |
| 3069 | if ne get exp_cxw_vis pi 0 |
| 3070 | put exp_cxw_trunc pi 1 |
| 3071 | end |
| 3072 | end |
| 3073 | end |
| 3074 | end |
| 3075 | end |
| 3076 | |
| 3077 | fn exp_cx_bfs_disclose mn ulong to void |
| 3078 | cnt = get exp_cx_cnt 0 |
| 3079 | extra = ulong 0 |
| 3080 | for ci ulong cnt bound 64 |
| 3081 | if eq get exp_cx_status ci EXP_ST_CONTRADICTION |
| 3082 | if eq get exp_cxw_emit ci 0 |
| 3083 | if exp_cx_contra_touches ci |
| 3084 | if ge extra mn |
| 3085 | exp_cx_mark_contra_trunc ci |
| 3086 | else |
| 3087 | extra = add extra 1 |
| 3088 | put exp_cxw_emit ci 1 |
| 3089 | dn = exp_cx_emit_node ci |
| 3090 | end |
| 3091 | end |
| 3092 | end |
| 3093 | end |
| 3094 | end |
| 3095 | end |
| 3096 | |
| 3097 | fn exp_cx_seed_roots roots view ulong * mn ulong to void |
| 3098 | cnt = get exp_cx_cnt 0 |
| 3099 | rcnt = len roots |
| 3100 | for k ulong rcnt bound 64 |
| 3101 | ri = get roots k |
| 3102 | if lt ri cnt |
| 3103 | put exp_cxw_isroot ri 1 |
| 3104 | if eq get exp_cxw_vis ri 0 |
| 3105 | vcnt = get exp_w_bfs_st 1 |
| 3106 | if ge vcnt mn |
| 3107 | for m ulong cnt bound 64 |
| 3108 | if ne get exp_cxw_vis m 0 |
| 3109 | put exp_cxw_trunc m 1 |
| 3110 | end |
| 3111 | end |
| 3112 | break |
| 3113 | end |
| 3114 | qt = get exp_w_bfs_st 0 |
| 3115 | put exp_cxw_vis ri 1 |
| 3116 | put exp_cxw_depth ri 0 |
| 3117 | put exp_cxw_q qt ri |
| 3118 | put exp_w_bfs_st 0 add qt 1 |
| 3119 | put exp_w_bfs_st 1 add vcnt 1 |
| 3120 | end |
| 3121 | end |
| 3122 | end |
| 3123 | end |
| 3124 | |
| 3125 | fn exp_cx_bfs_expand u ulong md ulong mn ulong to void |
| 3126 | d = get exp_cxw_depth u |
| 3127 | pc = get exp_cx_pcnt 0 |
| 3128 | pid = new uchar 16 0 |
| 3129 | for p ulong pc bound 256 |
| 3130 | if eq get exp_cx_p_own p u |
| 3131 | base = mul p 16 |
| 3132 | for j ulong 16 |
| 3133 | put pid j get exp_cx_p_id add base j |
| 3134 | end |
| 3135 | pi = exp_cx_find pid |
| 3136 | if ne pi EXP_NONE |
| 3137 | if eq get exp_cxw_vis pi 0 |
| 3138 | vcnt = get exp_w_bfs_st 1 |
| 3139 | if or gt add d 1 md ge vcnt mn |
| 3140 | put exp_cxw_trunc u 1 |
| 3141 | else |
| 3142 | qt = get exp_w_bfs_st 0 |
| 3143 | put exp_cxw_vis pi 1 |
| 3144 | put exp_cxw_depth pi add d 1 |
| 3145 | put exp_cxw_q qt pi |
| 3146 | put exp_w_bfs_st 0 add qt 1 |
| 3147 | put exp_w_bfs_st 1 add vcnt 1 |
| 3148 | end |
| 3149 | end |
| 3150 | end |
| 3151 | end |
| 3152 | end |
| 3153 | end |
| 3154 | |
| 3155 | fn exp_cx_graph_from_roots roots view ulong * max_d ulong max_n ulong to void |
| 3156 | exp_g_reset_prod exp_txt_cx_prod |
| 3157 | cnt = get exp_cx_cnt 0 |
| 3158 | for i ulong cnt bound 64 |
| 3159 | put exp_cxw_vis i 0 |
| 3160 | put exp_cxw_depth i EXP_NONE |
| 3161 | put exp_cxw_trunc i 0 |
| 3162 | put exp_cxw_isroot i 0 |
| 3163 | put exp_cxw_emit i 0 |
| 3164 | end |
| 3165 | md = add max_d 1 |
| 3166 | mn = add max_n 1 |
| 3167 | put exp_w_bfs_st 0 0 |
| 3168 | put exp_w_bfs_st 1 0 |
| 3169 | exp_cx_seed_roots roots mn |
| 3170 | ocnt = ulong 0 |
| 3171 | qh = ulong 0 |
| 3172 | while lt qh get exp_w_bfs_st 0 bound 64 |
| 3173 | u = get exp_cxw_q qh |
| 3174 | qh = add qh 1 |
| 3175 | put exp_cxw_ord ocnt u |
| 3176 | ocnt = add ocnt 1 |
| 3177 | st = get exp_cx_status u |
| 3178 | skip = false |
| 3179 | if eq st EXP_ST_CONTRADICTION |
| 3180 | skip = true |
| 3181 | else |
| 3182 | if exp_status_is_grounding st |
| 3183 | if eq get exp_cxw_isroot u 0 |
| 3184 | skip = true |
| 3185 | end |
| 3186 | end |
| 3187 | end |
| 3188 | if not skip |
| 3189 | exp_cx_bfs_expand u md mn |
| 3190 | end |
| 3191 | end |
| 3192 | for k ulong ocnt bound 64 |
| 3193 | ri = get exp_cxw_ord k |
| 3194 | if eq get exp_cxw_emit ri 0 |
| 3195 | put exp_cxw_emit ri 1 |
| 3196 | dn = exp_cx_emit_node ri |
| 3197 | end |
| 3198 | end |
| 3199 | exp_cx_bfs_disclose mn |
| 3200 | pid = new uchar 16 0 |
| 3201 | for i ulong cnt bound 64 |
| 3202 | if ne get exp_cxw_trunc i 0 |
| 3203 | exp_cx_get_id i pid |
| 3204 | po = exp_cx_node_id_str pid |
| 3205 | exp_g_add_truncated_raw po 39 |
| 3206 | end |
| 3207 | end |
| 3208 | end |
| 3209 | |
| 3210 | fn exp_cx_graph_for_record id view uchar * max_d ulong max_n ulong to int |
| 3211 | ri = exp_cx_find id |
| 3212 | if eq ri EXP_NONE |
| 3213 | ret EXP_ERR_TARGET_NOT_FOUND |
| 3214 | end |
| 3215 | roots = new ulong 1 0 |
| 3216 | put roots 0 ri |
| 3217 | exp_cx_graph_from_roots roots max_d max_n |
| 3218 | ret EXP_OK |
| 3219 | end |
| 3220 | |
| 3221 | note Sealed Cortex journal export and tamper-detecting import |
| 3222 | fn exp_j_put_u64 dst edit uchar * off ulong v ulong to void |
| 3223 | for i ulong 8 |
| 3224 | sh = mul sub 7 i 8 |
| 3225 | put dst add off i uchar and shr v sh 0xff |
| 3226 | end |
| 3227 | end |
| 3228 | |
| 3229 | fn exp_j_get_u64 src view uchar * off ulong to ulong |
| 3230 | acc = ulong 0 |
| 3231 | for i ulong 8 |
| 3232 | acc = or shl acc 8 ulong get src add off i |
| 3233 | end |
| 3234 | ret acc |
| 3235 | end |
| 3236 | |
| 3237 | fn exp_j_put_cx_str dst edit uchar * pos ulong off ulong ln ulong to ulong |
| 3238 | exp_j_put_u64 dst pos ln |
| 3239 | pos = add pos 8 |
| 3240 | for k ulong ln bound 512 |
| 3241 | put dst add pos k get exp_cx_str add off k |
| 3242 | end |
| 3243 | ret add pos ln |
| 3244 | end |
| 3245 | |
| 3246 | fn exp_j_get_cx_str src view uchar * pos ulong ln ulong to ulong |
| 3247 | so = get exp_cx_str_len 0 |
| 3248 | for k ulong ln bound 512 |
| 3249 | p = get exp_cx_str_len 0 |
| 3250 | if lt p 16384 |
| 3251 | put exp_cx_str p get src add pos k |
| 3252 | put exp_cx_str_len 0 add p 1 |
| 3253 | end |
| 3254 | end |
| 3255 | ret so |
| 3256 | end |
| 3257 | |
| 3258 | fn exp_cx_export_rec dst edit uchar * pos ulong i ulong to ulong |
| 3259 | if gt add pos add add 136 get exp_cx_stmt_len i get exp_cx_prov_len i len dst |
| 3260 | ret 0 |
| 3261 | end |
| 3262 | base = mul i 16 |
| 3263 | for k ulong 16 |
| 3264 | put dst add pos k get exp_cx_id add base k |
| 3265 | end |
| 3266 | pos = add pos 16 |
| 3267 | base = mul i 32 |
| 3268 | for k ulong 32 |
| 3269 | put dst add pos k get exp_cx_evhash add base k |
| 3270 | end |
| 3271 | pos = add pos 32 |
| 3272 | exp_j_put_u64 dst pos get exp_cx_status i |
| 3273 | pos = add pos 8 |
| 3274 | exp_j_put_u64 dst pos get exp_cx_ts i |
| 3275 | pos = add pos 8 |
| 3276 | exp_j_put_u64 dst pos get exp_cx_has_conf i |
| 3277 | pos = add pos 8 |
| 3278 | exp_j_put_u64 dst pos get exp_cx_conf i |
| 3279 | pos = add pos 8 |
| 3280 | pos = exp_j_put_cx_str dst pos get exp_cx_stmt_off i get exp_cx_stmt_len i |
| 3281 | ret exp_j_put_cx_str dst pos get exp_cx_prov_off i get exp_cx_prov_len i |
| 3282 | end |
| 3283 | |
| 3284 | fn exp_cx_export_journal dst edit uchar * to ulong |
| 3285 | cap = len dst |
| 3286 | if lt cap 64 |
| 3287 | ret 0 |
| 3288 | end |
| 3289 | for i ulong 4 |
| 3290 | put dst i get exp_txt_jmagic i |
| 3291 | end |
| 3292 | exp_j_put_u64 dst 4 1 |
| 3293 | cnt = get exp_cx_cnt 0 |
| 3294 | pc = get exp_cx_pcnt 0 |
| 3295 | exp_j_put_u64 dst 12 cnt |
| 3296 | exp_j_put_u64 dst 20 pc |
| 3297 | pos = ulong 28 |
| 3298 | for i ulong cnt bound 64 |
| 3299 | pos = exp_cx_export_rec dst pos i |
| 3300 | if eq pos 0 |
| 3301 | ret 0 |
| 3302 | end |
| 3303 | end |
| 3304 | for p ulong pc bound 256 |
| 3305 | if gt add add pos 24 32 cap |
| 3306 | ret 0 |
| 3307 | end |
| 3308 | exp_j_put_u64 dst pos get exp_cx_p_own p |
| 3309 | pos = add pos 8 |
| 3310 | base = mul p 16 |
| 3311 | for k ulong 16 |
| 3312 | put dst add pos k get exp_cx_p_id add base k |
| 3313 | end |
| 3314 | pos = add pos 16 |
| 3315 | end |
| 3316 | seal_dig = new uchar 32 0 |
| 3317 | dl = sha2_digest 256 part dst 0 pos seal_dig |
| 3318 | for k ulong 32 |
| 3319 | put dst add pos k get seal_dig k |
| 3320 | end |
| 3321 | ret add pos 32 |
| 3322 | end |
| 3323 | |
| 3324 | fn exp_cx_import_rec src view uchar * pos ulong body_len ulong i ulong to ulong |
| 3325 | if gt add pos 96 body_len |
| 3326 | ret 0 |
| 3327 | end |
| 3328 | base = mul i 16 |
| 3329 | for k ulong 16 |
| 3330 | put exp_cx_id add base k get src add pos k |
| 3331 | end |
| 3332 | pos = add pos 16 |
| 3333 | base = mul i 32 |
| 3334 | for k ulong 32 |
| 3335 | put exp_cx_evhash add base k get src add pos k |
| 3336 | end |
| 3337 | pos = add pos 32 |
| 3338 | put exp_cx_status i exp_j_get_u64 src pos |
| 3339 | pos = add pos 8 |
| 3340 | put exp_cx_ts i exp_j_get_u64 src pos |
| 3341 | pos = add pos 8 |
| 3342 | put exp_cx_has_conf i exp_j_get_u64 src pos |
| 3343 | pos = add pos 8 |
| 3344 | put exp_cx_conf i exp_j_get_u64 src pos |
| 3345 | pos = add pos 8 |
| 3346 | ln = exp_j_get_u64 src pos |
| 3347 | pos = add pos 8 |
| 3348 | if gt add pos ln body_len |
| 3349 | ret 0 |
| 3350 | end |
| 3351 | put exp_cx_stmt_off i exp_j_get_cx_str src pos ln |
| 3352 | put exp_cx_stmt_len i ln |
| 3353 | pos = add pos ln |
| 3354 | if gt add pos 8 body_len |
| 3355 | ret 0 |
| 3356 | end |
| 3357 | ln = exp_j_get_u64 src pos |
| 3358 | pos = add pos 8 |
| 3359 | if gt add pos ln body_len |
| 3360 | ret 0 |
| 3361 | end |
| 3362 | put exp_cx_prov_off i exp_j_get_cx_str src pos ln |
| 3363 | put exp_cx_prov_len i ln |
| 3364 | put exp_cx_has_vby i 0 |
| 3365 | ret add pos ln |
| 3366 | end |
| 3367 | |
| 3368 | fn exp_cx_import_par src view uchar * pos ulong body_len ulong p ulong to ulong |
| 3369 | if gt add pos 24 body_len |
| 3370 | ret 0 |
| 3371 | end |
| 3372 | put exp_cx_p_own p exp_j_get_u64 src pos |
| 3373 | pos = add pos 8 |
| 3374 | base = mul p 16 |
| 3375 | for k ulong 16 |
| 3376 | put exp_cx_p_id add base k get src add pos k |
| 3377 | end |
| 3378 | ret add pos 16 |
| 3379 | end |
| 3380 | |
| 3381 | fn exp_cx_journal_seal_ok src view uchar * to bool |
| 3382 | n = len src |
| 3383 | if lt n 60 |
| 3384 | ret false |
| 3385 | end |
| 3386 | for i ulong 4 |
| 3387 | if ne get src i get exp_txt_jmagic i |
| 3388 | ret false |
| 3389 | end |
| 3390 | end |
| 3391 | if ne exp_j_get_u64 src 4 1 |
| 3392 | ret false |
| 3393 | end |
| 3394 | body_len = sub n 32 |
| 3395 | chk = new uchar 32 0 |
| 3396 | dl = sha2_digest 256 part src 0 body_len chk |
| 3397 | for i ulong 32 |
| 3398 | if ne get chk i get src add body_len i |
| 3399 | ret false |
| 3400 | end |
| 3401 | end |
| 3402 | ret true |
| 3403 | end |
| 3404 | |
| 3405 | fn exp_cx_import_journal src view uchar * to int |
| 3406 | if not exp_cx_journal_seal_ok src |
| 3407 | ret EXP_ERR_JOURNAL |
| 3408 | end |
| 3409 | body_len = sub len src 32 |
| 3410 | cnt = exp_j_get_u64 src 12 |
| 3411 | pc = exp_j_get_u64 src 20 |
| 3412 | if or gt cnt EXP_CX_CAP gt pc EXP_CX_PAR_CAP |
| 3413 | ret EXP_ERR_JOURNAL |
| 3414 | end |
| 3415 | exp_cx_reset |
| 3416 | pos = ulong 28 |
| 3417 | for i ulong cnt bound 64 |
| 3418 | pos = exp_cx_import_rec src pos body_len i |
| 3419 | if eq pos 0 |
| 3420 | ret EXP_ERR_JOURNAL |
| 3421 | end |
| 3422 | end |
| 3423 | for p ulong pc bound 256 |
| 3424 | pos = exp_cx_import_par src pos body_len p |
| 3425 | if eq pos 0 |
| 3426 | ret EXP_ERR_JOURNAL |
| 3427 | end |
| 3428 | end |
| 3429 | put exp_cx_cnt 0 cnt |
| 3430 | put exp_cx_pcnt 0 pc |
| 3431 | ret EXP_OK |
| 3432 | end |
| 3433 | |
| 3434 | note Agent branch adapter |
| 3435 | fn exp_br_reset to void |
| 3436 | put exp_br_cnt 0 0 |
| 3437 | put exp_br_rcnt 0 0 |
| 3438 | put exp_br_tcnt 0 0 |
| 3439 | end |
| 3440 | |
| 3441 | fn exp_br_create id view uchar * depth ulong ts ulong to ulong |
| 3442 | idx = get exp_br_cnt 0 |
| 3443 | if ge idx EXP_BR_CAP |
| 3444 | ret EXP_NONE |
| 3445 | end |
| 3446 | base = mul idx 16 |
| 3447 | for i ulong 16 |
| 3448 | put exp_br_id add base i get id i |
| 3449 | end |
| 3450 | put exp_br_has_par idx 0 |
| 3451 | put exp_br_depth idx depth |
| 3452 | put exp_br_ts idx ts |
| 3453 | put exp_br_cnt 0 add idx 1 |
| 3454 | ret idx |
| 3455 | end |
| 3456 | |
| 3457 | fn exp_br_set_parent bidx ulong pid view uchar * to void |
| 3458 | if ge bidx get exp_br_cnt 0 |
| 3459 | ret |
| 3460 | end |
| 3461 | base = mul bidx 16 |
| 3462 | for i ulong 16 |
| 3463 | put exp_br_par_id add base i get pid i |
| 3464 | end |
| 3465 | put exp_br_has_par bidx 1 |
| 3466 | end |
| 3467 | |
| 3468 | fn exp_br_add_ref bidx ulong rec_id view uchar * to void |
| 3469 | rc = get exp_br_rcnt 0 |
| 3470 | if ge rc EXP_BR_REF_CAP |
| 3471 | ret |
| 3472 | end |
| 3473 | put exp_br_r_own rc bidx |
| 3474 | base = mul rc 16 |
| 3475 | for i ulong 16 |
| 3476 | put exp_br_r_id add base i get rec_id i |
| 3477 | end |
| 3478 | put exp_br_rcnt 0 add rc 1 |
| 3479 | end |
| 3480 | |
| 3481 | fn exp_br_append_token bidx ulong tok ulong to void |
| 3482 | tc = get exp_br_tcnt 0 |
| 3483 | if ge tc EXP_BR_TOK_CAP |
| 3484 | ret |
| 3485 | end |
| 3486 | put exp_br_t_own tc bidx |
| 3487 | put exp_br_t_val tc tok |
| 3488 | put exp_br_tcnt 0 add tc 1 |
| 3489 | end |
| 3490 | |
| 3491 | fn exp_br_node_id_str bidx ulong to ulong |
| 3492 | st = exp_str_mark |
| 3493 | t0 = exp_str_add_view exp_txt_br_pfx |
| 3494 | exp_str_add_hex16 mul bidx 16 2 |
| 3495 | ret st |
| 3496 | end |
| 3497 | |
| 3498 | fn exp_br_emit_refs bidx ulong bnid ulong to void |
| 3499 | tot = get exp_br_rcnt 0 |
| 3500 | rid = new uchar 16 0 |
| 3501 | for i ulong tot bound 64 |
| 3502 | if eq get exp_br_r_own i bidx |
| 3503 | rbase = mul i 16 |
| 3504 | for k ulong 16 |
| 3505 | put rid k get exp_br_r_id add rbase k |
| 3506 | end |
| 3507 | co = exp_cx_node_id_str rid |
| 3508 | exp_g_add_edge_raw bnid co 39 EXP_REL_REFERENCES |
| 3509 | exp_g_add_scope_ref_raw co 39 |
| 3510 | end |
| 3511 | end |
| 3512 | end |
| 3513 | |
| 3514 | fn exp_br_emit_node bidx ulong to ulong |
| 3515 | bo = exp_br_node_id_str bidx |
| 3516 | bnid = exp_g_add_node_raw bo 39 EXP_KD_AGENT_BRANCH EXP_ST_UNCLASSIFIED |
| 3517 | st = exp_str_mark |
| 3518 | t = exp_str_add_view exp_s_br_a |
| 3519 | base = mul bidx 16 |
| 3520 | for i ulong 8 |
| 3521 | b = ulong get exp_br_id add base i |
| 3522 | exp_str_put_b get exp_txt_hex shr b 4 |
| 3523 | exp_str_put_b get exp_txt_hex and b 15 |
| 3524 | end |
| 3525 | t = exp_str_add_view exp_s_br_b |
| 3526 | exp_str_add_u64 get exp_br_depth bidx |
| 3527 | t = exp_str_add_view exp_s_br_c |
| 3528 | if ne get exp_br_has_par bidx 0 |
| 3529 | for i ulong 8 |
| 3530 | b = ulong get exp_br_par_id add base i |
| 3531 | exp_str_put_b get exp_txt_hex shr b 4 |
| 3532 | exp_str_put_b get exp_txt_hex and b 15 |
| 3533 | end |
| 3534 | else |
| 3535 | t = exp_str_add_view exp_s_br_root |
| 3536 | end |
| 3537 | t = exp_str_add_view exp_s_br_d |
| 3538 | ref_total = ulong 0 |
| 3539 | tot = get exp_br_rcnt 0 |
| 3540 | for i ulong tot bound 64 |
| 3541 | if eq get exp_br_r_own i bidx |
| 3542 | ref_total = add ref_total 1 |
| 3543 | end |
| 3544 | end |
| 3545 | exp_str_add_u64 ref_total |
| 3546 | t = exp_str_add_view exp_s_br_e |
| 3547 | put exp_gn_stmt_off bnid st |
| 3548 | put exp_gn_stmt_len bnid sub exp_str_mark st |
| 3549 | t = exp_str_add_view exp_txt_br_sys |
| 3550 | put exp_gn_sys_off bnid t |
| 3551 | put exp_gn_sys_len bnid len exp_txt_br_sys |
| 3552 | t = exp_str_add_view exp_txt_br_art |
| 3553 | put exp_gn_art_off bnid t |
| 3554 | put exp_gn_art_len bnid len exp_txt_br_art |
| 3555 | t = exp_str_mark |
| 3556 | exp_str_add_hex16 mul bidx 16 2 |
| 3557 | put exp_gn_ref_off bnid t |
| 3558 | put exp_gn_ref_len bnid 32 |
| 3559 | put exp_gn_has_ts bnid 1 |
| 3560 | put exp_gn_ts bnid get exp_br_ts bidx |
| 3561 | exp_g_set_scope_branch bo 39 |
| 3562 | exp_br_emit_refs bidx bnid |
| 3563 | ret bnid |
| 3564 | end |
| 3565 | |
| 3566 | fn exp_br_build_graph bidx ulong max_d ulong max_n ulong to ulong |
| 3567 | roots = new ulong 64 0 |
| 3568 | rcnt = ulong 0 |
| 3569 | tot = get exp_br_rcnt 0 |
| 3570 | rid = new uchar 16 0 |
| 3571 | for i ulong tot bound 64 |
| 3572 | if eq get exp_br_r_own i bidx |
| 3573 | base = mul i 16 |
| 3574 | for k ulong 16 |
| 3575 | put rid k get exp_br_r_id add base k |
| 3576 | end |
| 3577 | ri = exp_cx_find rid |
| 3578 | if ne ri EXP_NONE |
| 3579 | put roots rcnt ri |
| 3580 | rcnt = add rcnt 1 |
| 3581 | end |
| 3582 | end |
| 3583 | end |
| 3584 | exp_cx_graph_from_roots part roots 0 rcnt max_d max_n |
| 3585 | exp_g_set_producer exp_txt_br_prod |
| 3586 | ret exp_br_emit_node bidx |
| 3587 | end |
| 3588 | |
| 3589 | fn exp_br_scope_expand u ulong to void |
| 3590 | ec = get exp_g_ecnt 0 |
| 3591 | for e ulong ec bound 1024 |
| 3592 | if eq get exp_ge_owner e u |
| 3593 | if exp_rel_is_support get exp_ge_rel e |
| 3594 | pi = exp_g_find_node get exp_ge_to_off e get exp_ge_to_len e |
| 3595 | if ne pi EXP_NONE |
| 3596 | if eq get exp_w_vis pi 0 |
| 3597 | qt = get exp_w_bfs_st 0 |
| 3598 | put exp_w_vis pi 1 |
| 3599 | put exp_w_queue qt pi |
| 3600 | put exp_w_bfs_st 0 add qt 1 |
| 3601 | end |
| 3602 | end |
| 3603 | end |
| 3604 | end |
| 3605 | end |
| 3606 | end |
| 3607 | |
| 3608 | fn exp_br_scope_bfs to void |
| 3609 | nc = get exp_g_ncnt 0 |
| 3610 | for i ulong nc bound 320 |
| 3611 | put exp_w_vis i 0 |
| 3612 | end |
| 3613 | put exp_w_bfs_st 0 0 |
| 3614 | sc = get exp_g_scnt 0 |
| 3615 | for i ulong sc bound 128 |
| 3616 | ni = exp_g_find_node get exp_gs_off i get exp_gs_len i |
| 3617 | if ne ni EXP_NONE |
| 3618 | if eq get exp_w_vis ni 0 |
| 3619 | qt = get exp_w_bfs_st 0 |
| 3620 | put exp_w_vis ni 1 |
| 3621 | put exp_w_queue qt ni |
| 3622 | put exp_w_bfs_st 0 add qt 1 |
| 3623 | end |
| 3624 | end |
| 3625 | end |
| 3626 | qh = ulong 0 |
| 3627 | while lt qh get exp_w_bfs_st 0 bound 320 |
| 3628 | u = get exp_w_queue qh |
| 3629 | qh = add qh 1 |
| 3630 | exp_br_scope_expand u |
| 3631 | end |
| 3632 | end |
| 3633 | |
| 3634 | fn exp_br_graph_for_record bidx ulong rec_id view uchar * max_d ulong max_n ulong to int |
| 3635 | bnid = exp_br_build_graph bidx max_d max_n |
| 3636 | wo = exp_cx_node_id_str rec_id |
| 3637 | wnid = exp_g_find_node wo 39 |
| 3638 | if eq wnid EXP_NONE |
| 3639 | ret EXP_ERR_NOT_IN_BRANCH_SCOPE |
| 3640 | end |
| 3641 | exp_br_scope_bfs |
| 3642 | if eq get exp_w_vis wnid 0 |
| 3643 | ret EXP_ERR_NOT_IN_BRANCH_SCOPE |
| 3644 | end |
| 3645 | ret EXP_OK |
| 3646 | end |
| 3647 | |
| 3648 | note ============================================================================ |
| 3649 | note Section 10: Deterministic Renderer & Analogy Catalog (render.rs) |
| 3650 | note ============================================================================ |
| 3651 | |
| 3652 | fn exp_out_status_words st ulong to void |
| 3653 | if eq st EXP_ST_DIRECT_OBS |
| 3654 | exp_out_view exp_sw_1 |
| 3655 | ret |
| 3656 | end |
| 3657 | if eq st EXP_ST_VERIFIED_FACT |
| 3658 | exp_out_view exp_sw_2 |
| 3659 | ret |
| 3660 | end |
| 3661 | if eq st EXP_ST_INFERENCE |
| 3662 | exp_out_view exp_sw_3 |
| 3663 | ret |
| 3664 | end |
| 3665 | if eq st EXP_ST_HYPOTHESIS |
| 3666 | exp_out_view exp_sw_4 |
| 3667 | ret |
| 3668 | end |
| 3669 | if eq st EXP_ST_CONTRADICTION |
| 3670 | exp_out_view exp_sw_5 |
| 3671 | ret |
| 3672 | end |
| 3673 | if eq st EXP_ST_OPERATOR_DECISION |
| 3674 | exp_out_view exp_sw_6 |
| 3675 | ret |
| 3676 | end |
| 3677 | exp_out_view exp_sw_0 |
| 3678 | end |
| 3679 | |
| 3680 | fn exp_out_status_name st ulong to void |
| 3681 | if eq st EXP_ST_DIRECT_OBS |
| 3682 | exp_out_view exp_sn_1 |
| 3683 | ret |
| 3684 | end |
| 3685 | if eq st EXP_ST_VERIFIED_FACT |
| 3686 | exp_out_view exp_sn_2 |
| 3687 | ret |
| 3688 | end |
| 3689 | if eq st EXP_ST_INFERENCE |
| 3690 | exp_out_view exp_sn_3 |
| 3691 | ret |
| 3692 | end |
| 3693 | if eq st EXP_ST_HYPOTHESIS |
| 3694 | exp_out_view exp_sn_4 |
| 3695 | ret |
| 3696 | end |
| 3697 | if eq st EXP_ST_CONTRADICTION |
| 3698 | exp_out_view exp_sn_5 |
| 3699 | ret |
| 3700 | end |
| 3701 | if eq st EXP_ST_OPERATOR_DECISION |
| 3702 | exp_out_view exp_sn_6 |
| 3703 | ret |
| 3704 | end |
| 3705 | exp_out_view exp_sn_0 |
| 3706 | end |
| 3707 | |
| 3708 | fn exp_out_kind_name kd ulong to void |
| 3709 | if eq kd 1 |
| 3710 | exp_out_view exp_kn_1 |
| 3711 | ret |
| 3712 | end |
| 3713 | if eq kd 2 |
| 3714 | exp_out_view exp_kn_2 |
| 3715 | ret |
| 3716 | end |
| 3717 | if eq kd 3 |
| 3718 | exp_out_view exp_kn_3 |
| 3719 | ret |
| 3720 | end |
| 3721 | if eq kd 4 |
| 3722 | exp_out_view exp_kn_4 |
| 3723 | ret |
| 3724 | end |
| 3725 | if eq kd 5 |
| 3726 | exp_out_view exp_kn_5 |
| 3727 | ret |
| 3728 | end |
| 3729 | if eq kd 6 |
| 3730 | exp_out_view exp_kn_6 |
| 3731 | ret |
| 3732 | end |
| 3733 | if eq kd 7 |
| 3734 | exp_out_view exp_kn_7 |
| 3735 | ret |
| 3736 | end |
| 3737 | if eq kd 8 |
| 3738 | exp_out_view exp_kn_8 |
| 3739 | ret |
| 3740 | end |
| 3741 | if eq kd 9 |
| 3742 | exp_out_view exp_kn_9 |
| 3743 | ret |
| 3744 | end |
| 3745 | if eq kd 10 |
| 3746 | exp_out_view exp_kn_10 |
| 3747 | ret |
| 3748 | end |
| 3749 | if eq kd 11 |
| 3750 | exp_out_view exp_kn_11 |
| 3751 | ret |
| 3752 | end |
| 3753 | if eq kd 12 |
| 3754 | exp_out_view exp_kn_12 |
| 3755 | ret |
| 3756 | end |
| 3757 | exp_out_view exp_kn_13 |
| 3758 | end |
| 3759 | |
| 3760 | fn exp_out_rel_name rl ulong to void |
| 3761 | if eq rl 1 |
| 3762 | exp_out_view exp_rn_1 |
| 3763 | ret |
| 3764 | end |
| 3765 | if eq rl 2 |
| 3766 | exp_out_view exp_rn_2 |
| 3767 | ret |
| 3768 | end |
| 3769 | if eq rl 3 |
| 3770 | exp_out_view exp_rn_3 |
| 3771 | ret |
| 3772 | end |
| 3773 | if eq rl 4 |
| 3774 | exp_out_view exp_rn_4 |
| 3775 | ret |
| 3776 | end |
| 3777 | if eq rl 5 |
| 3778 | exp_out_view exp_rn_5 |
| 3779 | ret |
| 3780 | end |
| 3781 | exp_out_view exp_rn_6 |
| 3782 | end |
| 3783 | |
| 3784 | fn exp_out_role_heading rl ulong to void |
| 3785 | if eq rl 0 |
| 3786 | exp_out_view exp_rh_0 |
| 3787 | ret |
| 3788 | end |
| 3789 | if eq rl 1 |
| 3790 | exp_out_view exp_rh_1 |
| 3791 | ret |
| 3792 | end |
| 3793 | if eq rl 2 |
| 3794 | exp_out_view exp_rh_2 |
| 3795 | ret |
| 3796 | end |
| 3797 | if eq rl 3 |
| 3798 | exp_out_view exp_rh_3 |
| 3799 | ret |
| 3800 | end |
| 3801 | if eq rl 4 |
| 3802 | exp_out_view exp_rh_4 |
| 3803 | ret |
| 3804 | end |
| 3805 | if eq rl 5 |
| 3806 | exp_out_view exp_rh_5 |
| 3807 | ret |
| 3808 | end |
| 3809 | if eq rl 6 |
| 3810 | exp_out_view exp_rh_6 |
| 3811 | ret |
| 3812 | end |
| 3813 | exp_out_view exp_rh_7 |
| 3814 | end |
| 3815 | |
| 3816 | fn exp_out_role_debug rl ulong to void |
| 3817 | if eq rl 0 |
| 3818 | exp_out_view exp_rd_0 |
| 3819 | ret |
| 3820 | end |
| 3821 | if eq rl 1 |
| 3822 | exp_out_view exp_rd_1 |
| 3823 | ret |
| 3824 | end |
| 3825 | if eq rl 2 |
| 3826 | exp_out_view exp_rd_2 |
| 3827 | ret |
| 3828 | end |
| 3829 | if eq rl 3 |
| 3830 | exp_out_view exp_rd_3 |
| 3831 | ret |
| 3832 | end |
| 3833 | if eq rl 4 |
| 3834 | exp_out_view exp_rd_4 |
| 3835 | ret |
| 3836 | end |
| 3837 | if eq rl 5 |
| 3838 | exp_out_view exp_rd_5 |
| 3839 | ret |
| 3840 | end |
| 3841 | if eq rl 6 |
| 3842 | exp_out_view exp_rd_6 |
| 3843 | ret |
| 3844 | end |
| 3845 | exp_out_view exp_rd_7 |
| 3846 | end |
| 3847 | |
| 3848 | fn exp_out_uk_label uk ulong to void |
| 3849 | if eq uk 1 |
| 3850 | exp_out_view exp_ul_1 |
| 3851 | ret |
| 3852 | end |
| 3853 | if eq uk 2 |
| 3854 | exp_out_view exp_ul_2 |
| 3855 | ret |
| 3856 | end |
| 3857 | if eq uk 3 |
| 3858 | exp_out_view exp_ul_3 |
| 3859 | ret |
| 3860 | end |
| 3861 | if eq uk 4 |
| 3862 | exp_out_view exp_ul_4 |
| 3863 | ret |
| 3864 | end |
| 3865 | if eq uk 5 |
| 3866 | exp_out_view exp_ul_5 |
| 3867 | ret |
| 3868 | end |
| 3869 | if eq uk 6 |
| 3870 | exp_out_view exp_ul_6 |
| 3871 | ret |
| 3872 | end |
| 3873 | if eq uk 7 |
| 3874 | exp_out_view exp_ul_7 |
| 3875 | ret |
| 3876 | end |
| 3877 | if eq uk 8 |
| 3878 | exp_out_view exp_ul_8 |
| 3879 | ret |
| 3880 | end |
| 3881 | if eq uk 9 |
| 3882 | exp_out_view exp_ul_9 |
| 3883 | ret |
| 3884 | end |
| 3885 | exp_out_view exp_ul_10 |
| 3886 | end |
| 3887 | |
| 3888 | fn exp_out_uk_name uk ulong to void |
| 3889 | if eq uk 1 |
| 3890 | exp_out_view exp_un_1 |
| 3891 | ret |
| 3892 | end |
| 3893 | if eq uk 2 |
| 3894 | exp_out_view exp_un_2 |
| 3895 | ret |
| 3896 | end |
| 3897 | if eq uk 3 |
| 3898 | exp_out_view exp_un_3 |
| 3899 | ret |
| 3900 | end |
| 3901 | if eq uk 4 |
| 3902 | exp_out_view exp_un_4 |
| 3903 | ret |
| 3904 | end |
| 3905 | if eq uk 5 |
| 3906 | exp_out_view exp_un_5 |
| 3907 | ret |
| 3908 | end |
| 3909 | if eq uk 6 |
| 3910 | exp_out_view exp_un_6 |
| 3911 | ret |
| 3912 | end |
| 3913 | if eq uk 7 |
| 3914 | exp_out_view exp_un_7 |
| 3915 | ret |
| 3916 | end |
| 3917 | if eq uk 8 |
| 3918 | exp_out_view exp_un_8 |
| 3919 | ret |
| 3920 | end |
| 3921 | if eq uk 9 |
| 3922 | exp_out_view exp_un_9 |
| 3923 | ret |
| 3924 | end |
| 3925 | exp_out_view exp_un_10 |
| 3926 | end |
| 3927 | |
| 3928 | fn exp_effective_level md ulong lv ulong to ulong |
| 3929 | eff = lv |
| 3930 | if eq eff EXP_LEVEL_DEFAULT |
| 3931 | if eq md EXP_MODE_SUMMARY |
| 3932 | eff = 1 |
| 3933 | else |
| 3934 | if eq md EXP_MODE_TECHNICAL |
| 3935 | eff = 4 |
| 3936 | else |
| 3937 | eff = 2 |
| 3938 | end |
| 3939 | end |
| 3940 | end |
| 3941 | if gt eff EXP_MAX_LEVEL |
| 3942 | eff = EXP_MAX_LEVEL |
| 3943 | end |
| 3944 | ret eff |
| 3945 | end |
| 3946 | |
| 3947 | fn exp_aud_shows_ids aud ulong to bool |
| 3948 | ret ne aud EXP_AUD_GENERAL |
| 3949 | end |
| 3950 | |
| 3951 | fn exp_out_short_id off ulong ln ulong to void |
| 3952 | colon = EXP_NONE |
| 3953 | for i ulong ln bound 256 |
| 3954 | if eq get exp_str add off i 58 |
| 3955 | colon = i |
| 3956 | break |
| 3957 | end |
| 3958 | end |
| 3959 | if ne colon EXP_NONE |
| 3960 | rest = sub ln add colon 1 |
| 3961 | if gt rest 12 |
| 3962 | exp_out_arena off add colon 13 |
| 3963 | ret |
| 3964 | end |
| 3965 | end |
| 3966 | exp_out_arena off ln |
| 3967 | end |
| 3968 | |
| 3969 | fn exp_out_header aud ulong to void |
| 3970 | ci = sub get exp_b_concl_step 0 1 |
| 3971 | nid = get exp_bs_nid ci |
| 3972 | exp_out_view exp_s_concl |
| 3973 | exp_out_arena get exp_gn_stmt_off nid get exp_gn_stmt_len nid |
| 3974 | exp_out_b uchar 32 |
| 3975 | exp_out_b uchar 91 |
| 3976 | exp_out_status_words get exp_gn_status nid |
| 3977 | exp_out_b uchar 93 |
| 3978 | if exp_aud_shows_ids aud |
| 3979 | exp_out_b uchar 32 |
| 3980 | exp_out_b uchar 40 |
| 3981 | exp_out_short_id get exp_gn_id_off nid get exp_gn_id_len nid |
| 3982 | exp_out_b uchar 41 |
| 3983 | end |
| 3984 | exp_out_b uchar 10 |
| 3985 | end |
| 3986 | |
| 3987 | fn exp_out_meas_line nid ulong indent ulong to bool |
| 3988 | mc = get exp_g_mcnt 0 |
| 3989 | found = ulong 0 |
| 3990 | for m ulong mc bound 256 |
| 3991 | if eq get exp_gm_owner m nid |
| 3992 | if eq found 0 |
| 3993 | if ne indent 0 |
| 3994 | exp_out_view exp_s_meas |
| 3995 | else |
| 3996 | exp_out_view exp_s_meas_ni |
| 3997 | end |
| 3998 | else |
| 3999 | exp_out_b uchar 44 |
| 4000 | exp_out_b uchar 32 |
| 4001 | end |
| 4002 | exp_out_arena get exp_gm_name_off m get exp_gm_name_len m |
| 4003 | exp_out_b uchar 61 |
| 4004 | exp_out_arena get exp_gm_val_off m get exp_gm_val_len m |
| 4005 | if gt get exp_gm_unit_len m 0 |
| 4006 | exp_out_b uchar 32 |
| 4007 | exp_out_arena get exp_gm_unit_off m get exp_gm_unit_len m |
| 4008 | end |
| 4009 | found = add found 1 |
| 4010 | end |
| 4011 | end |
| 4012 | if gt found 0 |
| 4013 | exp_out_b uchar 10 |
| 4014 | ret true |
| 4015 | end |
| 4016 | ret false |
| 4017 | end |
| 4018 | |
| 4019 | fn exp_out_step_line si ulong lv ulong aud ulong to void |
| 4020 | nid = get exp_bs_nid si |
| 4021 | exp_out_u64 add si 1 |
| 4022 | exp_out_b uchar 46 |
| 4023 | exp_out_b uchar 32 |
| 4024 | exp_out_b uchar 91 |
| 4025 | exp_out_status_words get exp_gn_status nid |
| 4026 | exp_out_b uchar 93 |
| 4027 | exp_out_b uchar 32 |
| 4028 | exp_out_arena get exp_gn_stmt_off nid get exp_gn_stmt_len nid |
| 4029 | if ge lv 2 |
| 4030 | soff = get exp_bs_sup_off si |
| 4031 | scnt = get exp_bs_sup_cnt si |
| 4032 | sc = ulong 0 |
| 4033 | for k ulong scnt bound 1024 |
| 4034 | if ne get exp_bs_sup_rel add soff k EXP_REL_REFERENCES |
| 4035 | if eq sc 0 |
| 4036 | exp_out_view exp_s_rests |
| 4037 | else |
| 4038 | exp_out_b uchar 44 |
| 4039 | exp_out_b uchar 32 |
| 4040 | end |
| 4041 | exp_out_u64 get exp_bs_sup_step add soff k |
| 4042 | sc = add sc 1 |
| 4043 | end |
| 4044 | end |
| 4045 | if gt sc 0 |
| 4046 | exp_out_b uchar 41 |
| 4047 | end |
| 4048 | wc = ulong 0 |
| 4049 | for k ulong scnt bound 1024 |
| 4050 | if eq get exp_bs_sup_rel add soff k EXP_REL_REFERENCES |
| 4051 | if eq wc 0 |
| 4052 | exp_out_view exp_s_refers |
| 4053 | else |
| 4054 | exp_out_b uchar 44 |
| 4055 | exp_out_b uchar 32 |
| 4056 | end |
| 4057 | exp_out_u64 get exp_bs_sup_step add soff k |
| 4058 | wc = add wc 1 |
| 4059 | end |
| 4060 | end |
| 4061 | if gt wc 0 |
| 4062 | exp_out_view exp_s_weak |
| 4063 | end |
| 4064 | end |
| 4065 | if exp_aud_shows_ids aud |
| 4066 | exp_out_b uchar 32 |
| 4067 | exp_out_b uchar 91 |
| 4068 | exp_out_arena get exp_gn_sys_off nid get exp_gn_sys_len nid |
| 4069 | exp_out_b uchar 32 |
| 4070 | exp_out_short_id get exp_gn_id_off nid get exp_gn_id_len nid |
| 4071 | exp_out_b uchar 93 |
| 4072 | else |
| 4073 | exp_out_view exp_s_from |
| 4074 | exp_out_arena get exp_gn_sys_off nid get exp_gn_sys_len nid |
| 4075 | exp_out_b uchar 93 |
| 4076 | end |
| 4077 | if ne get exp_bs_bref si 0 |
| 4078 | exp_out_view exp_s_br_ref |
| 4079 | end |
| 4080 | exp_out_b uchar 10 |
| 4081 | end |
| 4082 | |
| 4083 | fn exp_reason_better si ulong best ulong to bool |
| 4084 | bg = get exp_bs_gnd best |
| 4085 | sg = get exp_bs_gnd si |
| 4086 | if gt sg bg |
| 4087 | ret true |
| 4088 | end |
| 4089 | if lt sg bg |
| 4090 | ret false |
| 4091 | end |
| 4092 | bnid = get exp_bs_nid best |
| 4093 | snid = get exp_bs_nid si |
| 4094 | bnh = ulong 1 |
| 4095 | if eq get exp_gn_status bnid EXP_ST_HYPOTHESIS |
| 4096 | bnh = 0 |
| 4097 | end |
| 4098 | snh = ulong 1 |
| 4099 | if eq get exp_gn_status snid EXP_ST_HYPOTHESIS |
| 4100 | snh = 0 |
| 4101 | end |
| 4102 | if gt snh bnh |
| 4103 | ret true |
| 4104 | end |
| 4105 | if lt snh bnh |
| 4106 | ret false |
| 4107 | end |
| 4108 | bc = ulong 0 |
| 4109 | if ne get exp_gn_has_conf bnid 0 |
| 4110 | bc = get exp_gn_conf bnid |
| 4111 | end |
| 4112 | sc = ulong 0 |
| 4113 | if ne get exp_gn_has_conf snid 0 |
| 4114 | sc = get exp_gn_conf snid |
| 4115 | end |
| 4116 | if gt sc bc |
| 4117 | ret true |
| 4118 | end |
| 4119 | if lt sc bc |
| 4120 | ret false |
| 4121 | end |
| 4122 | ret gt si best |
| 4123 | end |
| 4124 | |
| 4125 | fn exp_strongest_reason to ulong |
| 4126 | ci = sub get exp_b_concl_step 0 1 |
| 4127 | soff = get exp_bs_sup_off ci |
| 4128 | scnt = get exp_bs_sup_cnt ci |
| 4129 | best = EXP_NONE |
| 4130 | for k ulong scnt bound 1024 |
| 4131 | if ne get exp_bs_sup_rel add soff k EXP_REL_REFERENCES |
| 4132 | si = sub get exp_bs_sup_step add soff k 1 |
| 4133 | if eq best EXP_NONE |
| 4134 | best = si |
| 4135 | else |
| 4136 | if exp_reason_better si best |
| 4137 | best = si |
| 4138 | end |
| 4139 | end |
| 4140 | end |
| 4141 | end |
| 4142 | ret best |
| 4143 | end |
| 4144 | |
| 4145 | fn exp_strongest_evidence to ulong |
| 4146 | evc = get exp_be_cnt 0 |
| 4147 | best = EXP_NONE |
| 4148 | for i ulong evc bound 320 |
| 4149 | si = sub get exp_be_step i 1 |
| 4150 | if ne get exp_bs_gnd si 0 |
| 4151 | if eq best EXP_NONE |
| 4152 | best = si |
| 4153 | else |
| 4154 | db = get exp_bs_depth best |
| 4155 | ds = get exp_bs_depth si |
| 4156 | if lt ds db |
| 4157 | best = si |
| 4158 | else |
| 4159 | if eq ds db |
| 4160 | if lt si best |
| 4161 | best = si |
| 4162 | end |
| 4163 | end |
| 4164 | end |
| 4165 | end |
| 4166 | end |
| 4167 | end |
| 4168 | ret best |
| 4169 | end |
| 4170 | |
| 4171 | fn exp_material_unc to ulong |
| 4172 | uc = get exp_bu_cnt 0 |
| 4173 | best = EXP_NONE |
| 4174 | for i ulong uc bound 512 |
| 4175 | if eq best EXP_NONE |
| 4176 | best = i |
| 4177 | else |
| 4178 | sb = exp_uk_severity get exp_bu_kind best |
| 4179 | si = exp_uk_severity get exp_bu_kind i |
| 4180 | if lt si sb |
| 4181 | best = i |
| 4182 | else |
| 4183 | if eq si sb |
| 4184 | c1 = exp_str_cmp get exp_bu_nid_off i get exp_bu_nid_len i get exp_bu_nid_off best get exp_bu_nid_len best |
| 4185 | if lt c1 0 |
| 4186 | best = i |
| 4187 | else |
| 4188 | if eq c1 0 |
| 4189 | if lt exp_str_cmp get exp_bu_det_off i get exp_bu_det_len i get exp_bu_det_off best get exp_bu_det_len best 0 |
| 4190 | best = i |
| 4191 | end |
| 4192 | end |
| 4193 | end |
| 4194 | end |
| 4195 | end |
| 4196 | end |
| 4197 | end |
| 4198 | ret best |
| 4199 | end |
| 4200 | |
| 4201 | fn exp_render_step_body si ulong lv ulong aud ulong to void |
| 4202 | nid = get exp_bs_nid si |
| 4203 | if eq add si 1 get exp_b_concl_step 0 |
| 4204 | exp_out_u64 add si 1 |
| 4205 | exp_out_view exp_s_therefore |
| 4206 | exp_out_arena get exp_gn_stmt_off nid get exp_gn_stmt_len nid |
| 4207 | exp_out_b uchar 32 |
| 4208 | exp_out_b uchar 91 |
| 4209 | exp_out_status_words get exp_gn_status nid |
| 4210 | exp_out_b uchar 93 |
| 4211 | exp_out_b uchar 10 |
| 4212 | else |
| 4213 | exp_out_step_line si lv aud |
| 4214 | end |
| 4215 | if ge lv 3 |
| 4216 | dm = exp_out_meas_line nid 1 |
| 4217 | end |
| 4218 | if ge lv 4 |
| 4219 | exp_out_view exp_s_src |
| 4220 | exp_out_arena get exp_gn_sys_off nid get exp_gn_sys_len nid |
| 4221 | exp_out_b uchar 32 |
| 4222 | exp_out_arena get exp_gn_art_off nid get exp_gn_art_len nid |
| 4223 | exp_out_b uchar 32 |
| 4224 | exp_out_arena get exp_gn_ref_off nid get exp_gn_ref_len nid |
| 4225 | if ne get exp_gn_has_dig nid 0 |
| 4226 | exp_out_view exp_s_dig |
| 4227 | exp_out_arena get exp_gn_dig_off nid get exp_gn_dig_len nid |
| 4228 | end |
| 4229 | exp_out_b uchar 10 |
| 4230 | end |
| 4231 | end |
| 4232 | |
| 4233 | fn exp_render_steps_footer lv ulong to void |
| 4234 | exp_out_view exp_s_unc_hdr |
| 4235 | uc = get exp_bu_cnt 0 |
| 4236 | shown_u = ulong 0 |
| 4237 | for i ulong uc bound 512 |
| 4238 | uk = get exp_bu_kind i |
| 4239 | show = true |
| 4240 | if lt lv 3 |
| 4241 | if eq uk EXP_UK_INFERRED |
| 4242 | show = false |
| 4243 | end |
| 4244 | end |
| 4245 | if show |
| 4246 | exp_out_b uchar 45 |
| 4247 | exp_out_b uchar 32 |
| 4248 | exp_out_uk_label uk |
| 4249 | exp_out_b uchar 58 |
| 4250 | exp_out_b uchar 32 |
| 4251 | exp_out_arena get exp_bu_det_off i get exp_bu_det_len i |
| 4252 | exp_out_b uchar 10 |
| 4253 | shown_u = add shown_u 1 |
| 4254 | end |
| 4255 | end |
| 4256 | if eq shown_u 0 |
| 4257 | exp_out_view exp_s_no_gaps |
| 4258 | end |
| 4259 | cc = get exp_bc_cnt 0 |
| 4260 | if gt cc 0 |
| 4261 | exp_out_view exp_s_conf_hdr |
| 4262 | for i ulong cc bound 128 |
| 4263 | exp_out_b uchar 45 |
| 4264 | exp_out_b uchar 32 |
| 4265 | exp_out_arena get exp_bc_stmt_off i get exp_bc_stmt_len i |
| 4266 | exp_out_view exp_s_inv_a |
| 4267 | exp_out_u64 get exp_bc_inv_cnt i |
| 4268 | exp_out_view exp_s_inv_b |
| 4269 | end |
| 4270 | end |
| 4271 | if ge lv 2 |
| 4272 | ac = get exp_ba_cnt 0 |
| 4273 | if gt ac 0 |
| 4274 | exp_out_view exp_s_alt_hdr |
| 4275 | for i ulong ac bound 128 |
| 4276 | anid = get exp_ba_nid i |
| 4277 | exp_out_b uchar 45 |
| 4278 | exp_out_b uchar 32 |
| 4279 | exp_out_arena get exp_gn_stmt_off anid get exp_gn_stmt_len anid |
| 4280 | exp_out_b uchar 10 |
| 4281 | end |
| 4282 | end |
| 4283 | end |
| 4284 | end |
| 4285 | |
| 4286 | fn exp_render_steps lv ulong aud ulong to void |
| 4287 | if eq lv 0 |
| 4288 | exp_out_header aud |
| 4289 | ret |
| 4290 | end |
| 4291 | last_role = EXP_NONE |
| 4292 | if eq lv 1 |
| 4293 | ci = sub get exp_b_concl_step 0 1 |
| 4294 | soff = get exp_bs_sup_off ci |
| 4295 | scnt = get exp_bs_sup_cnt ci |
| 4296 | for k ulong scnt bound 1024 |
| 4297 | si = sub get exp_bs_sup_step add soff k 1 |
| 4298 | rl = get exp_bs_role si |
| 4299 | if ne rl last_role |
| 4300 | exp_out_role_heading rl |
| 4301 | exp_out_b uchar 58 |
| 4302 | exp_out_b uchar 10 |
| 4303 | last_role = rl |
| 4304 | end |
| 4305 | exp_render_step_body si lv aud |
| 4306 | end |
| 4307 | rl = get exp_bs_role ci |
| 4308 | if ne rl last_role |
| 4309 | exp_out_role_heading rl |
| 4310 | exp_out_b uchar 58 |
| 4311 | exp_out_b uchar 10 |
| 4312 | end |
| 4313 | exp_render_step_body ci lv aud |
| 4314 | else |
| 4315 | sc = get exp_b_scnt 0 |
| 4316 | for si ulong sc bound 320 |
| 4317 | rl = get exp_bs_role si |
| 4318 | if ne rl last_role |
| 4319 | exp_out_role_heading rl |
| 4320 | exp_out_b uchar 58 |
| 4321 | exp_out_b uchar 10 |
| 4322 | last_role = rl |
| 4323 | end |
| 4324 | exp_render_step_body si lv aud |
| 4325 | end |
| 4326 | end |
| 4327 | exp_render_steps_footer lv |
| 4328 | end |
| 4329 | |
| 4330 | fn exp_render_summary lv ulong aud ulong to void |
| 4331 | exp_out_header aud |
| 4332 | if eq lv 0 |
| 4333 | ret |
| 4334 | end |
| 4335 | sr = exp_strongest_reason |
| 4336 | if ne sr EXP_NONE |
| 4337 | rnid = get exp_bs_nid sr |
| 4338 | exp_out_view exp_s_mreason |
| 4339 | exp_out_arena get exp_gn_stmt_off rnid get exp_gn_stmt_len rnid |
| 4340 | exp_out_b uchar 32 |
| 4341 | exp_out_b uchar 91 |
| 4342 | exp_out_status_words get exp_gn_status rnid |
| 4343 | exp_out_b uchar 93 |
| 4344 | exp_out_b uchar 10 |
| 4345 | else |
| 4346 | exp_out_view exp_s_no_reason |
| 4347 | end |
| 4348 | se = exp_strongest_evidence |
| 4349 | if ne se EXP_NONE |
| 4350 | enid = get exp_bs_nid se |
| 4351 | exp_out_view exp_s_sev |
| 4352 | exp_out_arena get exp_gn_stmt_off enid get exp_gn_stmt_len enid |
| 4353 | exp_out_b uchar 32 |
| 4354 | exp_out_b uchar 91 |
| 4355 | exp_out_status_words get exp_gn_status enid |
| 4356 | exp_out_b uchar 93 |
| 4357 | exp_out_b uchar 10 |
| 4358 | dm = exp_out_meas_line enid 0 |
| 4359 | else |
| 4360 | exp_out_view exp_s_no_ev |
| 4361 | end |
| 4362 | mu = exp_material_unc |
| 4363 | if ne mu EXP_NONE |
| 4364 | exp_out_view exp_s_munc |
| 4365 | exp_out_arena get exp_bu_det_off mu get exp_bu_det_len mu |
| 4366 | exp_out_b uchar 10 |
| 4367 | else |
| 4368 | exp_out_view exp_s_no_munc |
| 4369 | end |
| 4370 | if ge lv 2 |
| 4371 | exp_out_b uchar 10 |
| 4372 | exp_render_steps lv aud |
| 4373 | end |
| 4374 | end |
| 4375 | |
| 4376 | fn exp_render_tech_step si ulong lv ulong to void |
| 4377 | nid = get exp_bs_nid si |
| 4378 | exp_out_view exp_s_t_step |
| 4379 | exp_out_u64 add si 1 |
| 4380 | exp_out_view exp_s_t_id |
| 4381 | exp_out_arena get exp_gn_id_off nid get exp_gn_id_len nid |
| 4382 | exp_out_view exp_s_t_kind |
| 4383 | exp_out_kind_name get exp_gn_kind nid |
| 4384 | exp_out_view exp_s_t_status |
| 4385 | exp_out_status_name get exp_gn_status nid |
| 4386 | exp_out_view exp_s_t_role |
| 4387 | exp_out_role_debug get exp_bs_role si |
| 4388 | exp_out_view exp_s_t_depth |
| 4389 | exp_out_u64 get exp_bs_depth si |
| 4390 | exp_out_view exp_s_t_gnd |
| 4391 | if ne get exp_bs_gnd si 0 |
| 4392 | exp_out_view exp_s_true |
| 4393 | else |
| 4394 | exp_out_view exp_s_false |
| 4395 | end |
| 4396 | exp_out_view exp_s_t_sup |
| 4397 | soff = get exp_bs_sup_off si |
| 4398 | scnt = get exp_bs_sup_cnt si |
| 4399 | for k ulong scnt bound 1024 |
| 4400 | if gt k 0 |
| 4401 | exp_out_b uchar 44 |
| 4402 | end |
| 4403 | exp_out_rel_name get exp_bs_sup_rel add soff k |
| 4404 | exp_out_b uchar 58 |
| 4405 | exp_out_u64 get exp_bs_sup_step add soff k |
| 4406 | end |
| 4407 | exp_out_b uchar 93 |
| 4408 | exp_out_b uchar 10 |
| 4409 | exp_out_view exp_s_t_stmt |
| 4410 | exp_out_arena get exp_gn_stmt_off nid get exp_gn_stmt_len nid |
| 4411 | exp_out_b uchar 10 |
| 4412 | if ne get exp_gn_has_conf nid 0 |
| 4413 | exp_out_view exp_s_t_conf |
| 4414 | exp_out_u64 get exp_gn_conf nid |
| 4415 | exp_out_view exp_s_t_basis |
| 4416 | if ne get exp_gn_has_cbas nid 0 |
| 4417 | exp_out_arena get exp_gn_cbas_off nid get exp_gn_cbas_len nid |
| 4418 | else |
| 4419 | exp_out_view exp_s_t_unspec |
| 4420 | end |
| 4421 | exp_out_b uchar 10 |
| 4422 | end |
| 4423 | if ge lv 3 |
| 4424 | dm = exp_out_meas_line nid 1 |
| 4425 | end |
| 4426 | if ge lv 4 |
| 4427 | exp_out_view exp_s_t_src |
| 4428 | exp_out_arena get exp_gn_sys_off nid get exp_gn_sys_len nid |
| 4429 | exp_out_view exp_s_t_art |
| 4430 | exp_out_arena get exp_gn_art_off nid get exp_gn_art_len nid |
| 4431 | exp_out_view exp_s_t_ref |
| 4432 | exp_out_arena get exp_gn_ref_off nid get exp_gn_ref_len nid |
| 4433 | exp_out_view exp_s_t_dig |
| 4434 | if ne get exp_gn_has_dig nid 0 |
| 4435 | exp_out_arena get exp_gn_dig_off nid get exp_gn_dig_len nid |
| 4436 | else |
| 4437 | exp_out_b uchar 45 |
| 4438 | end |
| 4439 | exp_out_b uchar 10 |
| 4440 | if ne get exp_gn_has_ts nid 0 |
| 4441 | exp_out_view exp_s_t_rec |
| 4442 | exp_out_u64 get exp_gn_ts nid |
| 4443 | exp_out_view exp_s_t_rec_n |
| 4444 | end |
| 4445 | end |
| 4446 | noc = get exp_g_notecnt 0 |
| 4447 | for o ulong noc bound 128 |
| 4448 | if eq get exp_go_owner o nid |
| 4449 | exp_out_view exp_s_t_note |
| 4450 | exp_out_arena get exp_go_off o get exp_go_len o |
| 4451 | exp_out_b uchar 10 |
| 4452 | end |
| 4453 | end |
| 4454 | end |
| 4455 | |
| 4456 | fn exp_render_tech_contra i ulong to void |
| 4457 | nid = get exp_bc_nid i |
| 4458 | exp_out_view exp_s_t_contra |
| 4459 | exp_out_arena get exp_gn_id_off nid get exp_gn_id_len nid |
| 4460 | exp_out_view exp_s_t_invs |
| 4461 | io = get exp_bc_inv_off i |
| 4462 | ic = get exp_bc_inv_cnt i |
| 4463 | for j ulong ic bound 128 |
| 4464 | if gt j 0 |
| 4465 | exp_out_b uchar 44 |
| 4466 | end |
| 4467 | exp_out_arena get exp_bc_inv_id_off add io j get exp_bc_inv_id_len add io j |
| 4468 | end |
| 4469 | exp_out_view exp_s_t_stmteq |
| 4470 | exp_out_arena get exp_bc_stmt_off i get exp_bc_stmt_len i |
| 4471 | exp_out_b uchar 10 |
| 4472 | end |
| 4473 | |
| 4474 | fn exp_render_tech_footer lv ulong to void |
| 4475 | uc = get exp_bu_cnt 0 |
| 4476 | for i ulong uc bound 512 |
| 4477 | uk = get exp_bu_kind i |
| 4478 | exp_out_view exp_s_t_unc |
| 4479 | exp_out_u64 uk |
| 4480 | exp_out_view exp_s_t_kind |
| 4481 | exp_out_uk_name uk |
| 4482 | exp_out_view exp_s_t_node |
| 4483 | exp_out_arena get exp_bu_nid_off i get exp_bu_nid_len i |
| 4484 | exp_out_view exp_s_t_rel |
| 4485 | if ne get exp_bu_has_rel i 0 |
| 4486 | exp_out_arena get exp_bu_rel_off i get exp_bu_rel_len i |
| 4487 | else |
| 4488 | exp_out_b uchar 45 |
| 4489 | end |
| 4490 | exp_out_view exp_s_t_det |
| 4491 | exp_out_arena get exp_bu_det_off i get exp_bu_det_len i |
| 4492 | exp_out_b uchar 10 |
| 4493 | end |
| 4494 | cc = get exp_bc_cnt 0 |
| 4495 | for i ulong cc bound 128 |
| 4496 | exp_render_tech_contra i |
| 4497 | end |
| 4498 | ac = get exp_ba_cnt 0 |
| 4499 | for i ulong ac bound 128 |
| 4500 | nid = get exp_ba_nid i |
| 4501 | exp_out_view exp_s_t_alt |
| 4502 | exp_out_arena get exp_gn_id_off nid get exp_gn_id_len nid |
| 4503 | exp_out_view exp_s_t_to |
| 4504 | exp_out_arena get exp_ba_to_off i get exp_ba_to_len i |
| 4505 | exp_out_view exp_s_t_stmts |
| 4506 | exp_out_arena get exp_gn_stmt_off nid get exp_gn_stmt_len nid |
| 4507 | exp_out_b uchar 10 |
| 4508 | end |
| 4509 | if ge lv 4 |
| 4510 | exp_out_view exp_s_t_idig |
| 4511 | exp_out_dig32 0 |
| 4512 | exp_out_view exp_s_t_bdig |
| 4513 | exp_out_dig32 1 |
| 4514 | exp_out_view exp_s_t_vis |
| 4515 | exp_out_u64 get exp_bp_vis 0 |
| 4516 | exp_out_b uchar 47 |
| 4517 | exp_out_u64 get exp_bp_gnodes 0 |
| 4518 | exp_out_view exp_s_t_lim |
| 4519 | exp_out_u64 get exp_bp_max_d 0 |
| 4520 | exp_out_view exp_s_t_nod |
| 4521 | exp_out_u64 get exp_bp_max_n 0 |
| 4522 | exp_out_b uchar 10 |
| 4523 | end |
| 4524 | end |
| 4525 | |
| 4526 | fn exp_render_technical lv ulong to void |
| 4527 | ci = sub get exp_b_concl_step 0 1 |
| 4528 | cnid = get exp_bs_nid ci |
| 4529 | exp_out_view exp_s_t_expl |
| 4530 | exp_out_arena get exp_bp_tgt_off 0 get exp_bp_tgt_len 0 |
| 4531 | exp_out_view exp_s_t_prod |
| 4532 | exp_out_arena get exp_g_prod_off 0 get exp_g_prod_len 0 |
| 4533 | exp_out_view exp_s_t_bldr |
| 4534 | exp_out_view exp_txt_builder |
| 4535 | exp_out_view exp_s_t_cont |
| 4536 | exp_out_arena get exp_g_fmt_off 0 get exp_g_fmt_len 0 |
| 4537 | exp_out_b uchar 32 |
| 4538 | exp_out_b uchar 118 |
| 4539 | exp_out_u64 get exp_g_ver 0 |
| 4540 | exp_out_b uchar 10 |
| 4541 | exp_out_view exp_s_t_concl |
| 4542 | exp_out_u64 add ci 1 |
| 4543 | exp_out_view exp_s_t_id |
| 4544 | exp_out_arena get exp_gn_id_off cnid get exp_gn_id_len cnid |
| 4545 | exp_out_view exp_s_t_status |
| 4546 | exp_out_status_name get exp_gn_status cnid |
| 4547 | exp_out_view exp_s_t_kind |
| 4548 | exp_out_kind_name get exp_gn_kind cnid |
| 4549 | exp_out_b uchar 10 |
| 4550 | if eq lv 0 |
| 4551 | ret |
| 4552 | end |
| 4553 | sc = get exp_b_scnt 0 |
| 4554 | for si ulong sc bound 320 |
| 4555 | exp_render_tech_step si lv |
| 4556 | end |
| 4557 | exp_render_tech_footer lv |
| 4558 | end |
| 4559 | |
| 4560 | fn exp_analogy_kind kd ulong st ulong to ulong |
| 4561 | if eq kd EXP_KD_CAP_ANALYSIS |
| 4562 | ret 1 |
| 4563 | end |
| 4564 | if eq kd EXP_KD_HYPOTHESIS |
| 4565 | ret 2 |
| 4566 | end |
| 4567 | if eq kd EXP_KD_CORTEX_RECORD |
| 4568 | if eq st EXP_ST_HYPOTHESIS |
| 4569 | ret 2 |
| 4570 | end |
| 4571 | end |
| 4572 | if eq kd EXP_KD_FALSIFICATION |
| 4573 | ret 3 |
| 4574 | end |
| 4575 | if eq kd EXP_KD_EVAL_RECEIPT |
| 4576 | ret 4 |
| 4577 | end |
| 4578 | if eq kd EXP_KD_AGENT_BRANCH |
| 4579 | ret 5 |
| 4580 | end |
| 4581 | if eq st EXP_ST_CONTRADICTION |
| 4582 | ret 6 |
| 4583 | end |
| 4584 | ret 0 |
| 4585 | end |
| 4586 | |
| 4587 | fn exp_out_analogy_text ak ulong to void |
| 4588 | if eq ak 1 |
| 4589 | exp_out_view exp_ana_cap |
| 4590 | ret |
| 4591 | end |
| 4592 | if eq ak 2 |
| 4593 | exp_out_view exp_ana_hyp |
| 4594 | ret |
| 4595 | end |
| 4596 | if eq ak 3 |
| 4597 | exp_out_view exp_ana_fal |
| 4598 | ret |
| 4599 | end |
| 4600 | if eq ak 4 |
| 4601 | exp_out_view exp_ana_evl |
| 4602 | ret |
| 4603 | end |
| 4604 | if eq ak 5 |
| 4605 | exp_out_view exp_ana_brn |
| 4606 | ret |
| 4607 | end |
| 4608 | exp_out_view exp_ana_con |
| 4609 | end |
| 4610 | |
| 4611 | fn exp_render_analogy lv ulong aud ulong to void |
| 4612 | sc = get exp_b_scnt 0 |
| 4613 | pick_step = ulong 0 |
| 4614 | pick_ak = ulong 0 |
| 4615 | for k ulong sc bound 320 |
| 4616 | si = sub sub sc 1 k |
| 4617 | nid = get exp_bs_nid si |
| 4618 | ak = exp_analogy_kind get exp_gn_kind nid get exp_gn_status nid |
| 4619 | if ne ak 0 |
| 4620 | pick_step = add si 1 |
| 4621 | pick_ak = ak |
| 4622 | break |
| 4623 | end |
| 4624 | end |
| 4625 | if ne pick_ak 0 |
| 4626 | exp_out_view exp_s_ana_hdr |
| 4627 | exp_out_u64 pick_step |
| 4628 | exp_out_b uchar 41 |
| 4629 | exp_out_b uchar 58 |
| 4630 | exp_out_b uchar 10 |
| 4631 | exp_out_b uchar 32 |
| 4632 | exp_out_b uchar 32 |
| 4633 | exp_out_analogy_text pick_ak |
| 4634 | exp_out_b uchar 10 |
| 4635 | else |
| 4636 | exp_out_view exp_s_ana_none |
| 4637 | end |
| 4638 | exp_out_view exp_s_ev_itself |
| 4639 | rlv = lv |
| 4640 | if lt rlv 1 |
| 4641 | rlv = 1 |
| 4642 | end |
| 4643 | exp_render_steps rlv aud |
| 4644 | end |
| 4645 | |
| 4646 | fn exp_render md ulong lv ulong aud ulong to ulong |
| 4647 | exp_out_reset |
| 4648 | eff = exp_effective_level md lv |
| 4649 | if eq md EXP_MODE_SUMMARY |
| 4650 | exp_render_summary eff aud |
| 4651 | else |
| 4652 | if eq md EXP_MODE_STEP_BY_STEP |
| 4653 | exp_out_header aud |
| 4654 | if gt eff 0 |
| 4655 | exp_out_b uchar 10 |
| 4656 | exp_render_steps eff aud |
| 4657 | end |
| 4658 | else |
| 4659 | if eq md EXP_MODE_TECHNICAL |
| 4660 | exp_render_technical eff |
| 4661 | else |
| 4662 | exp_render_analogy eff aud |
| 4663 | end |
| 4664 | end |
| 4665 | end |
| 4666 | ret get exp_out_len 0 |
| 4667 | end |
| 4668 | |
| 4669 | note ============================================================================ |
| 4670 | note Section 11: Clarification Session & CLI Helpers (session.rs, main.rs) |
| 4671 | note ============================================================================ |
| 4672 | |
| 4673 | fn exp_sess_init aud ulong to void |
| 4674 | put exp_sess 0 0 |
| 4675 | put exp_sess 1 aud |
| 4676 | end |
| 4677 | |
| 4678 | fn exp_sess_level to ulong |
| 4679 | ret get exp_sess 0 |
| 4680 | end |
| 4681 | |
| 4682 | fn exp_sess_explain to ulong |
| 4683 | put exp_sess 0 0 |
| 4684 | ret exp_render EXP_MODE_STEP_BY_STEP 0 get exp_sess 1 |
| 4685 | end |
| 4686 | |
| 4687 | fn exp_sess_deeper to ulong |
| 4688 | lv = add get exp_sess 0 1 |
| 4689 | if gt lv EXP_MAX_LEVEL |
| 4690 | lv = EXP_MAX_LEVEL |
| 4691 | end |
| 4692 | put exp_sess 0 lv |
| 4693 | ret exp_render EXP_MODE_STEP_BY_STEP lv get exp_sess 1 |
| 4694 | end |
| 4695 | |
| 4696 | fn exp_sess_shallower to ulong |
| 4697 | lv = get exp_sess 0 |
| 4698 | if gt lv 0 |
| 4699 | lv = sub lv 1 |
| 4700 | end |
| 4701 | put exp_sess 0 lv |
| 4702 | ret exp_render EXP_MODE_STEP_BY_STEP lv get exp_sess 1 |
| 4703 | end |
| 4704 | |
| 4705 | fn exp_sess_step_deps si ulong to void |
| 4706 | soff = get exp_bs_sup_off si |
| 4707 | scnt = get exp_bs_sup_cnt si |
| 4708 | if eq scnt 0 |
| 4709 | if ne get exp_bs_gnd si 0 |
| 4710 | exp_out_view exp_s_start_pt |
| 4711 | else |
| 4712 | exp_out_view exp_s_no_sup |
| 4713 | end |
| 4714 | ret |
| 4715 | end |
| 4716 | for k ulong scnt bound 1024 |
| 4717 | pstep = get exp_bs_sup_step add soff k |
| 4718 | prel = get exp_bs_sup_rel add soff k |
| 4719 | pnid = get exp_bs_nid sub pstep 1 |
| 4720 | if eq prel EXP_REL_REFERENCES |
| 4721 | exp_out_view exp_s_it_refs |
| 4722 | else |
| 4723 | exp_out_view exp_s_it_rests |
| 4724 | end |
| 4725 | exp_out_u64 pstep |
| 4726 | exp_out_b uchar 32 |
| 4727 | exp_out_b uchar 40 |
| 4728 | exp_out_rel_name prel |
| 4729 | exp_out_b uchar 41 |
| 4730 | exp_out_b uchar 58 |
| 4731 | exp_out_b uchar 32 |
| 4732 | exp_out_arena get exp_gn_stmt_off pnid get exp_gn_stmt_len pnid |
| 4733 | exp_out_b uchar 10 |
| 4734 | end |
| 4735 | end |
| 4736 | |
| 4737 | fn exp_sess_step_later n ulong nid ulong to void |
| 4738 | sc = get exp_b_scnt 0 |
| 4739 | for j ulong sc bound 320 |
| 4740 | lso = get exp_bs_sup_off j |
| 4741 | lsc = get exp_bs_sup_cnt j |
| 4742 | for k ulong lsc bound 1024 |
| 4743 | if eq get exp_bs_sup_step add lso k n |
| 4744 | exp_out_view exp_s_step_hdr |
| 4745 | exp_out_u64 add j 1 |
| 4746 | exp_out_view exp_s_rests_on_it |
| 4747 | break |
| 4748 | end |
| 4749 | end |
| 4750 | end |
| 4751 | uc = get exp_bu_cnt 0 |
| 4752 | no = get exp_gn_id_off nid |
| 4753 | nl = get exp_gn_id_len nid |
| 4754 | for i ulong uc bound 512 |
| 4755 | if exp_str_eq get exp_bu_nid_off i get exp_bu_nid_len i no nl |
| 4756 | exp_out_view exp_s_caveat |
| 4757 | exp_out_arena get exp_bu_det_off i get exp_bu_det_len i |
| 4758 | exp_out_b uchar 10 |
| 4759 | end |
| 4760 | end |
| 4761 | end |
| 4762 | |
| 4763 | fn exp_sess_step n ulong to int |
| 4764 | if eq n 0 |
| 4765 | ret EXP_ERR_STEP_OUT_OF_RANGE |
| 4766 | end |
| 4767 | if gt n get exp_b_scnt 0 |
| 4768 | ret EXP_ERR_STEP_OUT_OF_RANGE |
| 4769 | end |
| 4770 | exp_out_reset |
| 4771 | si = sub n 1 |
| 4772 | nid = get exp_bs_nid si |
| 4773 | exp_out_view exp_s_step_hdr |
| 4774 | exp_out_u64 n |
| 4775 | exp_out_b uchar 58 |
| 4776 | exp_out_b uchar 32 |
| 4777 | exp_out_arena get exp_gn_stmt_off nid get exp_gn_stmt_len nid |
| 4778 | exp_out_b uchar 32 |
| 4779 | exp_out_b uchar 91 |
| 4780 | exp_out_status_words get exp_gn_status nid |
| 4781 | exp_out_b uchar 93 |
| 4782 | exp_out_b uchar 10 |
| 4783 | exp_sess_step_deps si |
| 4784 | exp_sess_step_later n nid |
| 4785 | ret EXP_OK |
| 4786 | end |
| 4787 | |
| 4788 | fn exp_sess_source n ulong to int |
| 4789 | if eq n 0 |
| 4790 | ret EXP_ERR_STEP_OUT_OF_RANGE |
| 4791 | end |
| 4792 | if gt n get exp_b_scnt 0 |
| 4793 | ret EXP_ERR_STEP_OUT_OF_RANGE |
| 4794 | end |
| 4795 | exp_out_reset |
| 4796 | si = sub n 1 |
| 4797 | nid = get exp_bs_nid si |
| 4798 | exp_out_view exp_s_step_hdr |
| 4799 | exp_out_u64 n |
| 4800 | exp_out_view exp_s_comes |
| 4801 | exp_out_arena get exp_gn_sys_off nid get exp_gn_sys_len nid |
| 4802 | exp_out_b uchar 32 |
| 4803 | exp_out_b uchar 40 |
| 4804 | exp_out_arena get exp_gn_art_off nid get exp_gn_art_len nid |
| 4805 | exp_out_view exp_s_ref_lbl |
| 4806 | exp_out_arena get exp_gn_ref_off nid get exp_gn_ref_len nid |
| 4807 | if ne get exp_gn_has_dig nid 0 |
| 4808 | exp_out_view exp_s_dig_c |
| 4809 | exp_out_arena get exp_gn_dig_off nid get exp_gn_dig_len nid |
| 4810 | end |
| 4811 | exp_out_view exp_s_node_sc |
| 4812 | exp_out_arena get exp_gn_id_off nid get exp_gn_id_len nid |
| 4813 | exp_out_view exp_s_st_sc |
| 4814 | exp_out_status_name get exp_gn_status nid |
| 4815 | exp_out_b uchar 46 |
| 4816 | exp_out_b uchar 10 |
| 4817 | ret EXP_OK |
| 4818 | end |
| 4819 | |
| 4820 | fn exp_sess_evidence to ulong |
| 4821 | exp_out_reset |
| 4822 | evc = get exp_be_cnt 0 |
| 4823 | if eq evc 0 |
| 4824 | exp_out_view exp_s_no_ev_att |
| 4825 | ret get exp_out_len 0 |
| 4826 | end |
| 4827 | mc = get exp_g_mcnt 0 |
| 4828 | for i ulong evc bound 320 |
| 4829 | step = get exp_be_step i |
| 4830 | nid = get exp_bs_nid sub step 1 |
| 4831 | exp_out_view exp_s_step_hdr |
| 4832 | exp_out_u64 step |
| 4833 | exp_out_b uchar 58 |
| 4834 | exp_out_b uchar 32 |
| 4835 | exp_out_arena get exp_gn_sys_off nid get exp_gn_sys_len nid |
| 4836 | exp_out_b uchar 32 |
| 4837 | exp_out_arena get exp_gn_art_off nid get exp_gn_art_len nid |
| 4838 | exp_out_b uchar 32 |
| 4839 | exp_out_arena get exp_gn_ref_off nid get exp_gn_ref_len nid |
| 4840 | if ne get exp_gn_has_dig nid 0 |
| 4841 | exp_out_view exp_s_dig |
| 4842 | exp_out_arena get exp_gn_dig_off nid get exp_gn_dig_len nid |
| 4843 | end |
| 4844 | exp_out_b uchar 10 |
| 4845 | for m ulong mc bound 256 |
| 4846 | if eq get exp_gm_owner m nid |
| 4847 | exp_out_b uchar 32 |
| 4848 | exp_out_b uchar 32 |
| 4849 | exp_out_arena get exp_gm_name_off m get exp_gm_name_len m |
| 4850 | exp_out_b uchar 32 |
| 4851 | exp_out_b uchar 61 |
| 4852 | exp_out_b uchar 32 |
| 4853 | exp_out_arena get exp_gm_val_off m get exp_gm_val_len m |
| 4854 | exp_out_b uchar 32 |
| 4855 | exp_out_arena get exp_gm_unit_off m get exp_gm_unit_len m |
| 4856 | exp_out_b uchar 10 |
| 4857 | end |
| 4858 | end |
| 4859 | end |
| 4860 | ret get exp_out_len 0 |
| 4861 | end |
| 4862 | |
| 4863 | fn exp_sess_technical to ulong |
| 4864 | ret exp_render EXP_MODE_TECHNICAL EXP_MAX_LEVEL get exp_sess 1 |
| 4865 | end |
| 4866 | |
| 4867 | fn exp_sess_simple to ulong |
| 4868 | ret exp_render EXP_MODE_SUMMARY 1 EXP_AUD_GENERAL |
| 4869 | end |
| 4870 | |
| 4871 | fn exp_sess_analogy to ulong |
| 4872 | ret exp_render EXP_MODE_ANALOGY 2 get exp_sess 1 |
| 4873 | end |
| 4874 | |
| 4875 | fn exp_sess_uncertainties to ulong |
| 4876 | exp_out_reset |
| 4877 | uc = get exp_bu_cnt 0 |
| 4878 | cc = get exp_bc_cnt 0 |
| 4879 | if and eq uc 0 eq cc 0 |
| 4880 | exp_out_view exp_s_no_unc_rec |
| 4881 | ret get exp_out_len 0 |
| 4882 | end |
| 4883 | for i ulong uc bound 512 |
| 4884 | exp_out_b uchar 45 |
| 4885 | exp_out_b uchar 32 |
| 4886 | exp_out_uk_label get exp_bu_kind i |
| 4887 | exp_out_b uchar 58 |
| 4888 | exp_out_b uchar 32 |
| 4889 | exp_out_arena get exp_bu_det_off i get exp_bu_det_len i |
| 4890 | exp_out_b uchar 10 |
| 4891 | end |
| 4892 | for i ulong cc bound 128 |
| 4893 | exp_out_view exp_s_conflict_pfx |
| 4894 | exp_out_arena get exp_bc_stmt_off i get exp_bc_stmt_len i |
| 4895 | exp_out_b uchar 10 |
| 4896 | end |
| 4897 | ret get exp_out_len 0 |
| 4898 | end |
| 4899 | |
| 4900 | fn exp_sess_alternatives to ulong |
| 4901 | exp_out_reset |
| 4902 | ac = get exp_ba_cnt 0 |
| 4903 | if eq ac 0 |
| 4904 | exp_out_view exp_s_no_alt_rec |
| 4905 | ret get exp_out_len 0 |
| 4906 | end |
| 4907 | for i ulong ac bound 128 |
| 4908 | nid = get exp_ba_nid i |
| 4909 | exp_out_b uchar 45 |
| 4910 | exp_out_b uchar 32 |
| 4911 | exp_out_arena get exp_gn_stmt_off nid get exp_gn_stmt_len nid |
| 4912 | exp_out_b uchar 10 |
| 4913 | end |
| 4914 | ret get exp_out_len 0 |
| 4915 | end |
| 4916 | |
| 4917 | note CLI helpers (main.rs) |
| 4918 | fn exp_cli_exit_code err int to int |
| 4919 | if eq err EXP_OK |
| 4920 | ret 0 |
| 4921 | end |
| 4922 | ret add 10 err |
| 4923 | end |
| 4924 | |
| 4925 | fn exp_parse_audience s view uchar * to ulong |
| 4926 | if exp_view_eq s exp_an_0 |
| 4927 | ret EXP_AUD_GENERAL |
| 4928 | end |
| 4929 | if exp_view_eq s exp_an_1 |
| 4930 | ret EXP_AUD_OPERATOR |
| 4931 | end |
| 4932 | if exp_view_eq s exp_an_2 |
| 4933 | ret EXP_AUD_DEVELOPER |
| 4934 | end |
| 4935 | if exp_view_eq s exp_an_3 |
| 4936 | ret EXP_AUD_SYSTEMS_ENG |
| 4937 | end |
| 4938 | if exp_view_eq s exp_an_4 |
| 4939 | ret EXP_AUD_RESEARCHER |
| 4940 | end |
| 4941 | ret EXP_AUD_INVALID |
| 4942 | end |
| 4943 | |
| 4944 | fn exp_hex_nib b uchar to int |
| 4945 | if and ge b 48 le b 57 |
| 4946 | ret int sub b 48 |
| 4947 | end |
| 4948 | if and ge b 97 le b 102 |
| 4949 | ret int add 10 sub b 97 |
| 4950 | end |
| 4951 | if and ge b 65 le b 70 |
| 4952 | ret int add 10 sub b 65 |
| 4953 | end |
| 4954 | ret -1 |
| 4955 | end |
| 4956 | |
| 4957 | fn exp_parse_cortex_id s view uchar * dst edit uchar * to int |
| 4958 | n = len s |
| 4959 | off = ulong 0 |
| 4960 | if ge n 7 |
| 4961 | if exp_view_eq part s 0 7 exp_txt_cx_pfx |
| 4962 | off = 7 |
| 4963 | end |
| 4964 | end |
| 4965 | if ne sub n off 32 |
| 4966 | ret EXP_ERR_TARGET_NOT_FOUND |
| 4967 | end |
| 4968 | for k ulong 16 |
| 4969 | hi = exp_hex_nib get s add off mul k 2 |
| 4970 | lo = exp_hex_nib get s add add off mul k 2 1 |
| 4971 | if or lt hi 0 lt lo 0 |
| 4972 | ret EXP_ERR_TARGET_NOT_FOUND |
| 4973 | end |
| 4974 | put dst k uchar or shl ulong hi 4 ulong lo |
| 4975 | end |
| 4976 | ret EXP_OK |
| 4977 | end |