doc-src/isac/jrocnik/Inverse_Z_Transform/antiquote_setup.ML
branchdecompose-isar
changeset 42252 e633bb41ea42
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/doc-src/isac/jrocnik/Inverse_Z_Transform/antiquote_setup.ML	Thu Sep 08 23:17:35 2011 +0200
     1.3 @@ -0,0 +1,198 @@
     1.4 +(*  Title:      doc-src/antiquote_setup.ML
     1.5 +    Author:     Makarius
     1.6 +
     1.7 +Auxiliary antiquotations for the Isabelle manuals.
     1.8 +*)
     1.9 +
    1.10 +structure Antiquote_Setup: sig end =
    1.11 +struct
    1.12 +
    1.13 +(* misc utils *)
    1.14 +
    1.15 +fun translate f = Symbol.explode #> map f #> implode;
    1.16 +
    1.17 +val clean_string = translate
    1.18 +  (fn "_" => "\\_"
    1.19 +    | "#" => "\\#"
    1.20 +    | "<" => "$<$"
    1.21 +    | ">" => "$>$"
    1.22 +    | "{" => "\\{"
    1.23 +    | "|" => "$\\mid$"
    1.24 +    | "}" => "\\}"
    1.25 +    | "\<dash>" => "-"
    1.26 +    | c => c);
    1.27 +
    1.28 +fun clean_name "\<dots>" = "dots"
    1.29 +  | clean_name ".." = "ddot"
    1.30 +  | clean_name "." = "dot"
    1.31 +  | clean_name "_" = "underscore"
    1.32 +  | clean_name "{" = "braceleft"
    1.33 +  | clean_name "}" = "braceright"
    1.34 +  | clean_name s = s |> translate (fn "_" => "-" | "\<dash>" => "-" | c => c);
    1.35 +
    1.36 +
    1.37 +(* verbatim text *)
    1.38 +
    1.39 +val verbatim = space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|";
    1.40 +
    1.41 +val _ = Thy_Output.antiquotation "verbatim" (Scan.lift Args.name)
    1.42 +  (K (split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"));
    1.43 +
    1.44 +
    1.45 +(* ML text *)
    1.46 +
    1.47 +local
    1.48 +
    1.49 +fun ml_val (txt1, "") = "fn _ => (" ^ txt1 ^ ");"
    1.50 +  | ml_val (txt1, txt2) = "fn _ => (" ^ txt1 ^ " : " ^ txt2 ^ ");";
    1.51 +
    1.52 +fun ml_type (txt1, "") = "val _ = NONE : (" ^ txt1 ^ ") option;"
    1.53 +  | ml_type (txt1, txt2) = "val _ = [NONE : (" ^ txt1 ^ ") option, NONE : (" ^ txt2 ^ ") option];";
    1.54 +
    1.55 +fun ml_exn (txt1, "") = "fn _ => (" ^ txt1 ^ " : exn);"
    1.56 +  | ml_exn (txt1, txt2) = "fn _ => (" ^ txt1 ^ " : " ^ txt2 ^ " -> exn);";
    1.57 +
    1.58 +fun ml_structure (txt, _) = "functor XXX() = struct structure XX = " ^ txt ^ " end;";
    1.59 +
    1.60 +fun ml_functor (txt, _) = "ML_Env.check_functor " ^ ML_Syntax.print_string txt;
    1.61 +
    1.62 +val is_name = ML_Lex.kind_of #> (fn kind => kind = ML_Lex.Ident orelse kind = ML_Lex.LongIdent);
    1.63 +
    1.64 +fun ml_name txt =
    1.65 +  (case filter is_name (ML_Lex.tokenize txt) of
    1.66 +    toks as [_] => ML_Lex.flatten toks
    1.67 +  | _ => error ("Single ML name expected in input: " ^ quote txt));
    1.68 +
    1.69 +fun index_ml name kind ml = Thy_Output.antiquotation name
    1.70 +  (Scan.lift (Args.name -- Scan.optional (Args.colon |-- Args.name) ""))
    1.71 +  (fn {context = ctxt, ...} => fn (txt1, txt2) =>
    1.72 +    let
    1.73 +      val txt =
    1.74 +        if txt2 = "" then txt1
    1.75 +        else if kind = "type" then txt1 ^ " = " ^ txt2
    1.76 +        else if kind = "exception" then txt1 ^ " of " ^ txt2
    1.77 +        else if Syntax.is_identifier (Long_Name.base_name (ml_name txt1)) then txt1 ^ ": " ^ txt2
    1.78 +        else txt1 ^ " : " ^ txt2;
    1.79 +      val txt' = if kind = "" then txt else kind ^ " " ^ txt;
    1.80 +      val _ = ML_Context.eval_text_in (SOME ctxt) false Position.none (ml (txt1, txt2));  (* ML_Lex.read (!?) *)
    1.81 +      val kind' = if kind = "" then "ML" else "ML " ^ kind;
    1.82 +    in
    1.83 +      "\\indexdef{}{" ^ kind' ^ "}{" ^ clean_string (ml_name txt1) ^ "}" ^
    1.84 +      (txt'
    1.85 +      |> (if Config.get ctxt Thy_Output.quotes then quote else I)
    1.86 +      |> (if Config.get ctxt Thy_Output.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
    1.87 +          else split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))
    1.88 +    end);
    1.89 +
    1.90 +in
    1.91 +
    1.92 +val _ = index_ml "index_ML" "" ml_val;
    1.93 +val _ = index_ml "index_ML_type" "type" ml_type;
    1.94 +val _ = index_ml "index_ML_exn" "exception" ml_exn;
    1.95 +val _ = index_ml "index_ML_structure" "structure" ml_structure;
    1.96 +val _ = index_ml "index_ML_functor" "functor" ml_functor;
    1.97 +
    1.98 +end;
    1.99 +
   1.100 +
   1.101 +(* named theorems *)
   1.102 +
   1.103 +val _ = Thy_Output.antiquotation "named_thms"
   1.104 +  (Scan.repeat (Attrib.thm -- Scan.lift (Args.parens Args.name)))
   1.105 +  (fn {context = ctxt, ...} =>
   1.106 +    map (apfst (Thy_Output.pretty_thm ctxt))
   1.107 +    #> (if Config.get ctxt Thy_Output.quotes then map (apfst Pretty.quote) else I)
   1.108 +    #> (if Config.get ctxt Thy_Output.display
   1.109 +        then
   1.110 +          map (fn (p, name) =>
   1.111 +            Output.output (Pretty.string_of (Pretty.indent (Config.get ctxt Thy_Output.indent) p)) ^
   1.112 +            "\\rulename{" ^ Output.output (Pretty.str_of (Thy_Output.pretty_text ctxt name)) ^ "}")
   1.113 +          #> space_implode "\\par\\smallskip%\n"
   1.114 +          #> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
   1.115 +        else
   1.116 +          map (fn (p, name) =>
   1.117 +            Output.output (Pretty.str_of p) ^
   1.118 +            "\\rulename{" ^ Output.output (Pretty.str_of (Thy_Output.pretty_text ctxt name)) ^ "}")
   1.119 +          #> space_implode "\\par\\smallskip%\n"
   1.120 +          #> enclose "\\isa{" "}"));
   1.121 +
   1.122 +
   1.123 +(* theory file *)
   1.124 +
   1.125 +val _ = Thy_Output.antiquotation "thy_file" (Scan.lift Args.name)
   1.126 +  (fn {context = ctxt, ...} =>
   1.127 +    fn name => (Thy_Load.check_thy Path.current name; Thy_Output.output ctxt [Pretty.str name]));
   1.128 +
   1.129 +
   1.130 +(* Isabelle/Isar entities (with index) *)
   1.131 +
   1.132 +local
   1.133 +
   1.134 +fun no_check _ _ = true;
   1.135 +
   1.136 +fun thy_check intern defined ctxt =
   1.137 +  let val thy = ProofContext.theory_of ctxt
   1.138 +  in defined thy o intern thy end;
   1.139 +
   1.140 +fun check_tool name =
   1.141 +  File.exists (Path.append (Path.explode "~~/lib/Tools") (Path.basic name));
   1.142 +
   1.143 +val arg = enclose "{" "}" o clean_string;
   1.144 +
   1.145 +fun entity check markup kind index =
   1.146 +  Thy_Output.antiquotation
   1.147 +    (translate (fn " " => "_" | c => c) kind ^
   1.148 +      (case index of NONE => "" | SOME true => "_def" | SOME false => "_ref"))
   1.149 +    (Scan.lift (Scan.optional (Args.parens Args.name) "" -- Args.name))
   1.150 +    (fn {context = ctxt, ...} => fn (logic, name) =>
   1.151 +      let
   1.152 +        val hyper_name =
   1.153 +          "{" ^ Long_Name.append kind (Long_Name.append logic (clean_name name)) ^ "}";
   1.154 +        val hyper =
   1.155 +          enclose ("\\hyperlink" ^ hyper_name ^ "{") "}" #>
   1.156 +          index = SOME true ? enclose ("\\hypertarget" ^ hyper_name ^ "{") "}";
   1.157 +        val idx =
   1.158 +          (case index of
   1.159 +            NONE => ""
   1.160 +          | SOME is_def =>
   1.161 +              "\\index" ^ (if is_def then "def" else "ref") ^ arg logic ^ arg kind ^ arg name);
   1.162 +      in
   1.163 +        if check ctxt name then
   1.164 +          idx ^
   1.165 +          (Output.output name
   1.166 +            |> (if markup = "" then I else enclose ("\\" ^ markup ^ "{") "}")
   1.167 +            |> (if Config.get ctxt Thy_Output.quotes then quote else I)
   1.168 +            |> (if Config.get ctxt Thy_Output.display
   1.169 +                then enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
   1.170 +                else hyper o enclose "\\mbox{\\isa{" "}}"))
   1.171 +        else error ("Bad " ^ kind ^ " " ^ quote name)
   1.172 +      end);
   1.173 +
   1.174 +fun entity_antiqs check markup kind =
   1.175 + ((entity check markup kind NONE);
   1.176 +  (entity check markup kind (SOME true));
   1.177 +  (entity check markup kind (SOME false)));
   1.178 +
   1.179 +in
   1.180 +
   1.181 +val _ = entity_antiqs no_check "" "syntax";
   1.182 +val _ = entity_antiqs (K (is_some o Keyword.command_keyword)) "isacommand" "command";
   1.183 +val _ = entity_antiqs (K Keyword.is_keyword) "isakeyword" "keyword";
   1.184 +val _ = entity_antiqs (K Keyword.is_keyword) "isakeyword" "element";
   1.185 +val _ = entity_antiqs (thy_check Method.intern Method.defined) "" "method";
   1.186 +val _ = entity_antiqs (thy_check Attrib.intern Attrib.defined) "" "attribute";
   1.187 +val _ = entity_antiqs no_check "" "fact";
   1.188 +val _ = entity_antiqs no_check "" "variable";
   1.189 +val _ = entity_antiqs no_check "" "case";
   1.190 +val _ = entity_antiqs (K Thy_Output.defined_command) "" "antiquotation";
   1.191 +val _ = entity_antiqs (K Thy_Output.defined_option) "" "antiquotation option";
   1.192 +val _ = entity_antiqs (fn _ => fn name => is_some (OS.Process.getEnv name)) "isatt" "setting";
   1.193 +val _ = entity_antiqs no_check "" "inference";
   1.194 +val _ = entity_antiqs no_check "isatt" "executable";
   1.195 +val _ = entity_antiqs (K check_tool) "isatt" "tool";
   1.196 +val _ = entity_antiqs (K (can Thy_Info.get_theory)) "" "theory";
   1.197 +val _ = entity_antiqs no_check "" "ML antiquotation";  (* FIXME proper check *)
   1.198 +
   1.199 +end;
   1.200 +
   1.201 +end;