doc-src/IsarImplementation/Thy/document/ML.tex
changeset 28110 9d121b171a0a
parent 28086 db584d1d2af4
child 28283 dbe9c820e4d2
equal deleted inserted replaced
28109:3f76ae637f71 28110:9d121b171a0a
   367   continued by an application of a function \verb|g : foo -> bar|,
   367   continued by an application of a function \verb|g : foo -> bar|,
   368   and so on.  As a canoncial example, take functions enriching
   368   and so on.  As a canoncial example, take functions enriching
   369   a theory by constant declararion and primitive definitions:
   369   a theory by constant declararion and primitive definitions:
   370 
   370 
   371   \smallskip\begin{mldecls}
   371   \smallskip\begin{mldecls}
   372   \verb|Sign.declare_const: Properties.T -> bstring * typ * mixfix|\isasep\isanewline%
   372   \verb|Sign.declare_const: Properties.T -> (Name.binding * typ) * mixfix|\isasep\isanewline%
   373 \verb|  -> theory -> term * theory| \\
   373 \verb|  -> theory -> term * theory| \\
   374   \verb|Thm.add_def: bool -> bool -> bstring * term -> theory -> thm * theory|
   374   \verb|Thm.add_def: bool -> bool -> bstring * term -> theory -> thm * theory|
   375   \end{mldecls}
   375   \end{mldecls}
   376 
   376 
   377   \noindent Written with naive application, an addition of constant
   377   \noindent Written with naive application, an addition of constant
   380 
   380 
   381   \smallskip\begin{mldecls}
   381   \smallskip\begin{mldecls}
   382   \verb|(fn (t, thy) => Thm.add_def false false|\isasep\isanewline%
   382   \verb|(fn (t, thy) => Thm.add_def false false|\isasep\isanewline%
   383 \verb|  ("bar_def", Logic.mk_equals (t, @{term "%x. x"})) thy)|\isasep\isanewline%
   383 \verb|  ("bar_def", Logic.mk_equals (t, @{term "%x. x"})) thy)|\isasep\isanewline%
   384 \verb|    (Sign.declare_const []|\isasep\isanewline%
   384 \verb|    (Sign.declare_const []|\isasep\isanewline%
   385 \verb|      ("bar", @{typ "foo => foo"}, NoSyn) thy)|
   385 \verb|      ((Name.binding "bar", @{typ "foo => foo"}), NoSyn) thy)|
   386   \end{mldecls}
   386   \end{mldecls}
   387 
   387 
   388   \noindent With increasing numbers of applications, this code gets quite
   388   \noindent With increasing numbers of applications, this code gets quite
   389   unreadable.  Further, it is unintuitive that things are
   389   unreadable.  Further, it is unintuitive that things are
   390   written down in the opposite order as they actually ``happen''.%
   390   written down in the opposite order as they actually ``happen''.%
   395 \noindent At this stage, Isabelle offers some combinators which allow
   395 \noindent At this stage, Isabelle offers some combinators which allow
   396   for more convenient notation, most notably reverse application:
   396   for more convenient notation, most notably reverse application:
   397 
   397 
   398   \smallskip\begin{mldecls}
   398   \smallskip\begin{mldecls}
   399 \verb|thy|\isasep\isanewline%
   399 \verb|thy|\isasep\isanewline%
   400 \verb||\verb,|,\verb|> Sign.declare_const [] ("bar", @{typ "foo => foo"}, NoSyn)|\isasep\isanewline%
   400 \verb||\verb,|,\verb|> Sign.declare_const [] ((Name.binding "bar", @{typ "foo => foo"}), NoSyn)|\isasep\isanewline%
   401 \verb||\verb,|,\verb|> (fn (t, thy) => thy|\isasep\isanewline%
   401 \verb||\verb,|,\verb|> (fn (t, thy) => thy|\isasep\isanewline%
   402 \verb||\verb,|,\verb|> Thm.add_def false false|\isasep\isanewline%
   402 \verb||\verb,|,\verb|> Thm.add_def false false|\isasep\isanewline%
   403 \verb|     ("bar_def", Logic.mk_equals (t, @{term "%x. x"})))|
   403 \verb|     ("bar_def", Logic.mk_equals (t, @{term "%x. x"})))|
   404   \end{mldecls}%
   404   \end{mldecls}%
   405 \end{isamarkuptext}%
   405 \end{isamarkuptext}%
   434   set of combinators is at hand, most notably \verb|op |\verb,|,\verb|->|
   434   set of combinators is at hand, most notably \verb|op |\verb,|,\verb|->|
   435   which allows curried access to side results:
   435   which allows curried access to side results:
   436 
   436 
   437   \smallskip\begin{mldecls}
   437   \smallskip\begin{mldecls}
   438 \verb|thy|\isasep\isanewline%
   438 \verb|thy|\isasep\isanewline%
   439 \verb||\verb,|,\verb|> Sign.declare_const [] ("bar", @{typ "foo => foo"}, NoSyn)|\isasep\isanewline%
   439 \verb||\verb,|,\verb|> Sign.declare_const [] ((Name.binding "bar", @{typ "foo => foo"}), NoSyn)|\isasep\isanewline%
   440 \verb||\verb,|,\verb|-> (fn t => Thm.add_def false false|\isasep\isanewline%
   440 \verb||\verb,|,\verb|-> (fn t => Thm.add_def false false|\isasep\isanewline%
   441 \verb|      ("bar_def", Logic.mk_equals (t, @{term "%x. x"})))|\isasep\isanewline%
   441 \verb|      ("bar_def", Logic.mk_equals (t, @{term "%x. x"})))|\isasep\isanewline%
   442 
   442 
   443   \end{mldecls}
   443   \end{mldecls}
   444 
   444 
   445   \noindent \verb|op |\verb,|,\verb|>>| allows for processing side results on their own:
   445   \noindent \verb|op |\verb,|,\verb|>>| allows for processing side results on their own:
   446 
   446 
   447   \smallskip\begin{mldecls}
   447   \smallskip\begin{mldecls}
   448 \verb|thy|\isasep\isanewline%
   448 \verb|thy|\isasep\isanewline%
   449 \verb||\verb,|,\verb|> Sign.declare_const [] ("bar", @{typ "foo => foo"}, NoSyn)|\isasep\isanewline%
   449 \verb||\verb,|,\verb|> Sign.declare_const [] ((Name.binding "bar", @{typ "foo => foo"}), NoSyn)|\isasep\isanewline%
   450 \verb||\verb,|,\verb|>> (fn t => Logic.mk_equals (t, @{term "%x. x"}))|\isasep\isanewline%
   450 \verb||\verb,|,\verb|>> (fn t => Logic.mk_equals (t, @{term "%x. x"}))|\isasep\isanewline%
   451 \verb||\verb,|,\verb|-> (fn def => Thm.add_def false false ("bar_def", def))|\isasep\isanewline%
   451 \verb||\verb,|,\verb|-> (fn def => Thm.add_def false false ("bar_def", def))|\isasep\isanewline%
   452 
   452 
   453   \end{mldecls}
   453   \end{mldecls}
   454 
   454 
   455   \noindent Orthogonally, \verb|op |\verb,|,\verb||\verb,|,\verb|>| applies a transformation
   455   \noindent Orthogonally, \verb|op |\verb,|,\verb||\verb,|,\verb|>| applies a transformation
   456   in the presence of side results which are left unchanged:
   456   in the presence of side results which are left unchanged:
   457 
   457 
   458   \smallskip\begin{mldecls}
   458   \smallskip\begin{mldecls}
   459 \verb|thy|\isasep\isanewline%
   459 \verb|thy|\isasep\isanewline%
   460 \verb||\verb,|,\verb|> Sign.declare_const [] ("bar", @{typ "foo => foo"}, NoSyn)|\isasep\isanewline%
   460 \verb||\verb,|,\verb|> Sign.declare_const [] ((Name.binding "bar", @{typ "foo => foo"}), NoSyn)|\isasep\isanewline%
   461 \verb||\verb,|,\verb||\verb,|,\verb|> Sign.add_path "foobar"|\isasep\isanewline%
   461 \verb||\verb,|,\verb||\verb,|,\verb|> Sign.add_path "foobar"|\isasep\isanewline%
   462 \verb||\verb,|,\verb|-> (fn t => Thm.add_def false false|\isasep\isanewline%
   462 \verb||\verb,|,\verb|-> (fn t => Thm.add_def false false|\isasep\isanewline%
   463 \verb|      ("bar_def", Logic.mk_equals (t, @{term "%x. x"})))|\isasep\isanewline%
   463 \verb|      ("bar_def", Logic.mk_equals (t, @{term "%x. x"})))|\isasep\isanewline%
   464 \verb||\verb,|,\verb||\verb,|,\verb|> Sign.restore_naming thy|\isasep\isanewline%
   464 \verb||\verb,|,\verb||\verb,|,\verb|> Sign.restore_naming thy|\isasep\isanewline%
   465 
   465 
   467 
   467 
   468   \noindent \verb|op |\verb,|,\verb||\verb,|,\verb|>>| accumulates side results:
   468   \noindent \verb|op |\verb,|,\verb||\verb,|,\verb|>>| accumulates side results:
   469 
   469 
   470   \smallskip\begin{mldecls}
   470   \smallskip\begin{mldecls}
   471 \verb|thy|\isasep\isanewline%
   471 \verb|thy|\isasep\isanewline%
   472 \verb||\verb,|,\verb|> Sign.declare_const [] ("bar", @{typ "foo => foo"}, NoSyn)|\isasep\isanewline%
   472 \verb||\verb,|,\verb|> Sign.declare_const [] ((Name.binding "bar", @{typ "foo => foo"}), NoSyn)|\isasep\isanewline%
   473 \verb||\verb,|,\verb||\verb,|,\verb|>> Sign.declare_const [] ("foobar", @{typ "foo => foo"}, NoSyn)|\isasep\isanewline%
   473 \verb||\verb,|,\verb||\verb,|,\verb|>> Sign.declare_const [] ((Name.binding "foobar", @{typ "foo => foo"}), NoSyn)|\isasep\isanewline%
   474 \verb||\verb,|,\verb|-> (fn (t1, t2) => Thm.add_def false false|\isasep\isanewline%
   474 \verb||\verb,|,\verb|-> (fn (t1, t2) => Thm.add_def false false|\isasep\isanewline%
   475 \verb|      ("bar_def", Logic.mk_equals (t1, t2)))|\isasep\isanewline%
   475 \verb|      ("bar_def", Logic.mk_equals (t1, t2)))|\isasep\isanewline%
   476 
   476 
   477   \end{mldecls}%
   477   \end{mldecls}%
   478 \end{isamarkuptext}%
   478 \end{isamarkuptext}%
   529 \verb|let|\isasep\isanewline%
   529 \verb|let|\isasep\isanewline%
   530 \verb|  val consts = ["foo", "bar"];|\isasep\isanewline%
   530 \verb|  val consts = ["foo", "bar"];|\isasep\isanewline%
   531 \verb|in|\isasep\isanewline%
   531 \verb|in|\isasep\isanewline%
   532 \verb|  thy|\isasep\isanewline%
   532 \verb|  thy|\isasep\isanewline%
   533 \verb|  |\verb,|,\verb|> fold_map (fn const => Sign.declare_const []|\isasep\isanewline%
   533 \verb|  |\verb,|,\verb|> fold_map (fn const => Sign.declare_const []|\isasep\isanewline%
   534 \verb|       (const, @{typ "foo => foo"}, NoSyn)) consts|\isasep\isanewline%
   534 \verb|       ((Name.binding const, @{typ "foo => foo"}), NoSyn)) consts|\isasep\isanewline%
   535 \verb|  |\verb,|,\verb|>> map (fn t => Logic.mk_equals (t, @{term "%x. x"}))|\isasep\isanewline%
   535 \verb|  |\verb,|,\verb|>> map (fn t => Logic.mk_equals (t, @{term "%x. x"}))|\isasep\isanewline%
   536 \verb|  |\verb,|,\verb|-> (fn defs => fold_map (fn def =>|\isasep\isanewline%
   536 \verb|  |\verb,|,\verb|-> (fn defs => fold_map (fn def =>|\isasep\isanewline%
   537 \verb|       Thm.add_def false false ("", def)) defs)|\isasep\isanewline%
   537 \verb|       Thm.add_def false false ("", def)) defs)|\isasep\isanewline%
   538 \verb|end|
   538 \verb|end|
   539   \end{mldecls}%
   539   \end{mldecls}%
   597   in a series of context transformations:
   597   in a series of context transformations:
   598 
   598 
   599   \smallskip\begin{mldecls}
   599   \smallskip\begin{mldecls}
   600 \verb|thy|\isasep\isanewline%
   600 \verb|thy|\isasep\isanewline%
   601 \verb||\verb,|,\verb|> tap (fn _ => writeln "now adding constant")|\isasep\isanewline%
   601 \verb||\verb,|,\verb|> tap (fn _ => writeln "now adding constant")|\isasep\isanewline%
   602 \verb||\verb,|,\verb|> Sign.declare_const [] ("bar", @{typ "foo => foo"}, NoSyn)|\isasep\isanewline%
   602 \verb||\verb,|,\verb|> Sign.declare_const [] ((Name.binding "bar", @{typ "foo => foo"}), NoSyn)|\isasep\isanewline%
   603 \verb||\verb,|,\verb||\verb,|,\verb|>> `(fn thy => Sign.declared_const thy|\isasep\isanewline%
   603 \verb||\verb,|,\verb||\verb,|,\verb|>> `(fn thy => Sign.declared_const thy|\isasep\isanewline%
   604 \verb|         (Sign.full_name thy "foobar"))|\isasep\isanewline%
   604 \verb|         (Sign.full_name thy "foobar"))|\isasep\isanewline%
   605 \verb||\verb,|,\verb|-> (fn (t, b) => if b then I|\isasep\isanewline%
   605 \verb||\verb,|,\verb|-> (fn (t, b) => if b then I|\isasep\isanewline%
   606 \verb|       else Sign.declare_const []|\isasep\isanewline%
   606 \verb|       else Sign.declare_const []|\isasep\isanewline%
   607 \verb|         ("foobar", @{typ "foo => foo"}, NoSyn) #> snd)|\isasep\isanewline%
   607 \verb|         ((Name.binding "foobar", @{typ "foo => foo"}), NoSyn) #> snd)|\isasep\isanewline%
   608 
   608 
   609   \end{mldecls}%
   609   \end{mldecls}%
   610 \end{isamarkuptext}%
   610 \end{isamarkuptext}%
   611 \isamarkuptrue%
   611 \isamarkuptrue%
   612 %
   612 %