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