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**