~james/remy

3
3ac5bae085495f49c3f24dbfb40242ee81069c4f
main
remy/src/Model.thy -rw-r--r--4.0 KiB ViewLogBlameView rawPermalink 3ac5bae085 feat(compiler): add verified pixel compiler and templeos runner 2 months ago
1theory Model
2 imports "HOL-Library.Word" "HOL-Library.Code_Target_Numeral"
3begin
4
5unbundle bit_operations_syntax
6
7type_synonym pixel = "64 word"
8
9datatype input = X | Y | T
10
11datatype operator = Add | Sub | Mul | And | Or | Xor | Shl | Shr | Less
12
13fun operate :: "operator \<Rightarrow> pixel \<Rightarrow> pixel \<Rightarrow> pixel" where
14 "operate Add a b = a + b"
15| "operate Sub a b = a - b"
16| "operate Mul a b = a * b"
17| "operate And a b = a AND b"
18| "operate Or a b = a OR b"
19| "operate Xor a b = a XOR b"
20| "operate Shl a b = push_bit (unat b mod 64) a"
21| "operate Shr a b = drop_bit (unat b mod 64) a"
22| "operate Less a b = (if a < b then 1 else 0)"
23
24datatype expression = Literal pixel | Input input | Variable nat
25 | Binary operator expression expression
26 | Cond expression expression expression
27 | Let expression expression
28
29fun evaluate :: "expression \<Rightarrow> pixel list \<Rightarrow> (input \<Rightarrow> pixel) \<Rightarrow> pixel" where
30 "evaluate (Literal v) env inputs = v"
31| "evaluate (Input i) env inputs = inputs i"
32| "evaluate (Variable i) env inputs = env ! i"
33| "evaluate (Binary op a b) env inputs = operate op (evaluate a env inputs) (evaluate b env inputs)"
34| "evaluate (Cond c a b) env inputs = (if evaluate c env inputs = 0 then evaluate b env inputs else evaluate a env inputs)"
35| "evaluate (Let a b) env inputs = evaluate b (evaluate a env inputs # env) inputs"
36
37fun scoped :: "nat \<Rightarrow> expression \<Rightarrow> bool" where
38 "scoped n (Literal v) = True"
39| "scoped n (Input i) = True"
40| "scoped n (Variable i) = (i < n)"
41| "scoped n (Binary op a b) = (scoped n a \<and> scoped n b)"
42| "scoped n (Cond c a b) = (scoped n c \<and> scoped n a \<and> scoped n b)"
43| "scoped n (Let a b) = (scoped n a \<and> scoped (Suc n) b)"
44
45fun fold_binary :: "operator \<Rightarrow> expression \<Rightarrow> expression \<Rightarrow> expression" where
46 "fold_binary op (Literal a) (Literal b) = Literal (operate op a b)"
47| "fold_binary op a b = Binary op a b"
48
49fun fold_cond :: "expression \<Rightarrow> expression \<Rightarrow> expression \<Rightarrow> expression" where
50 "fold_cond (Literal c) a b = (if c = 0 then b else a)"
51| "fold_cond c a b = Cond c a b"
52
53fun optimise :: "expression \<Rightarrow> expression" where
54 "optimise (Binary op a b) = fold_binary op (optimise a) (optimise b)"
55| "optimise (Cond c a b) = fold_cond (optimise c) (optimise a) (optimise b)"
56| "optimise (Let a b) = Let (optimise a) (optimise b)"
57| "optimise e = e"
58
59lemma fold_binary_value:
60 "evaluate (fold_binary op a b) env inputs = operate op (evaluate a env inputs) (evaluate b env inputs)"
61 by (cases a; cases b) auto
62
63lemma fold_cond_value:
64 "evaluate (fold_cond c a b) env inputs = (if evaluate c env inputs = 0 then evaluate b env inputs else evaluate a env inputs)"
65 by (cases c) auto
66
67theorem optimise_correct:
68 "evaluate (optimise e) env inputs = evaluate e env inputs"
69 by (induction e arbitrary: env) (simp_all add: fold_binary_value fold_cond_value)
70
71lemma fold_binary_scoped:
72 "scoped n a \<Longrightarrow> scoped n b \<Longrightarrow> scoped n (fold_binary op a b)"
73 by (cases a; cases b) auto
74
75lemma fold_cond_scoped:
76 "scoped n c \<Longrightarrow> scoped n a \<Longrightarrow> scoped n b \<Longrightarrow> scoped n (fold_cond c a b)"
77 by (cases c) auto
78
79theorem optimise_scoped: "scoped n e \<Longrightarrow> scoped n (optimise e)"
80 by (induction e arbitrary: n) (auto intro: fold_binary_scoped fold_cond_scoped)
81
82fun nodes :: "expression \<Rightarrow> nat" where
83 "nodes (Binary op a b) = Suc (nodes a + nodes b)"
84| "nodes (Cond c a b) = Suc (nodes c + nodes a + nodes b)"
85| "nodes (Let a b) = Suc (nodes a + nodes b)"
86| "nodes _ = 1"
87
88lemma fold_binary_nodes: "nodes (fold_binary op a b) \<le> Suc (nodes a + nodes b)"
89 by (cases a; cases b) auto
90
91lemma fold_cond_nodes: "nodes (fold_cond c a b) \<le> Suc (nodes c + nodes a + nodes b)"
92 by (cases c) auto
93
94theorem optimise_size: "nodes (optimise e) \<le> nodes e"
95 by (induction e) (auto intro: order_trans[OF fold_binary_nodes] order_trans[OF fold_cond_nodes])
96
97end