src/HOL/Tools/Nitpick/nitpick_mono.ML
changeset 33232 f93390060bbe
parent 33192 08a39a957ed7
child 33571 3655e51f9958
equal deleted inserted replaced
33231:1711610c5b7d 33232:f93390060bbe
     5 Monotonicity predicate for higher-order logic.
     5 Monotonicity predicate for higher-order logic.
     6 *)
     6 *)
     7 
     7 
     8 signature NITPICK_MONO =
     8 signature NITPICK_MONO =
     9 sig
     9 sig
    10   type extended_context = NitpickHOL.extended_context
    10   type extended_context = Nitpick_HOL.extended_context
    11 
    11 
    12   val formulas_monotonic :
    12   val formulas_monotonic :
    13     extended_context -> typ -> term list -> term list -> term -> bool
    13     extended_context -> typ -> term list -> term list -> term -> bool
    14 end;
    14 end;
    15 
    15 
    16 structure NitpickMono : NITPICK_MONO =
    16 structure Nitpick_Mono : NITPICK_MONO =
    17 struct
    17 struct
    18 
    18 
    19 open NitpickUtil
    19 open Nitpick_Util
    20 open NitpickHOL
    20 open Nitpick_HOL
    21 
    21 
    22 type var = int
    22 type var = int
    23 
    23 
    24 datatype sign = Pos | Neg
    24 datatype sign = Pos | Neg
    25 datatype sign_atom = S of sign | V of var
    25 datatype sign_atom = S of sign | V of var
   361     |> do_ctype_comp Leq xs C12 C22
   361     |> do_ctype_comp Leq xs C12 C22
   362   | do_ctype_comp cmp xs (C1 as CPair (C11, C12)) (C2 as CPair (C21, C22))
   362   | do_ctype_comp cmp xs (C1 as CPair (C11, C12)) (C2 as CPair (C21, C22))
   363                   accum =
   363                   accum =
   364     (accum |> fold (uncurry (do_ctype_comp cmp xs)) [(C11, C21), (C12, C22)]
   364     (accum |> fold (uncurry (do_ctype_comp cmp xs)) [(C11, C21), (C12, C22)]
   365      handle Library.UnequalLengths =>
   365      handle Library.UnequalLengths =>
   366             raise CTYPE ("NitpickMono.do_ctype_comp", [C1, C2]))
   366             raise CTYPE ("Nitpick_Mono.do_ctype_comp", [C1, C2]))
   367   | do_ctype_comp cmp xs (CType _) (CType _) accum =
   367   | do_ctype_comp cmp xs (CType _) (CType _) accum =
   368     accum (* no need to compare them thanks to the cache *)
   368     accum (* no need to compare them thanks to the cache *)
   369   | do_ctype_comp _ _ C1 C2 _ =
   369   | do_ctype_comp _ _ C1 C2 _ =
   370     raise CTYPE ("NitpickMono.do_ctype_comp", [C1, C2])
   370     raise CTYPE ("Nitpick_Mono.do_ctype_comp", [C1, C2])
   371 
   371 
   372 (* comp_op -> ctype -> ctype -> constraint_set -> constraint_set *)
   372 (* comp_op -> ctype -> ctype -> constraint_set -> constraint_set *)
   373 fun add_ctype_comp _ _ _ UnsolvableCSet = UnsolvableCSet
   373 fun add_ctype_comp _ _ _ UnsolvableCSet = UnsolvableCSet
   374   | add_ctype_comp cmp C1 C2 (CSet (lits, comps, sexps)) =
   374   | add_ctype_comp cmp C1 C2 (CSet (lits, comps, sexps)) =
   375     (print_g ("*** Add " ^ string_for_ctype C1 ^ " " ^ string_for_comp_op cmp ^
   375     (print_g ("*** Add " ^ string_for_ctype C1 ^ " " ^ string_for_comp_op cmp ^
   411   | do_notin_ctype_fv sn sexp (CPair (C1, C2)) accum =
   411   | do_notin_ctype_fv sn sexp (CPair (C1, C2)) accum =
   412     accum |> fold (do_notin_ctype_fv sn sexp) [C1, C2]
   412     accum |> fold (do_notin_ctype_fv sn sexp) [C1, C2]
   413   | do_notin_ctype_fv sn sexp (CType (_, Cs)) accum =
   413   | do_notin_ctype_fv sn sexp (CType (_, Cs)) accum =
   414     accum |> fold (do_notin_ctype_fv sn sexp) Cs
   414     accum |> fold (do_notin_ctype_fv sn sexp) Cs
   415   | do_notin_ctype_fv _ _ C _ =
   415   | do_notin_ctype_fv _ _ C _ =
   416     raise CTYPE ("NitpickMono.do_notin_ctype_fv", [C])
   416     raise CTYPE ("Nitpick_Mono.do_notin_ctype_fv", [C])
   417 
   417 
   418 (* sign -> ctype -> constraint_set -> constraint_set *)
   418 (* sign -> ctype -> constraint_set -> constraint_set *)
   419 fun add_notin_ctype_fv _ _ UnsolvableCSet = UnsolvableCSet
   419 fun add_notin_ctype_fv _ _ UnsolvableCSet = UnsolvableCSet
   420   | add_notin_ctype_fv sn C (CSet (lits, comps, sexps)) =
   420   | add_notin_ctype_fv sn C (CSet (lits, comps, sexps)) =
   421     (print_g ("*** Add " ^ string_for_ctype C ^ " is right-" ^
   421     (print_g ("*** Add " ^ string_for_ctype C ^ " is right-" ^
   582     (* typ -> accumulator -> ctype * accumulator *)
   582     (* typ -> accumulator -> ctype * accumulator *)
   583     fun do_pair_constr T accum =
   583     fun do_pair_constr T accum =
   584       case ctype_for (nth_range_type 2 T) of
   584       case ctype_for (nth_range_type 2 T) of
   585         C as CPair (a_C, b_C) =>
   585         C as CPair (a_C, b_C) =>
   586         (CFun (a_C, S Neg, CFun (b_C, S Neg, C)), accum)
   586         (CFun (a_C, S Neg, CFun (b_C, S Neg, C)), accum)
   587       | C => raise CTYPE ("NitpickMono.consider_term.do_pair_constr", [C])
   587       | C => raise CTYPE ("Nitpick_Mono.consider_term.do_pair_constr", [C])
   588     (* int -> typ -> accumulator -> ctype * accumulator *)
   588     (* int -> typ -> accumulator -> ctype * accumulator *)
   589     fun do_nth_pair_sel n T =
   589     fun do_nth_pair_sel n T =
   590       case ctype_for (domain_type T) of
   590       case ctype_for (domain_type T) of
   591         C as CPair (a_C, b_C) =>
   591         C as CPair (a_C, b_C) =>
   592         pair (CFun (C, S Neg, if n = 0 then a_C else b_C))
   592         pair (CFun (C, S Neg, if n = 0 then a_C else b_C))
   593       | C => raise CTYPE ("NitpickMono.consider_term.do_nth_pair_sel", [C])
   593       | C => raise CTYPE ("Nitpick_Mono.consider_term.do_nth_pair_sel", [C])
   594     val unsolvable = (CType ("unsolvable", []), unsolvable_accum)
   594     val unsolvable = (CType ("unsolvable", []), unsolvable_accum)
   595     (* typ -> term -> accumulator -> ctype * accumulator *)
   595     (* typ -> term -> accumulator -> ctype * accumulator *)
   596     fun do_bounded_quantifier abs_T bound_t body_t accum =
   596     fun do_bounded_quantifier abs_T bound_t body_t accum =
   597       let
   597       let
   598         val abs_C = ctype_for abs_T
   598         val abs_C = ctype_for abs_T
   768              case accum of
   768              case accum of
   769                (_, UnsolvableCSet) => unsolvable
   769                (_, UnsolvableCSet) => unsolvable
   770              | _ => case C1 of
   770              | _ => case C1 of
   771                       CFun (C11, _, C12) =>
   771                       CFun (C11, _, C12) =>
   772                       (C12, accum ||> add_is_sub_ctype C2 C11)
   772                       (C12, accum ||> add_is_sub_ctype C2 C11)
   773                     | _ => raise CTYPE ("NitpickMono.consider_term.do_term \
   773                     | _ => raise CTYPE ("Nitpick_Mono.consider_term.do_term \
   774                                         \(op $)", [C1])
   774                                         \(op $)", [C1])
   775            end)
   775            end)
   776         |> tap (fn (C, _) =>
   776         |> tap (fn (C, _) =>
   777                    print_g ("  \<Gamma> \<turnstile> " ^
   777                    print_g ("  \<Gamma> \<turnstile> " ^
   778                             Syntax.string_of_term ctxt t ^ " : " ^
   778                             Syntax.string_of_term ctxt t ^ " : " ^
   904             accum |> do_formula t1 |> do_formula t2
   904             accum |> do_formula t1 |> do_formula t2
   905           | Const (@{const_name All}, _) $ Abs (_, T1, t1) => do_all T1 t1 accum
   905           | Const (@{const_name All}, _) $ Abs (_, T1, t1) => do_all T1 t1 accum
   906           | Const (@{const_name "op ="}, _) $ t1 $ t2 => do_equals t1 t2 accum
   906           | Const (@{const_name "op ="}, _) $ t1 $ t2 => do_equals t1 t2 accum
   907           | @{const "op &"} $ t1 $ t2 => accum |> do_formula t1 |> do_formula t2
   907           | @{const "op &"} $ t1 $ t2 => accum |> do_formula t1 |> do_formula t2
   908           | @{const "op -->"} $ t1 $ t2 => do_implies t1 t2 accum
   908           | @{const "op -->"} $ t1 $ t2 => do_implies t1 t2 accum
   909           | _ => raise TERM ("NitpickMono.consider_definitional_axiom.\
   909           | _ => raise TERM ("Nitpick_Mono.consider_definitional_axiom.\
   910                              \do_formula", [t])
   910                              \do_formula", [t])
   911     in do_formula t end
   911     in do_formula t end
   912 
   912 
   913 (* Proof.context -> literal list -> term -> ctype -> string *)
   913 (* Proof.context -> literal list -> term -> ctype -> string *)
   914 fun string_for_ctype_of_term ctxt lits t C =
   914 fun string_for_ctype_of_term ctxt lits t C =