Theory LaTeXsugar

(*  Title:      HOL/Library/LaTeXsugar.thy
    Author:     Gerwin Klein, Tobias Nipkow, Norbert Schirmer
    Copyright   2005 NICTA and TUM
*)

(*<*)
theory LaTeXsugar
imports Main
begin

(* LOGIC *)
notation (latex output)
  If  ("(latex\\textsf{›iflatex‹}› (_)/ latex\\textsf{›thenlatex‹}› (_)/ latex\\textsf{›elselatex‹}› (_))" 10)

syntax (latex output)

  "_Let"        :: "[letbinds, 'a] => 'a"
  ("(latex\\textsf{›letlatex‹}› (_)/ latex\\textsf{›inlatex‹}› (_))" 10)

  "_case_syntax":: "['a, cases_syn] => 'b"
  ("(latex\\textsf{›caselatex‹}› _ latex\\textsf{›oflatex‹}›/ _)" 10)


(* SETS *)

(* empty set *)
notation (latex)
  "Set.empty" ("")

(* insert *)
translations 
  "{x}  A" <= "CONST insert x A"
  "{x,y}" <= "{x}  {y}"
  "{x,y}  A" <= "{x}  ({y}  A)"
  "{x}" <= "{x}  "

(* set comprehension *)
syntax (latex output)
  "_Collect" :: "pttrn => bool => 'a set"              ("(1{_ | _})")
  "_CollectIn" :: "pttrn => 'a set => bool => 'a set"   ("(1{_  _ | _})")
translations
  "_Collect p P"      <= "{p. P}"
  "_Collect p P"      <= "{p|xs. P}"
  "_CollectIn p A P"  <= "{p : A. P}"

(* card *)
notation (latex output)
  card  ("|_|")

(* LISTS *)

(* Cons *)
notation (latex)
  Cons  ("_ / _" [66,65] 65)

(* length *)
notation (latex output)
  length  ("|_|")

(* nth *)
notation (latex output)
  nth  ("_latex\\ensuremath{_{[\\mathit{›_latex‹}]}}›" [1000,0] 1000)

(* DUMMY *)
consts DUMMY :: 'a ("latex\\_›")

(* THEOREMS *)
notation (Rule output)
  Pure.imp  ("latex\\mbox{}\\inferrule{\\mbox{›_latex‹}}›latex‹{\\mbox{›_latex‹}}›")

syntax (Rule output)
  "_bigimpl" :: "asms  prop  prop"
  ("latex\\mbox{}\\inferrule{›_latex‹}›latex‹{\\mbox{›_latex‹}}›")

  "_asms" :: "prop  asms  asms" 
  ("latex\\mbox{›_latex‹}\\\\›/ _")

  "_asm" :: "prop  asms" ("latex\\mbox{›_latex‹}›")

notation (Axiom output)
  "Trueprop"  ("latex\\mbox{}\\inferrule{\\mbox{}}{\\mbox{›_latex‹}}›")

notation (IfThen output)
  Pure.imp  ("latex‹{\\normalsize{}›Iflatex\\,}› _/ latex‹{\\normalsize \\,›thenlatex\\,}›/ _.")
syntax (IfThen output)
  "_bigimpl" :: "asms  prop  prop"
  ("latex‹{\\normalsize{}›Iflatex\\,}› _ /latex‹{\\normalsize \\,›thenlatex\\,}›/ _.")
  "_asms" :: "prop  asms  asms" ("latex\\mbox{›_latex‹}› /latex‹{\\normalsize \\,›andlatex\\,}›/ _")
  "_asm" :: "prop  asms" ("latex\\mbox{›_latex‹}›")

notation (IfThenNoBox output)
  Pure.imp  ("latex‹{\\normalsize{}›Iflatex\\,}› _/ latex‹{\\normalsize \\,›thenlatex\\,}›/ _.")
syntax (IfThenNoBox output)
  "_bigimpl" :: "asms  prop  prop"
  ("latex‹{\\normalsize{}›Iflatex\\,}› _ /latex‹{\\normalsize \\,›thenlatex\\,}›/ _.")
  "_asms" :: "prop  asms  asms" ("_ /latex‹{\\normalsize \\,›andlatex\\,}›/ _")
  "_asm" :: "prop  asms" ("_")

setup 
  Document_Output.antiquotation_pretty_source_embedded bindingconst_typ
    (Scan.lift Parse.embedded_inner_syntax)
    (fn ctxt => fn c =>
      let val tc = Proof_Context.read_const {proper = false, strict = false} ctxt c in
        Pretty.block [Document_Output.pretty_term ctxt tc, Pretty.str " ::",
          Pretty.brk 1, Syntax.pretty_typ ctxt (fastype_of tc)]
      end)


setup
let
  fun dummy_pats (wrap $ (eq $ lhs $ rhs)) =
    let
      val rhs_vars = Term.add_vars rhs [];
      fun dummy (v as Var (ixn as (_, T))) =
            if member ((=) ) rhs_vars ixn then v else Const (const_nameDUMMY, T)
        | dummy (t $ u) = dummy t $ dummy u
        | dummy (Abs (n, T, b)) = Abs (n, T, dummy b)
        | dummy t = t;
    in wrap $ (eq $ dummy lhs $ rhs) end
in
  Term_Style.setup bindingdummy_pats (Scan.succeed (K dummy_pats))
end


setup 
let

fun eta_expand Ts t xs = case t of
    Abs(x,T,t) =>
      let val (t', xs') = eta_expand (T::Ts) t xs
      in (Abs (x, T, t'), xs') end
  | _ =>
      let
        val (a,ts) = strip_comb t (* assume a atomic *)
        val (ts',xs') = fold_map (eta_expand Ts) ts xs
        val t' = list_comb (a, ts');
        val Bs = binder_types (fastype_of1 (Ts,t));
        val n = Int.min (length Bs, length xs');
        val bs = map Bound ((n - 1) downto 0);
        val xBs = ListPair.zip (xs',Bs);
        val xs'' = drop n xs';
        val t'' = fold_rev Term.abs xBs (list_comb(t', bs))
      in (t'', xs'') end

val style_eta_expand =
  (Scan.repeat Args.name) >> (fn xs => fn ctxt => fn t => fst (eta_expand [] t xs))

in Term_Style.setup bindingeta_expand style_eta_expand end


end
(*>*)