src/HOL/Tools/Nitpick/nitpick_mono.ML
changeset 41001 11715564e2ad
parent 41000 4bbff1684465
child 41002 11a442b472c7
     1.1 --- a/src/HOL/Tools/Nitpick/nitpick_mono.ML	Mon Dec 06 13:33:05 2010 +0100
     1.2 +++ b/src/HOL/Tools/Nitpick/nitpick_mono.ML	Mon Dec 06 13:33:09 2010 +0100
     1.3 @@ -11,9 +11,9 @@
     1.4  
     1.5    val trace : bool Unsynchronized.ref
     1.6    val formulas_monotonic :
     1.7 -    hol_context -> bool -> int -> typ -> term list * term list -> bool
     1.8 +    hol_context -> bool -> typ -> term list * term list -> bool
     1.9    val finitize_funs :
    1.10 -    hol_context -> bool -> (typ option * bool option) list -> int -> typ
    1.11 +    hol_context -> bool -> (typ option * bool option) list -> typ
    1.12      -> term list * term list -> term list * term list
    1.13  end;
    1.14  
    1.15 @@ -538,16 +538,6 @@
    1.16      PL.SOr (prop_for_exists_var_assign_literal xs Gen,
    1.17              prop_for_comp (aa1, aa2, cmp, []))
    1.18  
    1.19 -(* The "calculus" parameter may be 1, 2, or 3, corresponding approximately to
    1.20 -   the M1, M2, and M3 calculi from Blanchette & Krauss 2011. *)
    1.21 -fun variable_domain calculus =
    1.22 -  [Gen] @ (if calculus > 1 then [Fls] else [])
    1.23 -  @ (if calculus > 2 then [New, Tru] else [])
    1.24 -
    1.25 -fun prop_for_variable_domain calculus x =
    1.26 -  PL.exists (map (fn a => prop_for_assign_literal (x, (Plus, a)))
    1.27 -                 (variable_domain calculus))
    1.28 -
    1.29  fun extract_assigns max_var assigns asgs =
    1.30    fold (fn x => fn accum =>
    1.31             if AList.defined (op =) asgs x then
    1.32 @@ -558,11 +548,10 @@
    1.33                     :: accum)
    1.34         (max_var downto 1) asgs
    1.35  
    1.36 -fun print_problem calculus comps clauses =
    1.37 -  trace_msg (fn () =>
    1.38 -                "*** Problem (calculus M" ^ string_of_int calculus ^ "):\n" ^
    1.39 -                cat_lines (map string_for_comp comps @
    1.40 -                           map (string_for_assign_clause o SOME) clauses))
    1.41 +fun print_problem comps clauses =
    1.42 +  trace_msg (fn () => "*** Problem:\n" ^
    1.43 +                      cat_lines (map string_for_comp comps @
    1.44 +                                 map (string_for_assign_clause o SOME) clauses))
    1.45  
    1.46  fun print_solution asgs =
    1.47    trace_msg (fn () => "*** Solution:\n" ^
    1.48 @@ -573,20 +562,14 @@
    1.49                               string_for_vars ", " xs)
    1.50         |> space_implode "\n"))
    1.51  
    1.52 -fun solve calculus max_var (comps, clauses) =
    1.53 +fun solve max_var (comps, clauses) =
    1.54    let
    1.55      val asgs = map_filter (fn [(x, (_, a))] => SOME (x, a) | _ => NONE) clauses
    1.56      fun do_assigns assigns =
    1.57        SOME (extract_assigns max_var assigns asgs |> tap print_solution)
    1.58 -    val _ = print_problem calculus comps clauses
    1.59 +    val _ = print_problem comps clauses
    1.60      val prop =
    1.61 -      map prop_for_comp comps @
    1.62 -      map prop_for_assign_clause clauses @
    1.63 -      (if calculus < 3 then
    1.64 -         map (prop_for_variable_domain calculus) (1 upto max_var)
    1.65 -       else
    1.66 -         [])
    1.67 -      |> PL.all
    1.68 +      PL.all (map prop_for_comp comps @ map prop_for_assign_clause clauses)
    1.69    in
    1.70      if PL.eval (K false) prop then
    1.71        do_assigns (K (SOME false))
    1.72 @@ -809,26 +792,6 @@
    1.73          M as MPair (a_M, b_M) =>
    1.74          pair (MFun (M, A Gen, if n = 0 then a_M else b_M))
    1.75        | M => raise MTYPE ("Nitpick_Mono.consider_term.do_nth_pair_sel", [M], [])
    1.76 -    fun do_bounded_quantifier t0 abs_s abs_T connective_t bound_t body_t accum =
    1.77 -      let
    1.78 -        val abs_M = mtype_for abs_T
    1.79 -        val aa = V (Unsynchronized.inc max_fresh)
    1.80 -        val (bound_m, accum) =
    1.81 -          accum |>> push_bound aa abs_T abs_M |> do_term bound_t
    1.82 -        val expected_bound_M = plus_set_mtype_for_dom abs_M
    1.83 -        val (body_m, accum) =
    1.84 -          accum ||> add_mtypes_equal expected_bound_M (mtype_of_mterm bound_m)
    1.85 -                |> do_term body_t ||> apfst pop_bound
    1.86 -        val bound_M = mtype_of_mterm bound_m
    1.87 -        val (M1, aa', _) = dest_MFun bound_M
    1.88 -      in
    1.89 -        (MApp (MRaw (t0, MFun (bound_M, aa, bool_M)),
    1.90 -               MAbs (abs_s, abs_T, M1, aa',
    1.91 -                     MApp (MApp (MRaw (connective_t,
    1.92 -                                       mtype_for (fastype_of connective_t)),
    1.93 -                                 MApp (bound_m, MRaw (Bound 0, M1))),
    1.94 -                           body_m))), accum)
    1.95 -      end
    1.96      and do_connect conn mk_quasi_clauses t0 t1 t2 (accum as ({frame, ...}, _)) =
    1.97        let
    1.98          val frame1 = fresh_frame mdata (SOME Tru) NONE frame
    1.99 @@ -997,12 +960,6 @@
   1.100                          val (m', accum) =
   1.101                            do_term t' (accum |>> push_bound aa T M)
   1.102                        in (MAbs (s, T, M, aa, m'), accum |>> pop_bound) end))
   1.103 -         | (t0 as Const (@{const_name All}, _))
   1.104 -           $ Abs (s', T', (t10 as @{const implies}) $ (t11 $ Bound 0) $ t12) =>
   1.105 -           do_bounded_quantifier t0 s' T' t10 t11 t12 accum
   1.106 -         | (t0 as Const (@{const_name Ex}, _))
   1.107 -           $ Abs (s', T', (t10 as @{const conj}) $ (t11 $ Bound 0) $ t12) =>
   1.108 -           do_bounded_quantifier t0 s' T' t10 t11 t12 accum
   1.109           | @{const Not} $ t1 =>
   1.110             do_connect "\<implies>" imp_clauses @{const implies} t1
   1.111                        @{const False} accum
   1.112 @@ -1043,11 +1000,10 @@
   1.113                                         string_for_mterm ctxt m))
   1.114    in do_term end
   1.115  
   1.116 -fun force_minus_funs 0 _ = I
   1.117 -  | force_minus_funs n (M as MFun (M1, _, M2)) =
   1.118 -    add_mtypes_equal M (MFun (M1, A Gen, M2)) #> force_minus_funs (n - 1) M2
   1.119 -  | force_minus_funs _ M =
   1.120 -    raise MTYPE ("Nitpick_Mono.force_minus_funs", [M], [])
   1.121 +fun force_gen_funs 0 _ = I
   1.122 +  | force_gen_funs n (M as MFun (M1, _, M2)) =
   1.123 +    add_mtypes_equal M (MFun (M1, A Gen, M2)) #> force_gen_funs (n - 1) M2
   1.124 +  | force_gen_funs _ M = raise MTYPE ("Nitpick_Mono.force_gen_funs", [M], [])
   1.125  fun consider_general_equals mdata def (x as (_, T)) t1 t2 accum =
   1.126    let
   1.127      val (m1, accum) = consider_term mdata t1 accum
   1.128 @@ -1057,11 +1013,11 @@
   1.129      val accum = accum ||> add_mtypes_equal M1 M2
   1.130      val body_M = fresh_mtype_for_type mdata false (nth_range_type 2 T)
   1.131      val m = MApp (MApp (MRaw (Const x,
   1.132 -                           MFun (M1, A Gen, MFun (M2, A Gen, body_M))), m1), m2)
   1.133 +                           MFun (M1, A Gen, MFun (M2, A Fls, body_M))), m1), m2)
   1.134    in
   1.135      (m, if def then
   1.136            let val (head_m, arg_ms) = strip_mcomb m1 in
   1.137 -            accum ||> force_minus_funs (length arg_ms) (mtype_of_mterm head_m)
   1.138 +            accum ||> force_gen_funs (length arg_ms) (mtype_of_mterm head_m)
   1.139            end
   1.140          else
   1.141            accum)
   1.142 @@ -1079,9 +1035,10 @@
   1.143              val abs_M = mtype_for abs_T
   1.144              val x = Unsynchronized.inc max_fresh
   1.145              val side_cond = ((sn = Minus) = (quant_s = @{const_name Ex}))
   1.146 +            fun ann () = if quant_s = @{const_name Ex} then Fls else Tru
   1.147              val (body_m, accum) =
   1.148                accum ||> side_cond
   1.149 -                        ? add_mtype_is_complete [(x, (Minus, Fls))] abs_M
   1.150 +                        ? add_mtype_is_complete [(x, (Minus, ann ()))] abs_M
   1.151                      |>> push_bound (V x) abs_T abs_M |> do_formula sn body_t
   1.152              val body_M = mtype_of_mterm body_m
   1.153            in
   1.154 @@ -1114,8 +1071,7 @@
   1.155             end
   1.156           | Const (x as (@{const_name All}, _)) $ Abs (s1, T1, t1) =>
   1.157             do_quantifier x s1 T1 t1
   1.158 -         | Const (x0 as (@{const_name Ex}, T0))
   1.159 -           $ (t1 as Abs (s1, T1, t1')) =>
   1.160 +         | Const (x0 as (@{const_name Ex}, T0)) $ (t1 as Abs (s1, T1, t1')) =>
   1.161             (case sn of
   1.162                Plus => do_quantifier x0 s1 T1 t1'
   1.163              | Minus =>
   1.164 @@ -1240,7 +1196,7 @@
   1.165  fun amass f t (ms, accum) =
   1.166    let val (m, accum) = f t accum in (m :: ms, accum) end
   1.167  
   1.168 -fun infer which no_harmless (hol_ctxt as {ctxt, ...}) binarize calculus alpha_T
   1.169 +fun infer which no_harmless (hol_ctxt as {ctxt, ...}) binarize alpha_T
   1.170            (nondef_ts, def_ts) =
   1.171    let
   1.172      val _ = trace_msg (fn () => "****** " ^ which ^ " analysis: " ^
   1.173 @@ -1256,7 +1212,7 @@
   1.174      val (def_ms, (gamma, cset)) =
   1.175        ([], accum) |> fold (amass (consider_definitional_axiom mdata)) def_ts
   1.176    in
   1.177 -    case solve calculus (!max_fresh) cset of
   1.178 +    case solve (!max_fresh) cset of
   1.179        SOME asgs => (print_mcontext ctxt asgs gamma;
   1.180                      SOME (asgs, (nondef_ms, def_ms), !constr_mcache))
   1.181      | _ => NONE
   1.182 @@ -1268,15 +1224,14 @@
   1.183         | MTERM (loc, ms) =>
   1.184           raise BAD (loc, commas (map (string_for_mterm ctxt) ms))
   1.185  
   1.186 -fun formulas_monotonic hol_ctxt =
   1.187 -  is_some oooo infer "Monotonicity" false hol_ctxt
   1.188 +val formulas_monotonic = is_some oooo infer "Monotonicity" false
   1.189  
   1.190  fun fin_fun_constr T1 T2 =
   1.191    (@{const_name FinFun}, (T1 --> T2) --> Type (@{type_name fin_fun}, [T1, T2]))
   1.192  
   1.193  fun finitize_funs (hol_ctxt as {thy, ctxt, stds, constr_cache, ...}) binarize
   1.194 -                  finitizes calculus alpha_T tsp =
   1.195 -  case infer "Finiteness" true hol_ctxt binarize calculus alpha_T tsp of
   1.196 +                  finitizes alpha_T tsp =
   1.197 +  case infer "Finiteness" true hol_ctxt binarize alpha_T tsp of
   1.198      SOME (asgs, msp, constr_mtypes) =>
   1.199      if forall (curry (op =) Gen o snd) asgs then
   1.200        tsp