src/HOL/Tools/res_hol_clause.ML
changeset 30150 4d5a98cebb24
parent 30149 6b7ad52c5770
child 30151 629f3a92863e
     1.1 --- a/src/HOL/Tools/res_hol_clause.ML	Tue Feb 24 18:06:36 2009 +0100
     1.2 +++ b/src/HOL/Tools/res_hol_clause.ML	Wed Feb 25 10:02:10 2009 +0100
     1.3 @@ -59,14 +59,12 @@
     1.4    use of the "apply" operator. Use of hBOOL is also minimized.*)
     1.5  val minimize_applies = ref true;
     1.6  
     1.7 -val const_needs_hBOOL = ref (Symtab.empty : bool Symtab.table);
     1.8 -
     1.9  fun min_arity_of const_min_arity c = getOpt (Symtab.lookup const_min_arity c, 0);
    1.10  
    1.11  (*True if the constant ever appears outside of the top-level position in literals.
    1.12    If false, the constant always receives all of its arguments and is used as a predicate.*)
    1.13 -fun needs_hBOOL c = not (!minimize_applies) orelse
    1.14 -                    getOpt (Symtab.lookup(!const_needs_hBOOL) c, false);
    1.15 +fun needs_hBOOL const_needs_hBOOL c = not (!minimize_applies) orelse
    1.16 +                    getOpt (Symtab.lookup const_needs_hBOOL c, false);
    1.17  
    1.18  
    1.19  (******************************************************)
    1.20 @@ -216,8 +214,8 @@
    1.21  
    1.22  val type_wrapper = "ti";
    1.23  
    1.24 -fun head_needs_hBOOL (CombConst(c,_,_)) = needs_hBOOL c
    1.25 -  | head_needs_hBOOL _ = true;
    1.26 +fun head_needs_hBOOL const_needs_hBOOL (CombConst(c,_,_)) = needs_hBOOL const_needs_hBOOL c
    1.27 +  | head_needs_hBOOL const_needs_hBOOL _ = true;
    1.28  
    1.29  fun wrap_type (s, tp) =
    1.30    if !typ_level=T_FULL then
    1.31 @@ -233,9 +231,9 @@
    1.32  
    1.33  (*Apply an operator to the argument strings, using either the "apply" operator or
    1.34    direct function application.*)
    1.35 -fun string_of_applic const_min_arity (CombConst(c,tp,tvars), args) =
    1.36 +fun string_of_applic cma (CombConst(c,tp,tvars), args) =
    1.37        let val c = if c = "equal" then "c_fequal" else c
    1.38 -          val nargs = min_arity_of const_min_arity c
    1.39 +          val nargs = min_arity_of cma c
    1.40            val args1 = List.take(args, nargs)
    1.41              handle Subscript => error ("string_of_applic: " ^ c ^ " has arity " ^
    1.42                                           Int.toString nargs ^ " but is applied to " ^
    1.43 @@ -246,30 +244,30 @@
    1.44        in
    1.45            string_apply (c ^ RC.paren_pack (args1@targs), args2)
    1.46        end
    1.47 -  | string_of_applic const_min_arity (CombVar(v,tp), args) = string_apply (v, args)
    1.48 +  | string_of_applic cma (CombVar(v,tp), args) = string_apply (v, args)
    1.49    | string_of_applic _ _ = error "string_of_applic";
    1.50  
    1.51 -fun wrap_type_if (head, s, tp) = if head_needs_hBOOL head then wrap_type (s, tp) else s;
    1.52 +fun wrap_type_if cnh (head, s, tp) = if head_needs_hBOOL cnh head then wrap_type (s, tp) else s;
    1.53  
    1.54 -fun string_of_combterm const_min_arity t =
    1.55 +fun string_of_combterm cma cnh t =
    1.56    let val (head, args) = strip_comb t
    1.57 -  in  wrap_type_if (head,
    1.58 -                    string_of_applic const_min_arity (head, map (string_of_combterm const_min_arity) args),
    1.59 +  in  wrap_type_if cnh (head,
    1.60 +                    string_of_applic cma (head, map (string_of_combterm cma cnh) args),
    1.61                      type_of_combterm t)
    1.62    end;
    1.63  
    1.64  (*Boolean-valued terms are here converted to literals.*)
    1.65 -fun boolify const_min_arity t = "hBOOL" ^ RC.paren_pack [string_of_combterm const_min_arity t];
    1.66 +fun boolify cma cnh t = "hBOOL" ^ RC.paren_pack [string_of_combterm cma cnh t];
    1.67  
    1.68 -fun string_of_predicate const_min_arity t =
    1.69 +fun string_of_predicate cma cnh t =
    1.70    case t of
    1.71        (CombApp(CombApp(CombConst("equal",_,_), t1), t2)) =>
    1.72            (*DFG only: new TPTP prefers infix equality*)
    1.73 -          ("equal" ^ RC.paren_pack [string_of_combterm const_min_arity t1, string_of_combterm const_min_arity t2])
    1.74 +          ("equal" ^ RC.paren_pack [string_of_combterm cma cnh t1, string_of_combterm cma cnh t2])
    1.75      | _ =>
    1.76            case #1 (strip_comb t) of
    1.77 -              CombConst(c,_,_) => if needs_hBOOL c then boolify const_min_arity t else string_of_combterm const_min_arity t
    1.78 -            | _ => boolify const_min_arity t;
    1.79 +              CombConst(c,_,_) => if needs_hBOOL cnh c then boolify cma cnh t else string_of_combterm cma cnh t
    1.80 +            | _ => boolify cma cnh t;
    1.81  
    1.82  fun string_of_clausename (cls_id,ax_name) =
    1.83      RC.clause_prefix ^ RC.ascii_of ax_name ^ "_" ^ Int.toString cls_id;
    1.84 @@ -280,23 +278,23 @@
    1.85  
    1.86  (*** tptp format ***)
    1.87  
    1.88 -fun tptp_of_equality const_min_arity pol (t1,t2) =
    1.89 +fun tptp_of_equality cma cnh pol (t1,t2) =
    1.90    let val eqop = if pol then " = " else " != "
    1.91 -  in  string_of_combterm const_min_arity t1 ^ eqop ^ string_of_combterm const_min_arity t2  end;
    1.92 +  in  string_of_combterm cma cnh t1 ^ eqop ^ string_of_combterm cma cnh t2  end;
    1.93  
    1.94 -fun tptp_literal const_min_arity (Literal(pol, CombApp(CombApp(CombConst("equal",_,_), t1), t2))) =
    1.95 -      tptp_of_equality const_min_arity pol (t1,t2)
    1.96 -  | tptp_literal const_min_arity (Literal(pol,pred)) =
    1.97 -      RC.tptp_sign pol (string_of_predicate const_min_arity pred);
    1.98 +fun tptp_literal cma cnh (Literal(pol, CombApp(CombApp(CombConst("equal",_,_), t1), t2))) =
    1.99 +      tptp_of_equality cma cnh pol (t1,t2)
   1.100 +  | tptp_literal cma cnh (Literal(pol,pred)) =
   1.101 +      RC.tptp_sign pol (string_of_predicate cma cnh pred);
   1.102  
   1.103  (*Given a clause, returns its literals paired with a list of literals concerning TFrees;
   1.104    the latter should only occur in conjecture clauses.*)
   1.105 -fun tptp_type_lits const_min_arity pos (Clause{literals, ctypes_sorts, ...}) =
   1.106 -      (map (tptp_literal const_min_arity) literals, 
   1.107 +fun tptp_type_lits cma cnh pos (Clause{literals, ctypes_sorts, ...}) =
   1.108 +      (map (tptp_literal cma cnh) literals, 
   1.109         map (RC.tptp_of_typeLit pos) (RC.add_typs ctypes_sorts));
   1.110  
   1.111 -fun clause2tptp const_min_arity (cls as Clause{axiom_name,clause_id,kind,ctypes_sorts,...}) =
   1.112 -  let val (lits,tylits) = tptp_type_lits const_min_arity (kind = RC.Conjecture) cls
   1.113 +fun clause2tptp cma cnh (cls as Clause{axiom_name,clause_id,kind,ctypes_sorts,...}) =
   1.114 +  let val (lits,tylits) = tptp_type_lits cma cnh (kind = RC.Conjecture) cls
   1.115    in
   1.116        (RC.gen_tptp_cls(clause_id,axiom_name,kind,lits,tylits), tylits)
   1.117    end;
   1.118 @@ -304,10 +302,10 @@
   1.119  
   1.120  (*** dfg format ***)
   1.121  
   1.122 -fun dfg_literal const_min_arity (Literal(pol,pred)) = RC.dfg_sign pol (string_of_predicate const_min_arity pred);
   1.123 +fun dfg_literal cma cnh (Literal(pol,pred)) = RC.dfg_sign pol (string_of_predicate cma cnh pred);
   1.124  
   1.125 -fun dfg_type_lits const_min_arity pos (Clause{literals, ctypes_sorts, ...}) =
   1.126 -      (map (dfg_literal const_min_arity) literals, 
   1.127 +fun dfg_type_lits cma cnh pos (Clause{literals, ctypes_sorts, ...}) =
   1.128 +      (map (dfg_literal cma cnh) literals, 
   1.129         map (RC.dfg_of_typeLit pos) (RC.add_typs ctypes_sorts));
   1.130  
   1.131  fun get_uvars (CombConst _) vars = vars
   1.132 @@ -318,8 +316,8 @@
   1.133  
   1.134  fun dfg_vars (Clause {literals,...}) = RC.union_all (map get_uvars_l literals);
   1.135  
   1.136 -fun clause2dfg const_min_arity (cls as Clause{axiom_name,clause_id,kind,ctypes_sorts,...}) =
   1.137 -  let val (lits,tylits) = dfg_type_lits const_min_arity (kind = RC.Conjecture) cls
   1.138 +fun clause2dfg cma cnh (cls as Clause{axiom_name,clause_id,kind,ctypes_sorts,...}) =
   1.139 +  let val (lits,tylits) = dfg_type_lits cma cnh (kind = RC.Conjecture) cls
   1.140        val vars = dfg_vars cls
   1.141        val tvars = RC.get_tvar_strs ctypes_sorts
   1.142    in
   1.143 @@ -331,30 +329,30 @@
   1.144  
   1.145  fun addtypes tvars tab = foldl RC.add_foltype_funcs tab tvars;
   1.146  
   1.147 -fun add_decls const_min_arity (CombConst(c,tp,tvars), (funcs,preds)) =
   1.148 +fun add_decls cma cnh (CombConst(c,tp,tvars), (funcs,preds)) =
   1.149        if c = "equal" then (addtypes tvars funcs, preds)
   1.150        else
   1.151 -	let val arity = min_arity_of const_min_arity c
   1.152 +	let val arity = min_arity_of cma c
   1.153  	    val ntys = if !typ_level = T_CONST then length tvars else 0
   1.154  	    val addit = Symtab.update(c, arity+ntys)
   1.155  	in
   1.156 -	    if needs_hBOOL c then (addtypes tvars (addit funcs), preds)
   1.157 +	    if needs_hBOOL cnh c then (addtypes tvars (addit funcs), preds)
   1.158  	    else (addtypes tvars funcs, addit preds)
   1.159  	end
   1.160 -  | add_decls _ (CombVar(_,ctp), (funcs,preds)) =
   1.161 +  | add_decls _ _ (CombVar(_,ctp), (funcs,preds)) =
   1.162        (RC.add_foltype_funcs (ctp,funcs), preds)
   1.163 -  | add_decls const_min_arity (CombApp(P,Q),decls) = add_decls const_min_arity (P,add_decls const_min_arity (Q,decls));
   1.164 +  | add_decls cma cnh (CombApp(P,Q),decls) = add_decls cma cnh (P,add_decls cma cnh (Q,decls));
   1.165  
   1.166 -fun add_literal_decls const_min_arity (Literal(_,c), decls) = add_decls const_min_arity (c,decls);
   1.167 +fun add_literal_decls cma cnh (Literal(_,c), decls) = add_decls cma cnh (c,decls);
   1.168  
   1.169 -fun add_clause_decls const_min_arity (Clause {literals, ...}, decls) =
   1.170 -    foldl (add_literal_decls const_min_arity) decls literals
   1.171 +fun add_clause_decls cma cnh (Clause {literals, ...}, decls) =
   1.172 +    foldl (add_literal_decls cma cnh) decls literals
   1.173      handle Symtab.DUP a => error ("function " ^ a ^ " has multiple arities")
   1.174  
   1.175 -fun decls_of_clauses const_min_arity clauses arity_clauses =
   1.176 +fun decls_of_clauses cma cnh clauses arity_clauses =
   1.177    let val init_functab = Symtab.update (type_wrapper,2) (Symtab.update ("hAPP",2) RC.init_functab)
   1.178        val init_predtab = Symtab.update ("hBOOL",1) Symtab.empty
   1.179 -      val (functab,predtab) = (foldl (add_clause_decls const_min_arity) (init_functab, init_predtab) clauses)
   1.180 +      val (functab,predtab) = (foldl (add_clause_decls cma cnh) (init_functab, init_predtab) clauses)
   1.181    in
   1.182        (Symtab.dest (foldl RC.add_arityClause_funcs functab arity_clauses),
   1.183         Symtab.dest predtab)
   1.184 @@ -422,43 +420,42 @@
   1.185  
   1.186  (*Find the minimal arity of each function mentioned in the term. Also, note which uses
   1.187    are not at top level, to see if hBOOL is needed.*)
   1.188 -fun count_constants_term toplev t const_min_arity =
   1.189 +fun count_constants_term toplev t (const_min_arity, const_needs_hBOOL) =
   1.190    let val (head, args) = strip_comb t
   1.191        val n = length args
   1.192 -      val const_min_arity = fold (count_constants_term false) args const_min_arity
   1.193 +      val (const_min_arity, const_needs_hBOOL) = fold (count_constants_term false) args (const_min_arity, const_needs_hBOOL)
   1.194    in
   1.195        case head of
   1.196            CombConst (a,_,_) => (*predicate or function version of "equal"?*)
   1.197              let val a = if a="equal" andalso not toplev then "c_fequal" else a
   1.198              val const_min_arity = Symtab.map_default (a,n) (curry Int.min n) const_min_arity
   1.199              in
   1.200 -              if toplev then const_min_arity
   1.201 -              else (const_needs_hBOOL := Symtab.update (a,true) (!const_needs_hBOOL);
   1.202 -                const_min_arity)
   1.203 +              if toplev then (const_min_arity, const_needs_hBOOL)
   1.204 +              else (const_min_arity, Symtab.update (a,true) (const_needs_hBOOL))
   1.205              end
   1.206 -        | ts => const_min_arity
   1.207 +        | ts => (const_min_arity, const_needs_hBOOL)
   1.208    end;
   1.209  
   1.210  (*A literal is a top-level term*)
   1.211 -fun count_constants_lit (Literal (_,t)) const_min_arity = count_constants_term true t const_min_arity;
   1.212 +fun count_constants_lit (Literal (_,t)) (const_min_arity, const_needs_hBOOL) =
   1.213 +  count_constants_term true t (const_min_arity, const_needs_hBOOL);
   1.214  
   1.215 -fun count_constants_clause (Clause{literals,...}) const_min_arity =
   1.216 -  fold count_constants_lit literals const_min_arity;
   1.217 +fun count_constants_clause (Clause{literals,...}) (const_min_arity, const_needs_hBOOL) =
   1.218 +  fold count_constants_lit literals (const_min_arity, const_needs_hBOOL);
   1.219  
   1.220 -fun display_arity (c,n) =
   1.221 +fun display_arity const_needs_hBOOL (c,n) =
   1.222    Output.debug (fn () => "Constant: " ^ c ^ " arity:\t" ^ Int.toString n ^
   1.223 -                (if needs_hBOOL c then " needs hBOOL" else ""));
   1.224 +                (if needs_hBOOL const_needs_hBOOL c then " needs hBOOL" else ""));
   1.225  
   1.226  fun count_constants (conjectures, axclauses, helper_clauses) =
   1.227    if !minimize_applies then
   1.228 -    (const_needs_hBOOL := Symtab.empty;
   1.229 -     let val const_min_arity =
   1.230 -          fold count_constants_clause conjectures Symtab.empty
   1.231 +     let val (const_min_arity, const_needs_hBOOL) =
   1.232 +          fold count_constants_clause conjectures (Symtab.empty, Symtab.empty)
   1.233         |> fold count_constants_clause axclauses
   1.234         |> fold count_constants_clause helper_clauses
   1.235 -     val _ = List.app display_arity (Symtab.dest (const_min_arity))
   1.236 -     in const_min_arity end) 
   1.237 -  else Symtab.empty;
   1.238 +     val _ = List.app (display_arity const_needs_hBOOL) (Symtab.dest (const_min_arity))
   1.239 +     in (const_min_arity, const_needs_hBOOL) end
   1.240 +  else (Symtab.empty, Symtab.empty);
   1.241  
   1.242  (* tptp format *)
   1.243  
   1.244 @@ -469,17 +466,17 @@
   1.245          val conjectures = make_conjecture_clauses thy thms
   1.246          val (clnames,axclauses) = ListPair.unzip (make_axiom_clauses thy ax_tuples)
   1.247          val helper_clauses = get_helper_clauses thy isFO (conjectures, axclauses, user_lemmas)
   1.248 -        val const_min_arity = count_constants (conjectures, axclauses, helper_clauses);
   1.249 -        val (tptp_clss,tfree_litss) = ListPair.unzip (map (clause2tptp const_min_arity) conjectures)
   1.250 +        val (const_min_arity, const_needs_hBOOL) = count_constants (conjectures, axclauses, helper_clauses);
   1.251 +        val (tptp_clss,tfree_litss) = ListPair.unzip (map (clause2tptp const_min_arity const_needs_hBOOL) conjectures)
   1.252          val tfree_clss = map RC.tptp_tfree_clause (foldl (op union_string) [] tfree_litss)
   1.253          val out = TextIO.openOut filename
   1.254      in
   1.255 -        List.app (curry TextIO.output out o #1 o (clause2tptp const_min_arity)) axclauses;
   1.256 +        List.app (curry TextIO.output out o #1 o (clause2tptp const_min_arity const_needs_hBOOL)) axclauses;
   1.257          RC.writeln_strs out tfree_clss;
   1.258          RC.writeln_strs out tptp_clss;
   1.259          List.app (curry TextIO.output out o RC.tptp_classrelClause) classrel_clauses;
   1.260          List.app (curry TextIO.output out o RC.tptp_arity_clause) arity_clauses;
   1.261 -        List.app (curry TextIO.output out o #1 o (clause2tptp const_min_arity)) helper_clauses;
   1.262 +        List.app (curry TextIO.output out o #1 o (clause2tptp const_min_arity const_needs_hBOOL)) helper_clauses;
   1.263          TextIO.closeOut out;
   1.264          clnames
   1.265      end;
   1.266 @@ -493,14 +490,14 @@
   1.267          val conjectures = make_conjecture_clauses thy thms
   1.268          val (clnames,axclauses) = ListPair.unzip (make_axiom_clauses thy ax_tuples)
   1.269          val helper_clauses = get_helper_clauses thy isFO (conjectures, axclauses, user_lemmas)
   1.270 -        val const_min_arity = count_constants (conjectures, axclauses, helper_clauses);
   1.271 -        val (dfg_clss, tfree_litss) = ListPair.unzip (map (clause2dfg const_min_arity) conjectures)
   1.272 +        val (const_min_arity, const_needs_hBOOL) = count_constants (conjectures, axclauses, helper_clauses);
   1.273 +        val (dfg_clss, tfree_litss) = ListPair.unzip (map (clause2dfg const_min_arity const_needs_hBOOL) conjectures)
   1.274          and probname = Path.implode (Path.base (Path.explode filename))
   1.275 -        val axstrs = map (#1 o (clause2dfg const_min_arity)) axclauses
   1.276 +        val axstrs = map (#1 o (clause2dfg const_min_arity const_needs_hBOOL)) axclauses
   1.277          val tfree_clss = map RC.dfg_tfree_clause (RC.union_all tfree_litss)
   1.278          val out = TextIO.openOut filename
   1.279 -        val helper_clauses_strs = map (#1 o (clause2dfg const_min_arity)) helper_clauses
   1.280 -        val (funcs,cl_preds) = decls_of_clauses const_min_arity (helper_clauses @ conjectures @ axclauses) arity_clauses
   1.281 +        val helper_clauses_strs = map (#1 o (clause2dfg const_min_arity const_needs_hBOOL)) helper_clauses
   1.282 +        val (funcs,cl_preds) = decls_of_clauses const_min_arity const_needs_hBOOL (helper_clauses @ conjectures @ axclauses) arity_clauses
   1.283          and ty_preds = preds_of_clauses axclauses classrel_clauses arity_clauses
   1.284      in
   1.285          TextIO.output (out, RC.string_of_start probname);