|
| 1 | += Julia the Viper Type System Specification (V2 Corrected Draft) |
| 2 | +:version: 2.0.0-draft |
| 3 | +:date: 2026-04-16 |
| 4 | + |
| 5 | +SPDX-License-Identifier: PMPL-1.0-or-later |
| 6 | +Copyright (c) 2026 Jonathan D.A. Jewell |
| 7 | + |
| 8 | +== 1. Effect Lattice |
| 9 | + |
| 10 | +Total ⊑ Pure ⊑ Impure |
| 11 | + |
| 12 | +Interpretation: |
| 13 | +- Total = strongest guarantees (termination + no effects) |
| 14 | +- Impure = weakest guarantees |
| 15 | + |
| 16 | +Join (⊔) moves rightward (loses guarantees). |
| 17 | + |
| 18 | +== 2. Numeric Types and Widening |
| 19 | + |
| 20 | +Numeric ::= Int | Float | Rational | Complex | Hex | Binary |
| 21 | + |
| 22 | +Tower: |
| 23 | +Binary, Hex ⊂ Int ⊂ Rational ⊂ Float ⊂ Complex |
| 24 | + |
| 25 | +widen(τ₁, τ₂) = least upper bound in the tower |
| 26 | + |
| 27 | +Addition rule: |
| 28 | + |
| 29 | +Γ ⊢_D e₁ : τ₁ |
| 30 | +Γ ⊢_D e₂ : τ₂ |
| 31 | +τ₁, τ₂ ∈ Numeric |
| 32 | +-------------------------------- |
| 33 | +Γ ⊢_D e₁ + e₂ : widen(τ₁, τ₂) |
| 34 | +
|
| 35 | +== 3. Fragment Hierarchy |
| 36 | +
|
| 37 | +D ⊂ S ⊂ C |
| 38 | +
|
| 39 | +D = Data (total) |
| 40 | +S = Safe Control (total per input) |
| 41 | +C = Full Control (Turing-complete) |
| 42 | +
|
| 43 | +== 4. Data Fragment |
| 44 | +
|
| 45 | +Includes: |
| 46 | +- literals |
| 47 | +- variables |
| 48 | +- addition |
| 49 | +- negation |
| 50 | +- calls to @total functions |
| 51 | +
|
| 52 | +Guarantee: all Data expressions terminate. |
| 53 | +
|
| 54 | +== 5. Safe Control Fragment (S) |
| 55 | +
|
| 56 | +Allowed: |
| 57 | +- let bindings from D |
| 58 | +- sequencing |
| 59 | +- if with Data guards |
| 60 | +- bounded for loops |
| 61 | +- calls to @total functions |
| 62 | +
|
| 63 | +Forbidden: |
| 64 | +- while |
| 65 | +- recursion (unless proven terminating) |
| 66 | +- impure or pure calls |
| 67 | +- I/O |
| 68 | +
|
| 69 | +Note: termination is per-input, not bounded. |
| 70 | +
|
| 71 | +== 6. Safe Loop Rule |
| 72 | +
|
| 73 | +Γ ⊢_D start : Int |
| 74 | +Γ ⊢_D end : Int |
| 75 | +Γ, x:Int ⊢_S body : () |
| 76 | +-------------------------------- |
| 77 | +Γ ⊢_S for x in start..end { body } : () |
| 78 | + |
| 79 | +== 7. Function Effects |
| 80 | + |
| 81 | +@total: |
| 82 | +- must check entirely in S |
| 83 | +- no recursion unless verified |
| 84 | + |
| 85 | +@pure: |
| 86 | +- no I/O |
| 87 | +- may diverge |
| 88 | + |
| 89 | +@impure: |
| 90 | +- unrestricted |
| 91 | + |
| 92 | +== 8. Echo System |
| 93 | + |
| 94 | +Echo ::= EchoSafe | EchoNeutral | EchoBreaking |
| 95 | + |
| 96 | +Join: |
| 97 | + |
| 98 | +| ⊔ | Safe | Neutral | Breaking | |
| 99 | +|---|------|---------|----------| |
| 100 | +| Safe | Safe | Neutral | Breaking | |
| 101 | +| Neutral | Neutral | Neutral | Breaking | |
| 102 | +| Breaking | Breaking | Breaking | Breaking | |
| 103 | + |
| 104 | +Addition: |
| 105 | + |
| 106 | +Γ ⊢ e₁ : η₁ |
| 107 | +Γ ⊢ e₂ : η₂ |
| 108 | +--------------------------- |
| 109 | +Γ ⊢ e₁ + e₂ : η₁ ⊔ η₂ |
| 110 | +
|
| 111 | +== 9. Reverse Blocks |
| 112 | +
|
| 113 | +Allowed only if: |
| 114 | +- all statements invertible |
| 115 | +- no EchoBreaking statements |
| 116 | +- no loops in V2 |
| 117 | +
|
| 118 | +== 10. Soundness Theorem |
| 119 | +
|
| 120 | +Theorem (Safe Fragment Termination): |
| 121 | +
|
| 122 | +If Γ ⊢_S s : (), then for all environments ρ: |
| 123 | +eval(s, ρ) terminates. |
| 124 | +
|
| 125 | +Proof Sketch: |
| 126 | +
|
| 127 | +By induction on typing derivation: |
| 128 | +
|
| 129 | +- let: terminates (Data totality) |
| 130 | +- seq: composition of terminating statements |
| 131 | +- if: guard terminates, branch terminates |
| 132 | +- for: finite iteration over total domain |
| 133 | +- call: @total invariant guarantees termination |
| 134 | +
|
| 135 | +QED. |
| 136 | +
|
| 137 | +== 11. Decidability |
| 138 | +
|
| 139 | +Fragment membership is decidable syntactically. |
| 140 | +
|
| 141 | +@total checking depends on a termination checker T. |
| 142 | +
|
| 143 | +Default V2: |
| 144 | +- no recursion allowed |
| 145 | +
|
| 146 | +Future: |
| 147 | +- structural recursion |
| 148 | +- sized types |
| 149 | +
|
| 150 | +== 12. Invariants |
| 151 | +
|
| 152 | +- Data totality holds |
| 153 | +- Safe fragment termination holds |
| 154 | +- Halting boundary is syntactic |
| 155 | +- Echo does not affect value typing |
| 156 | +- Widening may be lossy and is independent of echo |
| 157 | +
|
0 commit comments