src/HOL/Statespace/distinct_tree_prover.ML
author wenzelm
Fri Mar 06 15:58:56 2015 +0100 (2015-03-06)
changeset 59621 291934bac95e
parent 59586 ddf6deaadfe8
child 60327 a3f565b8ba76
permissions -rw-r--r--
Thm.cterm_of and Thm.ctyp_of operate on local context;
     1 (*  Title:      HOL/Statespace/distinct_tree_prover.ML
     2     Author:     Norbert Schirmer, TU Muenchen
     3 *)
     4 
     5 signature DISTINCT_TREE_PROVER =
     6 sig
     7   datatype direction = Left | Right
     8   val mk_tree : ('a -> term) -> typ -> 'a list -> term
     9   val dest_tree : term -> term list
    10   val find_tree : term -> term -> direction list option
    11 
    12   val neq_to_eq_False : thm
    13   val distinctTreeProver : thm -> direction list -> direction list -> thm
    14   val neq_x_y : Proof.context -> term -> term -> string -> thm option
    15   val distinctFieldSolver : string list -> solver
    16   val distinctTree_tac : string list -> Proof.context -> int -> tactic
    17   val distinct_implProver : thm -> cterm -> thm
    18   val subtractProver : term -> cterm -> thm -> thm
    19   val distinct_simproc : string list -> simproc
    20 
    21   val discharge : thm list -> thm -> thm
    22 end;
    23 
    24 structure DistinctTreeProver : DISTINCT_TREE_PROVER =
    25 struct
    26 
    27 val neq_to_eq_False = @{thm neq_to_eq_False};
    28 
    29 datatype direction = Left | Right;
    30 
    31 fun treeT T = Type (@{type_name tree}, [T]);
    32 
    33 fun mk_tree' e T n [] = Const (@{const_name Tip}, treeT T)
    34   | mk_tree' e T n xs =
    35      let
    36        val m = (n - 1) div 2;
    37        val (xsl,x::xsr) = chop m xs;
    38        val l = mk_tree' e T m xsl;
    39        val r = mk_tree' e T (n-(m+1)) xsr;
    40      in
    41        Const (@{const_name Node}, treeT T --> T --> HOLogic.boolT--> treeT T --> treeT T) $
    42          l $ e x $ @{term False} $ r
    43      end
    44 
    45 fun mk_tree e T xs = mk_tree' e T (length xs) xs;
    46 
    47 fun dest_tree (Const (@{const_name Tip}, _)) = []
    48   | dest_tree (Const (@{const_name Node}, _) $ l $ e $ _ $ r) = dest_tree l @ e :: dest_tree r
    49   | dest_tree t = raise TERM ("dest_tree", [t]);
    50 
    51 
    52 
    53 fun lin_find_tree e (Const (@{const_name Tip}, _)) = NONE
    54   | lin_find_tree e (Const (@{const_name Node}, _) $ l $ x $ _ $ r) =
    55       if e aconv x
    56       then SOME []
    57       else
    58         (case lin_find_tree e l of
    59           SOME path => SOME (Left :: path)
    60         | NONE =>
    61             (case lin_find_tree e r of
    62               SOME path => SOME (Right :: path)
    63             | NONE => NONE))
    64   | lin_find_tree e t = raise TERM ("find_tree: input not a tree", [t])
    65 
    66 fun bin_find_tree order e (Const (@{const_name Tip}, _)) = NONE
    67   | bin_find_tree order e (Const (@{const_name Node}, _) $ l $ x $ _ $ r) =
    68       (case order (e, x) of
    69         EQUAL => SOME []
    70       | LESS => Option.map (cons Left) (bin_find_tree order e l)
    71       | GREATER => Option.map (cons Right) (bin_find_tree order e r))
    72   | bin_find_tree order e t = raise TERM ("find_tree: input not a tree", [t])
    73 
    74 fun find_tree e t =
    75   (case bin_find_tree Term_Ord.fast_term_ord e t of
    76     NONE => lin_find_tree e t
    77   | x => x);
    78 
    79 
    80 fun split_common_prefix xs [] = ([], xs, [])
    81   | split_common_prefix [] ys = ([], [], ys)
    82   | split_common_prefix (xs as (x :: xs')) (ys as (y :: ys')) =
    83       if x = y
    84       then let val (ps, xs'', ys'') = split_common_prefix xs' ys' in (x :: ps, xs'', ys'') end
    85       else ([], xs, ys)
    86 
    87 
    88 (* Wrapper around Thm.instantiate. The type instiations of instTs are applied to
    89  * the right hand sides of insts
    90  *)
    91 fun instantiate instTs insts =
    92   let
    93     val instTs' = map (fn (T, U) => (dest_TVar (Thm.typ_of T), Thm.typ_of U)) instTs;
    94     fun substT x = (case AList.lookup (op =) instTs' x of NONE => TVar x | SOME T' => T');
    95     fun mapT_and_recertify ct =
    96       let
    97         val thy = Thm.theory_of_cterm ct;
    98       in (Thm.global_cterm_of thy (Term.map_types (Term.map_type_tvar substT) (Thm.term_of ct))) end;
    99     val insts' = map (apfst mapT_and_recertify) insts;
   100   in Thm.instantiate (instTs, insts') end;
   101 
   102 fun tvar_clash ixn S S' =
   103   raise TYPE ("Type variable has two distinct sorts", [TVar (ixn, S), TVar (ixn, S')], []);
   104 
   105 fun lookup (tye, (ixn, S)) =
   106   (case AList.lookup (op =) tye ixn of
   107     NONE => NONE
   108   | SOME (S', T) => if S = S' then SOME T else tvar_clash ixn S S');
   109 
   110 val naive_typ_match =
   111   let
   112     fun match (TVar (v, S), T) subs =
   113           (case lookup (subs, (v, S)) of
   114             NONE => ((v, (S, T))::subs)
   115           | SOME _ => subs)
   116       | match (Type (a, Ts), Type (b, Us)) subs =
   117           if a <> b then raise Type.TYPE_MATCH
   118           else matches (Ts, Us) subs
   119       | match (TFree x, TFree y) subs =
   120           if x = y then subs else raise Type.TYPE_MATCH
   121       | match _ _ = raise Type.TYPE_MATCH
   122     and matches (T :: Ts, U :: Us) subs = matches (Ts, Us) (match (T, U) subs)
   123       | matches _ subs = subs;
   124   in match end;
   125 
   126 
   127 (* expects that relevant type variables are already contained in
   128  * term variables. First instantiation of variables is returned without further
   129  * checking.
   130  *)
   131 fun naive_cterm_first_order_match (t, ct) env =
   132   let
   133     fun mtch (env as (tyinsts, insts)) =
   134       fn (Var (ixn, T), ct) =>
   135           (case AList.lookup (op =) insts ixn of
   136             NONE => (naive_typ_match (T, Thm.typ_of_cterm ct) tyinsts, (ixn, ct) :: insts)
   137           | SOME _ => env)
   138        | (f $ t, ct) =>
   139           let val (cf, ct') = Thm.dest_comb ct;
   140           in mtch (mtch env (f, cf)) (t, ct') end
   141        | _ => env;
   142   in mtch env (t, ct) end;
   143 
   144 
   145 fun discharge prems rule =
   146   let
   147     val thy = Thm.theory_of_thm (hd prems);
   148     val (tyinsts,insts) =
   149       fold naive_cterm_first_order_match (Thm.prems_of rule ~~ map Thm.cprop_of prems) ([], []);
   150 
   151     val tyinsts' =
   152       map (fn (v, (S, U)) =>
   153         (Thm.global_ctyp_of thy (TVar (v, S)), Thm.global_ctyp_of thy U)) tyinsts;
   154     val insts' =
   155       map (fn (idxn, ct) =>
   156         (Thm.global_cterm_of thy (Var (idxn, Thm.typ_of_cterm ct)), ct)) insts;
   157     val rule' = Thm.instantiate (tyinsts', insts') rule;
   158   in fold Thm.elim_implies prems rule' end;
   159 
   160 local
   161 
   162 val (l_in_set_root, x_in_set_root, r_in_set_root) =
   163   let
   164     val (Node_l_x_d, r) =
   165       Thm.cprop_of @{thm in_set_root}
   166       |> Thm.dest_comb |> #2
   167       |> Thm.dest_comb |> #2 |> Thm.dest_comb |> #2 |> Thm.dest_comb;
   168     val (Node_l, x) = Node_l_x_d |> Thm.dest_comb |> #1 |> Thm.dest_comb;
   169     val l = Node_l |> Thm.dest_comb |> #2;
   170   in (l,x,r) end;
   171 
   172 val (x_in_set_left, r_in_set_left) =
   173   let
   174     val (Node_l_x_d, r) =
   175       Thm.cprop_of @{thm in_set_left}
   176       |> Thm.dest_comb |> #2 |> Thm.dest_comb |> #2
   177       |> Thm.dest_comb |> #2 |> Thm.dest_comb |> #2 |> Thm.dest_comb;
   178     val x = Node_l_x_d |> Thm.dest_comb |> #1 |> Thm.dest_comb |> #2;
   179   in (x, r) end;
   180 
   181 val (x_in_set_right, l_in_set_right) =
   182   let
   183     val (Node_l, x) =
   184       Thm.cprop_of @{thm in_set_right}
   185       |> Thm.dest_comb |> #2 |> Thm.dest_comb |> #2
   186       |> Thm.dest_comb |> #2 |> Thm.dest_comb |> #2
   187       |> Thm.dest_comb |> #1 |> Thm.dest_comb |> #1
   188       |> Thm.dest_comb;
   189     val l = Node_l |> Thm.dest_comb |> #2;
   190   in (x, l) end;
   191 
   192 in
   193 (*
   194 1. First get paths x_path y_path of x and y in the tree.
   195 2. For the common prefix descend into the tree according to the path
   196    and lemmas all_distinct_left/right
   197 3. If one restpath is empty use distinct_left/right,
   198    otherwise all_distinct_left_right
   199 *)
   200 
   201 fun distinctTreeProver dist_thm x_path y_path =
   202   let
   203     fun dist_subtree [] thm = thm
   204       | dist_subtree (p :: ps) thm =
   205          let
   206            val rule =
   207             (case p of Left => @{thm all_distinct_left} | Right => @{thm all_distinct_right})
   208          in dist_subtree ps (discharge [thm] rule) end;
   209 
   210     val (ps, x_rest, y_rest) = split_common_prefix x_path y_path;
   211     val dist_subtree_thm = dist_subtree ps dist_thm;
   212     val subtree = Thm.cprop_of dist_subtree_thm |> Thm.dest_comb |> #2 |> Thm.dest_comb |> #2;
   213     val (_, [l, _, _, r]) = Drule.strip_comb subtree;
   214 
   215     fun in_set ps tree =
   216       let
   217         val (_, [l, x, _, r]) = Drule.strip_comb tree;
   218         val xT = Thm.ctyp_of_cterm x;
   219       in
   220         (case ps of
   221           [] =>
   222             instantiate
   223               [(Thm.ctyp_of_cterm x_in_set_root, xT)]
   224               [(l_in_set_root, l), (x_in_set_root, x), (r_in_set_root, r)] @{thm in_set_root}
   225         | Left :: ps' =>
   226             let
   227               val in_set_l = in_set ps' l;
   228               val in_set_left' =
   229                 instantiate
   230                   [(Thm.ctyp_of_cterm x_in_set_left, xT)]
   231                   [(x_in_set_left, x), (r_in_set_left, r)] @{thm in_set_left};
   232             in discharge [in_set_l] in_set_left' end
   233         | Right :: ps' =>
   234             let
   235               val in_set_r = in_set ps' r;
   236               val in_set_right' =
   237                 instantiate
   238                   [(Thm.ctyp_of_cterm x_in_set_right, xT)]
   239                   [(x_in_set_right, x), (l_in_set_right, l)] @{thm in_set_right};
   240             in discharge [in_set_r] in_set_right' end)
   241       end;
   242 
   243   fun in_set' [] = raise TERM ("distinctTreeProver", [])
   244     | in_set' (Left :: ps) = in_set ps l
   245     | in_set' (Right :: ps) = in_set ps r;
   246 
   247   fun distinct_lr node_in_set Left = discharge [dist_subtree_thm, node_in_set] @{thm distinct_left}
   248     | distinct_lr node_in_set Right = discharge [dist_subtree_thm, node_in_set] @{thm distinct_right}
   249 
   250   val (swap, neq) =
   251     (case x_rest of
   252       [] =>
   253         let val y_in_set = in_set' y_rest;
   254         in (false, distinct_lr y_in_set (hd y_rest)) end
   255     | xr :: xrs =>
   256         (case y_rest of
   257           [] =>
   258             let val x_in_set = in_set' x_rest;
   259             in (true, distinct_lr x_in_set (hd x_rest)) end
   260         | yr :: yrs =>
   261             let
   262               val x_in_set = in_set' x_rest;
   263               val y_in_set = in_set' y_rest;
   264             in
   265               (case xr of
   266                 Left =>
   267                   (false, discharge [dist_subtree_thm, x_in_set, y_in_set] @{thm distinct_left_right})
   268               | Right =>
   269                   (true, discharge [dist_subtree_thm, y_in_set, x_in_set] @{thm distinct_left_right}))
   270            end));
   271   in if swap then discharge [neq] @{thm swap_neq} else neq end;
   272 
   273 
   274 fun deleteProver dist_thm [] = @{thm delete_root} OF [dist_thm]
   275   | deleteProver dist_thm (p::ps) =
   276       let
   277         val dist_rule =
   278           (case p of Left => @{thm all_distinct_left} | Right => @{thm all_distinct_right});
   279         val dist_thm' = discharge [dist_thm] dist_rule;
   280         val del_rule = (case p of Left => @{thm delete_left} | Right => @{thm delete_right});
   281         val del = deleteProver dist_thm' ps;
   282       in discharge [dist_thm, del] del_rule end;
   283 
   284 local
   285   val (alpha, v) =
   286     let
   287       val ct =
   288         @{thm subtract_Tip} |> Thm.cprop_of |> Thm.dest_comb |> #2 |> Thm.dest_comb |> #2
   289         |> Thm.dest_comb |> #2;
   290       val [alpha] = ct |> Thm.ctyp_of_cterm |> Thm.dest_ctyp;
   291     in (alpha, #1 (dest_Var (Thm.term_of ct))) end;
   292 in
   293 
   294 fun subtractProver (Const (@{const_name Tip}, T)) ct dist_thm =
   295       let
   296         val ct' = dist_thm |> Thm.cprop_of |> Thm.dest_comb |> #2 |> Thm.dest_comb |> #2;
   297         val thy = Thm.theory_of_cterm ct;
   298         val [alphaI] = #2 (dest_Type T);
   299       in
   300         Thm.instantiate
   301           ([(alpha, Thm.global_ctyp_of thy alphaI)],
   302            [(Thm.global_cterm_of thy (Var (v, treeT alphaI)), ct')]) @{thm subtract_Tip}
   303       end
   304   | subtractProver (Const (@{const_name Node}, nT) $ l $ x $ d $ r) ct dist_thm =
   305       let
   306         val ct' = dist_thm |> Thm.cprop_of |> Thm.dest_comb |> #2 |> Thm.dest_comb |> #2;
   307         val (_, [cl, _, _, cr]) = Drule.strip_comb ct;
   308         val ps = the (find_tree x (Thm.term_of ct'));
   309         val del_tree = deleteProver dist_thm ps;
   310         val dist_thm' = discharge [del_tree, dist_thm] @{thm delete_Some_all_distinct};
   311         val sub_l = subtractProver (Thm.term_of cl) cl (dist_thm');
   312         val sub_r =
   313           subtractProver (Thm.term_of cr) cr
   314             (discharge [sub_l, dist_thm'] @{thm subtract_Some_all_distinct_res});
   315       in discharge [del_tree, sub_l, sub_r] @{thm subtract_Node} end;
   316 
   317 end;
   318 
   319 fun distinct_implProver dist_thm ct =
   320   let
   321     val ctree = ct |> Thm.dest_comb |> #2 |> Thm.dest_comb |> #2;
   322     val sub = subtractProver (Thm.term_of ctree) ctree dist_thm;
   323   in @{thm subtract_Some_all_distinct} OF [sub, dist_thm] end;
   324 
   325 fun get_fst_success f [] = NONE
   326   | get_fst_success f (x :: xs) =
   327       (case f x of
   328         NONE => get_fst_success f xs
   329       | SOME v => SOME v);
   330 
   331 fun neq_x_y ctxt x y name =
   332   (let
   333     val dist_thm = the (try (Proof_Context.get_thm ctxt) name);
   334     val ctree = Thm.cprop_of dist_thm |> Thm.dest_comb |> #2 |> Thm.dest_comb |> #2;
   335     val tree = Thm.term_of ctree;
   336     val x_path = the (find_tree x tree);
   337     val y_path = the (find_tree y tree);
   338     val thm = distinctTreeProver dist_thm x_path y_path;
   339   in SOME thm
   340   end handle Option.Option => NONE);
   341 
   342 fun distinctTree_tac names ctxt = SUBGOAL (fn (goal, i) =>
   343     (case goal of
   344       Const (@{const_name Trueprop}, _) $
   345           (Const (@{const_name Not}, _) $ (Const (@{const_name HOL.eq}, _) $ x $ y)) =>
   346         (case get_fst_success (neq_x_y ctxt x y) names of
   347           SOME neq => rtac neq i
   348         | NONE => no_tac)
   349     | _ => no_tac))
   350 
   351 fun distinctFieldSolver names =
   352   mk_solver "distinctFieldSolver" (distinctTree_tac names);
   353 
   354 fun distinct_simproc names =
   355   Simplifier.simproc_global @{theory HOL} "DistinctTreeProver.distinct_simproc" ["x = y"]
   356     (fn ctxt =>
   357       (fn Const (@{const_name HOL.eq}, _) $ x $ y =>
   358           Option.map (fn neq => @{thm neq_to_eq_False} OF [neq])
   359             (get_fst_success (neq_x_y ctxt x y) names)
   360         | _ => NONE));
   361 
   362 end;
   363 
   364 end;