src/HOL/Predicate_Compile_Examples/Lambda_Example.thy
author hoelzl
Mon, 03 Dec 2012 18:19:08 +0100
changeset 50328 25b1e8686ce0
parent 43937 768c70befd59
child 52666 391913d17d15
permissions -rw-r--r--
tuned proof
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
38948
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
     1
theory Lambda_Example
41956
c15ef1b85035 modernized imports (untested!?);
wenzelm
parents: 40924
diff changeset
     2
imports "~~/src/HOL/Library/Code_Prolog"
38948
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
     3
begin
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
     4
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
     5
subsection {* Lambda *}
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
     6
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
     7
datatype type =
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
     8
    Atom nat
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
     9
  | Fun type type    (infixr "\<Rightarrow>" 200)
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    10
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    11
datatype dB =
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    12
    Var nat
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    13
  | App dB dB (infixl "\<degree>" 200)
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    14
  | Abs type dB
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    15
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    16
primrec
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    17
  nth_el :: "'a list \<Rightarrow> nat \<Rightarrow> 'a option" ("_\<langle>_\<rangle>" [90, 0] 91)
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    18
where
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    19
  "[]\<langle>i\<rangle> = None"
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    20
| "(x # xs)\<langle>i\<rangle> = (case i of 0 \<Rightarrow> Some x | Suc j \<Rightarrow> xs \<langle>j\<rangle>)"
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    21
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    22
inductive nth_el1 :: "'a list \<Rightarrow> nat \<Rightarrow> 'a \<Rightarrow> bool"
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    23
where
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    24
  "nth_el1 (x # xs) 0 x"
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    25
| "nth_el1 xs i y \<Longrightarrow> nth_el1 (x # xs) (Suc i) y"
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    26
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    27
inductive typing :: "type list \<Rightarrow> dB \<Rightarrow> type \<Rightarrow> bool"  ("_ \<turnstile> _ : _" [50, 50, 50] 50)
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    28
  where
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    29
    Var [intro!]: "nth_el1 env x T \<Longrightarrow> env \<turnstile> Var x : T"
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    30
  | Abs [intro!]: "T # env \<turnstile> t : U \<Longrightarrow> env \<turnstile> Abs T t : (T \<Rightarrow> U)"
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    31
  | App [intro!]: "env \<turnstile> s : U \<Rightarrow> T \<Longrightarrow> env \<turnstile> t : T \<Longrightarrow> env \<turnstile> (s \<degree> t) : U"
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    32
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    33
primrec
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    34
  lift :: "[dB, nat] => dB"
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    35
where
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    36
    "lift (Var i) k = (if i < k then Var i else Var (i + 1))"
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    37
  | "lift (s \<degree> t) k = lift s k \<degree> lift t k"
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    38
  | "lift (Abs T s) k = Abs T (lift s (k + 1))"
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    39
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    40
primrec pred :: "nat => nat"
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    41
where
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    42
  "pred (Suc i) = i"
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    43
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    44
primrec
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    45
  subst :: "[dB, dB, nat] => dB"  ("_[_'/_]" [300, 0, 0] 300)
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    46
where
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    47
    subst_Var: "(Var i)[s/k] =
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    48
      (if k < i then Var (pred i) else if i = k then s else Var i)"
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    49
  | subst_App: "(t \<degree> u)[s/k] = t[s/k] \<degree> u[s/k]"
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    50
  | subst_Abs: "(Abs T t)[s/k] = Abs T (t[lift s 0 / k+1])"
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    51
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    52
inductive beta :: "[dB, dB] => bool"  (infixl "\<rightarrow>\<^sub>\<beta>" 50)
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    53
  where
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    54
    beta [simp, intro!]: "Abs T s \<degree> t \<rightarrow>\<^sub>\<beta> s[t/0]"
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    55
  | appL [simp, intro!]: "s \<rightarrow>\<^sub>\<beta> t ==> s \<degree> u \<rightarrow>\<^sub>\<beta> t \<degree> u"
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    56
  | appR [simp, intro!]: "s \<rightarrow>\<^sub>\<beta> t ==> u \<degree> s \<rightarrow>\<^sub>\<beta> u \<degree> t"
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    57
  | abs [simp, intro!]: "s \<rightarrow>\<^sub>\<beta> t ==> Abs T s \<rightarrow>\<^sub>\<beta> Abs T t"
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    58
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    59
subsection {* Inductive definitions for ordering on naturals *}
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    60
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    61
inductive less_nat
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    62
where
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    63
  "less_nat 0 (Suc y)"
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    64
| "less_nat x y ==> less_nat (Suc x) (Suc y)"
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    65
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    66
lemma less_nat[code_pred_inline]:
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    67
  "x < y = less_nat x y"
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    68
apply (rule iffI)
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    69
apply (induct x arbitrary: y)
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    70
apply (case_tac y) apply (auto intro: less_nat.intros)
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    71
apply (case_tac y)
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    72
apply (auto intro: less_nat.intros)
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    73
apply (induct rule: less_nat.induct)
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    74
apply auto
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    75
done
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    76
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    77
lemma [code_pred_inline]: "(x::nat) + 1 = Suc x"
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    78
by simp
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    79
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    80
section {* Manual setup to find counterexample *}
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    81
43937
768c70befd59 adapting two examples in Predicate_Compile_Examples
bulwahn
parents: 41956
diff changeset
    82
setup {* Context.theory_map (Quickcheck.add_tester ("prolog", (Code_Prolog.active, Code_Prolog.test_goals))) *}
38948
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    83
38950
62578950e748 storing options for prolog code generation in the theory
bulwahn
parents: 38948
diff changeset
    84
setup {* Code_Prolog.map_code_options (K 
38948
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    85
  { ensure_groundness = true,
39800
17e29ddd538e adapting manual configuration in examples
bulwahn
parents: 39463
diff changeset
    86
    limit_globally = NONE,
38948
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    87
    limited_types = [(@{typ nat}, 1), (@{typ "type"}, 1), (@{typ dB}, 1), (@{typ "type list"}, 1)],
38963
b5d126d7be4b adapting and tuning example theories
bulwahn
parents: 38958
diff changeset
    88
    limited_predicates = [(["typing"], 2), (["nthel1"], 2)],
38948
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    89
    replacing = [(("typing", "limited_typing"), "quickcheck"),
38958
08eb0ffa2413 improving clash-free naming of variables and preds in code_prolog
bulwahn
parents: 38950
diff changeset
    90
                 (("nthel1", "limited_nthel1"), "lim_typing")],
39463
7ce0ed8dc4d6 adapting examples
bulwahn
parents: 39189
diff changeset
    91
    manual_reorder = []}) *}
38948
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    92
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    93
lemma
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    94
  "\<Gamma> \<turnstile> t : U \<Longrightarrow> t \<rightarrow>\<^sub>\<beta> t' \<Longrightarrow> \<Gamma> \<turnstile> t' : U"
40924
a9be7f26b4e6 adapting predicate_compile_quickcheck
bulwahn
parents: 39800
diff changeset
    95
quickcheck[tester = prolog, iterations = 1, expect = counterexample]
38948
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    96
oops
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    97
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    98
text {* Verifying that the found counterexample really is one by means of a proof *}
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
    99
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
   100
lemma
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
   101
assumes
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
   102
  "t' = Var 0"
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
   103
  "U = Atom 0"
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
   104
  "\<Gamma> = [Atom 1]"
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
   105
  "t = App (Abs (Atom 0) (Var 1)) (Var 0)"
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
   106
shows
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
   107
  "\<Gamma> \<turnstile> t : U"
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
   108
  "t \<rightarrow>\<^sub>\<beta> t'"
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
   109
  "\<not> \<Gamma> \<turnstile> t' : U"
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
   110
proof -
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
   111
  from assms show "\<Gamma> \<turnstile> t : U"
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
   112
    by (auto intro!: typing.intros nth_el1.intros)
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
   113
next
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
   114
  from assms have "t \<rightarrow>\<^sub>\<beta> (Var 1)[Var 0/0]"
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
   115
    by (auto simp only: intro: beta.intros)
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
   116
  moreover
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
   117
  from assms have "(Var 1)[Var 0/0] = t'" by simp
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
   118
  ultimately show "t \<rightarrow>\<^sub>\<beta> t'" by simp
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
   119
next
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
   120
  from assms show "\<not> \<Gamma> \<turnstile> t' : U"
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
   121
    by (auto elim: typing.cases nth_el1.cases)
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
   122
qed
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
   123
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
   124
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
   125
end
c4e6afaa8dcd adding Lambda example theory; tuned
bulwahn
parents:
diff changeset
   126