wenzelm@30394: (* Title: doc-src/more_antiquote.ML haftmann@28440: Author: Florian Haftmann, TU Muenchen haftmann@28440: haftmann@28440: More antiquotations. haftmann@28440: *) haftmann@28440: haftmann@28440: signature MORE_ANTIQUOTE = haftmann@28440: sig haftmann@28727: val typewriter: string -> string haftmann@28440: end; haftmann@28440: haftmann@28440: structure More_Antiquote : MORE_ANTIQUOTE = haftmann@28440: struct haftmann@28440: haftmann@28727: (* printing typewriter lines *) haftmann@28440: haftmann@28727: fun typewriter s = haftmann@28714: let haftmann@28714: val parse = Scan.repeat haftmann@28714: (Scan.this_string "\n" |-- Scan.succeed "\\\\\n\\hspace*{0pt}" wenzelm@30394: || (Scan.this_string " " haftmann@28921: || Scan.this_string "." haftmann@28921: || Scan.this_string "," haftmann@28714: || Scan.this_string ":" haftmann@28921: || Scan.this_string ";" haftmann@28714: || Scan.this_string "\"" |-- Scan.succeed "{\\char34}" haftmann@28714: || Scan.this_string "#" |-- Scan.succeed "{\\char35}" haftmann@28714: || Scan.this_string "$" |-- Scan.succeed "{\\char36}" haftmann@28714: || Scan.this_string "%" |-- Scan.succeed "{\\char37}" haftmann@28714: || Scan.this_string "&" |-- Scan.succeed "{\\char38}" haftmann@28714: || Scan.this_string "\\" |-- Scan.succeed "{\\char92}" haftmann@28714: || Scan.this_string "^" |-- Scan.succeed "{\\char94}" haftmann@28714: || Scan.this_string "_" |-- Scan.succeed "{\\char95}" haftmann@28714: || Scan.this_string "{" |-- Scan.succeed "{\\char123}" haftmann@28714: || Scan.this_string "}" |-- Scan.succeed "{\\char125}" haftmann@28714: || Scan.this_string "~" |-- Scan.succeed "{\\char126}") haftmann@28714: -- Scan.repeat (Scan.this_string " ") haftmann@28714: >> (fn (x, xs) => x ^ replicate_string (length xs) "~") haftmann@28714: || Scan.one Symbol.not_eof); haftmann@28714: fun is_newline s = (s = "\n"); haftmann@28714: val cs = explode s |> take_prefix is_newline |> snd haftmann@28714: |> take_suffix is_newline |> fst; haftmann@28714: val (texts, []) = Scan.finite Symbol.stopper parse cs haftmann@28714: in if null texts haftmann@28714: then "" haftmann@28727: else implode ("\\isatypewriter%\n\\noindent%\n\\hspace*{0pt}" :: texts) haftmann@28714: end haftmann@28440: haftmann@28440: haftmann@29394: (* class and type constructor antiquotation *) haftmann@28440: haftmann@28440: local haftmann@28440: haftmann@28634: val pr_text = enclose "\\isa{" "}" o Pretty.output o Pretty.str; haftmann@28634: haftmann@28634: fun pr_class ctxt = Sign.read_class (ProofContext.theory_of ctxt) haftmann@28634: #> Sign.extern_class (ProofContext.theory_of ctxt) haftmann@28634: #> pr_text; haftmann@28634: haftmann@28634: fun pr_type ctxt = Sign.intern_type (ProofContext.theory_of ctxt) haftmann@28634: #> tap (Sign.arity_number (ProofContext.theory_of ctxt)) haftmann@28634: #> Sign.extern_type (ProofContext.theory_of ctxt) haftmann@28634: #> pr_text; haftmann@28440: haftmann@28440: in haftmann@28440: wenzelm@30394: val _ = ThyOutput.antiquotation "class" (Scan.lift Args.name) (pr_class o #context); wenzelm@30394: val _ = ThyOutput.antiquotation "type" (Scan.lift Args.name) (pr_type o #context); haftmann@28440: haftmann@28440: end; haftmann@28440: haftmann@28440: haftmann@29394: (* code theorem antiquotation *) haftmann@29394: haftmann@29394: local haftmann@29394: haftmann@29394: fun pretty_term ctxt t = Syntax.pretty_term (Variable.auto_fixes t ctxt) t; haftmann@29394: haftmann@29394: fun pretty_thm ctxt = pretty_term ctxt o Thm.full_prop_of; haftmann@29394: haftmann@29394: fun no_vars ctxt thm = haftmann@29394: let haftmann@29394: val ctxt' = Variable.set_body false ctxt; haftmann@29394: val ((_, [thm]), _) = Variable.import_thms true [thm] ctxt'; haftmann@29394: in thm end; haftmann@29394: haftmann@29394: fun pretty_code_thm src ctxt raw_const = haftmann@29394: let haftmann@29394: val thy = ProofContext.theory_of ctxt; haftmann@29394: val const = Code_Unit.check_const thy raw_const; haftmann@31143: val (_, funcgr) = Code_Preproc.obtain thy [const] []; haftmann@29811: fun holize thm = @{thm meta_eq_to_obj_eq} OF [thm]; haftmann@31143: val thms = Code_Preproc.eqns funcgr const haftmann@29394: |> map_filter (fn (thm, linear) => if linear then SOME thm else NONE) haftmann@29811: |> map (holize o no_vars ctxt o AxClass.overload thy); wenzelm@30394: in ThyOutput.output (ThyOutput.maybe_pretty_source (pretty_thm ctxt) src thms) end; haftmann@29394: haftmann@29394: in haftmann@29394: wenzelm@30394: val _ = ThyOutput.antiquotation "code_thms" Args.term wenzelm@30394: (fn {source, context, ...} => pretty_code_thm source context); haftmann@29394: haftmann@29394: end; haftmann@29394: haftmann@29394: haftmann@28440: (* code antiquotation *) haftmann@28440: haftmann@28440: local haftmann@28440: haftmann@28440: val parse_const_terms = Scan.repeat1 Args.term haftmann@28440: >> (fn ts => fn thy => map (Code_Unit.check_const thy) ts); haftmann@28440: val parse_consts = Scan.lift (Args.parens (Args.$$$ "consts")) |-- parse_const_terms haftmann@29619: >> (fn mk_cs => fn thy => fn naming => map_filter (Code_Thingol.lookup_const naming) (mk_cs thy)); haftmann@28440: val parse_types = Scan.lift (Args.parens (Args.$$$ "types") |-- Scan.repeat1 Args.name) haftmann@29619: >> (fn tycos => fn thy => fn naming => map_filter (Code_Thingol.lookup_tyco naming o Sign.intern_type thy) tycos); haftmann@28440: val parse_classes = Scan.lift (Args.parens (Args.$$$ "classes") |-- Scan.repeat1 Args.name) haftmann@29619: >> (fn classes => fn thy => fn naming => map_filter (Code_Thingol.lookup_class naming o Sign.intern_class thy) classes); haftmann@28440: val parse_instances = Scan.lift (Args.parens (Args.$$$ "instances") |-- Scan.repeat1 (Args.name --| Args.$$$ "::" -- Args.name)) haftmann@29619: >> (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); wenzelm@30394: val parse_names = parse_consts || parse_types || parse_classes || parse_instances; haftmann@28440: haftmann@28440: in haftmann@28440: wenzelm@30394: val _ = ThyOutput.antiquotation "code_stmts" wenzelm@30394: (parse_const_terms -- Scan.repeat parse_names -- Scan.lift (Args.parens Args.name)) wenzelm@30394: (fn {context = ctxt, ...} => fn ((mk_cs, mk_stmtss), target) => wenzelm@30394: let val thy = ProofContext.theory_of ctxt in wenzelm@30394: Code_Target.code_of thy wenzelm@30394: target "Example" (mk_cs thy) wenzelm@30394: (fn naming => maps (fn f => f thy naming) mk_stmtss) wenzelm@30394: |> typewriter wenzelm@30394: end); haftmann@28440: haftmann@28440: end; wenzelm@30394: wenzelm@30394: end;