src/HOL/Tools/Nitpick/nitpick_kodkod.ML
changeset 34936 c4f04bee79f3
parent 34288 cf455b5880e1
child 34982 7b8c366e34a2
equal deleted inserted replaced
34935:cb011ba38950 34936:c4f04bee79f3
   378         val binder_schema = atom_schema_of_reps (binder_reps R)
   378         val binder_schema = atom_schema_of_reps (binder_reps R)
   379         val body_schema = atom_schema_of_rep body_R
   379         val body_schema = atom_schema_of_rep body_R
   380         val one = is_one_rep body_R
   380         val one = is_one_rep body_R
   381         val opt_x = case r of KK.Rel x => SOME x | _ => NONE
   381         val opt_x = case r of KK.Rel x => SOME x | _ => NONE
   382       in
   382       in
   383         if opt_x <> NONE andalso length binder_schema = 1
   383         if opt_x <> NONE andalso length binder_schema = 1 andalso
   384            andalso length body_schema = 1 then
   384            length body_schema = 1 then
   385           (if one then KK.Function else KK.Functional)
   385           (if one then KK.Function else KK.Functional)
   386               (the opt_x, KK.AtomSeq (hd binder_schema),
   386               (the opt_x, KK.AtomSeq (hd binder_schema),
   387                KK.AtomSeq (hd body_schema))
   387                KK.AtomSeq (hd body_schema))
   388         else
   388         else
   389           let
   389           let
   488 fun lone_rep_fallback kk new_R old_R r =
   488 fun lone_rep_fallback kk new_R old_R r =
   489   if old_R = new_R then
   489   if old_R = new_R then
   490     r
   490     r
   491   else
   491   else
   492     let val card = card_of_rep old_R in
   492     let val card = card_of_rep old_R in
   493       if is_lone_rep old_R andalso is_lone_rep new_R
   493       if is_lone_rep old_R andalso is_lone_rep new_R andalso
   494          andalso card = card_of_rep new_R then
   494          card = card_of_rep new_R then
   495         if card >= lone_rep_fallback_max_card then
   495         if card >= lone_rep_fallback_max_card then
   496           raise TOO_LARGE ("Nitpick_Kodkod.lone_rep_fallback",
   496           raise TOO_LARGE ("Nitpick_Kodkod.lone_rep_fallback",
   497                            "too high cardinality (" ^ string_of_int card ^ ")")
   497                            "too high cardinality (" ^ string_of_int card ^ ")")
   498         else
   498         else
   499           kk_case_switch kk old_R new_R r (all_singletons_for_rep old_R)
   499           kk_case_switch kk old_R new_R r (all_singletons_for_rep old_R)
  1003          | Op1 (Not, _, _, u1) =>
  1003          | Op1 (Not, _, _, u1) =>
  1004            kk_not (to_f_with_polarity (flip_polarity polar) u1)
  1004            kk_not (to_f_with_polarity (flip_polarity polar) u1)
  1005          | Op1 (Finite, _, _, u1) =>
  1005          | Op1 (Finite, _, _, u1) =>
  1006            let val opt1 = is_opt_rep (rep_of u1) in
  1006            let val opt1 = is_opt_rep (rep_of u1) in
  1007              case polar of
  1007              case polar of
  1008                Neut => if opt1 then
  1008                Neut =>
  1009                          raise NUT ("Nitpick_Kodkod.to_f (Finite)", [u])
  1009                if opt1 then raise NUT ("Nitpick_Kodkod.to_f (Finite)", [u])
  1010                        else
  1010                else KK.True
  1011                          KK.True
       
  1012              | Pos => formula_for_bool (not opt1)
  1011              | Pos => formula_for_bool (not opt1)
  1013              | Neg => KK.True
  1012              | Neg => KK.True
  1014            end
  1013            end
       
  1014          | Op1 (IsUnknown, _, _, u1) => kk_no (to_r u1)
  1015          | Op1 (Cast, _, _, u1) => to_f_with_polarity polar u1
  1015          | Op1 (Cast, _, _, u1) => to_f_with_polarity polar u1
  1016          | Op2 (All, _, _, u1, u2) =>
  1016          | Op2 (All, _, _, u1, u2) =>
  1017            kk_all (untuple to_decl u1) (to_f_with_polarity polar u2)
  1017            kk_all (untuple to_decl u1) (to_f_with_polarity polar u2)
  1018          | Op2 (Exist, _, _, u1, u2) =>
  1018          | Op2 (Exist, _, _, u1, u2) =>
  1019            kk_exist (untuple to_decl u1) (to_f_with_polarity polar u2)
  1019            kk_exist (untuple to_decl u1) (to_f_with_polarity polar u2)
  1068                 fun args (Op2 (Apply, _, _, u1, u2)) = u2 :: args u1
  1068                 fun args (Op2 (Apply, _, _, u1, u2)) = u2 :: args u1
  1069                   | args (Tuple (_, _, us)) = us
  1069                   | args (Tuple (_, _, us)) = us
  1070                   | args _ = []
  1070                   | args _ = []
  1071                 val opt_arg_us = filter (is_opt_rep o rep_of) (args u1)
  1071                 val opt_arg_us = filter (is_opt_rep o rep_of) (args u1)
  1072               in
  1072               in
  1073                 if null opt_arg_us orelse not (is_Opt min_R)
  1073                 if null opt_arg_us orelse not (is_Opt min_R) orelse
  1074                    orelse is_eval_name u1 then
  1074                    is_eval_name u1 then
  1075                   fold (kk_or o (kk_no o to_r)) opt_arg_us
  1075                   fold (kk_or o (kk_no o to_r)) opt_arg_us
  1076                        (kk_rel_eq (to_rep min_R u1) (to_rep min_R u2))
  1076                        (kk_rel_eq (to_rep min_R u1) (to_rep min_R u2))
  1077                 else
  1077                 else
  1078                   kk_subset (to_rep min_R u1) (to_rep min_R u2)
  1078                   kk_subset (to_rep min_R u1) (to_rep min_R u2)
  1079               end)
  1079               end)
  1098                     val both_opt = forall (is_opt_rep o rep_of) [u1, u2]
  1098                     val both_opt = forall (is_opt_rep o rep_of) [u1, u2]
  1099                   in
  1099                   in
  1100                     (if polar = Pos then
  1100                     (if polar = Pos then
  1101                        if not both_opt then
  1101                        if not both_opt then
  1102                          kk_rel_eq r1 r2
  1102                          kk_rel_eq r1 r2
  1103                        else if is_lone_rep min_R
  1103                        else if is_lone_rep min_R andalso
  1104                                andalso arity_of_rep min_R = 1 then
  1104                                arity_of_rep min_R = 1 then
  1105                          kk_some (kk_intersect r1 r2)
  1105                          kk_some (kk_intersect r1 r2)
  1106                        else
  1106                        else
  1107                          raise SAME ()
  1107                          raise SAME ()
  1108                      else
  1108                      else
  1109                        if is_lone_rep min_R then
  1109                        if is_lone_rep min_R then
  1130                   if is_one_rep min_R then
  1130                   if is_one_rep min_R then
  1131                     let
  1131                     let
  1132                       val rs1 = unpack_products r1
  1132                       val rs1 = unpack_products r1
  1133                       val rs2 = unpack_products r2
  1133                       val rs2 = unpack_products r2
  1134                     in
  1134                     in
  1135                       if length rs1 = length rs2
  1135                       if length rs1 = length rs2 andalso
  1136                          andalso map KK.arity_of_rel_expr rs1
  1136                          map KK.arity_of_rel_expr rs1
  1137                                  = map KK.arity_of_rel_expr rs2 then
  1137                          = map KK.arity_of_rel_expr rs2 then
  1138                         fold1 kk_and (map2 kk_subset rs1 rs2)
  1138                         fold1 kk_and (map2 kk_subset rs1 rs2)
  1139                       else
  1139                       else
  1140                         kk_subset r1 r2
  1140                         kk_subset r1 r2
  1141                     end
  1141                     end
  1142                   else
  1142                   else
  1580         if is_Cst Unrep u2 andalso not (is_opt_rep (rep_of u1)) then
  1580         if is_Cst Unrep u2 andalso not (is_opt_rep (rep_of u1)) then
  1581           KK.Atom (offset_of_type ofs nat_T)
  1581           KK.Atom (offset_of_type ofs nat_T)
  1582         else
  1582         else
  1583           fold kk_join (map to_integer [u1, u2]) (KK.Rel nat_subtract_rel)
  1583           fold kk_join (map to_integer [u1, u2]) (KK.Rel nat_subtract_rel)
  1584       | Op2 (Apply, _, R, u1, u2) =>
  1584       | Op2 (Apply, _, R, u1, u2) =>
  1585         if is_Cst Unrep u2 andalso is_set_type (type_of u1)
  1585         if is_Cst Unrep u2 andalso is_set_type (type_of u1) andalso
  1586            andalso is_FreeName u1 then
  1586            is_FreeName u1 then
  1587           false_atom
  1587           false_atom
  1588         else
  1588         else
  1589           to_apply R u1 u2
  1589           to_apply R u1 u2
  1590       | Op2 (Lambda, T, R as Opt (Atom (1, j0)), u1, u2) =>
  1590       | Op2 (Lambda, T, R as Opt (Atom (1, j0)), u1, u2) =>
  1591         to_guard [u1, u2] R (KK.Atom j0)
  1591         to_guard [u1, u2] R (KK.Atom j0)