-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathMachineState.lean
More file actions
66 lines (50 loc) · 1.9 KB
/
Copy pathMachineState.lean
File metadata and controls
66 lines (50 loc) · 1.9 KB
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
/-
Copyright (c) 2026 Mateo Petel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mateo Petel
-/
module
public import Cslib.Foundations.Data.Lens.Basic
/-!
# Machine state example
Lawful lens over the program counter and a verified step that preserves memory and the halt flag.
-/
@[expose] public section
namespace Cslib.Foundations.Data.Lens.Examples
open Cslib Lens
/-- A minimal interpreter-style machine state. -/
structure MachineState where
/-- Program counter. -/
pc : Nat
/-- Main memory as a list of natural numbers. -/
memory : List Nat
/-- Whether execution has halted. -/
halted : Bool
namespace MachineState
/-- Lawful lens focusing on the program counter. -/
def pcLens : LawfulLens MachineState Nat :=
mkLawful
(get := MachineState.pc)
(set := fun s pc => { s with pc := pc })
(get_set := by intro s pc; rfl)
(set_get := by intro s; rfl)
(set_set := by intro s pc₁ pc₂; rfl)
/-- Advance the program counter when the machine has not halted. -/
def step (s : MachineState) : MachineState :=
if s.halted then s else over pcLens (· + 1) s
/-- `step` leaves memory unchanged. -/
theorem step_preserves_memory (s : MachineState) : (step s).memory = s.memory := by
rcases s with ⟨pc, memory, hal⟩
cases hal <;> dsimp [step, pcLens, over, mkLawful]
/-- `step` leaves the halt flag unchanged. -/
theorem step_preserves_halted (s : MachineState) : (step s).halted = s.halted := by
rcases s with ⟨pc, memory, hal⟩
cases hal <;> dsimp [step, pcLens, over, mkLawful]
/-- When not halted, `step` increments the program counter by one. -/
theorem step_increments_pc (s : MachineState) (h : ¬ s.halted) : (step s).pc = s.pc + 1 := by
rcases s with ⟨pc, memory, hal⟩
cases hal with
| true => simp at h
| false => dsimp [step, pcLens, over, mkLawful]
end MachineState
end Cslib.Foundations.Data.Lens.Examples