doc-src/more_antiquote.ML
author haftmann
Wed Feb 11 15:05:25 2009 +0100 (2009-02-11)
changeset 29874 0b92f68124e8
parent 29619 82054da94a74
child 30202 2775062fd3a9
permissions -rw-r--r--
display code theorems with HOL equality
     1 (*  Title:      Doc/more_antiquote.ML
     2     ID:         $Id$
     3     Author:     Florian Haftmann, TU Muenchen
     4 
     5 More antiquotations.
     6 *)
     7 
     8 signature MORE_ANTIQUOTE =
     9 sig
    10   val typewriter: string -> string
    11 end;
    12 
    13 structure More_Antiquote : MORE_ANTIQUOTE =
    14 struct
    15 
    16 structure O = ThyOutput;
    17 
    18 (* printing typewriter lines *)
    19 
    20 fun typewriter s =
    21   let
    22     val parse = Scan.repeat
    23       (Scan.this_string "\n" |-- Scan.succeed "\\\\\n\\hspace*{0pt}"
    24         || (Scan.this_string " " 
    25           || Scan.this_string "."
    26           || Scan.this_string ","
    27           || Scan.this_string ":"
    28           || Scan.this_string ";"
    29           || Scan.this_string "\"" |-- Scan.succeed "{\\char34}"
    30           || Scan.this_string "#" |-- Scan.succeed "{\\char35}"
    31           || Scan.this_string "$" |-- Scan.succeed "{\\char36}"
    32           || Scan.this_string "%" |-- Scan.succeed "{\\char37}"
    33           || Scan.this_string "&" |-- Scan.succeed "{\\char38}"
    34           || Scan.this_string "\\" |-- Scan.succeed "{\\char92}"
    35           || Scan.this_string "^" |-- Scan.succeed "{\\char94}"
    36           || Scan.this_string "_" |-- Scan.succeed "{\\char95}"
    37           || Scan.this_string "{" |-- Scan.succeed "{\\char123}"
    38           || Scan.this_string "}" |-- Scan.succeed "{\\char125}"
    39           || Scan.this_string "~" |-- Scan.succeed "{\\char126}")
    40           -- Scan.repeat (Scan.this_string " ")
    41           >> (fn (x, xs) => x ^ replicate_string (length xs) "~")
    42         || Scan.one Symbol.not_eof);
    43     fun is_newline s = (s = "\n");
    44     val cs = explode s |> take_prefix is_newline |> snd
    45       |> take_suffix is_newline |> fst;
    46     val (texts, []) =  Scan.finite Symbol.stopper parse cs
    47   in if null texts
    48     then ""
    49     else implode ("\\isatypewriter%\n\\noindent%\n\\hspace*{0pt}" :: texts)
    50   end
    51 
    52 
    53 (* class and type constructor antiquotation *)
    54 
    55 local
    56 
    57 val pr_text = enclose "\\isa{" "}" o Pretty.output o Pretty.str;
    58 
    59 fun pr_class ctxt = Sign.read_class (ProofContext.theory_of ctxt)
    60   #> Sign.extern_class (ProofContext.theory_of ctxt)
    61   #> pr_text;
    62 
    63 fun pr_type ctxt = Sign.intern_type (ProofContext.theory_of ctxt)
    64   #> tap (Sign.arity_number (ProofContext.theory_of ctxt))
    65   #> Sign.extern_type (ProofContext.theory_of ctxt)
    66   #> pr_text;
    67 
    68 in
    69 
    70 val _ = O.add_commands
    71   [("class", ThyOutput.args (Scan.lift Args.name) (K pr_class)),
    72     ("type", ThyOutput.args (Scan.lift Args.name) (K pr_type))]
    73 
    74 end;
    75 
    76 
    77 (* code theorem antiquotation *)
    78 
    79 local
    80 
    81 fun pretty_term ctxt t = Syntax.pretty_term (Variable.auto_fixes t ctxt) t;
    82 
    83 fun pretty_thm ctxt = pretty_term ctxt o Thm.full_prop_of;
    84 
    85 fun no_vars ctxt thm =
    86   let
    87     val ctxt' = Variable.set_body false ctxt;
    88     val ((_, [thm]), _) = Variable.import_thms true [thm] ctxt';
    89   in thm end;
    90 
    91 fun pretty_code_thm src ctxt raw_const =
    92   let
    93     val thy = ProofContext.theory_of ctxt;
    94     val const = Code_Unit.check_const thy raw_const;
    95     val (_, funcgr) = Code_Funcgr.make thy [const];
    96     fun holize thm = @{thm meta_eq_to_obj_eq} OF [thm];
    97     val thms = Code_Funcgr.eqns funcgr const
    98       |> map_filter (fn (thm, linear) => if linear then SOME thm else NONE)
    99       |> map (holize o no_vars ctxt o AxClass.overload thy);
   100   in ThyOutput.output_list pretty_thm src ctxt thms end;
   101 
   102 in
   103 
   104 val _ = O.add_commands
   105   [("code_thms", ThyOutput.args Args.term pretty_code_thm)];
   106 
   107 end;
   108 
   109 
   110 (* code antiquotation *)
   111 
   112 local
   113 
   114   val parse_const_terms = Scan.repeat1 Args.term
   115     >> (fn ts => fn thy => map (Code_Unit.check_const thy) ts);
   116   val parse_consts = Scan.lift (Args.parens (Args.$$$ "consts")) |-- parse_const_terms
   117     >> (fn mk_cs => fn thy => fn naming => map_filter (Code_Thingol.lookup_const naming) (mk_cs thy));
   118   val parse_types = Scan.lift (Args.parens (Args.$$$ "types") |-- Scan.repeat1 Args.name)
   119     >> (fn tycos => fn thy => fn naming => map_filter (Code_Thingol.lookup_tyco naming o Sign.intern_type thy) tycos);
   120   val parse_classes = Scan.lift (Args.parens (Args.$$$ "classes") |-- Scan.repeat1 Args.name)
   121     >> (fn classes => fn thy => fn naming => map_filter (Code_Thingol.lookup_class naming o Sign.intern_class thy) classes);
   122   val parse_instances = Scan.lift (Args.parens (Args.$$$ "instances") |-- Scan.repeat1 (Args.name --| Args.$$$ "::" -- Args.name))
   123     >> (fn insts => fn thy => fn naming => map_filter (Code_Thingol.lookup_instance naming o apsnd (Sign.intern_type thy) o apfst (Sign.intern_class thy) o swap) insts);
   124   val parse_names = parse_consts || parse_types || parse_classes || parse_instances; 
   125 
   126   fun code_stmts src ctxt ((mk_cs, mk_stmtss), target) =
   127     Code_Target.code_of (ProofContext.theory_of ctxt)
   128       target "Example" (mk_cs (ProofContext.theory_of ctxt))
   129         (fn naming => maps (fn f => f (ProofContext.theory_of ctxt) naming) mk_stmtss)
   130     |> typewriter;
   131 
   132 in
   133 
   134 val _ = O.add_commands
   135   [("code_stmts", O.args
   136       (parse_const_terms -- Scan.repeat parse_names -- Scan.lift (Args.parens Args.name))
   137         code_stmts)]
   138 
   139 end
   140 
   141 end;