# HG changeset patch # User bulwahn # Date 1269876652 -7200 # Node ID 7106f079bd0544cc6f10bf8335c3ea29d8fb064d # Parent dfd30b5b4e73f4895cd675dbf321b7c2808f3b3c adding specialisation examples of the predicate compiler diff -r dfd30b5b4e73 -r 7106f079bd05 src/HOL/Predicate_Compile_Examples/ROOT.ML --- a/src/HOL/Predicate_Compile_Examples/ROOT.ML Mon Mar 29 17:30:52 2010 +0200 +++ b/src/HOL/Predicate_Compile_Examples/ROOT.ML Mon Mar 29 17:30:52 2010 +0200 @@ -1,1 +1,1 @@ -use_thys ["Predicate_Compile_Examples", "Predicate_Compile_Quickcheck_Examples"]; +use_thys ["Predicate_Compile_Examples", "Predicate_Compile_Quickcheck_Examples", "Specialisation_Examples"]; diff -r dfd30b5b4e73 -r 7106f079bd05 src/HOL/Predicate_Compile_Examples/Specialisation_Examples.thy --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/HOL/Predicate_Compile_Examples/Specialisation_Examples.thy Mon Mar 29 17:30:52 2010 +0200 @@ -0,0 +1,241 @@ +theory Specialisation_Examples +imports Main "../ex/Predicate_Compile_Alternative_Defs" +begin + +section {* Specialisation Examples *} + +fun nth_el' +where + "nth_el' [] i = None" +| "nth_el' (x # xs) i = (case i of 0 => Some x | Suc j => nth_el' xs j)" + +definition + "greater_than_index xs = (\i x. nth_el' xs i = Some x --> x > i)" + +code_pred (expected_modes: i => bool) [inductify] greater_than_index . +ML {* Predicate_Compile_Core.intros_of @{theory} @{const_name specialised_nth_el'P} *} + +thm greater_than_index.equation + +values [expected "{()}"] "{x. greater_than_index [1,2,4,6]}" +values [expected "{}"] "{x. greater_than_index [0,2,3,2]}" + +subsection {* Common subterms *} + +text {* If a predicate is called with common subterms as arguments, + this predicate should be specialised. +*} + +definition max_nat :: "nat => nat => nat" + where "max_nat a b = (if a <= b then b else a)" + +lemma [code_pred_inline]: + "max = max_nat" +by (simp add: expand_fun_eq max_def max_nat_def) + +definition + "max_of_my_Suc x = max x (Suc x)" + +text {* In this example, max is specialised, hence the mode o => i => bool is possible *} + +code_pred (modes: o => i => bool) [inductify] max_of_my_Suc . + +thm max_of_my_SucP.equation + +ML {* Predicate_Compile_Core.intros_of @{theory} @{const_name specialised_max_natP} *} + +values "{x. max_of_my_SucP x 6}" + +subsection {* Sorts *} + +code_pred [inductify] sorted . +thm sorted.equation + +section {* Specialisation in POPLmark theory *} + +notation + Some ("\_\") + +notation + None ("\") + +notation + length ("\_\") + +notation + Cons ("_ \/ _" [66, 65] 65) + +primrec + nth_el :: "'a list \ nat \ 'a option" ("_\_\" [90, 0] 91) +where + "[]\i\ = \" +| "(x # xs)\i\ = (case i of 0 \ \x\ | Suc j \ xs \j\)" + +primrec assoc :: "('a \ 'b) list \ 'a \ 'b option" ("_\_\\<^isub>?" [90, 0] 91) +where + "[]\a\\<^isub>? = \" +| "(x # xs)\a\\<^isub>? = (if fst x = a then \snd x\ else xs\a\\<^isub>?)" + +primrec unique :: "('a \ 'b) list \ bool" +where + "unique [] = True" +| "unique (x # xs) = (xs\fst x\\<^isub>? = \ \ unique xs)" + +datatype type = + TVar nat + | Top + | Fun type type (infixr "\" 200) + | TyAll type type ("(3\<:_./ _)" [0, 10] 10) + +datatype binding = VarB type | TVarB type +types env = "binding list" + +primrec is_TVarB :: "binding \ bool" +where + "is_TVarB (VarB T) = False" +| "is_TVarB (TVarB T) = True" + +primrec type_ofB :: "binding \ type" +where + "type_ofB (VarB T) = T" +| "type_ofB (TVarB T) = T" + +primrec mapB :: "(type \ type) \ binding \ binding" +where + "mapB f (VarB T) = VarB (f T)" +| "mapB f (TVarB T) = TVarB (f T)" + +datatype trm = + Var nat + | Abs type trm ("(3\:_./ _)" [0, 10] 10) + | TAbs type trm ("(3\<:_./ _)" [0, 10] 10) + | App trm trm (infixl "\" 200) + | TApp trm type (infixl "\\<^isub>\" 200) + +primrec liftT :: "nat \ nat \ type \ type" ("\\<^isub>\") +where + "\\<^isub>\ n k (TVar i) = (if i < k then TVar i else TVar (i + n))" +| "\\<^isub>\ n k Top = Top" +| "\\<^isub>\ n k (T \ U) = \\<^isub>\ n k T \ \\<^isub>\ n k U" +| "\\<^isub>\ n k (\<:T. U) = (\<:\\<^isub>\ n k T. \\<^isub>\ n (k + 1) U)" + +primrec lift :: "nat \ nat \ trm \ trm" ("\") +where + "\ n k (Var i) = (if i < k then Var i else Var (i + n))" +| "\ n k (\:T. t) = (\:\\<^isub>\ n k T. \ n (k + 1) t)" +| "\ n k (\<:T. t) = (\<:\\<^isub>\ n k T. \ n (k + 1) t)" +| "\ n k (s \ t) = \ n k s \ \ n k t" +| "\ n k (t \\<^isub>\ T) = \ n k t \\<^isub>\ \\<^isub>\ n k T" + +primrec substTT :: "type \ nat \ type \ type" ("_[_ \\<^isub>\ _]\<^isub>\" [300, 0, 0] 300) +where + "(TVar i)[k \\<^isub>\ S]\<^isub>\ = + (if k < i then TVar (i - 1) else if i = k then \\<^isub>\ k 0 S else TVar i)" +| "Top[k \\<^isub>\ S]\<^isub>\ = Top" +| "(T \ U)[k \\<^isub>\ S]\<^isub>\ = T[k \\<^isub>\ S]\<^isub>\ \ U[k \\<^isub>\ S]\<^isub>\" +| "(\<:T. U)[k \\<^isub>\ S]\<^isub>\ = (\<:T[k \\<^isub>\ S]\<^isub>\. U[k+1 \\<^isub>\ S]\<^isub>\)" + +primrec decT :: "nat \ nat \ type \ type" ("\\<^isub>\") +where + "\\<^isub>\ 0 k T = T" +| "\\<^isub>\ (Suc n) k T = \\<^isub>\ n k (T[k \\<^isub>\ Top]\<^isub>\)" + +primrec subst :: "trm \ nat \ trm \ trm" ("_[_ \ _]" [300, 0, 0] 300) +where + "(Var i)[k \ s] = (if k < i then Var (i - 1) else if i = k then \ k 0 s else Var i)" +| "(t \ u)[k \ s] = t[k \ s] \ u[k \ s]" +| "(t \\<^isub>\ T)[k \ s] = t[k \ s] \\<^isub>\ \\<^isub>\ 1 k T" +| "(\:T. t)[k \ s] = (\:\\<^isub>\ 1 k T. t[k+1 \ s])" +| "(\<:T. t)[k \ s] = (\<:\\<^isub>\ 1 k T. t[k+1 \ s])" + +primrec substT :: "trm \ nat \ type \ trm" ("_[_ \\<^isub>\ _]" [300, 0, 0] 300) +where + "(Var i)[k \\<^isub>\ S] = (if k < i then Var (i - 1) else Var i)" +| "(t \ u)[k \\<^isub>\ S] = t[k \\<^isub>\ S] \ u[k \\<^isub>\ S]" +| "(t \\<^isub>\ T)[k \\<^isub>\ S] = t[k \\<^isub>\ S] \\<^isub>\ T[k \\<^isub>\ S]\<^isub>\" +| "(\:T. t)[k \\<^isub>\ S] = (\:T[k \\<^isub>\ S]\<^isub>\. t[k+1 \\<^isub>\ S])" +| "(\<:T. t)[k \\<^isub>\ S] = (\<:T[k \\<^isub>\ S]\<^isub>\. t[k+1 \\<^isub>\ S])" + +primrec liftE :: "nat \ nat \ env \ env" ("\\<^isub>e") +where + "\\<^isub>e n k [] = []" +| "\\<^isub>e n k (B \ \) = mapB (\\<^isub>\ n (k + \\\)) B \ \\<^isub>e n k \" + +primrec substE :: "env \ nat \ type \ env" ("_[_ \\<^isub>\ _]\<^isub>e" [300, 0, 0] 300) +where + "[][k \\<^isub>\ T]\<^isub>e = []" +| "(B \ \)[k \\<^isub>\ T]\<^isub>e = mapB (\U. U[k + \\\ \\<^isub>\ T]\<^isub>\) B \ \[k \\<^isub>\ T]\<^isub>e" + +primrec decE :: "nat \ nat \ env \ env" ("\\<^isub>e") +where + "\\<^isub>e 0 k \ = \" +| "\\<^isub>e (Suc n) k \ = \\<^isub>e n k (\[k \\<^isub>\ Top]\<^isub>e)" + +inductive + well_formed :: "env \ type \ bool" ("_ \\<^bsub>wf\<^esub> _" [50, 50] 50) +where + wf_TVar: "\\i\ = \TVarB T\ \ \ \\<^bsub>wf\<^esub> TVar i" +| wf_Top: "\ \\<^bsub>wf\<^esub> Top" +| wf_arrow: "\ \\<^bsub>wf\<^esub> T \ \ \\<^bsub>wf\<^esub> U \ \ \\<^bsub>wf\<^esub> T \ U" +| wf_all: "\ \\<^bsub>wf\<^esub> T \ TVarB T \ \ \\<^bsub>wf\<^esub> U \ \ \\<^bsub>wf\<^esub> (\<:T. U)" + +inductive + well_formedE :: "env \ bool" ("_ \\<^bsub>wf\<^esub>" [50] 50) + and well_formedB :: "env \ binding \ bool" ("_ \\<^bsub>wfB\<^esub> _" [50, 50] 50) +where + "\ \\<^bsub>wfB\<^esub> B \ \ \\<^bsub>wf\<^esub> type_ofB B" +| wf_Nil: "[] \\<^bsub>wf\<^esub>" +| wf_Cons: "\ \\<^bsub>wfB\<^esub> B \ \ \\<^bsub>wf\<^esub> \ B \ \ \\<^bsub>wf\<^esub>" + +inductive_cases well_formed_cases: + "\ \\<^bsub>wf\<^esub> TVar i" + "\ \\<^bsub>wf\<^esub> Top" + "\ \\<^bsub>wf\<^esub> T \ U" + "\ \\<^bsub>wf\<^esub> (\<:T. U)" + +inductive_cases well_formedE_cases: + "B \ \ \\<^bsub>wf\<^esub>" + +inductive + subtyping :: "env \ type \ type \ bool" ("_ \ _ <: _" [50, 50, 50] 50) +where + SA_Top: "\ \\<^bsub>wf\<^esub> \ \ \\<^bsub>wf\<^esub> S \ \ \ S <: Top" +| SA_refl_TVar: "\ \\<^bsub>wf\<^esub> \ \ \\<^bsub>wf\<^esub> TVar i \ \ \ TVar i <: TVar i" +| SA_trans_TVar: "\\i\ = \TVarB U\ \ + \ \ \\<^isub>\ (Suc i) 0 U <: T \ \ \ TVar i <: T" +| SA_arrow: "\ \ T\<^isub>1 <: S\<^isub>1 \ \ \ S\<^isub>2 <: T\<^isub>2 \ \ \ S\<^isub>1 \ S\<^isub>2 <: T\<^isub>1 \ T\<^isub>2" +| SA_all: "\ \ T\<^isub>1 <: S\<^isub>1 \ TVarB T\<^isub>1 \ \ \ S\<^isub>2 <: T\<^isub>2 \ + \ \ (\<:S\<^isub>1. S\<^isub>2) <: (\<:T\<^isub>1. T\<^isub>2)" + +inductive + typing :: "env \ trm \ type \ bool" ("_ \ _ : _" [50, 50, 50] 50) +where + T_Var: "\ \\<^bsub>wf\<^esub> \ \\i\ = \VarB U\ \ T = \\<^isub>\ (Suc i) 0 U \ \ \ Var i : T" +| T_Abs: "VarB T\<^isub>1 \ \ \ t\<^isub>2 : T\<^isub>2 \ \ \ (\:T\<^isub>1. t\<^isub>2) : T\<^isub>1 \ \\<^isub>\ 1 0 T\<^isub>2" +| T_App: "\ \ t\<^isub>1 : T\<^isub>1\<^isub>1 \ T\<^isub>1\<^isub>2 \ \ \ t\<^isub>2 : T\<^isub>1\<^isub>1 \ \ \ t\<^isub>1 \ t\<^isub>2 : T\<^isub>1\<^isub>2" +| T_TAbs: "TVarB T\<^isub>1 \ \ \ t\<^isub>2 : T\<^isub>2 \ \ \ (\<:T\<^isub>1. t\<^isub>2) : (\<:T\<^isub>1. T\<^isub>2)" +| T_TApp: "\ \ t\<^isub>1 : (\<:T\<^isub>1\<^isub>1. T\<^isub>1\<^isub>2) \ \ \ T\<^isub>2 <: T\<^isub>1\<^isub>1 \ + \ \ t\<^isub>1 \\<^isub>\ T\<^isub>2 : T\<^isub>1\<^isub>2[0 \\<^isub>\ T\<^isub>2]\<^isub>\" +| T_Sub: "\ \ t : S \ \ \ S <: T \ \ \ t : T" + +code_pred [inductify] typing . + +thm typing.equation + +values 6 "{(E, t, T). typing E t T}" + +subsection {* Higher-order predicate *} + +code_pred [inductify] mapB . + +subsection {* Multiple instances *} + +inductive subtype_refl' where + "\ \ t : T ==> \ (\ \ T <: T) ==> subtype_refl' t T" + +code_pred (modes: i => i => bool, o => i => bool, i => o => bool, o => o => bool) [inductify] subtype_refl' . + +thm subtype_refl'.equation + + +end \ No newline at end of file