File: provers.c

package info (click to toggle)
ladr 0.0.200902a-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 11,400 kB
  • ctags: 7,168
  • sloc: ansic: 59,953; perl: 1,006; python: 620; makefile: 403; sh: 86; csh: 58; modula3: 13
file content (357 lines) | stat: -rw-r--r-- 10,008 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*  Copyright (C) 2006, 2007 William McCune

    This file is part of the LADR Deduction Library.

    The LADR Deduction Library is free software; you can redistribute it
    and/or modify it under the terms of the GNU General Public License,
    version 2.

    The LADR Deduction Library is distributed in the hope that it will be
    useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with the LADR Deduction Library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "provers.h"

#include <unistd.h>  /* for getopt */
#include <signal.h>

/* Private definitions and types */

static char Help_string[] = 
"\nUsage: prover9 [-h] [-x] [-p] [-t <n>] [-f <files>]\n"
"\n"
"  -h         Help.  Also see http://www.cs.unm.edu/~mccune/prover9/\n"
"  -x         set(auto2).  (enhanced auto mode)\n"
"  -p         Fully parenthesize output.\n"
"  -t n       assign(max_seconds, n).  (overrides ordinary input)\n"
"  -f files   Take input from files instead of from standard input.\n"
"\n";

struct arg_options {
  BOOL parenthesize_output;
  BOOL auto2;
  int  max_seconds;
  BOOL files;
};

/*************
 *
 *   get_command_line_args()
 *
 * This gets the command-line options and stores them in a structure
 * for later processing.  If an option is not recognized, the help
 * string is printed and then exit(1).
 *
 *************/

static
struct arg_options get_command_line_args(int argc, char **argv)
{
  extern char *optarg;
  int c;
  struct arg_options opts = {FALSE, FALSE, INT_MAX, FALSE};

  /* getopt() option string

     Initial colon: unrecognized options will not cause errors.

     No colons after option:  no argument.
     One colon after option:  argument required.
     Two colon after options: argument optional. (GNU extension! Don't use it!)
  */
  
  while ((c = getopt(argc, argv,":hapxt:f")) != EOF) {
    switch (c) {
    case 'x':
      opts.auto2 = TRUE;
      break;
    case 'p':
      opts.parenthesize_output = TRUE;
      break;
    case 't':
      opts.max_seconds = atoi(optarg);
      break;
    case 'f':  /* input files */
      opts.files = TRUE;
      break;
    case 'h':
    default:
      printf("%s", Help_string);
      exit(1);
      break;
    }
  }
  if (FALSE) {
    int i;
    for (i = 0; i < argc; i++)
      printf("arg: %s\n", argv[i]);
  }
    
  return opts;
}  /* get_command_line_args */

/*************
 *
 *   max_megs_exit()
 *
 *   This is intended to be called from the memory package.
 *
 *************/

static
void max_megs_exit(void)
{
  fprint_all_stats(stdout, "all");
  exit_with_message(stdout, MAX_MEGS_EXIT);
}  /* max_megs_exit */

/*************
 *
 *   process_command_line_args_1()
 *
 *   This is called BEFORE the input (clauses, etc) are read.  This is
 *   intended for high-level options that affect how input is read.
 *
 *************/

static
void process_command_line_args_1(struct arg_options command_opt,
				 Prover_options prover_opt)
{
  if (command_opt.auto2) {
    printf("\n%% From the command line: set(auto2).\n");
    set_flag(prover_opt->auto2, TRUE);
  }

  if (command_opt.parenthesize_output) {
    printf("\n%% From the command line: parenthesize output.\n");
    parenthesize(TRUE);  /* tell the parsing/printing package */
  }

}  /* process_command_line_args_1 */

/*************
 *
 *   process_command_line_args_2()
 *
 * This is called AFTER the input (clauses, etc) are read.  This allows
 * command-line args to override settings in the ordinary input.
 *
 *************/

static
void process_command_line_args_2(struct arg_options command_opt,
				 Prover_options prover_opt)
{
  if (command_opt.max_seconds != INT_MAX) {
    int n = command_opt.max_seconds;
    int id = prover_opt->max_seconds;
    printf("\n%% From the command line: assign(%s, %d).\n",
	   parm_id_to_str(id), n);
    assign_parm(id, n, TRUE);
  }

  set_max_megs(parm(prover_opt->max_megs));
  set_max_megs_proc(max_megs_exit);

}  /* process_command_line_args_2 */

/*************
 *
 *   prover_sig_handler()
 *
 *************/

static
void prover_sig_handler(int condition)
{
  printf("\nProver catching signal %d.\n", condition);
  switch (condition) {
  case SIGSEGV:
    fprint_all_stats(stdout, "all");
    exit_with_message(stdout, SIGSEGV_EXIT);
    break;
  case SIGINT:
    fprint_all_stats(stdout, "all");
    exit_with_message(stdout, SIGINT_EXIT);
    break;
  case SIGUSR1:
    report(stdout, "");
    report(stderr, "");
    break;
  default: fatal_error("prover_sig_handler, unknown signal");
  }
}  /* prover_sig_handler */

/*************
 *
 *   std_prover_init_and_input()
 *
 *************/

/* DOCUMENTATION
*/

/* PUBLIC */
Prover_input std_prover_init_and_input(int argc, char **argv,
				       BOOL clausify,
				       BOOL echo,
				       int unknown_action)
{
  Prover_input pi = calloc(1, sizeof(struct prover_input));

  struct arg_options opts = get_command_line_args(argc, argv);

  init_standard_ladr();

  pi->options = init_prover_options();

  init_prover_attributes();

  signal(SIGINT,  prover_sig_handler);
  signal(SIGUSR1, prover_sig_handler);
  signal(SIGSEGV, prover_sig_handler);

  // Tell the top_input package what lists to accept and where to put them.

  accept_list("sos",          FORMULAS, FALSE, &(pi->sos));
  accept_list("assumptions",  FORMULAS, FALSE, &(pi->sos));  // Synonym for sos
  accept_list("goals",        FORMULAS, FALSE, &(pi->goals));
  accept_list("usable",       FORMULAS, FALSE, &(pi->usable));
  accept_list("demodulators", FORMULAS, FALSE, &(pi->demods));
  accept_list("hints",        FORMULAS, TRUE,  &(pi->hints));

  accept_list("actions",         TERMS, FALSE, &(pi->actions));
  accept_list("weights",         TERMS, FALSE, &(pi->weights));
  accept_list("kbo_weights",     TERMS, FALSE, &(pi->kbo_weights));
  accept_list("interpretations", TERMS, FALSE, &(pi->interps));
  accept_list("given_selection", TERMS, FALSE, &(pi->given_selection));
  accept_list("keep",            TERMS, FALSE, &(pi->keep_rules));
  accept_list("delete",          TERMS, FALSE, &(pi->delete_rules));

  if (echo)
    print_separator(stdout, "INPUT", TRUE);

  process_command_line_args_1(opts, pi->options);  // high-level, e.g., auto2

  // Read commands such as set, clear, op, lex.
  // Read lists, filling in variables given to the accept_list calls.

  read_all_input(argc, argv, stdout, echo, unknown_action);

  if (echo)
    print_separator(stdout, "end of input", TRUE);
  process_command_line_args_2(opts, pi->options);  // others, which override

  if (!option_dependencies_state()) {
    printf("\n%% Enabling option dependencies (ignore applies only on input).\n");
    enable_option_dependencies();
  }

  if (clausify) {
    Plist denials;

    pi->sos    = embed_formulas_in_topforms(pi->sos, TRUE);
    pi->usable = embed_formulas_in_topforms(pi->usable, TRUE);
    pi->demods = embed_formulas_in_topforms(pi->demods, TRUE);
    pi->hints  = embed_formulas_in_topforms(pi->hints, TRUE);
    pi->goals  = embed_formulas_in_topforms(pi->goals, FALSE);

    if (flag(pi->options->expand_relational_defs)) {
      Plist defs, nondefs, p;
      separate_definitions(pi->sos, &defs, &nondefs);
      pi->sos = NULL;
      print_separator(stdout, "EXPAND RELATIONAL DEFINITIONS", TRUE);
      if (defs) {
	Plist results, rewritten, defs2;

	printf("\n%% Relational Definitions:\n");
	for (p = defs; p; p = p->next) {
	  Topform tf = p->v;
	  assign_clause_id(tf);
	  fwrite_clause(stdout, tf, CL_FORM_STD);
	}

	/* Expand definitions w.r.t. themselves. */
	process_definitions(defs, &results, &defs2, &rewritten);
	if (results != NULL)
	  fatal_error("Circular relational definitions");

	defs = defs2;

	if (rewritten) {
	  printf("\n%% Relational Definitions, Expanded:\n");
	  for (p = defs; p; p = p->next)
	    if (!has_input_just(p->v))
	      fwrite_clause(stdout, p->v, CL_FORM_STD);
	}

	results = NULL;
	expand_with_definitions(nondefs, defs, &results, &rewritten);
	pi->sos = reverse_plist(results);

	results = NULL;
	expand_with_definitions(pi->usable, defs, &results, &rewritten);
	pi->usable = reverse_plist(results);

	results = NULL;
	expand_with_definitions(pi->hints, defs, &results, &rewritten);
	pi->hints = reverse_plist(results);

	results = NULL;
	expand_with_definitions(pi->goals, defs, &results, &rewritten);
	pi->goals = reverse_plist(results);

	for (p = defs; p; p = p->next)
	  append_label_attribute(p->v, "non_clause");

	if (rewritten) {
	  printf("\n%% Formulas Being Expanded:\n");
	  rewritten = reverse_plist(rewritten);
	  for (p = rewritten; p; p = p->next) {
	    Topform tf = p->v;
	    fwrite_clause(stdout, tf, CL_FORM_STD);
	    append_label_attribute(tf, "non_clause");
	  }
	}
	/* After this point, the "defs" and "rewritten" formulas
	   will be accessible only from the Clause ID table, e.g.,
	   for inclusion in proofs.
	 */
      }
      else {
	printf("\n%% No relational definitions were found.\n");
	pi->sos = nondefs;
      }
      print_separator(stdout, "end of expand relational definitions", TRUE);
    }

    print_separator(stdout, "PROCESS NON-CLAUSAL FORMULAS", TRUE);
    if (echo)
      printf("\n%% Formulas that are not ordinary clauses:\n");

    pi->sos    = process_input_formulas(pi->sos, echo);
    pi->usable = process_input_formulas(pi->usable, echo);
    pi->demods = process_demod_formulas(pi->demods, echo);
    pi->hints  = process_input_formulas(pi->hints, echo);
    denials    = process_goal_formulas(pi->goals, echo);

    pi->goals = NULL;

    /* move to denials (negated goals) to the end of sos */

    pi->sos = plist_cat(pi->sos, denials);

    print_separator(stdout, "end of process non-clausal formulas", TRUE);
  }

  return pi;
}  /* std_prover_init_and_input */