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