Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 144 additions & 0 deletions test/oracle/Oracle_Data.thy
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,150 @@ python_check r_eq: "(\<lparr>px = 1, py = 2\<rparr> = \<lparr>px = 1, py = (2::i
python_check r_ext: "col (\<lparr>px = 1, py = 2, col = True\<rparr>\<lparr>py := 3\<rparr>)"
python_check r_ext_px: "px (\<lparr>px = 1, py = 2, col = True\<rparr>\<lparr>py := 3\<rparr>)"

section \<open>Mutually recursive datatypes\<close>

text \<open>Arithmetic and boolean expressions, each embedding the other.\<close>

datatype aexp = N int | Var nat | Plus aexp aexp | Times aexp aexp | Ite bexp aexp aexp
and bexp = BC bool | Less aexp aexp | Not bexp | And bexp bexp

fun aval :: "aexp \<Rightarrow> (nat \<Rightarrow> int) \<Rightarrow> int" and bval :: "bexp \<Rightarrow> (nat \<Rightarrow> int) \<Rightarrow> bool" where
"aval (N n) s = n"
| "aval (Var x) s = s x"
| "aval (Plus a b) s = aval a s + aval b s"
| "aval (Times a b) s = aval a s * aval b s"
| "aval (Ite c a b) s = (if bval c s then aval a s else aval b s)"
| "bval (BC v) s = v"
| "bval (Less a b) s = (aval a s < aval b s)"
| "bval (Not b) s = (\<not> bval b s)"
| "bval (And b1 b2) s = (bval b1 s \<and> bval b2 s)"

text \<open>Constant folding: rebuilds values of both types.\<close>

fun asimp :: "aexp \<Rightarrow> aexp" and bsimp :: "bexp \<Rightarrow> bexp" where
"asimp (Plus a b) = (case (asimp a, asimp b) of
(N x, N y) \<Rightarrow> N (x + y) | (a', b') \<Rightarrow> Plus a' b')"
| "asimp (Times a b) = (case (asimp a, asimp b) of
(N x, N y) \<Rightarrow> N (x * y) | (a', b') \<Rightarrow> Times a' b')"
| "asimp (Ite c a b) = (case bsimp c of
BC True \<Rightarrow> asimp a | BC False \<Rightarrow> asimp b | c' \<Rightarrow> Ite c' (asimp a) (asimp b))"
| "asimp a = a"
| "bsimp (Less a b) = (case (asimp a, asimp b) of
(N x, N y) \<Rightarrow> BC (x < y) | (a', b') \<Rightarrow> Less a' b')"
| "bsimp (Not b) = (case bsimp b of BC v \<Rightarrow> BC (\<not> v) | b' \<Rightarrow> Not b')"
| "bsimp (And b1 b2) = (case (bsimp b1, bsimp b2) of
(BC False, _) \<Rightarrow> BC False | (BC True, b') \<Rightarrow> b' | (b', BC True) \<Rightarrow> b'
| (b1', b2') \<Rightarrow> And b1' b2')"
| "bsimp b = b"

fun avars :: "aexp \<Rightarrow> nat list" and bvars :: "bexp \<Rightarrow> nat list" where
"avars (N _) = []"
| "avars (Var x) = [x]"
| "avars (Plus a b) = avars a @ avars b"
| "avars (Times a b) = avars a @ avars b"
| "avars (Ite c a b) = bvars c @ avars a @ avars b"
| "bvars (BC _) = []"
| "bvars (Less a b) = avars a @ avars b"
| "bvars (Not b) = bvars b"
| "bvars (And b1 b2) = bvars b1 @ bvars b2"

definition ex_a :: aexp where
"ex_a = Ite (And (Less (Var 0) (N 5)) (Not (BC False)))
(Plus (Var 0) (Times (N 2) (Var 1)))
(Ite (Less (N 1) (N 0)) (N 7) (Var 1))"

definition ex_s :: "nat \<Rightarrow> int" where
"ex_s x = (if x = 0 then 3 else if x = 1 then -4 else 0)"

python_check m_aval: "aval ex_a ex_s"
python_check m_aval_else: "aval ex_a (\<lambda>x. 10)"
python_check m_bval: "bval (And (Less (Var 0) (Var 1)) (Not (BC False))) (\<lambda>x. int x)"
python_check m_asimp: "asimp (Plus (Times (N 2) (N 3)) (Ite (Less (N 1) (N 2)) (Var 0) (N 9)))"
python_check m_asimp_ex: "asimp ex_a"
python_check m_bsimp: "bsimp (And (Not (BC False)) (Less (Var 0) (Plus (N 1) (N 1))))"
python_check m_bsimp_short: "bsimp (And (Less (N 3) (N 2)) (Less (Var 0) (Var 1)))"
python_check m_simp_sound: "aval (asimp ex_a) ex_s = aval ex_a ex_s"
python_check m_vars: "(avars ex_a, bvars (Not (Less (Var 2) (Var 3))))"
python_check m_eq: "(asimp (Plus (N 1) (N 2)) = N 3, Not (BC True) = Not (BC False))"
python_check m_eq_cross: "(Ite (BC True) (N 1) (N 2) = Ite (BC True) (N 1) (N 2),
Ite (BC True) (N 1) (N 2) = Ite (Not (BC False)) (N 1) (N 2))"
python_check m_case: "case Ite (BC True) (N 1) (N 2) of Ite c _ _ \<Rightarrow> c | _ \<Rightarrow> BC False"
python_check m_size: "(size ex_a, size (And (BC True) (BC False)))"

text \<open>A polymorphic rose tree as a tree/forest pair.\<close>

datatype 'a tree = Node 'a "'a forest"
and 'a forest = FNil | FCons "'a tree" "'a forest"

fun tsum :: "int tree \<Rightarrow> int" and fsum :: "int forest \<Rightarrow> int" where
"tsum (Node x f) = x + fsum f"
| "fsum FNil = 0"
| "fsum (FCons t f) = tsum t + fsum f"

fun tflat :: "'a tree \<Rightarrow> 'a list" and fflat :: "'a forest \<Rightarrow> 'a list" where
"tflat (Node x f) = x # fflat f"
| "fflat FNil = []"
| "fflat (FCons t f) = tflat t @ fflat f"

fun tmap :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a tree \<Rightarrow> 'b tree"
and fmap :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a forest \<Rightarrow> 'b forest" where
"tmap g (Node x f) = Node (g x) (fmap g f)"
| "fmap g FNil = FNil"
| "fmap g (FCons t f) = FCons (tmap g t) (fmap g f)"

fun tdepth :: "'a tree \<Rightarrow> nat" and fdepth :: "'a forest \<Rightarrow> nat" where
"tdepth (Node _ f) = Suc (fdepth f)"
| "fdepth FNil = 0"
| "fdepth (FCons t f) = max (tdepth t) (fdepth f)"

text \<open>Reverses the children at every level, via an accumulator over the forest.\<close>

fun tmirror :: "'a tree \<Rightarrow> 'a tree" and fmirror :: "'a forest \<Rightarrow> 'a forest \<Rightarrow> 'a forest" where
"tmirror (Node x f) = Node x (fmirror f FNil)"
| "fmirror FNil acc = acc"
| "fmirror (FCons t f) acc = fmirror f (FCons (tmirror t) acc)"

fun forest_of :: "'a tree list \<Rightarrow> 'a forest" where
"forest_of [] = FNil"
| "forest_of (t # ts) = FCons t (forest_of ts)"

definition ex_t :: "int tree" where
"ex_t = Node 1 (forest_of [Node 2 FNil, Node 3 (forest_of [Node 4 FNil, Node (-5) FNil]), Node 6 FNil])"

python_check m_tsum: "tsum ex_t"
python_check m_tflat: "tflat ex_t"
python_check m_tmap: "tmap (\<lambda>x. x > 2) ex_t"
python_check m_tmap_flat: "tflat (tmap (\<lambda>x. STR ''n'' + (if x > 0 then STR ''+'' else STR ''-'')) ex_t)"
python_check m_tdepth: "(tdepth ex_t, tdepth (Node () FNil), fdepth (FNil :: nat forest))"
python_check m_tmirror: "tflat (tmirror ex_t)"
python_check m_tmirror_inv: "tmirror (tmirror ex_t) = ex_t"
python_check m_tmirror_neq: "tmirror ex_t = ex_t"
python_check m_forest: "fmap (\<lambda>x. [x, x]) (forest_of [Node (1::nat) FNil, Node 2 FNil])"
python_check m_tree_pair: "Node (1::int, True) (FCons (Node (2, False) FNil) FNil)"

text \<open>Three types in a cycle, with an option and a list in between.\<close>

datatype ta = A0 | A1 nat tb
and tb = B "tc option"
and tc = C "ta list"

fun ca :: "ta \<Rightarrow> nat" and cb :: "tb \<Rightarrow> nat" and cc :: "tc \<Rightarrow> nat" and cl :: "ta list \<Rightarrow> nat" where
"ca A0 = 1"
| "ca (A1 n b) = n + cb b"
| "cb (B None) = 0"
| "cb (B (Some c)) = cc c"
| "cc (C as) = cl as"
| "cl [] = 0"
| "cl (a # as) = ca a + cl as"

definition ex_ta :: ta where
"ex_ta = A1 10 (B (Some (C [A0, A1 5 (B None), A1 2 (B (Some (C [A0, A0])))])))"

python_check m_cycle: "ca ex_ta"
python_check m_cycle_val: "ex_ta"
python_check m_cycle_eq: "(ex_ta = ex_ta, B (Some (C [])) = B None)"
python_check_grid m_cycle_grid: "\<lambda>n. ca (A1 n (B (Some (C (replicate n A0)))))" over "[0, 1, 3]"

section \<open>Immutable arrays\<close>

python_check ia_sub: "IArray [1::int, 2, 3] !! 1"
Expand Down
Loading