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