src/HOL/Tools/inductive_realizer.ML
changeset 69593 3dda49e08b9d
parent 67316 adaf279ce67b
child 69829 3bfa28b3a5b2
equal deleted inserted replaced
69592:a80d8ec6c998 69593:3dda49e08b9d
    28       let val ys = subsets xs
    28       let val ys = subsets xs
    29       in ys @ map (cons x) ys end;
    29       in ys @ map (cons x) ys end;
    30 
    30 
    31 val pred_of = fst o dest_Const o head_of;
    31 val pred_of = fst o dest_Const o head_of;
    32 
    32 
    33 fun strip_all' used names (Const (@{const_name Pure.all}, _) $ Abs (s, T, t)) =
    33 fun strip_all' used names (Const (\<^const_name>\<open>Pure.all\<close>, _) $ Abs (s, T, t)) =
    34       let val (s', names') = (case names of
    34       let val (s', names') = (case names of
    35           [] => (singleton (Name.variant_list used) s, [])
    35           [] => (singleton (Name.variant_list used) s, [])
    36         | name :: names' => (name, names'))
    36         | name :: names' => (name, names'))
    37       in strip_all' (s'::used) names' (subst_bound (Free (s', T), t)) end
    37       in strip_all' (s'::used) names' (subst_bound (Free (s', T), t)) end
    38   | strip_all' used names ((t as Const (@{const_name Pure.imp}, _) $ P) $ Q) =
    38   | strip_all' used names ((t as Const (\<^const_name>\<open>Pure.imp\<close>, _) $ P) $ Q) =
    39       t $ strip_all' used names Q
    39       t $ strip_all' used names Q
    40   | strip_all' _ _ t = t;
    40   | strip_all' _ _ t = t;
    41 
    41 
    42 fun strip_all t = strip_all' (Term.add_free_names t []) [] t;
    42 fun strip_all t = strip_all' (Term.add_free_names t []) [] t;
    43 
    43 
    44 fun strip_one name
    44 fun strip_one name
    45     (Const (@{const_name Pure.all}, _) $ Abs (s, T, Const (@{const_name Pure.imp}, _) $ P $ Q)) =
    45     (Const (\<^const_name>\<open>Pure.all\<close>, _) $ Abs (s, T, Const (\<^const_name>\<open>Pure.imp\<close>, _) $ P $ Q)) =
    46       (subst_bound (Free (name, T), P), subst_bound (Free (name, T), Q))
    46       (subst_bound (Free (name, T), P), subst_bound (Free (name, T), Q))
    47   | strip_one _ (Const (@{const_name Pure.imp}, _) $ P $ Q) = (P, Q);
    47   | strip_one _ (Const (\<^const_name>\<open>Pure.imp\<close>, _) $ P $ Q) = (P, Q);
    48 
    48 
    49 fun relevant_vars prop = fold (fn ((a, i), T) => fn vs =>
    49 fun relevant_vars prop = fold (fn ((a, i), T) => fn vs =>
    50      (case strip_type T of
    50      (case strip_type T of
    51         (_, Type (s, _)) => if s = @{type_name bool} then (a, T) :: vs else vs
    51         (_, Type (s, _)) => if s = \<^type_name>\<open>bool\<close> then (a, T) :: vs else vs
    52       | _ => vs)) (Term.add_vars prop []) [];
    52       | _ => vs)) (Term.add_vars prop []) [];
    53 
    53 
    54 val attach_typeS = map_types (map_atyps
    54 val attach_typeS = map_types (map_atyps
    55   (fn TFree (s, []) => TFree (s, @{sort type})
    55   (fn TFree (s, []) => TFree (s, \<^sort>\<open>type\<close>)
    56     | TVar (ixn, []) => TVar (ixn, @{sort type})
    56     | TVar (ixn, []) => TVar (ixn, \<^sort>\<open>type\<close>)
    57     | T => T));
    57     | T => T));
    58 
    58 
    59 fun dt_of_intrs thy vs nparms intrs =
    59 fun dt_of_intrs thy vs nparms intrs =
    60   let
    60   let
    61     val iTs = rev (Term.add_tvars (Thm.prop_of (hd intrs)) []);
    61     val iTs = rev (Term.add_tvars (Thm.prop_of (hd intrs)) []);
   143     val conclT = Extraction.etype_of thy vs [] (Logic.strip_imp_concl rule');
   143     val conclT = Extraction.etype_of thy vs [] (Logic.strip_imp_concl rule');
   144     val used = map (fst o dest_Free) args;
   144     val used = map (fst o dest_Free) args;
   145 
   145 
   146     val is_rec = exists_Const (fn (c, _) => member (op =) rsets c);
   146     val is_rec = exists_Const (fn (c, _) => member (op =) rsets c);
   147 
   147 
   148     fun is_meta (Const (@{const_name Pure.all}, _) $ Abs (s, _, P)) = is_meta P
   148     fun is_meta (Const (\<^const_name>\<open>Pure.all\<close>, _) $ Abs (s, _, P)) = is_meta P
   149       | is_meta (Const (@{const_name Pure.imp}, _) $ _ $ Q) = is_meta Q
   149       | is_meta (Const (\<^const_name>\<open>Pure.imp\<close>, _) $ _ $ Q) = is_meta Q
   150       | is_meta (Const (@{const_name Trueprop}, _) $ t) =
   150       | is_meta (Const (\<^const_name>\<open>Trueprop\<close>, _) $ t) =
   151           (case head_of t of
   151           (case head_of t of
   152             Const (s, _) => can (Inductive.the_inductive_global ctxt) s
   152             Const (s, _) => can (Inductive.the_inductive_global ctxt) s
   153           | _ => true)
   153           | _ => true)
   154       | is_meta _ = false;
   154       | is_meta _ = false;
   155 
   155 
   172                   else fun_of (Free (x, T) :: ts) (Free (r, U) :: rts)
   172                   else fun_of (Free (x, T) :: ts) (Free (r, U) :: rts)
   173                     (Free (r, U) :: Free (x, T) :: args) (x :: r :: used) prems'
   173                     (Free (r, U) :: Free (x, T) :: args) (x :: r :: used) prems'
   174                 end
   174                 end
   175               else
   175               else
   176                 (case strip_type T of
   176                 (case strip_type T of
   177                   (Ts, Type (@{type_name Product_Type.prod}, [T1, T2])) =>
   177                   (Ts, Type (\<^type_name>\<open>Product_Type.prod\<close>, [T1, T2])) =>
   178                     let
   178                     let
   179                       val fx = Free (x, Ts ---> T1);
   179                       val fx = Free (x, Ts ---> T1);
   180                       val fr = Free (r, Ts ---> T2);
   180                       val fr = Free (r, Ts ---> T2);
   181                       val bs = map Bound (length Ts - 1 downto 0);
   181                       val bs = map Bound (length Ts - 1 downto 0);
   182                       val t =
   182                       val t =
   210     val fs = maps (fn ((intrs, prems), dummy) =>
   210     val fs = maps (fn ((intrs, prems), dummy) =>
   211       let
   211       let
   212         val fs = map (fn (rule, (ivs, intr)) =>
   212         val fs = map (fn (rule, (ivs, intr)) =>
   213           fun_of_prem thy rsets vs params rule ivs intr) (prems ~~ intrs)
   213           fun_of_prem thy rsets vs params rule ivs intr) (prems ~~ intrs)
   214       in
   214       in
   215         if dummy then Const (@{const_name default},
   215         if dummy then Const (\<^const_name>\<open>default\<close>,
   216             HOLogic.unitT --> body_type (fastype_of (hd fs))) :: fs
   216             HOLogic.unitT --> body_type (fastype_of (hd fs))) :: fs
   217         else fs
   217         else fs
   218       end) (premss ~~ dummies);
   218       end) (premss ~~ dummies);
   219     val frees = fold Term.add_frees fs [];
   219     val frees = fold Term.add_frees fs [];
   220     val Ts = map fastype_of fs;
   220     val Ts = map fastype_of fs;
   445           if null Ps then Extraction.nullt
   445           if null Ps then Extraction.nullt
   446           else
   446           else
   447             fold_rev (Term.abs o pair "x") Ts
   447             fold_rev (Term.abs o pair "x") Ts
   448               (list_comb (Const (case_name, T),
   448               (list_comb (Const (case_name, T),
   449                 (if dummy then
   449                 (if dummy then
   450                    [Abs ("x", HOLogic.unitT, Const (@{const_name default}, body_type T))]
   450                    [Abs ("x", HOLogic.unitT, Const (\<^const_name>\<open>default\<close>, body_type T))]
   451                  else []) @
   451                  else []) @
   452                 map reorder2 (intrs ~~ (length prems - 1 downto 0)) @
   452                 map reorder2 (intrs ~~ (length prems - 1 downto 0)) @
   453                 [Bound (length prems)]));
   453                 [Bound (length prems)]));
   454         val rlz = Extraction.realizes_of thy (vs @ Ps) r (Thm.prop_of elim);
   454         val rlz = Extraction.realizes_of thy (vs @ Ps) r (Thm.prop_of elim);
   455         val rlz' = attach_typeS (strip_all (Logic.unvarify_global rlz));
   455         val rlz' = attach_typeS (strip_all (Logic.unvarify_global rlz));