ProgTutorial/General.thy
changeset 394 0019ebf76e10
parent 393 d8b6d5003823
equal deleted inserted replaced
393:d8b6d5003823 394:0019ebf76e10
  1331   \]
  1331   \]
  1332 
  1332 
  1333   While we obtained a theorem as result, this theorem is not
  1333   While we obtained a theorem as result, this theorem is not
  1334   yet stored in Isabelle's theorem database. Consequently, it cannot be 
  1334   yet stored in Isabelle's theorem database. Consequently, it cannot be 
  1335   referenced on the user level. One way to store it in the theorem database is
  1335   referenced on the user level. One way to store it in the theorem database is
  1336   by using the function @{ML_ind note in LocalTheory}.\footnote{\bf FIXME: make sure a pointer
  1336   by using the function @{ML_ind note in Local_Theory}.\footnote{\bf FIXME: make sure a pointer
  1337   to the section about local-setup is given earlier.}
  1337   to the section about local-setup is given earlier.}
  1338 *}
  1338 *}
  1339 
  1339 
  1340 local_setup %gray {*
  1340 local_setup %gray {*
  1341   LocalTheory.note Thm.theoremK
  1341   Local_Theory.note ((@{binding "my_thm"}, []), [my_thm]) #> snd *}
  1342      ((@{binding "my_thm"}, []), [my_thm]) #> snd *}
  1342 
  1343 
  1343 text {*
  1344 text {*
  1344   The fourth argument of @{ML note in Local_Theory} is the list of theorems we
  1345   The fourth argument of @{ML note in LocalTheory} is the list of theorems we
       
  1346   want to store under a name. We can store more than one under a single name. 
  1345   want to store under a name. We can store more than one under a single name. 
  1347   The first argument @{ML_ind theoremK in Thm} is
  1346   The first argument @{ML_ind theoremK in Thm} is
  1348   a kind indicator, which classifies the theorem. There are several such kind
  1347   a kind indicator, which classifies the theorem. There are several such kind
  1349   indicators: for a theorem arising from a definition you should use @{ML_ind
  1348   indicators: for a theorem arising from a definition you should use @{ML_ind
  1350   definitionK in Thm}, for an axiom @{ML_ind axiomK in Thm}, and for
  1349   definitionK in Thm}, and for
  1351   ``normal'' theorems the kinds @{ML_ind theoremK in Thm} or @{ML_ind lemmaK
  1350   ``normal'' theorems the kinds @{ML_ind theoremK in Thm} or @{ML_ind lemmaK
  1352   in Thm}.  The second argument of @{ML note in LocalTheory} is the name under
  1351   in Thm}.  The second argument of @{ML note in Local_Theory} is the name under
  1353   which we store the theorem or theorems. The third argument can contain a
  1352   which we store the theorem or theorems. The third argument can contain a
  1354   list of theorem attributes, which we will explain in detail in
  1353   list of theorem attributes, which we will explain in detail in
  1355   Section~\ref{sec:attributes}. Below we just use one such attribute for
  1354   Section~\ref{sec:attributes}. Below we just use one such attribute for
  1356   adding the theorem to the simpset:
  1355   adding the theorem to the simpset:
  1357 *}
  1356 *}
  1358 
  1357 
  1359 local_setup %gray {*
  1358 local_setup %gray {*
  1360   LocalTheory.note Thm.theoremK
  1359   Local_Theory.note ((@{binding "my_thm_simp"}, 
  1361     ((@{binding "my_thm_simp"}, 
       
  1362        [Attrib.internal (K Simplifier.simp_add)]), [my_thm]) #> snd *}
  1360        [Attrib.internal (K Simplifier.simp_add)]), [my_thm]) #> snd *}
  1363 
  1361 
  1364 text {*
  1362 text {*
  1365   Note that we have to use another name under which the theorem is stored,
  1363   Note that we have to use another name under which the theorem is stored,
  1366   since Isabelle does not allow us to call  @{ML_ind note in LocalTheory} twice
  1364   since Isabelle does not allow us to call  @{ML_ind note in Local_Theory} twice
  1367   with the same name. The attribute needs to be wrapped inside the function @{ML_ind
  1365   with the same name. The attribute needs to be wrapped inside the function @{ML_ind
  1368   internal in Attrib} from the structure @{ML_struct Attrib}. If we use the function 
  1366   internal in Attrib} from the structure @{ML_struct Attrib}. If we use the function 
  1369   @{ML get_thm_names_from_ss} from
  1367   @{ML get_thm_names_from_ss} from
  1370   the previous chapter, we can check whether the theorem has actually been
  1368   the previous chapter, we can check whether the theorem has actually been
  1371   added.
  1369   added.
  1390   \end{isabelle}
  1388   \end{isabelle}
  1391 
  1389 
  1392   or with the @{text "@{thm \<dots>}"}-antiquotation on the ML-level. Otherwise the 
  1390   or with the @{text "@{thm \<dots>}"}-antiquotation on the ML-level. Otherwise the 
  1393   user has no access to these theorems. 
  1391   user has no access to these theorems. 
  1394 
  1392 
  1395   Recall that Isabelle does not let you call @{ML note in LocalTheory} twice
  1393   Recall that Isabelle does not let you call @{ML note in Local_Theory} twice
  1396   with the same theorem name. In effect, once a theorem is stored under a name, 
  1394   with the same theorem name. In effect, once a theorem is stored under a name, 
  1397   this association is fixed. While this is a ``safety-net'' to make sure a
  1395   this association is fixed. While this is a ``safety-net'' to make sure a
  1398   theorem name refers to a particular theorem or collection of theorems, it is 
  1396   theorem name refers to a particular theorem or collection of theorems, it is 
  1399   also a bit too restrictive in cases where a theorem name should refer to a 
  1397   also a bit too restrictive in cases where a theorem name should refer to a 
  1400   dynamically expanding list of theorems (like a simpset). Therefore Isabelle 
  1398   dynamically expanding list of theorems (like a simpset). Therefore Isabelle 
  1405 
  1403 
  1406 ML{*structure MyThmList = Generic_Data
  1404 ML{*structure MyThmList = Generic_Data
  1407   (type T = thm list
  1405   (type T = thm list
  1408    val empty = []
  1406    val empty = []
  1409    val extend = I
  1407    val extend = I
  1410    val merge = op @)
  1408    val merge = merge Thm.eq_thm_prop)
  1411 
  1409 
  1412 fun update thm = Context.theory_map (MyThmList.map (fn thms => thm::thms))*}
  1410 fun update thm = Context.theory_map (MyThmList.map (Thm.add_thm thm))*}
  1413 
  1411 
  1414 text {*
  1412 text {*
  1415   The function @{ML update} allows us to update the theorem list, for example
  1413   The function @{ML update} allows us to update the theorem list, for example
  1416   by adding the theorem @{thm [source] TrueI}.
  1414   by adding the theorem @{thm [source] TrueI}.
  1417 *}
  1415 *}
  1648   two premises of the lemma.  This can be done with the function @{ML_ind name in Rule_Cases}
  1646   two premises of the lemma.  This can be done with the function @{ML_ind name in Rule_Cases}
  1649   from the structure @{ML_struct Rule_Cases}.
  1647   from the structure @{ML_struct Rule_Cases}.
  1650 *}
  1648 *}
  1651 
  1649 
  1652 local_setup %gray {*
  1650 local_setup %gray {*
  1653   LocalTheory.note Thm.lemmaK
  1651   Local_Theory.note ((@{binding "foo_data'"}, []), 
  1654     ((@{binding "foo_data'"}, []), 
  1652     [(Rule_Cases.name ["foo_case_one", "foo_case_two"] 
  1655       [(Rule_Cases.name ["foo_case_one", "foo_case_two"] 
  1653        @{thm foo_data})]) #> snd *}
  1656         @{thm foo_data})]) #> snd *}
       
  1657 
  1654 
  1658 text {*
  1655 text {*
  1659   The data of the theorem @{thm [source] foo_data'} is then as follows:
  1656   The data of the theorem @{thm [source] foo_data'} is then as follows:
  1660 
  1657 
  1661   @{ML_response_fake [display,gray]
  1658   @{ML_response_fake [display,gray]
  2137 
  2134 
  2138 section {* Contexts (TBD) *}
  2135 section {* Contexts (TBD) *}
  2139 
  2136 
  2140 section {* Local Theories (TBD) *}
  2137 section {* Local Theories (TBD) *}
  2141 
  2138 
       
  2139 text {*
       
  2140   @{ML_ind "Local_Theory.declaration"}
       
  2141 *}
  2142 
  2142 
  2143 (*
  2143 (*
  2144 setup {*
  2144 setup {*
  2145  Sign.add_consts_i [(Binding"bar", @{typ "nat"},NoSyn)] 
  2145  Sign.add_consts_i [(Binding"bar", @{typ "nat"},NoSyn)] 
  2146 *}
  2146 *}
  2475 
  2475 
  2476 ML {* Pretty.mark Markup.hilite (Pretty.str "foo") |> Pretty.string_of  |> tracing *}
  2476 ML {* Pretty.mark Markup.hilite (Pretty.str "foo") |> Pretty.string_of  |> tracing *}
  2477 ML {* (Pretty.str "bar") |> Pretty.string_of |> tracing *}
  2477 ML {* (Pretty.str "bar") |> Pretty.string_of |> tracing *}
  2478 *)
  2478 *)
  2479 
  2479 
       
  2480 section {* Morphisms (TBD) *}
       
  2481 
       
  2482 text {*
       
  2483   Morphisms are arbitrary transformations over terms, types, theorems and bindings.
       
  2484   They can be constructed using the function @{ML_ind morphism in Morphism},
       
  2485   which expects a record with functions of type
       
  2486 
       
  2487   \begin{isabelle}
       
  2488   \begin{tabular}{rl}
       
  2489   @{text "binding:"} & @{text "binding -> binding"}\\
       
  2490   @{text "typ:"}     & @{text "typ -> typ"}\\
       
  2491   @{text "term:"}    & @{text "term -> term"}\\
       
  2492   @{text "fact:"}    & @{text "thm list -> thm list"}
       
  2493   \end{tabular}
       
  2494   \end{isabelle}
       
  2495 
       
  2496   The simplest morphism is the  @{ML_ind identity in Morphism}-morphism defined as
       
  2497 *}
       
  2498 
       
  2499 ML{*val identity = Morphism.morphism {binding = I, typ = I, term = I, fact = I}*}
       
  2500   
       
  2501 text {*
       
  2502   Morphisms can be composed with the function @{ML_ind "$>" in Morphism}
       
  2503 *}
       
  2504 
       
  2505 ML{*fun trm_phi (Free (x, T)) = Var ((x, 0), T) 
       
  2506   | trm_phi (Abs (x, T, t)) = Abs (x, T, trm_phi t)
       
  2507   | trm_phi (t $ s) = (trm_phi t) $ (trm_phi s)
       
  2508   | trm_phi t = t*}
       
  2509 
       
  2510 ML{*val phi = Morphism.term_morphism trm_phi*}
       
  2511 
       
  2512 ML{*Morphism.term phi @{term "P x y"}*}
       
  2513 
       
  2514 text {*
       
  2515   @{ML_ind term_morphism in Morphism}
       
  2516 
       
  2517   @{ML_ind term in Morphism},
       
  2518   @{ML_ind thm in Morphism}
       
  2519 
       
  2520   \begin{readmore}
       
  2521   Morphisms are implemented in the file @{ML_file "Pure/morphism.ML"}.
       
  2522   \end{readmore}
       
  2523 *}
  2480 
  2524 
  2481 section {* Misc (TBD) *}
  2525 section {* Misc (TBD) *}
  2482 
  2526 
  2483 ML {*Datatype.get_info @{theory} "List.list"*}
  2527 ML {*Datatype.get_info @{theory} "List.list"*}
  2484 
  2528 
  2499 
  2543 
  2500 
  2544 
  2501 text {* 
  2545 text {* 
  2502   @{ML_ind "Binding.str_of"} returns the string with markup;
  2546   @{ML_ind "Binding.str_of"} returns the string with markup;
  2503   @{ML_ind "Binding.name_of"} returns the string without markup
  2547   @{ML_ind "Binding.name_of"} returns the string without markup
       
  2548 
       
  2549   @{ML_ind "Binding.conceal"} 
  2504 *}
  2550 *}
  2505 
  2551 
  2506 section {* Concurrency (TBD) *}
  2552 section {* Concurrency (TBD) *}
  2507 
  2553 
  2508 text {*
  2554 text {*