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