ProgTutorial/FirstSteps.thy
changeset 347 01e71cddf6a3
parent 346 0fea8b7a14a1
child 350 364fffa80794
equal deleted inserted replaced
346:0fea8b7a14a1 347:01e71cddf6a3
    15 text {*
    15 text {*
    16    \begin{flushright}
    16    \begin{flushright}
    17   {\em
    17   {\em
    18   ``We will most likely never realize the full importance of painting the Tower,\\ 
    18   ``We will most likely never realize the full importance of painting the Tower,\\ 
    19   that it is the essential element in the conservation of metal works and the\\ 
    19   that it is the essential element in the conservation of metal works and the\\ 
    20   more meticulous the paint job, the longer the Tower shall endure.''} \\[1ex]
    20   more meticulous the paint job, the longer the tower shall endure.''} \\[1ex]
    21   Gustave Eiffel, In his book {\em The 300-Meter Tower}.\footnote{The Eiffel Tower has been 
    21   Gustave Eiffel, In his book {\em The 300-Meter Tower}.\footnote{The Eiffel Tower has been 
    22   re-painted 18 times since its initial construction, an average of once every 
    22   re-painted 18 times since its initial construction, an average of once every 
    23   seven years. It takes more than a year for a team of 25 painters to paint the tower 
    23   seven years. It takes more than a year for a team of 25 painters to paint the tower 
    24   from top to bottom.}
    24   from top to bottom.}
    25   \end{flushright}
    25   \end{flushright}
   212 
   212 
   213 ML {* Toplevel.program (fn () => innocent ()) *}
   213 ML {* Toplevel.program (fn () => innocent ()) *}
   214 *)
   214 *)
   215 
   215 
   216 text {*
   216 text {*
   217   Most often you want to inspect data of Isabelle's most basic data
   217   Most often you want to inspect data of Isabelle's basic data
   218   structures, namely @{ML_type term}, @{ML_type cterm} and @{ML_type
   218   structures, namely @{ML_type term}, @{ML_type cterm} and @{ML_type
   219   thm}. Isabelle contains elaborate pretty-printing functions for printing
   219   thm}. Isabelle contains elaborate pretty-printing functions for printing
   220   them (see Section \ref{sec:pretty}), but for quick-and-dirty solutions they
   220   them (see Section \ref{sec:pretty}), but for quick-and-dirty solutions they
   221   are a bit unwieldy. One way to transform a term into a string is to use the
   221   are a bit unwieldy. One way to transform a term into a string is to use the
   222   function @{ML_ind  string_of_term in Syntax} in the structure @{ML_struct
   222   function @{ML_ind  string_of_term in Syntax} in the structure @{ML_struct
  1043 ML{*fun ref_update n = WrongRefData.map 
  1043 ML{*fun ref_update n = WrongRefData.map 
  1044       (fn r => let val _ = r := n::(!r) in r end)*}
  1044       (fn r => let val _ = r := n::(!r) in r end)*}
  1045 
  1045 
  1046 text {*
  1046 text {*
  1047   which takes an integer and adds it to the content of the reference.
  1047   which takes an integer and adds it to the content of the reference.
  1048   As above we update the reference with the command 
  1048   As done above, we update the reference with the command 
  1049   \isacommand{setup}. 
  1049   \isacommand{setup}. 
  1050 *}
  1050 *}
  1051 
  1051 
  1052 setup %gray{* ref_update 1 *}
  1052 setup %gray {* ref_update 1 *}
  1053 
  1053 
  1054 text {*
  1054 text {*
  1055   A lookup in the current theory gives then the expected list 
  1055   A lookup in the current theory gives then the expected list 
  1056   @{ML "ref [1]" in Unsynchronized}.
  1056   @{ML "ref [1]" in Unsynchronized}.
  1057 
  1057 
  1058   @{ML_response_fake [display,gray]
  1058   @{ML_response_fake [display,gray]
  1059   "WrongRefData.get @{theory}"
  1059   "WrongRefData.get @{theory}"
  1060   "ref [1]"}
  1060   "ref [1]"}
  1061 
  1061 
  1062   So far everything is as expected. But, the trouble starts if we attempt
  1062   So far everything is as expected. But, the trouble starts if we attempt to
  1063   to backtrack to ``before'' the \isacommand{setup}-command. There, we 
  1063   backtrack to the point ``before'' the \isacommand{setup}-command. There, we
  1064   would expect that the list is empty again. But since it is stored in 
  1064   would expect that the list is empty again. But since it is stored in a
  1065   a reference, Isabelle has no control over it. So it is not
  1065   reference, Isabelle has no control over it. So it is not empty, but still
  1066   empty, but still @{ML "ref [1]" in Unsynchronized}. Adding to the trouble, 
  1066   @{ML "ref [1]" in Unsynchronized}. Adding to the trouble, if we execute the
  1067   if we execute the \isacommand{setup}-command again, we do not obtain 
  1067   \isacommand{setup}-command again, we do not obtain @{ML "ref [1]" in
  1068   @{ML "ref [1]" in Unsynchronized}, but
  1068   Unsynchronized}, but
  1069 
  1069 
  1070   @{ML_response_fake [display,gray]
  1070   @{ML_response_fake [display,gray]
  1071   "WrongRefData.get @{theory}"
  1071   "WrongRefData.get @{theory}"
  1072   "ref [1, 1]"}
  1072   "ref [1, 1]"}
  1073 
  1073 
  1078 
  1078 
  1079   \begin{readmore}
  1079   \begin{readmore}
  1080   The functors for data storage are defined in @{ML_file "Pure/context.ML"}.
  1080   The functors for data storage are defined in @{ML_file "Pure/context.ML"}.
  1081   Isabelle contains implementations of several container data structures,
  1081   Isabelle contains implementations of several container data structures,
  1082   including association lists in @{ML_file "Pure/General/alist.ML"},
  1082   including association lists in @{ML_file "Pure/General/alist.ML"},
  1083   directed graphs in @{ML_file "Pure/General/graph.ML"}. and 
  1083   directed graphs in @{ML_file "Pure/General/graph.ML"}, and 
  1084   tables and symtables in @{ML_file "Pure/General/table.ML"}.
  1084   tables and symtables in @{ML_file "Pure/General/table.ML"}.
  1085   \end{readmore}
  1085   \end{readmore}
  1086 
  1086 
  1087   Storing data in a proof context is done in a similar fashion. The
  1087   Storing data in a proof context is done in a similar fashion. The
  1088   corresponding functor is @{ML_funct_ind ProofDataFun}. With the 
  1088   corresponding functor is @{ML_funct_ind ProofDataFun}. With the 
  1112   Next we start with the context generated by the antiquotation 
  1112   Next we start with the context generated by the antiquotation 
  1113   @{text "@{context}"} and update it in various ways. 
  1113   @{text "@{context}"} and update it in various ways. 
  1114 
  1114 
  1115   @{ML_response_fake [display,gray]
  1115   @{ML_response_fake [display,gray]
  1116 "let
  1116 "let
  1117   val ctxt    = @{context}
  1117   val ctxt0 = @{context}
  1118   val ctxt'   = ctxt   |> update @{term \"False\"}
  1118   val ctxt1 = ctxt0 |> update @{term \"False\"}
  1119                        |> update @{term \"True \<and> True\"} 
  1119                     |> update @{term \"True \<and> True\"} 
  1120   val ctxt''  = ctxt   |> update @{term \"1::nat\"}
  1120   val ctxt2 = ctxt0 |> update @{term \"1::nat\"}
  1121   val ctxt''' = ctxt'' |> update @{term \"2::nat\"}
  1121   val ctxt3 = ctxt2 |> update @{term \"2::nat\"}
  1122 in
  1122 in
  1123   print ctxt; 
  1123   print ctxt0; 
  1124   print ctxt';
  1124   print ctxt1;
  1125   print ctxt'';
  1125   print ctxt2;
  1126   print ctxt'''
  1126   print ctxt3
  1127 end"
  1127 end"
  1128 "Empty!
  1128 "Empty!
  1129 True \<and> True, False
  1129 True \<and> True, False
  1130 1
  1130 1
  1131 2, 1"}
  1131 2, 1"}
  1136   Note also that the calculation above has no effect on the underlying
  1136   Note also that the calculation above has no effect on the underlying
  1137   theory. Once we throw away the contexts, we have no access to their
  1137   theory. Once we throw away the contexts, we have no access to their
  1138   associated data. This is different to theories, where the command 
  1138   associated data. This is different to theories, where the command 
  1139   \isacommand{setup} registers the data with the current and future 
  1139   \isacommand{setup} registers the data with the current and future 
  1140   theories, and therefore one can access the data potentially 
  1140   theories, and therefore one can access the data potentially 
  1141   indefinitely.\footnote{\bf FIXME: check this; it seems that is in
  1141   indefinitely.
  1142   conflict with the statements below.} 
       
  1143 
  1142 
  1144   For convenience there is an abstract layer, the type @{ML_type Context.generic}, 
  1143   For convenience there is an abstract layer, the type @{ML_type Context.generic}, 
  1145   for theories and proof contexts. This type is defined as follows
  1144   for theories and proof contexts. This type is defined as follows
  1146 *}
  1145 *}
  1147 
  1146 
  1197   \end{isabelle}
  1196   \end{isabelle}
  1198 
  1197 
  1199   On the ML-level, we can add theorems to the list with @{ML FooRules.add_thm}:
  1198   On the ML-level, we can add theorems to the list with @{ML FooRules.add_thm}:
  1200 *} 
  1199 *} 
  1201 
  1200 
  1202 setup %gray {* 
  1201 setup %gray {* Context.theory_map (FooRules.add_thm @{thm TrueI}) *}
  1203   Context.theory_map (FooRules.add_thm @{thm TrueI}) 
       
  1204 *}
       
  1205 
  1202 
  1206 text {*
  1203 text {*
  1207   The rules in the list can be retrieved using the function 
  1204   The rules in the list can be retrieved using the function 
  1208   @{ML FooRules.get}:
  1205   @{ML FooRules.get}:
  1209 
  1206 
  1210   @{ML_response_fake [display,gray] "FooRules.get @{context}" "[\"True\", \"?C\",\"?B\"]"}
  1207   @{ML_response_fake [display,gray] 
       
  1208   "FooRules.get @{context}" 
       
  1209   "[\"True\", \"?C\",\"?B\"]"}
       
  1210 
       
  1211   Note that this function takes a proof context as argument. This might be 
       
  1212   confusing, since the theorem list is stored as theory data. The proof context 
       
  1213   however conatains the information about the current theory and so the function
       
  1214   can access the theorem list in the theory via the context. 
  1211 
  1215 
  1212   \begin{readmore}
  1216   \begin{readmore}
  1213   For more information see @{ML_file "Pure/Tools/named_thms.ML"}.
  1217   For more information about named theorem lists see 
       
  1218   @{ML_file "Pure/Tools/named_thms.ML"}.
  1214   \end{readmore}
  1219   \end{readmore}
  1215 
  1220 
  1216   The second special instance of the data storage mechanism are configuration
  1221   The second special instance of the data storage mechanism are configuration
  1217   values. They are used to enable users to configure tools without having to
  1222   values. They are used to enable users to configure tools without having to
  1218   resort to the ML-level (and also to avoid references). Assume you want the
  1223   resort to the ML-level (and also to avoid references). Assume you want the
  1249 
  1254 
  1250   @{ML_response [display,gray] 
  1255   @{ML_response [display,gray] 
  1251   "Config.get @{context} bval" 
  1256   "Config.get @{context} bval" 
  1252   "true"}
  1257   "true"}
  1253 
  1258 
  1254   or from a theory using the function @{ML_ind get_thy in Config}
  1259   or directly from a theory using the function @{ML_ind get_thy in Config}
  1255 
  1260 
  1256   @{ML_response [display,gray] 
  1261   @{ML_response [display,gray] 
  1257   "Config.get_thy @{theory} bval" 
  1262   "Config.get_thy @{theory} bval" 
  1258   "true"}
  1263   "true"}
  1259 
  1264 
  1260   It is also possible to manipulate the configuration values
  1265   It is also possible to manipulate the configuration values
  1261   from the ML-level with the function @{ML_ind put in Config}
  1266   from the ML-level with the functions @{ML_ind put in Config}
  1262   or @{ML_ind put_thy in Config}. For example
  1267   and @{ML_ind put_thy in Config}. For example
  1263 
  1268 
  1264   @{ML_response [display,gray]
  1269   @{ML_response [display,gray]
  1265   "let
  1270   "let
  1266   val ctxt = @{context}
  1271   val ctxt = @{context}
  1267   val ctxt' = Config.put sval \"foo\" ctxt
  1272   val ctxt' = Config.put sval \"foo\" ctxt
       
  1273   val ctxt'' = Config.put sval \"bar\" ctxt'
  1268 in
  1274 in
  1269   (Config.get ctxt sval, Config.get ctxt' sval)
  1275   (Config.get ctxt sval, Config.get ctxt' sval, Config.get ctxt'' sval)
  1270 end"
  1276 end"
  1271   "(\"some string\", \"foo\")"}
  1277   "(\"some string\", \"foo\", \"bar\")"}
  1272 
  1278 
  1273   \begin{readmore}
  1279   \begin{readmore}
  1274   For more information about configuration values see 
  1280   For more information about configuration values see 
  1275   the files @{ML_file "Pure/Isar/attrib.ML"} and 
  1281   the files @{ML_file "Pure/Isar/attrib.ML"} and 
  1276   @{ML_file "Pure/config.ML"}.
  1282   @{ML_file "Pure/config.ML"}.
  1285   and @{ML_type thm}. The section on ML-antiquotations shows how to refer 
  1291   and @{ML_type thm}. The section on ML-antiquotations shows how to refer 
  1286   statically to entities from the logic level of Isabelle. Isabelle also
  1292   statically to entities from the logic level of Isabelle. Isabelle also
  1287   contains mechanisms for storing arbitrary data in theory and proof 
  1293   contains mechanisms for storing arbitrary data in theory and proof 
  1288   contexts.
  1294   contexts.
  1289 
  1295 
  1290   This chapter also mentions two coding conventions: namely printing
  1296   \begin{conventions}
  1291   entities belonging together as one string, and not using references
  1297   \begin{itemize}
  1292   in any Isabelle code.
  1298   \item Print messages that belong together as a single string.
       
  1299   \item Do not use references in any Isabelle code.
       
  1300   \end{itemize}
       
  1301   \end{conventions}
       
  1302 
  1293 *}
  1303 *}
  1294 
  1304 
  1295 
  1305 
  1296 (**************************************************)
  1306 (**************************************************)
  1297 (* bak                                            *)
  1307 (* bak                                            *)