src/HOL/Tools/Mirabelle/mirabelle.ML
changeset 73694 60519a7bfc53
parent 73691 2f9877db82a1
child 73697 0e7a5c7a14c8
equal deleted inserted replaced
73693:3ab18af9b2b5 73694:60519a7bfc53
     8   (*core*)
     8   (*core*)
     9   val print_name: string -> string
     9   val print_name: string -> string
    10   val print_properties: Properties.T -> string
    10   val print_properties: Properties.T -> string
    11   type context = {tag: string, arguments: Properties.T, timeout: Time.time, theory: theory}
    11   type context = {tag: string, arguments: Properties.T, timeout: Time.time, theory: theory}
    12   type command = {name: string, pos: Position.T, pre: Proof.state, post: Toplevel.state}
    12   type command = {name: string, pos: Position.T, pre: Proof.state, post: Toplevel.state}
    13   val command_result: command -> XML.body -> XML.tree
    13   val theory_action: binding -> (context -> command list -> XML.body) -> theory -> theory
       
    14   val log_command: command -> XML.body -> XML.tree
       
    15   val log_report: Properties.T -> XML.body -> XML.tree
    14   val print_exn: exn -> string
    16   val print_exn: exn -> string
    15   val theory_action: binding -> (context -> command list -> XML.body) -> theory -> theory
       
    16   val command_action: binding -> (context -> command -> string) -> theory -> theory
    17   val command_action: binding -> (context -> command -> string) -> theory -> theory
    17 
    18 
    18   (*utility functions*)
    19   (*utility functions*)
    19   val can_apply : Time.time -> (Proof.context -> int -> tactic) ->
    20   val can_apply : Time.time -> (Proof.context -> int -> tactic) ->
    20     Proof.state -> bool
    21     Proof.state -> bool
    36 val keywords = Keyword.no_command_keywords (Thy_Header.get_keywords \<^theory>);
    37 val keywords = Keyword.no_command_keywords (Thy_Header.get_keywords \<^theory>);
    37 
    38 
    38 val print_name = Token.print_name keywords;
    39 val print_name = Token.print_name keywords;
    39 val print_properties = Token.print_properties keywords;
    40 val print_properties = Token.print_properties keywords;
    40 
    41 
    41 fun print_action name [] = name
       
    42   | print_action name arguments = name ^ " " ^ print_properties arguments;
       
    43 
       
    44 fun read_actions str =
    42 fun read_actions str =
    45   Token.read_body keywords
    43   Token.read_body keywords
    46     (Parse.enum ";" (Parse.name -- Sledgehammer_Commands.parse_params))
    44     (Parse.enum ";" (Parse.name -- Sledgehammer_Commands.parse_params))
    47     (Symbol_Pos.explode0 str);
    45     (Symbol_Pos.explode0 str);
    48 
    46 
    62 
    60 
    63 fun theory_action binding action thy =
    61 fun theory_action binding action thy =
    64   let val context = Context.Theory thy |> Name_Space.map_naming (K Name_Space.global_naming);
    62   let val context = Context.Theory thy |> Name_Space.map_naming (K Name_Space.global_naming);
    65   in thy |> Data.map (#2 o Name_Space.define context true (binding, action)) end;
    63   in thy |> Data.map (#2 o Name_Space.define context true (binding, action)) end;
    66 
    64 
       
    65 
       
    66 (* log content *)
       
    67 
       
    68 fun log_action name arguments =
       
    69   XML.Elem (("action", (Markup.nameN, name) :: arguments),
       
    70     [XML.Text (print_name name ^ (if null arguments then "" else " " ^ print_properties arguments))]);
       
    71 
       
    72 fun log_command ({name, pos, ...}: command) body =
       
    73   XML.Elem (("command", (Markup.nameN, name) :: Position.properties_of pos), body);
       
    74 
       
    75 fun log_report props body =
       
    76   XML.Elem (("report", props), body);
       
    77 
       
    78 
       
    79 (* apply actions *)
       
    80 
    67 fun apply_action index name arguments timeout commands thy =
    81 fun apply_action index name arguments timeout commands thy =
    68   let
    82   let
    69     val action = #2 (Name_Space.check (Context.Theory thy) (Data.get thy) (name, Position.none));
    83     val action = #2 (Name_Space.check (Context.Theory thy) (Data.get thy) (name, Position.none));
    70     val tag = "#" ^ Value.print_int index ^ " " ^ name ^ ": ";
    84     val tag = "#" ^ Value.print_int index ^ " " ^ name ^ ": ";
    71     val context = {tag = tag, arguments = arguments, timeout = timeout, theory = thy};
    85     val context = {tag = tag, arguments = arguments, timeout = timeout, theory = thy};
    72     val export_body = action context commands;
    86     val export_body = action context commands;
    73     val export_head =
    87     val export_head = log_action name arguments;
    74       XML.Elem (("action", (Markup.nameN, name) :: arguments),
    88     val export_name = Path.binding0 (Path.basic "mirabelle" + Path.basic (Value.print_int index));
    75         [XML.Text (print_action name arguments)]);
    89   in
    76     val export_path = Path.binding0 (Path.basic "mirabelle" + Path.basic (Value.print_int index));
    90     if null export_body then ()
    77   in Export.export thy export_path (export_head :: export_body) end;
    91     else Export.export thy export_name (export_head :: export_body)
    78 
    92   end;
    79 
       
    80 (* command action *)
       
    81 
       
    82 fun command_result ({name, pos, ...}: command) body =
       
    83   XML.Elem (("command", (Markup.nameN, name) :: Position.properties_of pos), body);
       
    84 
    93 
    85 fun print_exn exn =
    94 fun print_exn exn =
    86   (case exn of
    95   (case exn of
    87     Timeout.TIMEOUT _ => "timeout"
    96     Timeout.TIMEOUT _ => "timeout"
    88   | ERROR msg => "error: " ^ msg
    97   | ERROR msg => "error: " ^ msg
    95         action context command handle exn =>
   104         action context command handle exn =>
    96           if Exn.is_interrupt exn then Exn.reraise exn
   105           if Exn.is_interrupt exn then Exn.reraise exn
    97           else #tag context ^ print_exn exn;
   106           else #tag context ^ print_exn exn;
    98       in
   107       in
    99         if s = "" then NONE
   108         if s = "" then NONE
   100         else SOME (command_result command [XML.Text s]) end;
   109         else SOME (log_command command [XML.Text s]) end;
   101   in theory_action binding (map_filter o apply) end;
   110   in theory_action binding (map_filter o apply) end;
   102 
   111 
   103 
   112 
   104 (* theory line range *)
   113 (* theory line range *)
   105 
   114