src/FOL/ex/LocaleTest.thy
changeset 15696 1da4ce092c0b
parent 15624 484178635bd8
child 15763 b901a127ac73
equal deleted inserted replaced
15695:f072119afa4e 15696:1da4ce092c0b
     8 
     8 
     9 header {* Test of Locale instantiation *}
     9 header {* Test of Locale instantiation *}
    10 
    10 
    11 theory LocaleTest = FOL:
    11 theory LocaleTest = FOL:
    12 
    12 
       
    13 ML {* set quick_and_dirty *}    (* allow for thm command in batch mode *)
       
    14 ML {* set Toplevel.debug *}
       
    15 ML {* set show_hyps *}
       
    16 ML {* set show_sorts *}
       
    17 
       
    18 section {* interpretation *}
       
    19 
    13 (* interpretation input syntax *)
    20 (* interpretation input syntax *)
    14 
    21 
    15 locale L
    22 locale L
    16 locale M = fixes a and b and c
    23 locale M = fixes a and b and c
    17 
    24 
    24 
    31 
    25 interpretation L .
    32 interpretation L .
    26 
    33 
    27 (* processing of locale expression *)
    34 (* processing of locale expression *)
    28 
    35 
    29 ML {* reset show_hyps *}
       
    30 
       
    31 locale A = fixes a assumes asm_A: "a = a"
    36 locale A = fixes a assumes asm_A: "a = a"
    32 
    37 
    33 locale (open) B = fixes b assumes asm_B [simp]: "b = b"
    38 locale (open) B = fixes b assumes asm_B [simp]: "b = b"
    34 
    39 
    35 locale C = A + B + assumes asm_C: "c = c"
    40 locale C = A + B + assumes asm_C: "c = c"
    36   (* TODO: independent type var in c, prohibit locale declaration *)
    41   (* TODO: independent type var in c, prohibit locale declaration *)
    37 
    42 
    38 locale D = A + B + fixes d defines def_D: "d == (a = b)"
    43 locale D = A + B + fixes d defines def_D: "d == (a = b)"
    39 
    44 
    40 ML {* set show_sorts *}
    45 theorem (in A)
       
    46   includes D
       
    47   shows True ..
       
    48 
       
    49 theorem (in D) True ..
    41 
    50 
    42 typedecl i
    51 typedecl i
    43 arities i :: "term"
    52 arities i :: "term"
    44 
    53 
    45 ML {* set Toplevel.debug *}
       
    46 
       
    47 ML {* set show_hyps *}
       
    48 
    54 
    49 interpretation p1: C ["X::'b" "Y::'b"] by (auto intro: A.intro C_axioms.intro)
    55 interpretation p1: C ["X::'b" "Y::'b"] by (auto intro: A.intro C_axioms.intro)
    50   (* both X and Y get type 'b since 'b is the internal type of parameter b,
    56   (* both X and Y get type 'b since 'b is the internal type of parameter b,
    51      not wanted, but put up with for now. *)
    57      not wanted, but put up with for now. *)
    52 
    58 
    53 print_interps A
    59 print_interps A
    54 
    60 
    55 (* possible accesses
    61 (* possible accesses *)
    56 thm p1.a.asm_A thm LocaleTest.p1.a.asm_A
    62 thm p1.a.asm_A thm LocaleTest.p1.a.asm_A
    57 thm LocaleTest.asm_A thm p1.asm_A
    63 thm LocaleTest.asm_A thm p1.asm_A
    58 *)
    64 
    59 
    65 
    60 (* without prefix *)
    66 (* without prefix *)
    61 
    67 
    62 interpretation C ["W::'b" "Z::'b"] by (auto intro: A.intro C_axioms.intro)
    68 interpretation C ["W::'b" "Z::'b"] by (auto intro: A.intro C_axioms.intro)
    63 
    69 
    64 print_interps A
    70 print_interps A
    65 
    71 
    66 (* possible accesses
    72 (* possible accesses *)
    67 thm a.asm_A thm asm_A thm LocaleTest.a.asm_A thm LocaleTest.asm_A
    73 thm a.asm_A thm asm_A thm LocaleTest.a.asm_A thm LocaleTest.asm_A
    68 *)
    74 
    69 
    75 
    70 interpretation p2: D [X Y "Y = X"] by (auto intro: A.intro simp: eq_commute)
    76 interpretation p2: D [X Y "Y = X"] by (auto intro: A.intro simp: eq_commute)
    71 
    77 
    72 print_interps D
    78 print_interps D
    73 
    79 
    74 (*
       
    75 thm p2.a.asm_A
    80 thm p2.a.asm_A
    76 *)
    81 
    77 
    82 
    78 interpretation p3: D [X Y] .
    83 interpretation p3: D [X Y] .
    79 
    84 
    80 (* duplicate: not registered *)
    85 (* duplicate: not registered *)
    81 (*
    86 
    82 thm p3.a.asm_A
    87 (* thm p3.a.asm_A *)
    83 *)
    88 
    84 
    89 
    85 print_interps A
    90 print_interps A
    86 print_interps B
    91 print_interps B
    87 print_interps C
    92 print_interps C
    88 print_interps D
    93 print_interps D
    89 
    94 
    90 (* not permitted *)
    95 (* not permitted
    91 (*
    96 
    92 interpretation p4: A ["x::?'a1"] apply (rule A.intro) apply rule done
    97 interpretation p4: A ["?x::?'a1"] apply (rule A.intro) apply rule done
       
    98 
       
    99 print_interps A
    93 *)
   100 *)
    94 print_interps A
   101 
    95 
   102 interpretation p10: D + D a' b' d' [X Y _ U V _] by (auto intro: A.intro)
    96 (* without a prefix *)
   103 
    97 (* TODO!!!
   104 corollary (in D) th_x: True ..
    98 interpretation A [Z] apply - apply (auto intro: A.intro) done
   105 
    99 *)
   106 (* possible accesses: for each registration *)
       
   107 
       
   108 thm p2.th_x thm p3.th_x thm p10.th_x
       
   109 
       
   110 lemma (in D) th_y: "d == (a = b)" .
       
   111 
       
   112 thm p2.th_y thm p3.th_y thm p10.th_y
       
   113 
       
   114 lemmas (in D) th_z = th_y
       
   115 
       
   116 thm p2.th_z
       
   117 
       
   118 thm asm_A
       
   119 
       
   120 section {* Interpretation in proof contexts *}
   100 
   121 
   101 theorem True
   122 theorem True
   102 proof -
   123 proof -
   103   fix alpha::i and beta::i and gamma::i
   124   fix alpha::i and beta::i and gamma::i
   104   assume "A(alpha)"
   125   have alpha_A: "A(alpha)" by (auto intro: A.intro)
   105   then interpret p5: A [alpha] .
   126   then interpret p5: A [alpha] .
   106   print_interps A
   127   print_interps A
       
   128   thm p5.asm_A
   107   interpret p6: C [alpha beta] by (auto intro: C_axioms.intro)
   129   interpret p6: C [alpha beta] by (auto intro: C_axioms.intro)
   108   print_interps A   (* p6 not added! *)
   130   print_interps A   (* p6 not added! *)
   109   print_interps C
   131   print_interps C
   110 qed rule
   132 qed rule
   111 
   133 
   112 (* lemma "bla.bla": True by rule *)
   134 theorem (in A) True
   113 
   135 proof -
       
   136   print_interps A
       
   137   fix beta and gamma
       
   138   interpret p9: D [a beta _]
       
   139     (* no proof obligation for A !!! *)
       
   140     apply - apply (rule refl) apply assumption done
       
   141 qed rule
       
   142 
       
   143 
       
   144 (* Definition involving free variable *)
       
   145 
       
   146 ML {* reset show_sorts *}
       
   147 
       
   148 locale E = fixes e defines e_def: "e(x) == x & x"
       
   149   notes e_def2 = e_def
       
   150 
       
   151 lemma (in E) True thm e_def by fast
       
   152 
       
   153 interpretation p7: E ["(%x. x)"] by simp
       
   154 
       
   155 (* TODO: goal mustn't be beta-reduced here, is doesn't match meta-hyp *)
       
   156 
       
   157 thm p7.e_def2
       
   158 
       
   159 locale E' = fixes e defines e_def: "e == (%x. x & x)"
       
   160   notes e_def2 = e_def
       
   161 
       
   162 interpretation p7': E' ["(%x. x)"] by simp
       
   163 
       
   164 thm p7'.e_def2
       
   165 
       
   166 (* Definition involving free variable in assm *)
       
   167 
       
   168 locale (open) F = fixes f assumes asm_F: "f --> x"
       
   169   notes asm_F2 = asm_F
       
   170 
       
   171 interpretation p8: F ["False"] by fast
       
   172 
       
   173 thm p8.asm_F2
       
   174 
       
   175 subsection {* Locale without assumptions *}
       
   176 
       
   177 locale L1 = notes rev_conjI [intro] = conjI [THEN iffD1 [OF conj_commute]]
       
   178 
       
   179 lemma "[| P; Q |] ==> P & Q"
       
   180 proof -
       
   181   interpret my: L1 .           txt {* No chained fact required. *}
       
   182   assume Q and P               txt {* order reversed *}
       
   183   then show "P & Q" ..         txt {* Applies @{thm my.rev_conjI}. *}
       
   184 qed
       
   185 
       
   186 locale L11 = notes rev_conjI = conjI [THEN iffD1 [OF conj_commute]]
       
   187 
       
   188 lemma "[| P; Q |] ==> P & Q"
       
   189 proof -
       
   190   interpret [intro]: L11 .     txt {* Attribute supplied at instantiation. *}
       
   191   assume Q and P
       
   192   then show "P & Q" ..
       
   193 qed
       
   194 
       
   195 subsection {* Simple locale with assumptions *}
       
   196 
       
   197 consts bin :: "[i, i] => i" (infixl "#" 60)
       
   198 
       
   199 axioms i_assoc: "(x # y) # z = x # (y # z)"
       
   200   i_comm: "x # y = y # x"
       
   201 
       
   202 locale L2 =
       
   203   fixes OP (infixl "+" 60)
       
   204   assumes assoc: "(x + y) + z = x + (y + z)"
       
   205     and comm: "x + y = y + x"
       
   206 
       
   207 lemma (in L2) lcomm: "x + (y + z) = y + (x + z)"
       
   208 proof -
       
   209   have "x + (y + z) = (x + y) + z" by (simp add: assoc)
       
   210   also have "... = (y + x) + z" by (simp add: comm)
       
   211   also have "... = y + (x + z)" by (simp add: assoc)
       
   212   finally show ?thesis .
       
   213 qed
       
   214 
       
   215 lemmas (in L2) AC = comm assoc lcomm
       
   216 
       
   217 lemma "(x::i) # y # z # w = y # x # w # z"
       
   218 proof -
       
   219   interpret my: L2 ["op #"] by (rule L2.intro [of "op #", OF i_assoc i_comm])
       
   220     txt {* Chained fact required to discharge assumptions of @{text L2}
       
   221       and instantiate parameters. *}
       
   222   show ?thesis by (simp only: my.OP.AC)  (* or simply AC *)
       
   223 qed
       
   224 
       
   225 subsection {* Nested locale with assumptions *}
       
   226 
       
   227 locale L3 =
       
   228   fixes OP (infixl "+" 60)
       
   229   assumes assoc: "(x + y) + z = x + (y + z)"
       
   230 
       
   231 locale L4 = L3 +
       
   232   assumes comm: "x + y = y + x"
       
   233 
       
   234 lemma (in L4) lcomm: "x + (y + z) = y + (x + z)"
       
   235 proof -
       
   236   have "x + (y + z) = (x + y) + z" by (simp add: assoc)
       
   237   also have "... = (y + x) + z" by (simp add: comm)
       
   238   also have "... = y + (x + z)" by (simp add: assoc)
       
   239   finally show ?thesis .
       
   240 qed
       
   241 
       
   242 lemmas (in L4) AC = comm assoc lcomm
       
   243 
       
   244 lemma "(x::i) # y # z # w = y # x # w # z"
       
   245 proof -
       
   246   interpret my: L4 ["op #"]
       
   247     by (auto intro: L3.intro L4_axioms.intro i_assoc i_comm)
       
   248   show ?thesis by (simp only: my.OP.AC)  (* or simply AC *)
       
   249 qed
       
   250 
       
   251 subsection {* Locale with definition *}
       
   252 
       
   253 text {* This example is admittedly not very creative :-) *}
       
   254 
       
   255 locale L5 = L4 + var A +
       
   256   defines A_def: "A == True"
       
   257 
       
   258 lemma (in L5) lem: A
       
   259   by (unfold A_def) rule
       
   260 
       
   261 lemma "L5(op #) ==> True"
       
   262 proof -
       
   263   assume "L5(op #)"
       
   264   then interpret L5 ["op #"] by (auto intro: L5.axioms)
       
   265   show ?thesis by (rule lem)  (* lem instantiated to True *)
       
   266 qed
       
   267 
       
   268 subsection {* Instantiation in a context with target *}
       
   269 
       
   270 lemma (in L4)
       
   271   fixes A (infixl "$" 60)
       
   272   assumes A: "L4(A)"
       
   273   shows "(x::i) $ y $ z $ w = y $ x $ w $ z"
       
   274 proof -
       
   275   from A interpret A: L4 ["A"] by (auto intro: L4.axioms)
       
   276   show ?thesis by (simp only: A.OP.AC)
       
   277 qed
   114 
   278 
   115 end
   279 end