src/HOL/Tools/Nunchaku/nunchaku_reconstruct.ML
changeset 66623 8fc868e9e1bf
parent 66616 ee56847876b2
child 66633 ec8fceca7fb6
equal deleted inserted replaced
66622:0916eb2dbaca 66623:8fc868e9e1bf
    15   type isa_model =
    15   type isa_model =
    16     {type_model: typ_entry list,
    16     {type_model: typ_entry list,
    17      free_model: term_entry list,
    17      free_model: term_entry list,
    18      pat_complete_model: term_entry list,
    18      pat_complete_model: term_entry list,
    19      pat_incomplete_model: term_entry list,
    19      pat_incomplete_model: term_entry list,
    20      skolem_model: term_entry list}
    20      skolem_model: term_entry list,
       
    21      auxiliary_model: term_entry list}
    21 
    22 
    22   val str_of_isa_model: Proof.context -> isa_model -> string
    23   val str_of_isa_model: Proof.context -> isa_model -> string
    23 
    24 
    24   val isa_model_of_nun: Proof.context -> term list -> (typ option * string list) list ->
    25   val isa_model_of_nun: Proof.context -> term list -> (typ option * string list) list ->
    25     nun_model -> isa_model
    26     nun_model -> isa_model
       
    27   val clean_up_isa_model: Proof.context -> isa_model -> isa_model
    26 end;
    28 end;
    27 
    29 
    28 structure Nunchaku_Reconstruct : NUNCHAKU_RECONSTRUCT =
    30 structure Nunchaku_Reconstruct : NUNCHAKU_RECONSTRUCT =
    29 struct
    31 struct
    30 
    32 
    39 type isa_model =
    41 type isa_model =
    40   {type_model: typ_entry list,
    42   {type_model: typ_entry list,
    41    free_model: term_entry list,
    43    free_model: term_entry list,
    42    pat_complete_model: term_entry list,
    44    pat_complete_model: term_entry list,
    43    pat_incomplete_model: term_entry list,
    45    pat_incomplete_model: term_entry list,
    44    skolem_model: term_entry list};
    46    skolem_model: term_entry list,
       
    47    auxiliary_model: term_entry list};
    45 
    48 
    46 val anonymousN = "anonymous";
    49 val anonymousN = "anonymous";
    47 val irrelevantN = "irrelevant";
    50 val irrelevantN = "irrelevant";
    48 val unparsableN = "unparsable";
    51 val unparsableN = "unparsable";
    49 
    52 
    93 
    96 
    94 fun str_of_term_entry ctxt (tm, value) =
    97 fun str_of_term_entry ctxt (tm, value) =
    95   "val " ^ Syntax.string_of_term ctxt tm ^ " := " ^ Syntax.string_of_term ctxt value ^ ".";
    98   "val " ^ Syntax.string_of_term ctxt tm ^ " := " ^ Syntax.string_of_term ctxt value ^ ".";
    96 
    99 
    97 fun str_of_isa_model ctxt
   100 fun str_of_isa_model ctxt
    98     {type_model, free_model, pat_complete_model, pat_incomplete_model, skolem_model} =
   101     {type_model, free_model, pat_complete_model, pat_incomplete_model, skolem_model,
       
   102      auxiliary_model} =
    99   map (str_of_typ_entry ctxt) type_model @ "" ::
   103   map (str_of_typ_entry ctxt) type_model @ "" ::
   100   map (str_of_term_entry ctxt) free_model @ "" ::
   104   map (str_of_term_entry ctxt) free_model @ "" ::
   101   map (str_of_term_entry ctxt) pat_complete_model @ "" ::
   105   map (str_of_term_entry ctxt) pat_complete_model @ "" ::
   102   map (str_of_term_entry ctxt) pat_incomplete_model @ "" ::
   106   map (str_of_term_entry ctxt) pat_incomplete_model @ "" ::
   103   map (str_of_term_entry ctxt) skolem_model
   107   map (str_of_term_entry ctxt) skolem_model @ "" ::
       
   108   map (str_of_term_entry ctxt) auxiliary_model
   104   |> cat_lines;
   109   |> cat_lines;
   105 
   110 
   106 fun typ_of_nun ctxt =
   111 fun typ_of_nun ctxt =
   107   let
   112   let
   108     fun typ_of (NType (id, tys)) =
   113     fun typ_of (NType (id, tys)) =
   152     fun term_of _ (NAtom (j, ty)) =
   157     fun term_of _ (NAtom (j, ty)) =
   153         let val T = typ_of ty in Var ((nth_atom T j, 0), T) end
   158         let val T = typ_of ty in Var ((nth_atom T j, 0), T) end
   154       | term_of bounds (NConst (id, tys0, ty)) =
   159       | term_of bounds (NConst (id, tys0, ty)) =
   155         if id = nun_conj then
   160         if id = nun_conj then
   156           HOLogic.conj
   161           HOLogic.conj
       
   162         else if id = nun_choice then
       
   163           Const (@{const_name Eps}, typ_of ty)
   157         else if id = nun_disj then
   164         else if id = nun_disj then
   158           HOLogic.disj
   165           HOLogic.disj
   159         else if id = nun_choice then
       
   160           Const (@{const_name Eps}, typ_of ty)
       
   161         else if id = nun_equals then
   166         else if id = nun_equals then
   162           Const (@{const_name HOL.eq}, typ_of ty)
   167           Const (@{const_name HOL.eq}, typ_of ty)
   163         else if id = nun_false then
   168         else if id = nun_false then
   164           @{const False}
   169           @{const False}
   165         else if id = nun_if then
   170         else if id = nun_if then
   166           Const (@{const_name If}, typ_of ty)
   171           Const (@{const_name If}, typ_of ty)
   167         else if id = nun_implies then
   172         else if id = nun_implies then
   168           @{term implies}
   173           @{term implies}
       
   174         else if id = nun_not then
       
   175           HOLogic.Not
   169         else if id = nun_unique then
   176         else if id = nun_unique then
   170           Const (@{const_name The}, typ_of ty)
   177           Const (@{const_name The}, typ_of ty)
   171         else if id = nun_unique_unsafe then
   178         else if id = nun_unique_unsafe then
   172           Const (@{const_name The_unsafe}, typ_of ty)
   179           Const (@{const_name The_unsafe}, typ_of ty)
   173         else if id = nun_true then
   180         else if id = nun_true then
   174           @{const True}
   181           @{const True}
   175         else if String.isPrefix nun_anon_fun_prefix id then
   182         else if String.isPrefix nun_dollar_anon_fun_prefix id then
   176           let val j = Int.fromString (unprefix nun_anon_fun_prefix id) |> the_default ~1 in
   183           let val j = Int.fromString (unprefix nun_dollar_anon_fun_prefix id) |> the_default ~1 in
   177             Var ((anonymousN ^ nat_subscript (j + 1), 0), typ_of ty)
   184             Var ((anonymousN ^ nat_subscript (j + 1), 0), typ_of ty)
   178           end
   185           end
   179         else if String.isPrefix nun_irrelevant id then
   186         else if String.isPrefix nun_irrelevant id then
   180           Var ((irrelevantN ^ nat_subscript (Value.parse_int (unprefix nun_irrelevant id)), 0),
   187           Var ((irrelevantN ^ nat_subscript (Value.parse_int (unprefix nun_irrelevant id)), 0),
   181             dummyT)
   188             dummyT)
   225   (typ_of_nun ctxt ty, map (term_of_nun ctxt atomss) atoms);
   232   (typ_of_nun ctxt ty, map (term_of_nun ctxt atomss) atoms);
   226 
   233 
   227 fun isa_term_entry_of_nun ctxt atomss (tm, value) =
   234 fun isa_term_entry_of_nun ctxt atomss (tm, value) =
   228   (term_of_nun ctxt atomss tm, term_of_nun ctxt atomss value);
   235   (term_of_nun ctxt atomss tm, term_of_nun ctxt atomss value);
   229 
   236 
   230 fun isa_model_of_nun ctxt pat_completes atomss {type_model, const_model, skolem_model} =
   237 fun isa_model_of_nun ctxt pat_completes atomss
       
   238     {type_model, const_model, skolem_model, auxiliary_model} =
   231   let
   239   let
   232     val free_and_const_model = map (isa_term_entry_of_nun ctxt atomss) const_model;
   240     val free_and_const_model = map (isa_term_entry_of_nun ctxt atomss) const_model;
   233     val (free_model, (pat_complete_model, pat_incomplete_model)) =
   241     val (free_model, (pat_complete_model, pat_incomplete_model)) =
   234       List.partition (is_Free o fst) free_and_const_model
   242       List.partition (is_Free o fst) free_and_const_model
   235       ||> List.partition (member (op aconv) pat_completes o fst);
   243       ||> List.partition (member (op aconv) pat_completes o fst);
   236   in
   244   in
   237     {type_model = map (isa_typ_entry_of_nun ctxt atomss) type_model, free_model = free_model,
   245     {type_model = map (isa_typ_entry_of_nun ctxt atomss) type_model, free_model = free_model,
   238      pat_complete_model = pat_complete_model, pat_incomplete_model = pat_incomplete_model,
   246      pat_complete_model = pat_complete_model, pat_incomplete_model = pat_incomplete_model,
   239      skolem_model = map (isa_term_entry_of_nun ctxt atomss) skolem_model}
   247      skolem_model = map (isa_term_entry_of_nun ctxt atomss) skolem_model,
       
   248      auxiliary_model = map (isa_term_entry_of_nun ctxt atomss) auxiliary_model}
       
   249   end;
       
   250 
       
   251 fun clean_up_isa_model ctxt {type_model, free_model, pat_complete_model, pat_incomplete_model,
       
   252     skolem_model, auxiliary_model} =
       
   253   let
       
   254     val pat_complete_model' = if Config.get ctxt show_consts then pat_complete_model else [];
       
   255     val pat_incomplete_model' = pat_incomplete_model
       
   256       |> filter_out (can (fn Const (@{const_name unreachable}, _) => ()) o fst);
       
   257 
       
   258     val term_model = free_model @ pat_complete_model' @ pat_incomplete_model' @
       
   259       skolem_model @ auxiliary_model;
       
   260 
       
   261     (* Incomplete test: Can be led astray by references between the auxiliaries. *)
       
   262     fun is_auxiliary_referenced (aux, _) =
       
   263       exists (fn (lhs, rhs) =>
       
   264           not (lhs aconv aux) andalso exists_subterm (curry (op aconv) aux) rhs)
       
   265         term_model;
       
   266 
       
   267     val auxiliary_model' = filter is_auxiliary_referenced auxiliary_model;
       
   268   in
       
   269     {type_model = type_model, free_model = free_model, pat_complete_model = pat_complete_model',
       
   270      pat_incomplete_model = pat_incomplete_model', skolem_model = skolem_model,
       
   271      auxiliary_model = auxiliary_model'}
   240   end;
   272   end;
   241 
   273 
   242 end;
   274 end;