src/HOL/Predicate_Compile_Examples/Specialisation_Examples.thy
author wenzelm
Fri Dec 17 17:43:54 2010 +0100 (2010-12-17)
changeset 41229 d797baa3d57c
parent 40054 cd7b1fa20bce
child 41413 64cd30d6b0b8
permissions -rw-r--r--
replaced command 'nonterminals' by slightly modernized version 'nonterminal';
     1 theory Specialisation_Examples
     2 imports Main Predicate_Compile_Alternative_Defs
     3 begin
     4 
     5 section {* Specialisation Examples *}
     6 
     7 primrec nth_el'
     8 where
     9   "nth_el' [] i = None"
    10 | "nth_el' (x # xs) i = (case i of 0 => Some x | Suc j => nth_el' xs j)"
    11 
    12 definition
    13   "greater_than_index xs = (\<forall>i x. nth_el' xs i = Some x --> x > i)"
    14 
    15 code_pred (expected_modes: i => bool) [inductify, skip_proof, specialise] greater_than_index .
    16 ML {* Core_Data.intros_of @{context} @{const_name specialised_nth_el'P} *}
    17 
    18 thm greater_than_index.equation
    19 
    20 values [expected "{()}"] "{x. greater_than_index [1,2,4,6]}"
    21 values [expected "{}"] "{x. greater_than_index [0,2,3,2]}"
    22 
    23 subsection {* Common subterms *}
    24 
    25 text {* If a predicate is called with common subterms as arguments,
    26   this predicate should be specialised. 
    27 *}
    28 
    29 definition max_nat :: "nat => nat => nat"
    30   where "max_nat a b = (if a <= b then b else a)"
    31 
    32 lemma [code_pred_inline]:
    33   "max = max_nat"
    34 by (simp add: fun_eq_iff max_def max_nat_def)
    35 
    36 definition
    37   "max_of_my_Suc x = max x (Suc x)"
    38 
    39 text {* In this example, max is specialised, hence the mode o => i => bool is possible *}
    40 
    41 code_pred (modes: o => i => bool) [inductify, specialise, skip_proof] max_of_my_Suc .
    42 
    43 thm max_of_my_SucP.equation
    44 
    45 ML {* Core_Data.intros_of @{context} @{const_name specialised_max_natP} *}
    46 
    47 values "{x. max_of_my_SucP x 6}"
    48 
    49 subsection {* Sorts *}
    50 
    51 declare sorted.Nil [code_pred_intro]
    52   sorted_single [code_pred_intro]
    53   sorted_many [code_pred_intro]
    54 
    55 code_pred sorted proof -
    56   assume "sorted xa"
    57   assume 1: "xa = [] \<Longrightarrow> thesis"
    58   assume 2: "\<And>x. xa = [x] \<Longrightarrow> thesis"
    59   assume 3: "\<And>x y zs. xa = x # y # zs \<Longrightarrow> x \<le> y \<Longrightarrow> sorted (y # zs) \<Longrightarrow> thesis"
    60   show thesis proof (cases xa)
    61     case Nil with 1 show ?thesis .
    62   next
    63     case (Cons x xs) show ?thesis proof (cases xs)
    64       case Nil with Cons 2 show ?thesis by simp
    65     next
    66       case (Cons y zs) with `xa = x # xs` have "xa = x # y # zs" by simp
    67       moreover with `sorted xa` have "x \<le> y" and "sorted (y # zs)" by simp_all
    68       ultimately show ?thesis by (rule 3)
    69     qed
    70   qed
    71 qed
    72 thm sorted.equation
    73 
    74 section {* Specialisation in POPLmark theory *}
    75 
    76 notation
    77   Some ("\<lfloor>_\<rfloor>")
    78 
    79 notation
    80   None ("\<bottom>")
    81 
    82 notation
    83   length ("\<parallel>_\<parallel>")
    84 
    85 notation
    86   Cons ("_ \<Colon>/ _" [66, 65] 65)
    87 
    88 primrec
    89   nth_el :: "'a list \<Rightarrow> nat \<Rightarrow> 'a option" ("_\<langle>_\<rangle>" [90, 0] 91)
    90 where
    91   "[]\<langle>i\<rangle> = \<bottom>"
    92 | "(x # xs)\<langle>i\<rangle> = (case i of 0 \<Rightarrow> \<lfloor>x\<rfloor> | Suc j \<Rightarrow> xs \<langle>j\<rangle>)"
    93 
    94 primrec assoc :: "('a \<times> 'b) list \<Rightarrow> 'a \<Rightarrow> 'b option" ("_\<langle>_\<rangle>\<^isub>?" [90, 0] 91)
    95 where
    96   "[]\<langle>a\<rangle>\<^isub>? = \<bottom>"
    97 | "(x # xs)\<langle>a\<rangle>\<^isub>? = (if fst x = a then \<lfloor>snd x\<rfloor> else xs\<langle>a\<rangle>\<^isub>?)"
    98 
    99 primrec unique :: "('a \<times> 'b) list \<Rightarrow> bool"
   100 where
   101   "unique [] = True"
   102 | "unique (x # xs) = (xs\<langle>fst x\<rangle>\<^isub>? = \<bottom> \<and> unique xs)"
   103 
   104 datatype type =
   105     TVar nat
   106   | Top
   107   | Fun type type    (infixr "\<rightarrow>" 200)
   108   | TyAll type type  ("(3\<forall><:_./ _)" [0, 10] 10)
   109 
   110 datatype binding = VarB type | TVarB type
   111 types env = "binding list"
   112 
   113 primrec is_TVarB :: "binding \<Rightarrow> bool"
   114 where
   115   "is_TVarB (VarB T) = False"
   116 | "is_TVarB (TVarB T) = True"
   117 
   118 primrec type_ofB :: "binding \<Rightarrow> type"
   119 where
   120   "type_ofB (VarB T) = T"
   121 | "type_ofB (TVarB T) = T"
   122 
   123 primrec mapB :: "(type \<Rightarrow> type) \<Rightarrow> binding \<Rightarrow> binding"
   124 where
   125   "mapB f (VarB T) = VarB (f T)"
   126 | "mapB f (TVarB T) = TVarB (f T)"
   127 
   128 datatype trm =
   129     Var nat
   130   | Abs type trm   ("(3\<lambda>:_./ _)" [0, 10] 10)
   131   | TAbs type trm  ("(3\<lambda><:_./ _)" [0, 10] 10)
   132   | App trm trm    (infixl "\<bullet>" 200)
   133   | TApp trm type  (infixl "\<bullet>\<^isub>\<tau>" 200)
   134 
   135 primrec liftT :: "nat \<Rightarrow> nat \<Rightarrow> type \<Rightarrow> type" ("\<up>\<^isub>\<tau>")
   136 where
   137   "\<up>\<^isub>\<tau> n k (TVar i) = (if i < k then TVar i else TVar (i + n))"
   138 | "\<up>\<^isub>\<tau> n k Top = Top"
   139 | "\<up>\<^isub>\<tau> n k (T \<rightarrow> U) = \<up>\<^isub>\<tau> n k T \<rightarrow> \<up>\<^isub>\<tau> n k U"
   140 | "\<up>\<^isub>\<tau> n k (\<forall><:T. U) = (\<forall><:\<up>\<^isub>\<tau> n k T. \<up>\<^isub>\<tau> n (k + 1) U)"
   141 
   142 primrec lift :: "nat \<Rightarrow> nat \<Rightarrow> trm \<Rightarrow> trm" ("\<up>")
   143 where
   144   "\<up> n k (Var i) = (if i < k then Var i else Var (i + n))"
   145 | "\<up> n k (\<lambda>:T. t) = (\<lambda>:\<up>\<^isub>\<tau> n k T. \<up> n (k + 1) t)"
   146 | "\<up> n k (\<lambda><:T. t) = (\<lambda><:\<up>\<^isub>\<tau> n k T. \<up> n (k + 1) t)"
   147 | "\<up> n k (s \<bullet> t) = \<up> n k s \<bullet> \<up> n k t"
   148 | "\<up> n k (t \<bullet>\<^isub>\<tau> T) = \<up> n k t \<bullet>\<^isub>\<tau> \<up>\<^isub>\<tau> n k T"
   149 
   150 primrec substTT :: "type \<Rightarrow> nat \<Rightarrow> type \<Rightarrow> type"  ("_[_ \<mapsto>\<^isub>\<tau> _]\<^isub>\<tau>" [300, 0, 0] 300)
   151 where
   152   "(TVar i)[k \<mapsto>\<^isub>\<tau> S]\<^isub>\<tau> =
   153      (if k < i then TVar (i - 1) else if i = k then \<up>\<^isub>\<tau> k 0 S else TVar i)"
   154 | "Top[k \<mapsto>\<^isub>\<tau> S]\<^isub>\<tau> = Top"
   155 | "(T \<rightarrow> U)[k \<mapsto>\<^isub>\<tau> S]\<^isub>\<tau> = T[k \<mapsto>\<^isub>\<tau> S]\<^isub>\<tau> \<rightarrow> U[k \<mapsto>\<^isub>\<tau> S]\<^isub>\<tau>"
   156 | "(\<forall><:T. U)[k \<mapsto>\<^isub>\<tau> S]\<^isub>\<tau> = (\<forall><:T[k \<mapsto>\<^isub>\<tau> S]\<^isub>\<tau>. U[k+1 \<mapsto>\<^isub>\<tau> S]\<^isub>\<tau>)"
   157 
   158 primrec decT :: "nat \<Rightarrow> nat \<Rightarrow> type \<Rightarrow> type"  ("\<down>\<^isub>\<tau>")
   159 where
   160   "\<down>\<^isub>\<tau> 0 k T = T"
   161 | "\<down>\<^isub>\<tau> (Suc n) k T = \<down>\<^isub>\<tau> n k (T[k \<mapsto>\<^isub>\<tau> Top]\<^isub>\<tau>)"
   162 
   163 primrec subst :: "trm \<Rightarrow> nat \<Rightarrow> trm \<Rightarrow> trm"  ("_[_ \<mapsto> _]" [300, 0, 0] 300)
   164 where
   165   "(Var i)[k \<mapsto> s] = (if k < i then Var (i - 1) else if i = k then \<up> k 0 s else Var i)"
   166 | "(t \<bullet> u)[k \<mapsto> s] = t[k \<mapsto> s] \<bullet> u[k \<mapsto> s]"
   167 | "(t \<bullet>\<^isub>\<tau> T)[k \<mapsto> s] = t[k \<mapsto> s] \<bullet>\<^isub>\<tau> \<down>\<^isub>\<tau> 1 k T"
   168 | "(\<lambda>:T. t)[k \<mapsto> s] = (\<lambda>:\<down>\<^isub>\<tau> 1 k T. t[k+1 \<mapsto> s])"
   169 | "(\<lambda><:T. t)[k \<mapsto> s] = (\<lambda><:\<down>\<^isub>\<tau> 1 k T. t[k+1 \<mapsto> s])"
   170 
   171 primrec substT :: "trm \<Rightarrow> nat \<Rightarrow> type \<Rightarrow> trm"    ("_[_ \<mapsto>\<^isub>\<tau> _]" [300, 0, 0] 300)
   172 where
   173   "(Var i)[k \<mapsto>\<^isub>\<tau> S] = (if k < i then Var (i - 1) else Var i)"
   174 | "(t \<bullet> u)[k \<mapsto>\<^isub>\<tau> S] = t[k \<mapsto>\<^isub>\<tau> S] \<bullet> u[k \<mapsto>\<^isub>\<tau> S]"
   175 | "(t \<bullet>\<^isub>\<tau> T)[k \<mapsto>\<^isub>\<tau> S] = t[k \<mapsto>\<^isub>\<tau> S] \<bullet>\<^isub>\<tau> T[k \<mapsto>\<^isub>\<tau> S]\<^isub>\<tau>"
   176 | "(\<lambda>:T. t)[k \<mapsto>\<^isub>\<tau> S] = (\<lambda>:T[k \<mapsto>\<^isub>\<tau> S]\<^isub>\<tau>. t[k+1 \<mapsto>\<^isub>\<tau> S])"
   177 | "(\<lambda><:T. t)[k \<mapsto>\<^isub>\<tau> S] = (\<lambda><:T[k \<mapsto>\<^isub>\<tau> S]\<^isub>\<tau>. t[k+1 \<mapsto>\<^isub>\<tau> S])"
   178 
   179 primrec liftE :: "nat \<Rightarrow> nat \<Rightarrow> env \<Rightarrow> env" ("\<up>\<^isub>e")
   180 where
   181   "\<up>\<^isub>e n k [] = []"
   182 | "\<up>\<^isub>e n k (B \<Colon> \<Gamma>) = mapB (\<up>\<^isub>\<tau> n (k + \<parallel>\<Gamma>\<parallel>)) B \<Colon> \<up>\<^isub>e n k \<Gamma>"
   183 
   184 primrec substE :: "env \<Rightarrow> nat \<Rightarrow> type \<Rightarrow> env"  ("_[_ \<mapsto>\<^isub>\<tau> _]\<^isub>e" [300, 0, 0] 300)
   185 where
   186   "[][k \<mapsto>\<^isub>\<tau> T]\<^isub>e = []"
   187 | "(B \<Colon> \<Gamma>)[k \<mapsto>\<^isub>\<tau> T]\<^isub>e = mapB (\<lambda>U. U[k + \<parallel>\<Gamma>\<parallel> \<mapsto>\<^isub>\<tau> T]\<^isub>\<tau>) B \<Colon> \<Gamma>[k \<mapsto>\<^isub>\<tau> T]\<^isub>e"
   188 
   189 primrec decE :: "nat \<Rightarrow> nat \<Rightarrow> env \<Rightarrow> env"  ("\<down>\<^isub>e")
   190 where
   191   "\<down>\<^isub>e 0 k \<Gamma> = \<Gamma>"
   192 | "\<down>\<^isub>e (Suc n) k \<Gamma> = \<down>\<^isub>e n k (\<Gamma>[k \<mapsto>\<^isub>\<tau> Top]\<^isub>e)"
   193 
   194 inductive
   195   well_formed :: "env \<Rightarrow> type \<Rightarrow> bool"  ("_ \<turnstile>\<^bsub>wf\<^esub> _" [50, 50] 50)
   196 where
   197   wf_TVar: "\<Gamma>\<langle>i\<rangle> = \<lfloor>TVarB T\<rfloor> \<Longrightarrow> \<Gamma> \<turnstile>\<^bsub>wf\<^esub> TVar i"
   198 | wf_Top: "\<Gamma> \<turnstile>\<^bsub>wf\<^esub> Top"
   199 | wf_arrow: "\<Gamma> \<turnstile>\<^bsub>wf\<^esub> T \<Longrightarrow> \<Gamma> \<turnstile>\<^bsub>wf\<^esub> U \<Longrightarrow> \<Gamma> \<turnstile>\<^bsub>wf\<^esub> T \<rightarrow> U"
   200 | wf_all: "\<Gamma> \<turnstile>\<^bsub>wf\<^esub> T \<Longrightarrow> TVarB T \<Colon> \<Gamma> \<turnstile>\<^bsub>wf\<^esub> U \<Longrightarrow> \<Gamma> \<turnstile>\<^bsub>wf\<^esub> (\<forall><:T. U)"
   201 
   202 inductive
   203   well_formedE :: "env \<Rightarrow> bool"  ("_ \<turnstile>\<^bsub>wf\<^esub>" [50] 50)
   204   and well_formedB :: "env \<Rightarrow> binding \<Rightarrow> bool"  ("_ \<turnstile>\<^bsub>wfB\<^esub> _" [50, 50] 50)
   205 where
   206   "\<Gamma> \<turnstile>\<^bsub>wfB\<^esub> B \<equiv> \<Gamma> \<turnstile>\<^bsub>wf\<^esub> type_ofB B"
   207 | wf_Nil: "[] \<turnstile>\<^bsub>wf\<^esub>"
   208 | wf_Cons: "\<Gamma> \<turnstile>\<^bsub>wfB\<^esub> B \<Longrightarrow> \<Gamma> \<turnstile>\<^bsub>wf\<^esub> \<Longrightarrow> B \<Colon> \<Gamma> \<turnstile>\<^bsub>wf\<^esub>"
   209 
   210 inductive_cases well_formed_cases:
   211   "\<Gamma> \<turnstile>\<^bsub>wf\<^esub> TVar i"
   212   "\<Gamma> \<turnstile>\<^bsub>wf\<^esub> Top"
   213   "\<Gamma> \<turnstile>\<^bsub>wf\<^esub> T \<rightarrow> U"
   214   "\<Gamma> \<turnstile>\<^bsub>wf\<^esub> (\<forall><:T. U)"
   215 
   216 inductive_cases well_formedE_cases:
   217   "B \<Colon> \<Gamma> \<turnstile>\<^bsub>wf\<^esub>"
   218 
   219 inductive
   220   subtyping :: "env \<Rightarrow> type \<Rightarrow> type \<Rightarrow> bool"  ("_ \<turnstile> _ <: _" [50, 50, 50] 50)
   221 where
   222   SA_Top: "\<Gamma> \<turnstile>\<^bsub>wf\<^esub> \<Longrightarrow> \<Gamma> \<turnstile>\<^bsub>wf\<^esub> S \<Longrightarrow> \<Gamma> \<turnstile> S <: Top"
   223 | SA_refl_TVar: "\<Gamma> \<turnstile>\<^bsub>wf\<^esub> \<Longrightarrow> \<Gamma> \<turnstile>\<^bsub>wf\<^esub> TVar i \<Longrightarrow> \<Gamma> \<turnstile> TVar i <: TVar i"
   224 | SA_trans_TVar: "\<Gamma>\<langle>i\<rangle> = \<lfloor>TVarB U\<rfloor> \<Longrightarrow>
   225     \<Gamma> \<turnstile> \<up>\<^isub>\<tau> (Suc i) 0 U <: T \<Longrightarrow> \<Gamma> \<turnstile> TVar i <: T"
   226 | SA_arrow: "\<Gamma> \<turnstile> T\<^isub>1 <: S\<^isub>1 \<Longrightarrow> \<Gamma> \<turnstile> S\<^isub>2 <: T\<^isub>2 \<Longrightarrow> \<Gamma> \<turnstile> S\<^isub>1 \<rightarrow> S\<^isub>2 <: T\<^isub>1 \<rightarrow> T\<^isub>2"
   227 | SA_all: "\<Gamma> \<turnstile> T\<^isub>1 <: S\<^isub>1 \<Longrightarrow> TVarB T\<^isub>1 \<Colon> \<Gamma> \<turnstile> S\<^isub>2 <: T\<^isub>2 \<Longrightarrow>
   228     \<Gamma> \<turnstile> (\<forall><:S\<^isub>1. S\<^isub>2) <: (\<forall><:T\<^isub>1. T\<^isub>2)"
   229 
   230 inductive
   231   typing :: "env \<Rightarrow> trm \<Rightarrow> type \<Rightarrow> bool"    ("_ \<turnstile> _ : _" [50, 50, 50] 50)
   232 where
   233   T_Var: "\<Gamma> \<turnstile>\<^bsub>wf\<^esub> \<Longrightarrow> \<Gamma>\<langle>i\<rangle> = \<lfloor>VarB U\<rfloor> \<Longrightarrow> T = \<up>\<^isub>\<tau> (Suc i) 0 U \<Longrightarrow> \<Gamma> \<turnstile> Var i : T"
   234 | T_Abs: "VarB T\<^isub>1 \<Colon> \<Gamma> \<turnstile> t\<^isub>2 : T\<^isub>2 \<Longrightarrow> \<Gamma> \<turnstile> (\<lambda>:T\<^isub>1. t\<^isub>2) : T\<^isub>1 \<rightarrow> \<down>\<^isub>\<tau> 1 0 T\<^isub>2"
   235 | T_App: "\<Gamma> \<turnstile> t\<^isub>1 : T\<^isub>11 \<rightarrow> T\<^isub>12 \<Longrightarrow> \<Gamma> \<turnstile> t\<^isub>2 : T\<^isub>11 \<Longrightarrow> \<Gamma> \<turnstile> t\<^isub>1 \<bullet> t\<^isub>2 : T\<^isub>12"
   236 | T_TAbs: "TVarB T\<^isub>1 \<Colon> \<Gamma> \<turnstile> t\<^isub>2 : T\<^isub>2 \<Longrightarrow> \<Gamma> \<turnstile> (\<lambda><:T\<^isub>1. t\<^isub>2) : (\<forall><:T\<^isub>1. T\<^isub>2)"
   237 | T_TApp: "\<Gamma> \<turnstile> t\<^isub>1 : (\<forall><:T\<^isub>11. T\<^isub>12) \<Longrightarrow> \<Gamma> \<turnstile> T\<^isub>2 <: T\<^isub>11 \<Longrightarrow>
   238     \<Gamma> \<turnstile> t\<^isub>1 \<bullet>\<^isub>\<tau> T\<^isub>2 : T\<^isub>12[0 \<mapsto>\<^isub>\<tau> T\<^isub>2]\<^isub>\<tau>"
   239 | T_Sub: "\<Gamma> \<turnstile> t : S \<Longrightarrow> \<Gamma> \<turnstile> S <: T \<Longrightarrow> \<Gamma> \<turnstile> t : T"
   240 
   241 code_pred [inductify, skip_proof, specialise] typing .
   242 
   243 thm typing.equation
   244 
   245 values 6 "{(E, t, T). typing E t T}"
   246 
   247 subsection {* Higher-order predicate *}
   248 
   249 code_pred [inductify] mapB .
   250 
   251 subsection {* Multiple instances *}
   252 
   253 inductive subtype_refl' where
   254   "\<Gamma> \<turnstile> t : T ==> \<not> (\<Gamma> \<turnstile> T <: T) ==> subtype_refl' t T"
   255 
   256 code_pred (modes: i => i => bool, o => i => bool, i => o => bool, o => o => bool) [inductify] subtype_refl' .
   257 
   258 thm subtype_refl'.equation
   259 
   260 
   261 end