src/HOL/Library/LaTeXsugar.thy
author haftmann
Fri Mar 22 19:18:08 2019 +0000 (3 months ago)
changeset 69946 494934c30f38
parent 69593 3dda49e08b9d
permissions -rw-r--r--
improved code equations taken over from AFP
     1 (*  Title:      HOL/Library/LaTeXsugar.thy
     2     Author:     Gerwin Klein, Tobias Nipkow, Norbert Schirmer
     3     Copyright   2005 NICTA and TUM
     4 *)
     5 
     6 (*<*)
     7 theory LaTeXsugar
     8 imports Main
     9 begin
    10 
    11 (* LOGIC *)
    12 notation (latex output)
    13   If  ("(\<^latex>\<open>\\textsf{\<close>if\<^latex>\<open>}\<close> (_)/ \<^latex>\<open>\\textsf{\<close>then\<^latex>\<open>}\<close> (_)/ \<^latex>\<open>\\textsf{\<close>else\<^latex>\<open>}\<close> (_))" 10)
    14 
    15 syntax (latex output)
    16 
    17   "_Let"        :: "[letbinds, 'a] => 'a"
    18   ("(\<^latex>\<open>\\textsf{\<close>let\<^latex>\<open>}\<close> (_)/ \<^latex>\<open>\\textsf{\<close>in\<^latex>\<open>}\<close> (_))" 10)
    19 
    20   "_case_syntax":: "['a, cases_syn] => 'b"
    21   ("(\<^latex>\<open>\\textsf{\<close>case\<^latex>\<open>}\<close> _ \<^latex>\<open>\\textsf{\<close>of\<^latex>\<open>}\<close>/ _)" 10)
    22 
    23 
    24 (* SETS *)
    25 
    26 (* empty set *)
    27 notation (latex)
    28   "Set.empty" ("\<emptyset>")
    29 
    30 (* insert *)
    31 translations 
    32   "{x} \<union> A" <= "CONST insert x A"
    33   "{x,y}" <= "{x} \<union> {y}"
    34   "{x,y} \<union> A" <= "{x} \<union> ({y} \<union> A)"
    35   "{x}" <= "{x} \<union> \<emptyset>"
    36 
    37 (* set comprehension *)
    38 syntax (latex output)
    39   "_Collect" :: "pttrn => bool => 'a set"              ("(1{_ | _})")
    40   "_CollectIn" :: "pttrn => 'a set => bool => 'a set"   ("(1{_ \<in> _ | _})")
    41 translations
    42   "_Collect p P"      <= "{p. P}"
    43   "_Collect p P"      <= "{p|xs. P}"
    44   "_CollectIn p A P"  <= "{p : A. P}"
    45 
    46 (* card *)
    47 notation (latex output)
    48   card  ("|_|")
    49 
    50 (* LISTS *)
    51 
    52 (* Cons *)
    53 notation (latex)
    54   Cons  ("_ \<cdot>/ _" [66,65] 65)
    55 
    56 (* length *)
    57 notation (latex output)
    58   length  ("|_|")
    59 
    60 (* nth *)
    61 notation (latex output)
    62   nth  ("_\<^latex>\<open>\\ensuremath{_{[\\mathit{\<close>_\<^latex>\<open>}]}}\<close>" [1000,0] 1000)
    63 
    64 (* DUMMY *)
    65 consts DUMMY :: 'a ("\<^latex>\<open>\\_\<close>")
    66 
    67 (* THEOREMS *)
    68 notation (Rule output)
    69   Pure.imp  ("\<^latex>\<open>\\mbox{}\\inferrule{\\mbox{\<close>_\<^latex>\<open>}}\<close>\<^latex>\<open>{\\mbox{\<close>_\<^latex>\<open>}}\<close>")
    70 
    71 syntax (Rule output)
    72   "_bigimpl" :: "asms \<Rightarrow> prop \<Rightarrow> prop"
    73   ("\<^latex>\<open>\\mbox{}\\inferrule{\<close>_\<^latex>\<open>}\<close>\<^latex>\<open>{\\mbox{\<close>_\<^latex>\<open>}}\<close>")
    74 
    75   "_asms" :: "prop \<Rightarrow> asms \<Rightarrow> asms" 
    76   ("\<^latex>\<open>\\mbox{\<close>_\<^latex>\<open>}\\\\\<close>/ _")
    77 
    78   "_asm" :: "prop \<Rightarrow> asms" ("\<^latex>\<open>\\mbox{\<close>_\<^latex>\<open>}\<close>")
    79 
    80 notation (Axiom output)
    81   "Trueprop"  ("\<^latex>\<open>\\mbox{}\\inferrule{\\mbox{}}{\\mbox{\<close>_\<^latex>\<open>}}\<close>")
    82 
    83 notation (IfThen output)
    84   Pure.imp  ("\<^latex>\<open>{\\normalsize{}\<close>If\<^latex>\<open>\\,}\<close> _/ \<^latex>\<open>{\\normalsize \\,\<close>then\<^latex>\<open>\\,}\<close>/ _.")
    85 syntax (IfThen output)
    86   "_bigimpl" :: "asms \<Rightarrow> prop \<Rightarrow> prop"
    87   ("\<^latex>\<open>{\\normalsize{}\<close>If\<^latex>\<open>\\,}\<close> _ /\<^latex>\<open>{\\normalsize \\,\<close>then\<^latex>\<open>\\,}\<close>/ _.")
    88   "_asms" :: "prop \<Rightarrow> asms \<Rightarrow> asms" ("\<^latex>\<open>\\mbox{\<close>_\<^latex>\<open>}\<close> /\<^latex>\<open>{\\normalsize \\,\<close>and\<^latex>\<open>\\,}\<close>/ _")
    89   "_asm" :: "prop \<Rightarrow> asms" ("\<^latex>\<open>\\mbox{\<close>_\<^latex>\<open>}\<close>")
    90 
    91 notation (IfThenNoBox output)
    92   Pure.imp  ("\<^latex>\<open>{\\normalsize{}\<close>If\<^latex>\<open>\\,}\<close> _/ \<^latex>\<open>{\\normalsize \\,\<close>then\<^latex>\<open>\\,}\<close>/ _.")
    93 syntax (IfThenNoBox output)
    94   "_bigimpl" :: "asms \<Rightarrow> prop \<Rightarrow> prop"
    95   ("\<^latex>\<open>{\\normalsize{}\<close>If\<^latex>\<open>\\,}\<close> _ /\<^latex>\<open>{\\normalsize \\,\<close>then\<^latex>\<open>\\,}\<close>/ _.")
    96   "_asms" :: "prop \<Rightarrow> asms \<Rightarrow> asms" ("_ /\<^latex>\<open>{\\normalsize \\,\<close>and\<^latex>\<open>\\,}\<close>/ _")
    97   "_asm" :: "prop \<Rightarrow> asms" ("_")
    98 
    99 setup \<open>
   100   Thy_Output.antiquotation_pretty_source_embedded \<^binding>\<open>const_typ\<close>
   101     (Scan.lift Args.embedded_inner_syntax)
   102     (fn ctxt => fn c =>
   103       let val tc = Proof_Context.read_const {proper = false, strict = false} ctxt c in
   104         Pretty.block [Thy_Output.pretty_term ctxt tc, Pretty.str " ::",
   105           Pretty.brk 1, Syntax.pretty_typ ctxt (fastype_of tc)]
   106       end)
   107 \<close>
   108 
   109 setup\<open>
   110 let
   111   fun dummy_pats (wrap $ (eq $ lhs $ rhs)) =
   112     let
   113       val rhs_vars = Term.add_vars rhs [];
   114       fun dummy (v as Var (ixn as (_, T))) =
   115             if member ((=) ) rhs_vars ixn then v else Const (\<^const_name>\<open>DUMMY\<close>, T)
   116         | dummy (t $ u) = dummy t $ dummy u
   117         | dummy (Abs (n, T, b)) = Abs (n, T, dummy b)
   118         | dummy t = t;
   119     in wrap $ (eq $ dummy lhs $ rhs) end
   120 in
   121   Term_Style.setup \<^binding>\<open>dummy_pats\<close> (Scan.succeed (K dummy_pats))
   122 end
   123 \<close>
   124 
   125 setup \<open>
   126 let
   127 
   128 fun eta_expand Ts t xs = case t of
   129     Abs(x,T,t) =>
   130       let val (t', xs') = eta_expand (T::Ts) t xs
   131       in (Abs (x, T, t'), xs') end
   132   | _ =>
   133       let
   134         val (a,ts) = strip_comb t (* assume a atomic *)
   135         val (ts',xs') = fold_map (eta_expand Ts) ts xs
   136         val t' = list_comb (a, ts');
   137         val Bs = binder_types (fastype_of1 (Ts,t));
   138         val n = Int.min (length Bs, length xs');
   139         val bs = map Bound ((n - 1) downto 0);
   140         val xBs = ListPair.zip (xs',Bs);
   141         val xs'' = drop n xs';
   142         val t'' = fold_rev Term.abs xBs (list_comb(t', bs))
   143       in (t'', xs'') end
   144 
   145 val style_eta_expand =
   146   (Scan.repeat Args.name) >> (fn xs => fn ctxt => fn t => fst (eta_expand [] t xs))
   147 
   148 in Term_Style.setup \<^binding>\<open>eta_expand\<close> style_eta_expand end
   149 \<close>
   150 
   151 end
   152 (*>*)