Skip to content

Commit 1a93fac

Browse files
committed
[lec13] Agda live coding start
1 parent b379119 commit 1a93fac

File tree

3 files changed

+259
-1
lines changed

3 files changed

+259
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ Material: plt = course book, dragon = Dragon book. Slides follow closely the plt
5959
| Tue 10/12 | 13-15 | Type inference and polymorphism | plt 7.7-9, [script](notes/typing.html), [visualization tool](https://github.com/teach-plt/visualize-type-inference) |
6060
| Thu 12/12 **HC1** | 13-14 | Hands-on with Lab 4 (Haskell) | [live code](live/2024/lab4/Interpreter.hs) |
6161
| Thu 12/12 | 14-15 | Hands-on with Lab 4 (Java) | [live code](live/2024/lab4/Interpreter.java) |
62-
| Tue 17/12 | 13-15 | Dependent types (Agda) | |
62+
| Tue 17/12 | 13-15 | Dependent types (Agda) | live coding [start](live/2024/agda/JVM-start.agda) |
6363
| *Wed 18/12* | *23* | *Lab 3 deadline* | |
6464
| Thu 19/12 **SB-H5** | 13-15 | Preparing for the exam | |
6565

live/2024/agda/JVM-start.agda

Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
1+
-- Programming Language Technology DAT151/DIT231
2+
-- 2024-12-17 Agda (version ≥2.6.0)
3+
-- From Dependent Types to Verified Compilation
4+
5+
-- A correct compiler for Hutton's Razor to a stack machine
6+
7+
-- Similar to an early pen-and-paper proof (register machine):
8+
--
9+
-- John McCarthy and James Painter, 1967
10+
-- Correctness of a Compiler for Arithmetic Expressions
11+
-- http://jmc.stanford.edu/articles/mcpain/mcpain.pdf
12+
13+
open import Relation.Binary.PropositionalEquality
14+
open ≡-Reasoning
15+
16+
-- Data types and pattern matching
17+
---------------------------------------------------------------------------
18+
19+
-- Example of a data type.
20+
21+
data List (A : Set) : Set where
22+
[] : List A
23+
_∷_ : (x : A) (xs : List A) List A
24+
25+
-- Natural numbers in unary notation.
26+
27+
data Nat : Set where
28+
zero : Nat
29+
suc : (n : Nat) Nat
30+
31+
{-# BUILTIN NATURAL Nat #-}
32+
33+
-- NATURAL gives us decimal notation for Nat.
34+
35+
five = suc 4
36+
37+
infixl 10 _+_
38+
infixr 6 _∷_
39+
40+
-- Addition.
41+
42+
_+_ : (n m : Nat) Nat
43+
n + m = {!!}
44+
45+
{-
46+
plus-0 : ∀(n : Nat) → n + 0 ≡ n
47+
plus-0 n = {!!}
48+
49+
-- Indexed types: Fin, Vec
50+
---------------------------------------------------------------------------
51+
52+
{-
53+
-- Bounded numbers: Fin n = { m | n < m }.
54+
55+
data Fin : Nat → Set where
56+
zero : {n : Nat} → Fin (suc n)
57+
suc : {n : Nat} (i : Fin n) → Fin (suc n)
58+
59+
{-
60+
-- Example with hidden arguments.
61+
62+
three : Fin 5
63+
three = ?
64+
65+
{-
66+
-- Vectors (length-indexed lists).
67+
-- Vec : Set → Nat → Set
68+
69+
data Vec (A : Set) : Nat → Set where
70+
[] : Vec A zero
71+
_∷_ : {n : Nat} (x : A) (xs : Vec A n) → Vec A (suc n)
72+
73+
-- Reading an element of a vector.
74+
75+
lookup : ∀{n A} (i : Fin n) (xs : Vec A n) → A
76+
lookup i xs = {!!}
77+
78+
{-
79+
80+
-- Automatic quantification over hidden arguments.
81+
82+
variable
83+
n : Nat
84+
A : Set
85+
86+
{-
87+
-- Expressions and interpretation: "Hutton's razor"
88+
---------------------------------------------------------------------------
89+
90+
-- We have a single type of (natural) numbers.
91+
92+
Num = Nat
93+
94+
-- Expressions over n variables.
95+
96+
-- A variable is denoted by its number x < n.
97+
98+
data Exp (n : Nat) : Set where
99+
var : (x : Fin n) → Exp n
100+
num : (w : Num) → Exp n
101+
plus : (e e' : Exp n) → Exp n
102+
103+
-- An environment maps each variable to its value.
104+
105+
Value = Num
106+
Env = Vec Num
107+
108+
{-
109+
-- Interpretation of expressions.
110+
111+
eval : (e : Exp n) (γ : Env n) → Value
112+
eval e γ = ?
113+
114+
{-
115+
-- A fragment of JVM
116+
---------------------------------------------------------------------------
117+
118+
StackSize = Nat -- Current stack size
119+
StoreSize = Nat -- Local variable store limit
120+
Addr = Fin -- Local variable address
121+
Word = Nat -- Word (should be 32bit signed int, but we use Nat here)
122+
123+
-- JVM instructions.
124+
--
125+
-- n = number of local variables
126+
-- m = stack size before instruction
127+
-- m' = stack size after instruction
128+
129+
variable
130+
k l m m' : StackSize
131+
132+
data Ins (n : StoreSize) : (m m' : StackSize) → Set where
133+
load : (a : Addr n) → Ins n m (1 + m) -- Load variable onto stack.
134+
add : Ins n (2 + m) (1 + m) -- Add top stack elements.
135+
ldc : (w : Word) → Ins n m (1 + m) -- Load constant onto stack.
136+
pop : Ins n (1 + m) m -- Remove top stack element.
137+
138+
-- Instruction sequences.
139+
-- Note that for instruction concatenation, the index "l" has to match.
140+
141+
data Inss (n : StoreSize) (m : StackSize) : (m' : StackSize) → Set where
142+
[] : Inss n m m
143+
ins : Ins n m l → Inss n m l
144+
_∙_ : (i : Inss n m l) (is : Inss n l k) → Inss n m k
145+
146+
infixr 10 _∙_
147+
148+
{-
149+
-- Compilation of expressions
150+
---------------------------------------------------------------------------
151+
152+
-- The code for an expression leaves one additional value on the stack.
153+
154+
compile : (e : Exp n) → Inss n m (suc m)
155+
compile (var x) = ?
156+
compile (num w) = ?
157+
compile (plus e e') = ?
158+
159+
{-
160+
-- JVM small-step semantics
161+
---------------------------------------------------------------------------
162+
163+
-- JVM machine state (simplified).
164+
165+
Store = Env
166+
Stack = Vec Word
167+
168+
record State (n : StoreSize) (m : StackSize) : Set where
169+
constructor state
170+
field
171+
V : Store n
172+
S : Stack m
173+
open State
174+
175+
{-
176+
-- Executing a JVM instruction.
177+
178+
step : (i : Ins n m m') (s : State n m) → State n m'
179+
step i s = {!!}
180+
181+
{-
182+
183+
-- Compiler correctness
184+
---------------------------------------------------------------------------
185+
186+
-- Executing a series of JVM instructions.
187+
188+
steps : (is : Inss n m m') (s : State n m) → State n m'
189+
steps (ins i) s = {!!}
190+
steps [] s = {!!}
191+
steps (is ∙ is') s = {!!}
192+
193+
{-
194+
-- Pushing a word onto the stack.
195+
196+
push : Word → State n m → State n (suc m)
197+
push w (state V S) = state V (w ∷ S)
198+
199+
push-eval : (e : Exp n) (s : State n m) → State n (suc m)
200+
push-eval e (state V S) = state V (eval e V ∷ S)
201+
202+
203+
-- Compiler correctness theorem.
204+
205+
-- Running the instructions for an expression e
206+
-- leaves the value of e on top of the stack.
207+
208+
sound : (e : Exp n) (s : State n m) →
209+
210+
steps (compile e) s ≡ push-eval e s
211+
212+
-- Case: variables
213+
sound (var x) s = {!!}
214+
215+
-- Case: number literals.
216+
sound (num w) s = {!!}
217+
218+
-- Case: addition expression.
219+
sound (plus e e') s = begin
220+
steps (compile (plus e e')) s ≡⟨ {!!} ⟩
221+
push-eval (plus e e') s
222+
223+
where s' = push-eval e s
224+
225+
-- steps (compile (plus e e') s)
226+
-- = steps (compile e ∙ compile e' ∙ ins add) s
227+
-- = steps (compile e' ∙ ins add) (steps (compile e) s) -- by ind.hyp.
228+
-- = steps (compile e' ∙ ins add) (push-eval e s)
229+
-- = steps (ins add) (steps (compile e') (push-eval e s))
230+
-- = steps (ins add) (push-eval e' (push-eval e s))
231+
-- = step add (push-eval e' (push-eval e s))
232+
-- = step add (push-eval e' (push-eval e (state V S))) -- s =: state V S
233+
-- = step add (push-eval e' (state V (eval e V ∷ S)))
234+
-- = step add (state V (eval e' V ∷ eval e V ∷ S))
235+
-- = state V ((eval e V + eval e' V) ∷ S)
236+
-- = state V (eval (plus e e') V ∷ S)
237+
-- = push-eval (plus e e') (state V S)
238+
-- = push-eval (plus e e') s
239+
240+
-- -}
241+
-- -}
242+
-- -}
243+
-- -}
244+
-- -}
245+
-- -}
246+
-- -}
247+
-- -}
248+
-- -}
249+
-- -}
250+
-- -}
251+
-- -}
252+
-- -}
253+
-- -}
254+
-- -}
255+
-- -}
256+
-- -}

live/2024/agda/Main.agda-lib

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
depend : standard-library
2+
include : .

0 commit comments

Comments
 (0)