src/HOL/ex/predicate_compile.ML
changeset 32950 5d5e123443b3
parent 32740 9dd0a2f83429
child 32952 aeb1e44fbc19
equal deleted inserted replaced
32949:aa6c470a962a 32950:5d5e123443b3
   104 
   104 
   105 (** auxiliary **)
   105 (** auxiliary **)
   106 
   106 
   107 (* debug stuff *)
   107 (* debug stuff *)
   108 
   108 
   109 fun tracing s = (if ! Toplevel.debug then Output.tracing s else ());
   109 fun tracing s = (if ! Toplevel.debug then tracing s else ());
   110 
   110 
   111 fun print_tac s = Seq.single; (* (if ! Toplevel.debug then Tactical.print_tac s else Seq.single); *)
   111 fun print_tac s = Seq.single; (* (if ! Toplevel.debug then Tactical.print_tac s else Seq.single); *)
   112 fun debug_tac msg = Seq.single; (* (fn st => (Output.tracing msg; Seq.single st)); *)
   112 fun debug_tac msg = Seq.single; (* (fn st => (tracing msg; Seq.single st)); *)
   113 
   113 
   114 val do_proofs = Unsynchronized.ref true;
   114 val do_proofs = Unsynchronized.ref true;
   115 
   115 
   116 fun mycheat_tac thy i st =
   116 fun mycheat_tac thy i st =
   117   (Tactic.rtac (SkipProof.make_thm thy (Var (("A", 0), propT))) i) st
   117   (Tactic.rtac (SkipProof.make_thm thy (Var (("A", 0), propT))) i) st
   342 
   342 
   343 (*val generator_modes_of = (map fst) o #generators oo the_pred_data*)
   343 (*val generator_modes_of = (map fst) o #generators oo the_pred_data*)
   344      
   344      
   345 (* diagnostic display functions *)
   345 (* diagnostic display functions *)
   346 
   346 
   347 fun print_modes modes = Output.tracing ("Inferred modes:\n" ^
   347 fun print_modes modes = tracing ("Inferred modes:\n" ^
   348   cat_lines (map (fn (s, ms) => s ^ ": " ^ commas (map
   348   cat_lines (map (fn (s, ms) => s ^ ": " ^ commas (map
   349     string_of_mode ms)) modes));
   349     string_of_mode ms)) modes));
   350 
   350 
   351 fun print_pred_mode_table string_of_entry thy pred_mode_table =
   351 fun print_pred_mode_table string_of_entry thy pred_mode_table =
   352   let
   352   let
   353     fun print_mode pred (mode, entry) =  "mode : " ^ (string_of_mode mode)
   353     fun print_mode pred (mode, entry) =  "mode : " ^ (string_of_mode mode)
   354       ^ (string_of_entry pred mode entry)  
   354       ^ (string_of_entry pred mode entry)  
   355     fun print_pred (pred, modes) =
   355     fun print_pred (pred, modes) =
   356       "predicate " ^ pred ^ ": " ^ cat_lines (map (print_mode pred) modes)
   356       "predicate " ^ pred ^ ": " ^ cat_lines (map (print_mode pred) modes)
   357     val _ = Output.tracing (cat_lines (map print_pred pred_mode_table))
   357     val _ = tracing (cat_lines (map print_pred pred_mode_table))
   358   in () end;
   358   in () end;
   359 
   359 
   360 fun string_of_moded_prem thy (Prem (ts, p), tmode) =
   360 fun string_of_moded_prem thy (Prem (ts, p), tmode) =
   361     (Syntax.string_of_term_global thy (list_comb (p, ts))) ^
   361     (Syntax.string_of_term_global thy (list_comb (p, ts))) ^
   362     "(" ^ (string_of_tmode tmode) ^ ")"
   362     "(" ^ (string_of_tmode tmode) ^ ")"
  1000 fun check_modes_pred with_generator thy param_vs preds modes gen_modes (p, ms) =
  1000 fun check_modes_pred with_generator thy param_vs preds modes gen_modes (p, ms) =
  1001   let val SOME rs = AList.lookup (op =) preds p
  1001   let val SOME rs = AList.lookup (op =) preds p
  1002   in (p, List.filter (fn m => case find_index
  1002   in (p, List.filter (fn m => case find_index
  1003     (is_none o check_mode_clause with_generator thy param_vs modes gen_modes m) rs of
  1003     (is_none o check_mode_clause with_generator thy param_vs modes gen_modes m) rs of
  1004       ~1 => true
  1004       ~1 => true
  1005     | i => (Output.tracing ("Clause " ^ string_of_int (i + 1) ^ " of " ^
  1005     | i => (tracing ("Clause " ^ string_of_int (i + 1) ^ " of " ^
  1006       p ^ " violates mode " ^ string_of_mode m); false)) ms)
  1006       p ^ " violates mode " ^ string_of_mode m); false)) ms)
  1007   end;
  1007   end;
  1008 
  1008 
  1009 fun get_modes_pred with_generator thy param_vs preds modes gen_modes (p, ms) =
  1009 fun get_modes_pred with_generator thy param_vs preds modes gen_modes (p, ms) =
  1010   let
  1010   let
  1913 
  1913 
  1914 (** main function of predicate compiler **)
  1914 (** main function of predicate compiler **)
  1915 
  1915 
  1916 fun add_equations_of steps prednames thy =
  1916 fun add_equations_of steps prednames thy =
  1917   let
  1917   let
  1918     val _ = Output.tracing ("Starting predicate compiler for predicates " ^ commas prednames ^ "...")
  1918     val _ = tracing ("Starting predicate compiler for predicates " ^ commas prednames ^ "...")
  1919     val (preds, nparams, all_vs, param_vs, extra_modes, clauses, arities) =
  1919     val (preds, nparams, all_vs, param_vs, extra_modes, clauses, arities) =
  1920       prepare_intrs thy prednames
  1920       prepare_intrs thy prednames
  1921     val _ = Output.tracing "Infering modes..."
  1921     val _ = tracing "Infering modes..."
  1922     val moded_clauses = #infer_modes steps thy extra_modes arities param_vs clauses 
  1922     val moded_clauses = #infer_modes steps thy extra_modes arities param_vs clauses 
  1923     val modes = map (fn (p, mps) => (p, map fst mps)) moded_clauses
  1923     val modes = map (fn (p, mps) => (p, map fst mps)) moded_clauses
  1924     val _ = print_modes modes
  1924     val _ = print_modes modes
  1925     val _ = print_moded_clauses thy moded_clauses
  1925     val _ = print_moded_clauses thy moded_clauses
  1926     val _ = Output.tracing "Defining executable functions..."
  1926     val _ = tracing "Defining executable functions..."
  1927     val thy' = fold (#create_definitions steps preds) modes thy
  1927     val thy' = fold (#create_definitions steps preds) modes thy
  1928       |> Theory.checkpoint
  1928       |> Theory.checkpoint
  1929     val _ = Output.tracing "Compiling equations..."
  1929     val _ = tracing "Compiling equations..."
  1930     val compiled_terms =
  1930     val compiled_terms =
  1931       (#compile_preds steps) thy' all_vs param_vs preds moded_clauses
  1931       (#compile_preds steps) thy' all_vs param_vs preds moded_clauses
  1932     val _ = print_compiled_terms thy' compiled_terms
  1932     val _ = print_compiled_terms thy' compiled_terms
  1933     val _ = Output.tracing "Proving equations..."
  1933     val _ = tracing "Proving equations..."
  1934     val result_thms = #prove steps thy' clauses preds (extra_modes @ modes)
  1934     val result_thms = #prove steps thy' clauses preds (extra_modes @ modes)
  1935       moded_clauses compiled_terms
  1935       moded_clauses compiled_terms
  1936     val qname = #qname steps
  1936     val qname = #qname steps
  1937     (* val attrib = gn thy => Attrib.attribute_i thy Code.add_eqn_attrib *)
  1937     (* val attrib = gn thy => Attrib.attribute_i thy Code.add_eqn_attrib *)
  1938     val attrib = fn thy => Attrib.attribute_i thy (Attrib.internal (K (Thm.declaration_attribute
  1938     val attrib = fn thy => Attrib.attribute_i thy (Attrib.internal (K (Thm.declaration_attribute