src/Pure/Isar/outer_keyword.ML
author wenzelm
Mon May 03 14:25:56 2010 +0200 (2010-05-03)
changeset 36610 bafd82950e24
parent 36315 e859879079c8
child 36681 dffeca08d3bf
permissions -rw-r--r--
renamed ProofContext.init to ProofContext.init_global to emphasize that this is not the real thing;
     1 (*  Title:      Pure/Isar/outer_keyword.ML
     2     Author:     Makarius
     3 
     4 Isar command keyword classification and global keyword tables.
     5 *)
     6 
     7 signature OUTER_KEYWORD =
     8 sig
     9   type T
    10   val kind_of: T -> string
    11   val control: T
    12   val diag: T
    13   val thy_begin: T
    14   val thy_switch: T
    15   val thy_end: T
    16   val thy_heading: T
    17   val thy_decl: T
    18   val thy_script: T
    19   val thy_goal: T
    20   val thy_schematic_goal: T
    21   val qed: T
    22   val qed_block: T
    23   val qed_global: T
    24   val prf_heading: T
    25   val prf_goal: T
    26   val prf_block: T
    27   val prf_open: T
    28   val prf_close: T
    29   val prf_chain: T
    30   val prf_decl: T
    31   val prf_asm: T
    32   val prf_asm_goal: T
    33   val prf_script: T
    34   val kinds: string list
    35   val update_tags: string -> string list -> string list
    36   val tag: string -> T -> T
    37   val tags_of: T -> string list
    38   val tag_theory: T -> T
    39   val tag_proof: T -> T
    40   val tag_ml: T -> T
    41   val get_lexicons: unit -> Scan.lexicon * Scan.lexicon
    42   val is_keyword: string -> bool
    43   val dest_keywords: unit -> string list
    44   val dest_commands: unit -> string list
    45   val command_keyword: string -> T option
    46   val command_tags: string -> string list
    47   val keyword_status_reportN: string
    48   val report: unit -> unit
    49   val keyword: string -> unit
    50   val command: string -> T -> unit
    51   val is_diag: string -> bool
    52   val is_control: string -> bool
    53   val is_regular: string -> bool
    54   val is_theory_begin: string -> bool
    55   val is_theory: string -> bool
    56   val is_proof: string -> bool
    57   val is_theory_goal: string -> bool
    58   val is_proof_goal: string -> bool
    59   val is_schematic_goal: string -> bool
    60   val is_qed: string -> bool
    61   val is_qed_global: string -> bool
    62 end;
    63 
    64 structure OuterKeyword: OUTER_KEYWORD =
    65 struct
    66 
    67 (** keyword classification **)
    68 
    69 datatype T = Keyword of string * string list;
    70 
    71 fun kind s = Keyword (s, []);
    72 fun kind_of (Keyword (s, _)) = s;
    73 
    74 
    75 (* kinds *)
    76 
    77 val control = kind "control";
    78 val diag = kind "diag";
    79 val thy_begin = kind "theory-begin";
    80 val thy_switch = kind "theory-switch";
    81 val thy_end = kind "theory-end";
    82 val thy_heading = kind "theory-heading";
    83 val thy_decl = kind "theory-decl";
    84 val thy_script = kind "theory-script";
    85 val thy_goal = kind "theory-goal";
    86 val thy_schematic_goal = kind "theory-schematic-goal";
    87 val qed = kind "qed";
    88 val qed_block = kind "qed-block";
    89 val qed_global = kind "qed-global";
    90 val prf_heading = kind "proof-heading";
    91 val prf_goal = kind "proof-goal";
    92 val prf_block = kind "proof-block";
    93 val prf_open = kind "proof-open";
    94 val prf_close = kind "proof-close";
    95 val prf_chain = kind "proof-chain";
    96 val prf_decl = kind "proof-decl";
    97 val prf_asm = kind "proof-asm";
    98 val prf_asm_goal = kind "proof-asm-goal";
    99 val prf_script = kind "proof-script";
   100 
   101 val kinds =
   102   [control, diag, thy_begin, thy_switch, thy_end, thy_heading, thy_decl, thy_script, thy_goal,
   103     thy_schematic_goal, qed, qed_block, qed_global, prf_heading, prf_goal, prf_block, prf_open,
   104     prf_close, prf_chain, prf_decl, prf_asm, prf_asm_goal, prf_script]
   105  |> map kind_of;
   106 
   107 
   108 (* tags *)
   109 
   110 fun update_tags t ts = t :: remove (op = : string * string -> bool) t ts;
   111 
   112 fun tag t (Keyword (s, ts)) = Keyword (s, update_tags t ts);
   113 fun tags_of (Keyword (_, ts)) = ts;
   114 
   115 val tag_theory = tag "theory";
   116 val tag_proof = tag "proof";
   117 val tag_ml = tag "ML";
   118 
   119 
   120 
   121 (** global keyword tables **)
   122 
   123 local
   124 
   125 val global_commands = Unsynchronized.ref (Symtab.empty: T Symtab.table);
   126 val global_lexicons = Unsynchronized.ref (Scan.empty_lexicon, Scan.empty_lexicon);
   127 
   128 in
   129 
   130 fun get_commands () = ! global_commands;
   131 fun get_lexicons () = ! global_lexicons;
   132 
   133 fun change_commands f = CRITICAL (fn () => Unsynchronized.change global_commands f);
   134 fun change_lexicons f = CRITICAL (fn () => Unsynchronized.change global_lexicons f);
   135 
   136 end;
   137 
   138 
   139 (* lookup *)
   140 
   141 fun is_keyword s = Scan.is_literal (#1 (get_lexicons ())) (Symbol.explode s);
   142 fun dest_keywords () = sort_strings (Scan.dest_lexicon (#1 (get_lexicons ())));
   143 
   144 fun dest_commands () = sort_strings (Symtab.keys (get_commands ()));
   145 fun command_keyword name = Symtab.lookup (get_commands ()) name;
   146 fun command_tags name = these (Option.map tags_of (command_keyword name));
   147 
   148 
   149 (* report *)
   150 
   151 val keyword_status_reportN = "keyword_status_report";
   152 
   153 fun report_message s =
   154   (if print_mode_active keyword_status_reportN then Output.status else writeln) s;
   155 
   156 fun report_keyword name =
   157   report_message (Markup.markup (Markup.keyword_decl name)
   158     ("Outer syntax keyword: " ^ quote name));
   159 
   160 fun report_command (name, kind) =
   161   report_message (Markup.markup (Markup.command_decl name (kind_of kind))
   162     ("Outer syntax command: " ^ quote name ^ " (" ^ kind_of kind ^ ")"));
   163 
   164 fun report () =
   165   let val (keywords, commands) = CRITICAL (fn () =>
   166     (dest_keywords (), sort_wrt #1 (Symtab.dest (get_commands ()))))
   167   in
   168     List.app report_keyword keywords;
   169     List.app report_command commands
   170   end;
   171 
   172 
   173 (* augment tables *)
   174 
   175 fun keyword name =
   176  (change_lexicons (apfst (Scan.extend_lexicon (Symbol.explode name)));
   177   report_keyword name);
   178 
   179 fun command name kind =
   180  (change_lexicons (apsnd (Scan.extend_lexicon (Symbol.explode name)));
   181   change_commands (Symtab.update (name, kind));
   182   report_command (name, kind));
   183 
   184 
   185 (* command categories *)
   186 
   187 fun command_category ks name =
   188   (case command_keyword name of
   189     NONE => false
   190   | SOME k => member (op = o pairself kind_of) ks k);
   191 
   192 val is_diag = command_category [diag];
   193 val is_control = command_category [control];
   194 fun is_regular name = not (is_diag name orelse is_control name);
   195 
   196 val is_theory_begin = command_category [thy_begin];
   197 
   198 val is_theory = command_category
   199   [thy_begin, thy_switch, thy_end, thy_heading, thy_decl, thy_script, thy_goal, thy_schematic_goal];
   200 
   201 val is_proof = command_category
   202   [qed, qed_block, qed_global, prf_heading, prf_goal, prf_block, prf_open, prf_close,
   203     prf_chain, prf_decl, prf_asm, prf_asm_goal, prf_script];
   204 
   205 val is_theory_goal = command_category [thy_goal, thy_schematic_goal];
   206 val is_proof_goal = command_category [prf_goal, prf_asm_goal];
   207 val is_schematic_goal = command_category [thy_schematic_goal];
   208 val is_qed = command_category [qed, qed_block];
   209 val is_qed_global = command_category [qed_global];
   210 
   211 end;