(angel-notes
  (:schema angel-lisp/0.2
   :profile semantic-twin/0.1
   :identity cs617/chapter11-recursion
   :role NOTES
   :audience solver
   :status provisional-functional-contract-not-issued
   :representation typed-relational-contract
   :equivalence :intended-not-proven
   :truth-policy (missing UNKNOWN) (closed-world nil)
   :normative-vocabulary (MUST MUST-NOT SHOULD MAY NOT-REQUIRED OPEN ADVISORY))

  (:interpretation
    (MUST :obligation)
    (MUST-NOT :prohibition)
    (SHOULD :strong-expectation-not-absolute-obligation)
    (MAY :permitted)
    (NOT-REQUIRED :absence-of-obligation)
    (OPEN :implementation-choice-not-fixed)
    (ADVISORY :non-normative-guidance)
    (UNKNOWN :not-specified)
    (:rule absence-of-clause -/-> prohibition)
    (:rule absence-of-clause -/-> obligation)
    (:rule unspecified-HOW -> OPEN-unless-constrained-elsewhere))

  (:problem-space
    (:target PB-2000C)
    (:artifact one-ROM-application)
    (:visible-theme recursion)
    (:problems (N-Queens Towers-of-Hanoi-three-peg))
    (:physical-display (columns 32) (rows 4))
    (:shared-service VT (virtual-columns 32) (virtual-lines 24)))

  (:requirements

    ;; -----------------------------------------------------------------
    ;; ROM shell + navigation
    ;; -----------------------------------------------------------------

    (R001 MUST (artifact-count ROM 1))
    (R002 MUST (target ROM PB-2000C))
    (R003 MUST (cold-start-splash centered))
    (R004 MUST (cold-start-splash -> menu :transition bounded-automatic :requires-EXE false))
    (R005 MUST (warm-MENU-return :replay-splash false))
    (R006 MUST (action-row :cells 4 :cell-width 8))
    (R007 MUST (top-menu-row "[queens][hanoi ][vttest][      ]"))
    (R008 MUST (FK4 :state reserved))
    (R009 MUST (MENU :scope Chapter11-anywhere
                      :effect (abandon-current-activity
                               goto-top-menu
                               preserve queens.N
                               preserve hanoi.N
                               replay-splash false)))
    (R010 MUST (BRK :scope Chapter11-anywhere
                     :effect established-normal-BRK-exit
                     :equivalent-to-MENU false
                     :resume-interrupted-calculation false))

    ;; -----------------------------------------------------------------
    ;; Shared VT: identity + ownership
    ;; -----------------------------------------------------------------

    (R011 MUST (VT :implementation-count 1 :shared-by (queens hanoi vttest)))
    (R012 MUST (VT :public-services (VT_CLEAR VT_WRITE VT_VIEW)))
    (R013 MUST (VT.surface :geometry (columns 32) (lines 24) :kind character-surface))
    (R014 MUST (VT.backing-storage :ownership private-library-state))
    (R015 MUST-NOT (consumer direct-read VT.backing-storage))
    (R016 MUST-NOT (consumer direct-write VT.backing-storage))
    (R017 MUST-NOT (consumer depend-on (VT.backing-storage address layout representation)))
    (R018 MUST (consumer page-mutation :only-through (VT_CLEAR VT_WRITE)))
    (R019 MUST (VT_VIEW :access VT.surface :mode read-only))
    (R020 MUST (VT_VIEW :modify VT.surface false))
    (R021 MUST (VT.surface :persist-after VT_VIEW-return true))
    (R022 MAY (consumer build-page incrementally))
    (R023 MUST (VT_WRITE :overlap-semantics later-write-overwrites-earlier-cells))
    (R024 MUST (ownership pagination consumer))
    (R025 MUST (ownership next-page-policy consumer))
    (R026 MUST (ownership recursion-state consumer))
    (R027 MUST (ownership continuation consumer))
    (R028 MUST-NOT (VT own (pagination next-page-policy recursion-state continuation)))

    ;; -----------------------------------------------------------------
    ;; VT_CLEAR
    ;; -----------------------------------------------------------------

    (R029 MUST (VT_CLEAR :effect (fill VT.surface ASCII-space)))
    (R030 MUST (VT_CLEAR :application-status-result none))

    ;; -----------------------------------------------------------------
    ;; VT_WRITE
    ;; -----------------------------------------------------------------

    (R031 MUST (VT_WRITE :signature (line column string)))
    (R032 MUST (VT_WRITE :coordinates conceptual-1-based))
    (R033 MUST (VT_WRITE :placement fixed-position-overwrite))
    (R034 MUST (VT_WRITE :wrap false))
    (R035 MUST (VT_WRITE :store (characters whose-mapped-columns within-1..32)))
    (R036 MUST (VT_WRITE :clip (characters whose-mapped-columns left-of-1)))
    (R037 MUST (VT_WRITE :clip (characters whose-mapped-columns right-of-32)))
    (R038 MUST (VT_WRITE :if-line-outside-1..24 store-none))
    (R039 MUST-NOT (VT_WRITE modify-memory-outside VT.surface))
    (R040 MUST (VT_WRITE :return 00 :when every-requested-character-stored))
    (R041 MUST (VT_WRITE :return 01 :when one-or-more-requested-characters-not-stored))
    (R042 MUST (VT_WRITE.status :describes virtual-storage-success))
    (R043 MUST-NOT (VT_WRITE.status :describe later-physical-visibility))
    (R044 MUST (stored-character-covered-by-viewer-overlay -> VT_WRITE.status-still-stored))

    ;; -----------------------------------------------------------------
    ;; VT_VIEW
    ;; -----------------------------------------------------------------

    (R045 MUST (VT_VIEW :present VT.surface :through physical-LCD-32x4))
    (R046 MUST (VT_VIEW :on-each-invocation :top-virtual-line 1 :visible-lines 1..4))
    (R047 MUST (VT_VIEW :key UP :effect scroll-one-virtual-line-up))
    (R048 MUST (VT_VIEW :key DOWN :effect scroll-one-virtual-line-down))
    (R049 MUST (VT_VIEW :key Q :return 00))
    (R050 MUST (VT_VIEW :key MENU :return 01))
    (R051 MUST (VT_VIEW :key BRK :return 02))
    (R052 MUST (VT_VIEW :all-other-keys ignored))
    (R053 MUST (VT_VIEW :scroll bounded :wrap false))
    (R054 MUST (VT_VIEW :when top-virtual-line=1 :UP -> unchanged))
    (R055 MUST (VT_VIEW :when visible-lines=21..24 :DOWN -> unchanged))
    (R056 MUST (VT_VIEW.overlay :physical-row 1 :columns 1..2 :text "VT"))
    (R057 MUST (VT_VIEW.overlay :physical-row 4 :columns 31..32
                                :text (zero-padded-decimal top-visible-virtual-line)
                                :domain 01..21))
    (R058 MUST (VT_VIEW.overlay :scope physical-display-only))
    (R059 MUST-NOT (VT_VIEW.overlay modify VT.surface))
    (R060 MUST (consumer :account-for overlay-covering-consumer-characters))
    (R061 MUST (VT_VIEW :return-semantics report-stop-reason-only))
    (R062 MUST-NOT (VT_VIEW decide consumer-policy))
    (R063 MUST (Q-within-VT_VIEW :semantic return-to-caller-only))
    (R064 MUST (caller interpret-Q-as (next-solution next-page completion return-screen) :as-applicable))

    ;; -----------------------------------------------------------------
    ;; VTTEST
    ;; -----------------------------------------------------------------

    (R065 MUST (vttest :consumer-of shared-VT))
    (R066 MUST (vttest :construct-page :only-through public-VT-API))
    (R067 MUST (vttest.page :virtual-lines 24 :content pseudo-document))
    (R068 MUST (vttest.page :each-line visibly-numbered))
    (R069 MUST (vttest.page :each-line Lorem-Ipsum-style-text))
    (R070 MUST (vttest :call VT_VIEW))
    (R071 SHOULD (vttest :make one-line-UP-DOWN-scrolling visually-obvious))
    (R072 SHOULD (vttest :permit visual-comparison document-line-number viewer-origin-indicator))
    (R073 MUST-NOT (vttest :purpose error-test-harness))
    (R074 MUST (vttest :Q-normal-return -> top-menu))
    (R075 MUST (vttest :MENU -> standard-MENU-semantics))
    (R076 MUST (vttest :BRK -> standard-BRK-semantics))

    ;; -----------------------------------------------------------------
    ;; Independent retained N state
    ;; -----------------------------------------------------------------

    (R077 MUST (queens.N :independent-of hanoi.N))
    (R078 MUST (hanoi.N :independent-of queens.N))
    (R079 MUST (cold-start -> queens.N=8))
    (R080 MUST (cold-start -> hanoi.N=8))
    (R081 MUST (queens.N :range 1..10))
    (R082 MUST (hanoi.N :range 1..10))
    (R083 MUST (change queens.N -/-> change hanoi.N))
    (R084 MUST (change hanoi.N -/-> change queens.N))
    (R085 MUST (accepted-N :persist until (changed-again cold-start)))
    (R086 MUST (new-n :input decimal-N))
    (R087 MUST (new-n :EXE accepts valid-N))
    (R088 MUST (new-n :invalid-N -> (visible-rejection no-computation-start)))
    (R089 MUST (new-n :MENU available))
    (R090 MUST (new-n :BRK available))

    ;; -----------------------------------------------------------------
    ;; Queens common contract
    ;; -----------------------------------------------------------------

    (R091 MUST (queens :problem classical-N-Queens))
    (R092 MUST (queens :solve recursively))
    (R093 MUST (queens :enumerate all-valid-solutions))
    (R094 MUST (queens :each-valid-solution exactly-once))
    (R095 OPEN (queens :solution-order))
    (R096 MUST (queens :action-row "[view  ][count ][new n ][menu  ]"))
    (R097 MUST (queens.screen :show current-N))

    ;; -----------------------------------------------------------------
    ;; Queens view
    ;; -----------------------------------------------------------------

    (R098 MUST (queens.view :enumerate-and-present one-solution-at-a-time))
    (R099 MUST (queens.view.search-display :surface physical-32x4))
    (R100 MUST (queens.view.search-display :show current-N))
    (R101 MUST (queens.view.search-display :show solutions-found-so-far))
    (R102 MUST (queens.view.search-display :show throttled-rotating-working-indicator))
    (R103 MUST (working-indicator :advance-at reasonable-implementation-defined-work-quantum))
    (R104 MUST-NOT (working-indicator :refresh-at every-elementary-search-operation))
    (R105 ADVISORY (progress-service-point :good-place-for cooperative-MENU-BRK-service))
    (R106 MUST (queens.view :each-found-solution -> one-VT-page))
    (R107 MUST (queens.view.solution-page :contain current-N))
    (R108 MUST (queens.view.solution-page :contain sequential-solution-number))
    (R109 MUST (queens.view.solution-page :contain complete-N-by-N-ASCII-board))
    (R110 MUST (queens.view.board :symbol queen "Q"))
    (R111 MUST (queens.view.board :symbol empty "."))
    (R112 MAY (queens.view.board :decoration (borders coordinates other)))
    (R113 MUST (queens.view.solution-page :contain Q-continuation-cue))
    (R114 NOT-REQUIRED (queens.view.solution-page :contain final-total))
    (R115 NOT-REQUIRED (queens.view :know-final-total-before-first-solution))
    (R116 MUST (queens.view :Q-on-solution-page -> (return-from-VT resume-search-next-solution)))
    (R117 MUST (queens.view :when-enumeration-exhausted -> final-VT-completion-page))
    (R118 MUST (queens.view.completion-page :contain total-solutions-found))
    (R119 MUST (queens.view.completion-page :represent zero-solution-total including-N=2-or-N=3))
    (R120 MUST (queens.view :Q-on-completion-page -> queens-action-screen))
    (R121 MUST (queens.view :Q-on-completion-page :preserve current-N))

    ;; -----------------------------------------------------------------
    ;; Queens count
    ;; -----------------------------------------------------------------

    (R122 MUST (queens.count :enumerate all-solutions))
    (R123 MUST (queens.count :pause-for-each-board false))
    (R124 MUST (queens.count.search-display :surface physical-32x4))
    (R125 MUST (queens.count.search-display :show current-N))
    (R126 MUST (queens.count.search-display :show solutions-found-so-far))
    (R127 MUST (queens.count.search-display :show same-throttled-rotating-working-indicator))
    (R128 MUST (queens.count.working-indicator :demonstrate-activity even-when-solution-count-static))
    (R129 MUST-NOT (queens.count.working-indicator :refresh-at every-low-level-search-event))
    (R130 MUST (queens.count :when-enumeration-finished -> VT-result-page))
    (R131 MUST (queens.count.result-page :contain final-solution-count))
    (R132 MUST (queens.count :Q-on-result-page -> queens-action-screen))
    (R133 MUST (queens.count :Q-on-result-page :preserve current-N))

    ;; -----------------------------------------------------------------
    ;; Hanoi identity + correctness
    ;; -----------------------------------------------------------------

    (R134 MUST (hanoi :problem classical-three-peg-Towers-of-Hanoi))
    (R135 MUST (hanoi :chapter-role recursion-experiment))
    (R136 MUST (hanoi :N-means disks))
    (R137 MUST (hanoi :peg A :role initial-source))
    (R138 MUST (hanoi :peg B :role auxiliary))
    (R139 MUST (hanoi :peg C :role destination))
    (R140 MUST (hanoi :action-row "[start ][new n ][      ][menu  ]"))
    (R141 MUST (hanoi.start :generate complete-minimal-legal-solution A->C using-B))
    (R142 MUST (hanoi.move :moves-exactly-one-disk))
    (R143 MUST-NOT (hanoi.move place-larger-disk-on-smaller-disk))
    (R144 MUST (hanoi.solution :move-count (= (- (pow 2 N) 1))))
    (R145 MUST (hanoi.output-stream :sufficient-to-replay-and-verify-every-move-from-known-initial-state))

    ;; -----------------------------------------------------------------
    ;; Hanoi move representation
    ;; -----------------------------------------------------------------

    (R146 MUST (hanoi.move-format "DD:S>D"))
    (R147 MUST (hanoi.move-format.DD :zero-padded-disk-number :domain 01..10))
    (R148 MUST (hanoi.move-format.SD :peg-letters))
    (R149 MUST (hanoi.move-field :width 7 :composition (six-visible-characters trailing-blank)))
    (R150 MUST (hanoi.move-line :fields 4 :first-column 3 :occupied-columns 3..30))
    (R151 MUST (hanoi.move-line :leave-columns-clear (1..2 31..32)))

    ;; -----------------------------------------------------------------
    ;; Hanoi page + continuation
    ;; -----------------------------------------------------------------

    (R152 MUST (hanoi.move-page.line1 :contain (hanoi-identity current-N move-number-range-on-page)))
    (R153 MUST (hanoi.move-page.lines2..23 :move-lines up-to-22))
    (R154 MUST (hanoi.move-page :moves-per-line 4))
    (R155 MUST (hanoi.move-page :max-moves 88))
    (R156 MUST (hanoi.move-page.line24 :contain Q-continuation-or-completion-cue))
    (R157 MUST (ownership hanoi-pagination hanoi-consumer))
    (R158 MUST (hanoi :Q-on-non-final-move-page ->
                       (return-from-VT continue-computation construct-next-logical-VT-page call-VT_VIEW-again)))
    (R159 MUST-NOT (VT :concept next-page))
    (R160 NOT-REQUIRED (hanoi :separate-live-progress-display))
    (R161 MUST (hanoi :while-generating-moves cooperative-MENU-BRK-available))
    (R162 MUST (hanoi :Q-on-final-move-page -> separate-VT-completion-page))
    (R163 MUST (hanoi.completion-page :contain total-moves-generated))
    (R164 MUST (hanoi.correct-output -> satisfies (= total-moves (- (pow 2 N) 1))))
    (R165 MUST (hanoi :Q-on-completion-page -> hanoi-action-screen))
    (R166 MUST (hanoi :Q-on-completion-page :preserve current-N))

    ;; -----------------------------------------------------------------
    ;; Explicit normal-return graph
    ;; -----------------------------------------------------------------

    (R167 MUST (return-graph queens.view.solution-page.Q -> queens.view.resume-search))
    (R168 MUST (return-graph queens.view.completion-page.Q -> queens.action-screen))
    (R169 MUST (return-graph queens.count.result-page.Q -> queens.action-screen))
    (R170 MUST (return-graph hanoi.non-final-move-page.Q -> hanoi.next-page-production))
    (R171 MUST (return-graph hanoi.final-move-page.Q -> hanoi.completion-page))
    (R172 MUST (return-graph hanoi.completion-page.Q -> hanoi.action-screen))
    (R173 MUST (return-graph vttest.Q -> top-menu))
    (R174 MUST (return-graph MENU-anywhere -> top-menu))
    (R175 MUST (return-graph BRK-anywhere -> established-normal-BRK-exit))

    ;; -----------------------------------------------------------------
    ;; Freedom boundary: WHAT fixed, HOW open
    ;; -----------------------------------------------------------------

    (R176 OPEN (queens :solution-order))
    (R177 OPEN (queens :recursive-routine-decomposition))
    (R178 OPEN (queens :candidate-order))
    (R179 OPEN (VT.backing-storage :address))
    (R180 OPEN (VT.backing-storage :representation))
    (R181 OPEN (machine :register-allocation))
    (R182 OPEN (machine :stack-layout))
    (R183 OPEN (implementation :local-data-representation))
    (R184 OPEN (implementation :internal-labels :except (VT_CLEAR VT_WRITE VT_VIEW)))
    (R185 OPEN (VT :calling-mechanics-below-published-interface))
    (R186 OPEN (progress :exact-work-quantum))
    (R187 OPEN (presentation :cosmetic-decoration-beyond-required-text))

    ;; -----------------------------------------------------------------
    ;; Evidence boundary
    ;; -----------------------------------------------------------------

    (R188 MUST (chapter-stage design-only))
    (R189 MUST-NOT (this-document claim source-result))
    (R190 MUST-NOT (this-document claim ROM-result))
    (R191 MUST-NOT (this-document claim listing-result))
    (R192 MUST-NOT (this-document claim emulator-result))
    (R193 MUST-NOT (this-document claim hardware-result)))

  (:derived-structure
    ;; These clauses only re-express relations already stated by requirements.
    ;; They add no new obligation.
    ((queens hanoi vttest) --consume--> VT)
    (VT --owns--> (surface backing-storage viewport overlays key-decoding return-reason))
    ((queens hanoi vttest) --own--> application-policy)
    (queens --owns--> (solution-sequence count-progress view-continuation queens.N))
    (hanoi --owns--> (move-sequence pagination hanoi.N))
    (VT_VIEW --observes--> VT.surface)
    ((VT_CLEAR VT_WRITE) --mutate--> VT.surface)
    (Q@VT_VIEW --returns-to--> caller)
    (MENU@consumer --navigates-to--> top-menu)
    (BRK@consumer --exits-via--> established-normal-BRK-path))

  (:semantic-guard
    (:no-private-oracle-values t)
    (:no-reference-solution t)
    (:no-prescribed-register-map t)
    (:no-prescribed-buffer-layout t)
    (:no-prescribed-queens-order t)
    (:no-prescribed-recursive-decomposition t)
    (:no-unpublished-VT-service t)
    (:no-runtime-evidence-claim t)
    (:equivalence-with-human-grammar hypothesis-not-proof))

  (:end
    (:solver-instruction derive-a-defensible-HOW-from-this-WHAT)
    (:contract-test satisfy-R001..R193-subject-to-modality)
    (:do-not-infer hidden-requirements)))
