File size: 6,054 Bytes
4cba1a6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | # Sovereign Stack Machine: Type Rules (Formal)
**Version:** 1.0.0
---
## Judgment Forms
```
Ξ β’ p β (Ο, O, Ξ')
where:
Ξ = input stack type
p = program (instruction sequence)
Ο = type of computed value (if any)
O = generated obligations
Ξ' = output stack type
```
---
## Forward Type Inference (Deterministic)
```
Algorithm: infer(p: List Instr, Ξβ: StackType) β (Ξ_out: StackType, O: List Obligation) βͺ Error
```
**Base Case:** Empty program
```
Ξ β’ [] β (unit, [], Ξ)
```
**Inductive Step:**
```
Ξ β’ i β (Ο, O_i, Ξ') Ξ' β’ p β (Ο', O_p, Ξ'')
ββββββββββββββββββββββββββββββββββββββββββββββββββββ
Ξ β’ (i :: p) β (Ο', O_i ++ O_p, Ξ'')
```
---
## Individual Instruction Rules
### `Push x`
```
Ξ β’ Push x β (int64_t, [], Scalar :: Ξ)
```
### `Dup`
```
h β Ξ Ξ = h :: t
βββββββββββββββββββ
Ξ β’ Dup β (unit, [], h :: h :: t)
```
**Error:** Stack empty
### `Swap`
```
Ξ = hβ :: hβ :: t
βββββββββββββββββββ
Ξ β’ Swap β (unit, [], hβ :: hβ :: t)
```
**Error:** Fewer than 2 elements
### `Add`
```
Case 1: Scalar + Scalar
Ξ = Scalar :: Scalar :: t
βββββββββββββββββββββββββββ
Ξ β’ Add β (unit, [AddBounds], Scalar :: t)
Case 2: Vec n + Vec n
Ξ = Vec n :: Vec n :: t
βββββββββββββββββββββββββββ
Ξ β’ Add β (unit, [VecAddBounds n], Vec n :: t)
Case 3: Mat m n + Mat m n
Ξ = Mat m n :: Mat m n :: t
ββββββββββββββββββββββββββββββ
Ξ β’ Add β (unit, [MatAddBounds m n], Mat m n :: t)
```
**Error:** Shape mismatch
### `MatPush m n`
```
ββββββββββββββββββββββββ
Ξ β’ MatPush m n β (unit, [], Mat m n :: Ξ)
```
### `MatFill`
```
Ξ = Mat m n :: Scalar :: ... :: Scalar :: t (m*n scalars)
ββββββββββββββββββββββββββββββββββββββββββββββββ
Ξ β’ MatFill β (unit, [MatFillBounds m n], Mat m n :: t)
```
**Error:** Insufficient scalars, or shape mismatch
### `MatMul`
```
Ξ = Mat m n :: Mat n k :: t
ββββββββββββββββββββββββββββββββββββββββββββ
Ξ β’ MatMul β (unit, [MatMulShape m n k, MatMulBounds m n k], Mat m k :: t)
```
**Obligations:**
- `MatMulShape m n k`: Assert n values match (compile-time, runtime check)
- `MatMulBounds m n k`: No overflow in any element
**Error:** Shape mismatch
### `MatTrans`
```
Ξ = Mat m n :: t
ββββββββββββββββββ
Ξ β’ MatTrans β (unit, [], Mat n m :: t)
```
### `MatAug`
```
Ξ = Mat m nβ :: Mat m nβ :: t
ββββββββββββββββββββββββββββββββ
Ξ β’ MatAug β (unit, [MatAugShape m nβ nβ], Mat m (nβ + nβ) :: t)
```
**Error:** Row mismatch
### `Inv`
```
Ξ = Proof("Invertible") :: Mat n n :: t
βββββββββββββββββββββββββββββββββββββββββββ
Ξ β’ Inv β (unit, [InvBounds n], Mat n n :: t)
```
**Obligation:** `InvBounds n` β check no overflow in Gauss-Jordan steps
**Error:** Non-square, or missing proof
### `Solve`
```
Ξ = Proof("Invertible") :: Mat n 1 :: Mat n n :: t
βββββββββββββββββββββββββββββββββββββββββββββββββββ
Ξ β’ Solve β (unit, [SolveBounds n], Mat n 1 :: t)
```
### `Lstsq`
```
Ξ = Proof("FullColumnRank") :: Mat m 1 :: Mat m n :: t (m β₯ n)
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Ξ β’ Lstsq β (unit, [LstsqBounds m n], Mat n 1 :: t)
```
### `VerifyInv`
```
Ξ = Mat n n :: Mat n n :: t
ββββββββββββββββββββββββββββ
Ξ β’ VerifyInv β (unit, [VerifyInvObligation n], Proof("InvOK") :: t)
```
**Obligation:** `VerifyInvObligation n` β verify A*X == I
### `VerifySol`
```
Ξ = Mat n 1 :: Mat n n :: Mat n 1 :: t
ββββββββββββββββββββββββββββββββββββββ
Ξ β’ VerifySol β (unit, [VerifySolObligation n], Proof("SolOK") :: t)
```
### `VerifyLstsq`
```
Ξ = Mat n 1 :: Mat m n :: Mat m 1 :: t (m β₯ n)
ββββββββββββββββββββββββββββββββββββββββββββββββββ
Ξ β’ VerifyLstsq β (unit, [VerifyLstsqObligation m n], Proof("LstsqOK") :: t)
```
---
## Unification Algorithm
**For symbolic dimensions in branching code (not in core ISA):**
```
unify(Οβ: ValType, Οβ: ValType) β (Substitution βͺ Conflict)
Base cases:
unify(Scalar, Scalar) = {}
unify(Vec nβ, Vec nβ) = if nβ == nβ then {} else Conflict("Vec dimension mismatch")
unify(Mat mβ nβ, Mat mβ nβ) = if mβ==mβ && nβ==nβ then {} else Conflict(...)
unify(Οβ, Οβ) when Οβ β Οβ = Conflict("Type mismatch")
```
**Apply substitution:**
```
apply(Ο: Substitution, Ξ: StackType) : StackType
```
---
## Completeness & Soundness
**Completeness:** If a program is valid (no underflow, correct shapes), `infer` succeeds and produces `Ξ'`.
**Soundness:** If `infer(p, Ξ)` succeeds with `(Ξ', O)`, then execution of `p` starting with stack type `Ξ` produces stack type `Ξ'` and only generates obligations in `O`.
**Proof:** By induction on program length and instruction structure. Each instruction rule preserves type safety and obligation correctness.
---
**END TYPE RULES**
|