setup\<open> let
val target = "Haskell_Quickcheck"; fun print _ = Code_Haskell.print_numeral "Prelude.Int"; in
Numeral.add_code \<^const_name>\<open>Code_Numeral.Pos\<close> I print target
#> Numeral.add_code \<^const_name>\<open>Code_Numeral.Neg\<close> (~) print target end \<close>
primrec map_cons :: "('a => 'b) => 'a narrowing_cons => 'b narrowing_cons" where "map_cons f (Narrowing_cons ty cs) = Narrowing_cons ty (map (\c. f \ c) cs)"
subsubsection \<open>From narrowing's deep representation of terms to \<^theory>\<open>HOL.Code_Evaluation\<close>'s terms\<close>
type_synonym'a narrowing = "integer => 'a narrowing_cons"
definition cons :: "'a => 'a narrowing" where "cons a d = (Narrowing_cons (Narrowing_sum_of_products [[]]) [(\_. a)])"
fun conv :: "(narrowing_term list => 'a) list => narrowing_term => 'a" where "conv cs (Narrowing_variable p _) = error (marker # map toEnum p)"
| "conv cs (Narrowing_constructor i xs) = (nth cs i) xs"
fun non_empty :: "narrowing_type => bool" where "non_empty (Narrowing_sum_of_products ps) = (\ (List.null ps))"
definition"apply" :: "('a => 'b) narrowing => 'a narrowing => 'b narrowing" where "apply f a d = (if d > 0 then
(case f d of Narrowing_cons (Narrowing_sum_of_products ps) cfs \<Rightarrow> case a (d - 1) of Narrowing_cons ta cas \<Rightarrow> let
shallow = non_empty ta;
cs = [(\<lambda>(x # xs) \<Rightarrow> cf xs (conv cas x)). shallow, cf \<leftarrow> cfs] in Narrowing_cons (Narrowing_sum_of_products [ta # p. shallow, p \<leftarrow> ps]) cs)
else Narrowing_cons (Narrowing_sum_of_products []) [])"
definition sum :: "'a narrowing => 'a narrowing => 'a narrowing" where "sum a b d =
(case a d of Narrowing_cons (Narrowing_sum_of_products ssa) ca \<Rightarrow> case b d of Narrowing_cons (Narrowing_sum_of_products ssb) cb \<Rightarrow>
Narrowing_cons (Narrowing_sum_of_products (ssa @ ssb)) (ca @ cb))"
lemma [fundef_cong]: assumes"a d = a' d""b d = b' d""d = d'" shows"sum a b d = sum a' b' d'" using assms unfolding sum_def by (auto split: narrowing_cons.split narrowing_type.split)
lemma [fundef_cong]: assumes"f d = f' d""(\d'. 0 \ d' \ d' < d \ a d' = a' d')" assumes"d = d'" shows"apply f a d = apply f' a' d'" proof - note assms moreoverhave"0 < d' \ 0 \ d' - 1" by (simp add: less_integer_def less_eq_integer_def) ultimatelyshow ?thesis by (auto simp add: apply_def Let_def
split: narrowing_cons.split narrowing_type.split) qed
subsubsection \<open>Narrowing generator type class\<close>
class narrowing = fixes narrowing :: "integer => 'a narrowing_cons"
(* FIXME: hard-wired maximal depth of 100 here *) definition exists :: "('a :: {narrowing, partial_term_of} => property) => property" where "exists f = (case narrowing (100 :: integer) of Narrowing_cons ty cs \ Existential ty (\ t. f (conv cs t)) (partial_term_of (TYPE('a))))"
definition"all" :: "('a :: {narrowing, partial_term_of} => property) => property" where "all f = (case narrowing (100 :: integer) of Narrowing_cons ty cs \ Universal ty (\t. f (conv cs t)) (partial_term_of (TYPE('a))))"
primrec eval_ffun :: "('a, 'b) ffun => 'a => 'b" where "eval_ffun (Constant c) x = c"
| "eval_ffun (Update x' y f) x = (if x = x' then y else eval_ffun f x)"
definition drawn_from :: "'a list \ 'a narrowing_cons" where "drawn_from xs =
Narrowing_cons (Narrowing_sum_of_products (map (\<lambda>_. []) xs)) (map (\<lambda>x _. x) xs)"
function around_zero :: "int \ int list" where "around_zero i = (if i < 0 then [] else (if i = 0 then [0] else around_zero (i - 1) @ [i, -i]))" by pat_completeness auto terminationby (relation "measure nat") auto
declare around_zero.simps [simp del]
lemma length_around_zero: assumes"i >= 0" shows"length (around_zero i) = 2 * nat i + 1" proof (induct rule: int_ge_induct [OF assms]) case 1 from 1 show ?caseby (simp add: around_zero.simps) next case (2 i) from 2 show ?case by (simp add: around_zero.simps [of "i + 1"]) qed
instantiation int :: narrowing begin
definition "narrowing_int d = (let (u :: _ \ _ \ unit) = conv; i = int_of_integer d in drawn_from (around_zero i))"
instance ..
end
lemma [code]: "partial_term_of (ty :: int itself) (Narrowing_variable p t) \
Code_Evaluation.Free (STR ''_'') (Typerep.Typerep (STR ''Int.int'') [])" "partial_term_of (ty :: int itself) (Narrowing_constructor i []) \
(if i mod 2 = 0 then Code_Evaluation.term_of (- (int_of_integer i) div 2)
else Code_Evaluation.term_of ((int_of_integer i + 1) div 2))" by (rule partial_term_of_anything)+
instantiation integer :: narrowing begin
definition "narrowing_integer d = (let (u :: _ \ _ \ unit) = conv; i = int_of_integer d in drawn_from (map integer_of_int (around_zero i)))"
instance ..
end
lemma [code]: "partial_term_of (ty :: integer itself) (Narrowing_variable p t) \
Code_Evaluation.Free (STR ''_'') (Typerep.Typerep (STR ''Code_Numeral.integer'') [])" "partial_term_of (ty :: integer itself) (Narrowing_constructor i []) \
(if i mod 2 = 0 then Code_Evaluation.term_of (- i div 2)
else Code_Evaluation.term_of ((i + 1) div 2))" by (rule partial_term_of_anything)+
code_printing constant "Code_Evaluation.term_of :: integer \ term" \ (Haskell_Quickcheck) "(let { t = Typerep.Typerep \"Code'_Numeral.integer\" [];
mkFunT s t = Typerep.Typerep \"fun\" [s, t];
numT = Typerep.Typerep \"Num.num\" [];
mkBit 0 = Generated'_Code.Const \"Num.num.Bit0\" (mkFunT numT numT);
mkBit 1 = Generated'_Code.Const \"Num.num.Bit1\" (mkFunT numT numT);
mkNumeral 1 = Generated'_Code.Const \"Num.num.One\" numT;
mkNumeral i = let { q = i `Prelude.div` 2; r = i `Prelude.mod` 2 } in Generated'_Code.App (mkBit r) (mkNumeral q);
mkNumber 0 = Generated'_Code.Const \"Groups.zero'_class.zero\" t;
mkNumber 1 = Generated'_Code.Const \"Groups.one'_class.one\" t;
mkNumber i = if i > 0 then
Generated'_Code.App
(Generated'_Code.Const \"Num.numeral'_class.numeral\"
(mkFunT numT t))
(mkNumeral i)
else
Generated'_Code.App
(Generated'_Code.Const \"Groups.uminus'_class.uminus\" (mkFunT t t))
(mkNumber (- i)); } in mkNumber)"
¤ Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.0.13Bemerkung:
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.