ProgTutorial/Parsing.thy
changeset 522 0ed6f49277c4
parent 521 9728b0432fb2
child 523 0753bc271fd5
equal deleted inserted replaced
521:9728b0432fb2 522:0ed6f49277c4
   978 text {*
   978 text {*
   979   The crucial function @{ML_ind local_theory in Outer_Syntax} expects
   979   The crucial function @{ML_ind local_theory in Outer_Syntax} expects
   980   the name for the command, a kind indicator, a short description and 
   980   the name for the command, a kind indicator, a short description and 
   981   a parser producing a local theory transition (explained later). For the
   981   a parser producing a local theory transition (explained later). For the
   982   name and the kind, you can use the ML-antiquotation @{text "@{command_spec ...}"}.
   982   name and the kind, you can use the ML-antiquotation @{text "@{command_spec ...}"}.
   983   After this, you can write in your theory 
   983   You can now write in your theory 
   984 *}
   984 *}
   985 
   985 
   986 foobar
   986 foobar
   987 
   987 
   988 text {*
   988 text {*
   989   but of course you will not see ``anything''.
   989   but of course you will not see anything since \isacommand{foobar} is
   990   Remember that this only works in jEdit. In order to enable also Proof-General 
   990   not intended to do anything.  Remember, however, that this only
   991   recognise this command, a keyword file needs to be generated (see next
   991   works in jEdit. In order to enable also Proof-General recognise this
   992   section).
   992   command, a keyword file needs to be generated (see next section).
   993 
   993 
   994   At the moment the command \isacommand{foobar} is not very useful. Let us
   994   As it stands, the command \isacommand{foobar} is not very useful. Let
   995   refine it a bit next by letting it take a proposition as argument and
   995   us refine it a bit next by letting it take a proposition as argument
   996   printing this proposition inside the tracing buffer. We announce the
   996   and printing this proposition inside the tracing buffer. We announce
   997   command in the theory header as
   997   the command \isacommand{foobar\_trace} in the theory header as
   998 
   998 
   999   \begin{graybox}
   999   \begin{graybox}
  1000   \isacommand{keywords} @{text [quotes] "foobar_trace"} @{text "::"} @{text "thy_decl"}
  1000   \isacommand{keywords} @{text [quotes] "foobar_trace"} @{text "::"} @{text "thy_decl"}
  1001   \end{graybox}
  1001   \end{graybox}
  1002 
  1002 
  1089 apply(rule TrueI)+
  1089 apply(rule TrueI)+
  1090 done
  1090 done
  1091 
  1091 
  1092 text {*
  1092 text {*
  1093   The last command we describe here is
  1093   The last command we describe here is
  1094   \isacommand{foobar\_proof}. Like \isacommand{lemma}, its purpose is
  1094   \isacommand{foobar\_proof}. Like \isacommand{foobar\_goal}, its purpose is
  1095   to take a proposition and open a corresponding proof-state that
  1095   to take a proposition and open a corresponding proof-state that
  1096   allows us to give a proof for it. However, unlike
  1096   allows us to give a proof for it. However, unlike
  1097   \isacommand{lemma}, the proposition will be given as a
  1097   \isacommand{foobar\_goal}, the proposition will be given as a
  1098   ML-value. Such a command is quite useful during development
  1098   ML-value. Such a command is quite useful during development
  1099   when you generate a goal on the ML-level and want to see
  1099   when you generate a goal on the ML-level and want to see
  1100   whether it is provable. In addition allow the proved 
  1100   whether it is provable. In addition we want to allow the proved 
  1101   proposition to have a name that can be referenced later on.
  1101   proposition to have a name that can be referenced later on.
  1102 
  1102 
  1103   The first problem for this command is to parse some text as 
  1103   The first problem for \isacommand{foobar\_proof} is to parse some
  1104   ML-source and then interpret it as an Isabelle term. For the parsing we can 
  1104   text as ML-source and then interpret it as an Isabelle term using
  1105   use the function @{ML_ind "ML_source" in Parse} in the structure 
  1105   the ML-runtime.  For the parsing part, we can use the function
  1106   @{ML_struct Parse}. For running the ML-interpreter we need the 
  1106   @{ML_ind "ML_source" in Parse} in the structure @{ML_struct
  1107   following scaffolding code.
  1107   Parse}. For running the ML-interpreter we need the following
       
  1108   scaffolding code.
  1108 *}
  1109 *}
  1109 
  1110 
  1110 ML %grayML{*
  1111 ML %grayML{*
  1111 structure Result = Proof_Data (
  1112 structure Result = 
  1112   type T = unit -> term
  1113   Proof_Data (type T = unit -> term
  1113   fun init thy () = error "Result")
  1114               fun init thy () = error "Result")
  1114 
  1115 
  1115 val result_cookie = (Result.get, Result.put, "Result.put") *}
  1116 val result_cookie = (Result.get, Result.put, "Result.put") *}
  1116 
  1117 
  1117 text {*
  1118 text {*
  1118   With this in place, we can write the code for \isacommand{foobar\_prove}.
  1119   With this in place, we can implement the code for \isacommand{foobar\_prove} 
       
  1120   as follows.
  1119 *}
  1121 *}
  1120 
  1122 
  1121 ML %linenosgray{*let
  1123 ML %linenosgray{*let
  1122    fun after_qed thm_name thms lthy =
  1124    fun after_qed thm_name thms lthy =
  1123         Local_Theory.note (thm_name, (flat thms)) lthy |> snd
  1125         Local_Theory.note (thm_name, (flat thms)) lthy |> snd
  1140   In Line 12, we implement a parser that first reads in an optional lemma name (terminated 
  1142   In Line 12, we implement a parser that first reads in an optional lemma name (terminated 
  1141   by ``:'') and then some ML-code. The function in Lines 5 to 10 takes the ML-text
  1143   by ``:'') and then some ML-code. The function in Lines 5 to 10 takes the ML-text
  1142   and lets the ML-runtime evaluate it using the function @{ML_ind value in Code_Runtime}
  1144   and lets the ML-runtime evaluate it using the function @{ML_ind value in Code_Runtime}
  1143   in the structure @{ML_struct Code_Runtime}.  Once the ML-text has been turned into a term, 
  1145   in the structure @{ML_struct Code_Runtime}.  Once the ML-text has been turned into a term, 
  1144   the function @{ML theorem in Proof} opens a corresponding proof-state. This function takes the
  1146   the function @{ML theorem in Proof} opens a corresponding proof-state. This function takes the
  1145   function @{ML_text "after_qed"} as argument, whose purpose is to store the theorem,
  1147   function @{text "after_qed"} as argument, whose purpose is to store the theorem
  1146   once it is proven, under the given name @{text "thm_name"}.
  1148   (once it is proven) under the given name @{text "thm_name"}.
  1147 
  1149 
  1148   You can now define a term, for example
  1150   You can now define a term, for example
  1149 *}
  1151 *}
  1150 
  1152 
  1151 ML %grayML{*val prop_true = @{prop "True"}*}
  1153 ML %grayML{*val prop_true = @{prop "True"}*}
  1157 foobar_prove test: prop_true
  1159 foobar_prove test: prop_true
  1158 apply(rule TrueI)
  1160 apply(rule TrueI)
  1159 done
  1161 done
  1160 
  1162 
  1161 text {*
  1163 text {*
  1162   Finally we can test whether the lemma has been stored under the given name.
  1164   Finally you can test whether the lemma has been stored under the given name.
  1163   
  1165   
  1164   \begin{isabelle}
  1166   \begin{isabelle}
  1165   \isacommand{thm}~@{text "test"}\\
  1167   \isacommand{thm}~@{text "test"}\\
  1166   @{text "> "}~@{thm TrueI}
  1168   @{text "> "}~@{thm TrueI}
  1167   \end{isabelle}
  1169   \end{isabelle}
  1311 text {*
  1313 text {*
  1312   Also if the kind of a command changes, from @{text "thy_decl"} to 
  1314   Also if the kind of a command changes, from @{text "thy_decl"} to 
  1313   @{text "thy_goal"} say,  you need to recreate the keyword file.
  1315   @{text "thy_goal"} say,  you need to recreate the keyword file.
  1314 *}
  1316 *}
  1315 
  1317 
       
  1318 
       
  1319 
       
  1320 
  1316 text {*
  1321 text {*
  1317   {\bf TBD below}
  1322   {\bf TBD below}
  1318 
  1323 
  1319   (FIXME: read a name and show how to store theorems; see @{ML_ind note in Local_Theory})
  1324  *}
  1320   
       
  1321 *}
       
  1322 
  1325 
  1323 
  1326 
  1324 
  1327 
  1325 
  1328 
  1326 
  1329