src/HOL/ex/Cartouche_Examples.thy
changeset 63120 629a4c5e953e
parent 63054 1b237d147cc4
child 67382 39e4668ded4f
equal deleted inserted replaced
63119:547460dc5c1e 63120:629a4c5e953e
     5 section \<open>Some examples with text cartouches\<close>
     5 section \<open>Some examples with text cartouches\<close>
     6 
     6 
     7 theory Cartouche_Examples
     7 theory Cartouche_Examples
     8 imports Main
     8 imports Main
     9 keywords
     9 keywords
    10   "cartouche" "term_cartouche" :: diag and
    10   "cartouche" :: diag and
    11   "text_cartouche" :: thy_decl
    11   "text_cartouche" :: thy_decl
    12 begin
    12 begin
    13 
    13 
    14 subsection \<open>Regular outer syntax\<close>
    14 subsection \<open>Regular outer syntax\<close>
    15 
    15 
    17   as alternative to the traditional \<open>verbatim\<close> tokens.  An example is
    17   as alternative to the traditional \<open>verbatim\<close> tokens.  An example is
    18   this text block.\<close>  \<comment> \<open>The same works for small side-comments.\<close>
    18   this text block.\<close>  \<comment> \<open>The same works for small side-comments.\<close>
    19 
    19 
    20 notepad
    20 notepad
    21 begin
    21 begin
    22   txt \<open>Moreover, cartouches work as additional syntax in the
    22   txt \<open>Cartouches work as additional syntax for embedded language tokens
    23     \<open>altstring\<close> category, for literal fact references.  For example:\<close>
    23     (types, terms, props) and as a replacement for the \<open>altstring\<close> category
       
    24     (for literal fact references). For example:\<close>
    24 
    25 
    25   fix x y :: 'a
    26   fix x y :: 'a
    26   assume "x = y"
    27   assume \<open>x = y\<close>
    27   note \<open>x = y\<close>
    28   note \<open>x = y\<close>
    28   have "x = y" by (rule \<open>x = y\<close>)
    29   have \<open>x = y\<close> by (rule \<open>x = y\<close>)
    29   from \<open>x = y\<close> have "x = y" .
    30   from \<open>x = y\<close> have \<open>x = y\<close> .
    30 
    31 
    31   txt \<open>Of course, this can be nested inside formal comments and
    32   txt \<open>Of course, this can be nested inside formal comments and
    32     antiquotations, e.g. like this @{thm \<open>x = y\<close>} or this @{thm sym
    33     antiquotations, e.g. like this @{thm \<open>x = y\<close>} or this @{thm sym
    33     [OF \<open>x = y\<close>]}.\<close>
    34     [OF \<open>x = y\<close>]}.\<close>
    34 
    35 
    35   have "x = y"
    36   have \<open>x = y\<close>
    36     by (tactic \<open>resolve_tac @{context} @{thms \<open>x = y\<close>} 1\<close>)  \<comment> \<open>more cartouches involving ML\<close>
    37     by (tactic \<open>resolve_tac @{context} @{thms \<open>x = y\<close>} 1\<close>)
       
    38       \<comment> \<open>more cartouches involving ML\<close>
    37 end
    39 end
    38 
    40 
    39 
    41 
    40 subsection \<open>Outer syntax: cartouche within command syntax\<close>
    42 subsection \<open>Outer syntax: cartouche within command syntax\<close>
    41 
    43 
    76         | _ => err ())
    78         | _ => err ())
    77       end;
    79       end;
    78   end;
    80   end;
    79 \<close>
    81 \<close>
    80 
    82 
    81 syntax "_cartouche_string" :: "cartouche_position \<Rightarrow> string"  ("_")
    83 syntax "_cartouche_string" :: \<open>cartouche_position \<Rightarrow> string\<close>  ("_")
    82 
    84 
    83 parse_translation \<open>
    85 parse_translation \<open>
    84   [(@{syntax_const "_cartouche_string"},
    86   [(@{syntax_const "_cartouche_string"},
    85     K (string_tr (Symbol_Pos.cartouche_content o Symbol_Pos.explode)))]
    87     K (string_tr (Symbol_Pos.cartouche_content o Symbol_Pos.explode)))]
    86 \<close>
    88 \<close>
    87 
    89 
    88 term "\<open>\<close>"
    90 term \<open>\<open>\<close>\<close>
    89 term "\<open>abc\<close>"
    91 term \<open>\<open>abc\<close>\<close>
    90 term "\<open>abc\<close> @ \<open>xyz\<close>"
    92 term \<open>\<open>abc\<close> @ \<open>xyz\<close>\<close>
    91 term "\<open>\<newline>\<close>"
    93 term \<open>\<open>\<newline>\<close>\<close>
    92 term "\<open>\001\010\100\<close>"
       
    93 
    94 
    94 
    95 
    95 subsection \<open>Alternate outer and inner syntax: string literals\<close>
    96 subsection \<open>Alternate outer and inner syntax: string literals\<close>
    96 
    97 
    97 subsubsection \<open>Nested quotes\<close>
    98 subsubsection \<open>Nested quotes\<close>
    98 
    99 
    99 syntax "_string_string" :: "string_position \<Rightarrow> string"  ("_")
   100 syntax "_string_string" :: \<open>string_position \<Rightarrow> string\<close>  ("_")
   100 
   101 
   101 parse_translation \<open>
   102 parse_translation \<open>
   102   [(@{syntax_const "_string_string"}, K (string_tr Lexicon.explode_string))]
   103   [(@{syntax_const "_string_string"}, K (string_tr Lexicon.explode_string))]
   103 \<close>
   104 \<close>
   104 
   105 
   105 term "\"\""
   106 term \<open>""\<close>
   106 term "\"abc\""
   107 term \<open>"abc"\<close>
   107 term "\"abc\" @ \"xyz\""
   108 term \<open>"abc" @ "xyz"\<close>
   108 term "\"\<newline>\""
   109 term \<open>"\<newline>"\<close>
   109 term "\"\001\010\100\""
   110 term \<open>"\001\010\100"\<close>
   110 
       
   111 
       
   112 subsubsection \<open>Term cartouche and regular quotes\<close>
       
   113 
       
   114 ML \<open>
       
   115   Outer_Syntax.command @{command_keyword term_cartouche} ""
       
   116     (Parse.inner_syntax Parse.cartouche >> (fn s =>
       
   117       Toplevel.keep (fn state =>
       
   118         let
       
   119           val ctxt = Toplevel.context_of state;
       
   120           val t = Syntax.read_term ctxt s;
       
   121         in writeln (Syntax.string_of_term ctxt t) end)))
       
   122 \<close>
       
   123 
       
   124 term_cartouche \<open>""\<close>
       
   125 term_cartouche \<open>"abc"\<close>
       
   126 term_cartouche \<open>"abc" @ "xyz"\<close>
       
   127 term_cartouche \<open>"\<newline>"\<close>
       
   128 term_cartouche \<open>"\001\010\100"\<close>
       
   129 
   111 
   130 
   112 
   131 subsubsection \<open>Further nesting: antiquotations\<close>
   113 subsubsection \<open>Further nesting: antiquotations\<close>
   132 
   114 
   133 setup \<comment> "ML antiquotation"
   115 ML \<open>
   134 \<open>
   116   @{term \<open>""\<close>};
   135   ML_Antiquotation.inline @{binding term_cartouche}
   117   @{term \<open>"abc"\<close>};
   136     (Args.context -- Scan.lift (Parse.inner_syntax Parse.cartouche) >>
   118   @{term \<open>"abc" @ "xyz"\<close>};
   137       (fn (ctxt, s) => ML_Syntax.atomic (ML_Syntax.print_term (Syntax.read_term ctxt s))))
   119   @{term \<open>"\<newline>"\<close>};
   138 \<close>
   120   @{term \<open>"\001\010\100"\<close>};
   139 
       
   140 setup \<comment> "document antiquotation"
       
   141 \<open>
       
   142   Thy_Output.antiquotation @{binding ML_cartouche}
       
   143     (Scan.lift Args.cartouche_input) (fn {context, ...} => fn source =>
       
   144       let
       
   145         val toks = ML_Lex.read "fn _ => (" @ ML_Lex.read_source false source @ ML_Lex.read ");";
       
   146         val _ = ML_Context.eval_in (SOME context) ML_Compiler.flags (Input.pos_of source) toks;
       
   147       in "" end);
       
   148 \<close>
       
   149 
       
   150 ML \<open>
       
   151   @{term_cartouche \<open>""\<close>};
       
   152   @{term_cartouche \<open>"abc"\<close>};
       
   153   @{term_cartouche \<open>"abc" @ "xyz"\<close>};
       
   154   @{term_cartouche \<open>"\<newline>"\<close>};
       
   155   @{term_cartouche \<open>"\001\010\100"\<close>};
       
   156 \<close>
   121 \<close>
   157 
   122 
   158 text \<open>
   123 text \<open>
   159   @{ML_cartouche
   124   @{ML
   160     \<open>
   125     \<open>
   161       (
   126       (
   162         @{term_cartouche \<open>""\<close>};
   127         @{term \<open>""\<close>};
   163         @{term_cartouche \<open>"abc"\<close>};
   128         @{term \<open>"abc"\<close>};
   164         @{term_cartouche \<open>"abc" @ "xyz"\<close>};
   129         @{term \<open>"abc" @ "xyz"\<close>};
   165         @{term_cartouche \<open>"\<newline>"\<close>};
   130         @{term \<open>"\<newline>"\<close>};
   166         @{term_cartouche \<open>"\001\010\100"\<close>}
   131         @{term \<open>"\001\010\100"\<close>}
   167       )
   132       )
   168     \<close>
   133     \<close>
   169   }
   134   }
   170 \<close>
   135 \<close>
   171 
   136 
   179       >> Thy_Output.document_command {markdown = true})
   144       >> Thy_Output.document_command {markdown = true})
   180 \<close>
   145 \<close>
   181 
   146 
   182 text_cartouche
   147 text_cartouche
   183 \<open>
   148 \<open>
   184   @{ML_cartouche
   149   @{ML
   185     \<open>
   150     \<open>
   186       (
   151       (
   187         @{term_cartouche \<open>""\<close>};
   152         @{term \<open>""\<close>};
   188         @{term_cartouche \<open>"abc"\<close>};
   153         @{term \<open>"abc"\<close>};
   189         @{term_cartouche \<open>"abc" @ "xyz"\<close>};
   154         @{term \<open>"abc" @ "xyz"\<close>};
   190         @{term_cartouche \<open>"\<newline>"\<close>};
   155         @{term \<open>"\<newline>"\<close>};
   191         @{term_cartouche \<open>"\001\010\100"\<close>}
   156         @{term \<open>"\001\010\100"\<close>}
   192       )
   157       )
   193     \<close>
   158     \<close>
   194   }
   159   }
   195 \<close>
   160 \<close>
   196 
   161 
   224 method_setup ml_tactic = \<open>
   189 method_setup ml_tactic = \<open>
   225   Scan.lift Args.cartouche_input
   190   Scan.lift Args.cartouche_input
   226     >> (fn arg => fn ctxt => SIMPLE_METHOD (ML_Tactic.ml_tactic arg ctxt))
   191     >> (fn arg => fn ctxt => SIMPLE_METHOD (ML_Tactic.ml_tactic arg ctxt))
   227 \<close>
   192 \<close>
   228 
   193 
   229 lemma "A \<and> B \<longrightarrow> B \<and> A"
   194 lemma \<open>A \<and> B \<longrightarrow> B \<and> A\<close>
   230   apply (ml_tactic \<open>resolve_tac @{context} @{thms impI} 1\<close>)
   195   apply (ml_tactic \<open>resolve_tac @{context} @{thms impI} 1\<close>)
   231   apply (ml_tactic \<open>eresolve_tac @{context} @{thms conjE} 1\<close>)
   196   apply (ml_tactic \<open>eresolve_tac @{context} @{thms conjE} 1\<close>)
   232   apply (ml_tactic \<open>resolve_tac @{context} @{thms conjI} 1\<close>)
   197   apply (ml_tactic \<open>resolve_tac @{context} @{thms conjI} 1\<close>)
   233   apply (ml_tactic \<open>ALLGOALS (assume_tac @{context})\<close>)
   198   apply (ml_tactic \<open>ALLGOALS (assume_tac @{context})\<close>)
   234   done
   199   done
   235 
   200 
   236 lemma "A \<and> B \<longrightarrow> B \<and> A" by (ml_tactic \<open>blast_tac ctxt 1\<close>)
   201 lemma \<open>A \<and> B \<longrightarrow> B \<and> A\<close> by (ml_tactic \<open>blast_tac ctxt 1\<close>)
   237 
   202 
   238 ML \<open>@{lemma "A \<and> B \<longrightarrow> B \<and> A" by (ml_tactic \<open>blast_tac ctxt 1\<close>)}\<close>
   203 ML \<open>@{lemma \<open>A \<and> B \<longrightarrow> B \<and> A\<close> by (ml_tactic \<open>blast_tac ctxt 1\<close>)}\<close>
   239 
   204 
   240 text \<open>@{ML "@{lemma \"A \<and> B \<longrightarrow> B \<and> A\" by (ml_tactic \<open>blast_tac ctxt 1\<close>)}"}\<close>
   205 text \<open>@{ML \<open>@{lemma \<open>A \<and> B \<longrightarrow> B \<and> A\<close> by (ml_tactic \<open>blast_tac ctxt 1\<close>)}\<close>}\<close>
   241 
   206 
   242 
   207 
   243 subsubsection \<open>Implicit version: method with special name "cartouche" (dynamic!)\<close>
   208 subsubsection \<open>Implicit version: method with special name "cartouche" (dynamic!)\<close>
   244 
   209 
   245 method_setup "cartouche" = \<open>
   210 method_setup "cartouche" = \<open>
   246   Scan.lift Args.cartouche_input
   211   Scan.lift Args.cartouche_input
   247     >> (fn arg => fn ctxt => SIMPLE_METHOD (ML_Tactic.ml_tactic arg ctxt))
   212     >> (fn arg => fn ctxt => SIMPLE_METHOD (ML_Tactic.ml_tactic arg ctxt))
   248 \<close>
   213 \<close>
   249 
   214 
   250 lemma "A \<and> B \<longrightarrow> B \<and> A"
   215 lemma \<open>A \<and> B \<longrightarrow> B \<and> A\<close>
   251   apply \<open>resolve_tac @{context} @{thms impI} 1\<close>
   216   apply \<open>resolve_tac @{context} @{thms impI} 1\<close>
   252   apply \<open>eresolve_tac @{context} @{thms conjE} 1\<close>
   217   apply \<open>eresolve_tac @{context} @{thms conjE} 1\<close>
   253   apply \<open>resolve_tac @{context} @{thms conjI} 1\<close>
   218   apply \<open>resolve_tac @{context} @{thms conjI} 1\<close>
   254   apply \<open>ALLGOALS (assume_tac @{context})\<close>
   219   apply \<open>ALLGOALS (assume_tac @{context})\<close>
   255   done
   220   done
   256 
   221 
   257 lemma "A \<and> B \<longrightarrow> B \<and> A"
   222 lemma \<open>A \<and> B \<longrightarrow> B \<and> A\<close>
   258   by (\<open>resolve_tac @{context} @{thms impI} 1\<close>,
   223   by (\<open>resolve_tac @{context} @{thms impI} 1\<close>,
   259     \<open>eresolve_tac @{context} @{thms conjE} 1\<close>,
   224     \<open>eresolve_tac @{context} @{thms conjE} 1\<close>,
   260     \<open>resolve_tac @{context} @{thms conjI} 1\<close>,
   225     \<open>resolve_tac @{context} @{thms conjI} 1\<close>,
   261     \<open>assume_tac @{context} 1\<close>+)
   226     \<open>assume_tac @{context} 1\<close>+)
   262 
   227