diff --git a/test/oracle/Oracle_Data.thy b/test/oracle/Oracle_Data.thy index ebaba0b..596883a 100644 --- a/test/oracle/Oracle_Data.thy +++ b/test/oracle/Oracle_Data.thy @@ -91,6 +91,150 @@ python_check r_eq: "(\px = 1, py = 2\ = \px = 1, py = (2::i python_check r_ext: "col (\px = 1, py = 2, col = True\\py := 3\)" python_check r_ext_px: "px (\px = 1, py = 2, col = True\\py := 3\)" +section \Mutually recursive datatypes\ + +text \Arithmetic and boolean expressions, each embedding the other.\ + +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 \ (nat \ int) \ int" and bval :: "bexp \ (nat \ int) \ 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 = (\ bval b s)" +| "bval (And b1 b2) s = (bval b1 s \ bval b2 s)" + +text \Constant folding: rebuilds values of both types.\ + +fun asimp :: "aexp \ aexp" and bsimp :: "bexp \ bexp" where + "asimp (Plus a b) = (case (asimp a, asimp b) of + (N x, N y) \ N (x + y) | (a', b') \ Plus a' b')" +| "asimp (Times a b) = (case (asimp a, asimp b) of + (N x, N y) \ N (x * y) | (a', b') \ Times a' b')" +| "asimp (Ite c a b) = (case bsimp c of + BC True \ asimp a | BC False \ asimp b | c' \ Ite c' (asimp a) (asimp b))" +| "asimp a = a" +| "bsimp (Less a b) = (case (asimp a, asimp b) of + (N x, N y) \ BC (x < y) | (a', b') \ Less a' b')" +| "bsimp (Not b) = (case bsimp b of BC v \ BC (\ v) | b' \ Not b')" +| "bsimp (And b1 b2) = (case (bsimp b1, bsimp b2) of + (BC False, _) \ BC False | (BC True, b') \ b' | (b', BC True) \ b' + | (b1', b2') \ And b1' b2')" +| "bsimp b = b" + +fun avars :: "aexp \ nat list" and bvars :: "bexp \ 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 \ 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 (\x. 10)" +python_check m_bval: "bval (And (Less (Var 0) (Var 1)) (Not (BC False))) (\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 _ _ \ c | _ \ BC False" +python_check m_size: "(size ex_a, size (And (BC True) (BC False)))" + +text \A polymorphic rose tree as a tree/forest pair.\ + +datatype 'a tree = Node 'a "'a forest" +and 'a forest = FNil | FCons "'a tree" "'a forest" + +fun tsum :: "int tree \ int" and fsum :: "int forest \ int" where + "tsum (Node x f) = x + fsum f" +| "fsum FNil = 0" +| "fsum (FCons t f) = tsum t + fsum f" + +fun tflat :: "'a tree \ 'a list" and fflat :: "'a forest \ 'a list" where + "tflat (Node x f) = x # fflat f" +| "fflat FNil = []" +| "fflat (FCons t f) = tflat t @ fflat f" + +fun tmap :: "('a \ 'b) \ 'a tree \ 'b tree" + and fmap :: "('a \ 'b) \ 'a forest \ '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 \ nat" and fdepth :: "'a forest \ nat" where + "tdepth (Node _ f) = Suc (fdepth f)" +| "fdepth FNil = 0" +| "fdepth (FCons t f) = max (tdepth t) (fdepth f)" + +text \Reverses the children at every level, via an accumulator over the forest.\ + +fun tmirror :: "'a tree \ 'a tree" and fmirror :: "'a forest \ 'a forest \ '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 \ '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 (\x. x > 2) ex_t" +python_check m_tmap_flat: "tflat (tmap (\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 (\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 \Three types in a cycle, with an option and a list in between.\ + +datatype ta = A0 | A1 nat tb +and tb = B "tc option" +and tc = C "ta list" + +fun ca :: "ta \ nat" and cb :: "tb \ nat" and cc :: "tc \ nat" and cl :: "ta list \ 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: "\n. ca (A1 n (B (Some (C (replicate n A0)))))" over "[0, 1, 3]" + section \Immutable arrays\ python_check ia_sub: "IArray [1::int, 2, 3] !! 1"