src/HOL/Tools/res_reconstruct.ML
changeset 31840 beeaa1ed1f47
parent 31479 08e2a70d002a
child 31866 d262a0d46246
equal deleted inserted replaced
31839:26f18ec0e293 31840:beeaa1ed1f47
    14   val make_tvar: string -> typ
    14   val make_tvar: string -> typ
    15   val strip_prefix: string -> string -> string option
    15   val strip_prefix: string -> string -> string option
    16   val setup: Context.theory -> Context.theory
    16   val setup: Context.theory -> Context.theory
    17   (* extracting lemma list*)
    17   (* extracting lemma list*)
    18   val find_failure: string -> string option
    18   val find_failure: string -> string option
    19   val lemma_list_dfg: string -> string * string vector * Proof.context * Thm.thm * int -> string
    19   val lemma_list: bool -> string ->
    20   val lemma_list_tstp: string -> string * string vector * Proof.context * Thm.thm * int -> string
    20     string * string vector * (int * int) * Proof.context * Thm.thm * int -> string
    21   (* structured proofs *)
    21   (* structured proofs *)
    22   val structured_proof: string -> string * string vector * Proof.context * Thm.thm * int -> string
    22   val structured_proof: string ->
       
    23     string * string vector * (int * int) * Proof.context * Thm.thm * int -> string
    23 end;
    24 end;
    24 
    25 
    25 structure ResReconstruct : RES_RECONSTRUCT =
    26 structure ResReconstruct : RES_RECONSTRUCT =
    26 struct
    27 struct
    27 
    28 
   494     in proofextract end;
   495     in proofextract end;
   495   
   496   
   496   (* === EXTRACTING LEMMAS === *)
   497   (* === EXTRACTING LEMMAS === *)
   497   (* lines have the form "cnf(108, axiom, ...",
   498   (* lines have the form "cnf(108, axiom, ...",
   498   the number (108) has to be extracted)*)
   499   the number (108) has to be extracted)*)
   499   fun get_step_nums_tstp proofextract =
   500   fun get_step_nums false proofextract =
   500     let val toks = String.tokens (not o Char.isAlphaNum)
   501     let val toks = String.tokens (not o Char.isAlphaNum)
   501     fun inputno ("cnf"::ntok::"axiom"::_) = Int.fromString ntok
   502     fun inputno ("cnf"::ntok::"axiom"::_) = Int.fromString ntok
       
   503       | inputno ("cnf"::ntok::"negated"::"conjecture"::_) = Int.fromString ntok
   502       | inputno _ = NONE
   504       | inputno _ = NONE
   503     val lines = split_lines proofextract
   505     val lines = split_lines proofextract
   504     in  List.mapPartial (inputno o toks) lines  end
   506     in  List.mapPartial (inputno o toks) lines  end
   505     
   507   (*String contains multiple lines. We want those of the form
   506     (*String contains multiple lines. We want those of the form
       
   507     "253[0:Inp] et cetera..."
   508     "253[0:Inp] et cetera..."
   508     A list consisting of the first number in each line is returned. *)
   509     A list consisting of the first number in each line is returned. *)
   509   fun get_step_nums_dfg proofextract =
   510   |  get_step_nums true proofextract =
   510     let val toks = String.tokens (not o Char.isAlphaNum)
   511     let val toks = String.tokens (not o Char.isAlphaNum)
   511     fun inputno (ntok::"0"::"Inp"::_) = Int.fromString ntok
   512     fun inputno (ntok::"0"::"Inp"::_) = Int.fromString ntok
   512       | inputno _ = NONE
   513       | inputno _ = NONE
   513     val lines = split_lines proofextract
   514     val lines = split_lines proofextract
   514     in  List.mapPartial (inputno o toks) lines  end
   515     in  List.mapPartial (inputno o toks) lines  end
   515     
   516     
   516   (*extracting lemmas from tstp-output between the lines from above*)
   517   (*extracting lemmas from tstp-output between the lines from above*)
   517   fun extract_lemmas get_step_nums (proof, thm_names, _, _, _) =
   518   fun extract_lemmas get_step_nums (proof, thm_names, conj_count, _, _, _) =
   518     let
   519     let
   519     (* get the names of axioms from their numbers*)
   520     (* get the names of axioms from their numbers*)
   520     fun get_axiom_names thm_names step_nums =
   521     fun get_axiom_names thm_names step_nums =
   521       let
   522       let
   522       fun is_axiom n = n <= Vector.length thm_names
   523       val last_axiom = Vector.length thm_names
       
   524       fun is_axiom n = n <= last_axiom
       
   525       fun is_conj n = n >= #1 conj_count andalso n < #1 conj_count + #2 conj_count
   523       fun getname i = Vector.sub(thm_names, i-1)
   526       fun getname i = Vector.sub(thm_names, i-1)
   524       in
   527       in
   525         sort_distinct string_ord (filter (fn x => x <> "??.unknown") (map getname (filter is_axiom step_nums)))
   528         (sort_distinct string_ord (filter (fn x => x <> "??.unknown")
       
   529           (map getname (filter is_axiom step_nums))),
       
   530         exists is_conj step_nums)
   526       end
   531       end
   527     val proofextract = get_proof_extract proof
   532     val proofextract = get_proof_extract proof
   528     in
   533     in
   529       get_axiom_names thm_names (get_step_nums proofextract)
   534       get_axiom_names thm_names (get_step_nums proofextract)
   530     end;
   535     end;
   543           (Markup.markup Markup.sendback) ("atp_minimize [atp=" ^ name ^ "] " ^
   548           (Markup.markup Markup.sendback) ("atp_minimize [atp=" ^ name ^ "] " ^
   544                                            space_implode " " (nochained lemmas))
   549                                            space_implode " " (nochained lemmas))
   545 
   550 
   546   fun sendback_metis_nochained lemmas =
   551   fun sendback_metis_nochained lemmas =
   547     (Markup.markup Markup.sendback o metis_line) (nochained lemmas)
   552     (Markup.markup Markup.sendback o metis_line) (nochained lemmas)
   548   fun lemma_list_tstp name result =
   553 
   549     let val lemmas = extract_lemmas get_step_nums_tstp result
   554   fun lemma_list dfg name result =
   550     in sendback_metis_nochained lemmas ^ "\n" ^ minimize_line name lemmas end;
   555     let val (lemmas, used_conj) = extract_lemmas (get_step_nums dfg) result
   551   fun lemma_list_dfg name result =
   556     in sendback_metis_nochained lemmas ^ "\n" ^ minimize_line name lemmas ^
   552     let val lemmas = extract_lemmas get_step_nums_dfg result
   557       (if used_conj then ""
   553     in sendback_metis_nochained lemmas ^ "\n" ^ minimize_line name lemmas end;
   558        else "\nWarning: Goal is provable because context is inconsistent.")
       
   559     end;
   554 
   560 
   555   (* === Extracting structured Isar-proof === *)
   561   (* === Extracting structured Isar-proof === *)
   556   fun structured_proof name (result as (proof, thm_names, ctxt, goal, subgoalno)) =
   562   fun structured_proof name (result as (proof, thm_names, conj_count, ctxt, goal, subgoalno)) =
   557     let
   563     let
   558     (*Could use split_lines, but it can return blank lines...*)
   564     (*Could use split_lines, but it can return blank lines...*)
   559     val lines = String.tokens (equal #"\n");
   565     val lines = String.tokens (equal #"\n");
   560     val nospaces = String.translate (fn c => if Char.isSpace c then "" else str c)
   566     val nospaces = String.translate (fn c => if Char.isSpace c then "" else str c)
   561     val proofextract = get_proof_extract proof
   567     val proofextract = get_proof_extract proof
   562     val cnfs = filter (String.isPrefix "cnf(") (map nospaces (lines proofextract))
   568     val cnfs = filter (String.isPrefix "cnf(") (map nospaces (lines proofextract))
   563     val one_line_proof = lemma_list_tstp name result
   569     val one_line_proof = lemma_list false name result
   564     val structured = if chained_hint mem_string (String.tokens (fn c => c = #" ") one_line_proof) then ""
   570     val structured = if chained_hint mem_string (String.tokens (fn c => c = #" ") one_line_proof) then ""
   565                 else decode_tstp_file cnfs ctxt goal subgoalno thm_names
   571                 else decode_tstp_file cnfs ctxt goal subgoalno thm_names
   566     in
   572     in
   567     one_line_proof ^ "\n\n" ^ (Markup.markup Markup.sendback) structured
   573     one_line_proof ^ "\n\n" ^ (Markup.markup Markup.sendback) structured
   568   end
   574   end