File: cte_typecheck.c

package info (click to toggle)
eprover 2.6%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 21,288 kB
  • sloc: ansic: 331,111; csh: 12,026; python: 10,178; awk: 5,825; makefile: 461; sh: 389
file content (431 lines) | stat: -rw-r--r-- 13,066 bytes parent folder | download
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/*-----------------------------------------------------------------------

  File  : cte_typecheck.c

  Author: Simon Cruanes, Petar Vucmirovic, Stephan Schulz

  Contents

  Type checking and inference for Simple types

  Copyright 2011-2020 by the author.
  This code is released under the GNU General Public Licence.
  See the file COPYING in the main CLIB directory for details.
  Run "eprover -h" for contact information.

  Created: Mon Jul  8 17:15:05 CEST 2013

  -----------------------------------------------------------------------*/

#include "cte_typecheck.h"
#include "cte_termfunc.h"
#include <cte_typebanks.h>

/*---------------------------------------------------------------------*/
/*                        Global Variables                             */
/*---------------------------------------------------------------------*/
extern bool app_encode;

/*---------------------------------------------------------------------*/
/*                      Forward Declarations                           */
/*---------------------------------------------------------------------*/


/*---------------------------------------------------------------------*/
/*                         Internal Functions                          */
/*---------------------------------------------------------------------*/

/*-----------------------------------------------------------------------
//
// Function: term_determine_type()
//
//   Given number of arguments and type, return the type of the term
//   resulting from consuming the number of arguments. Returned type is
//   shared.
//
// Global Variables: -
//
// Side Effects    : -
//
/----------------------------------------------------------------------*/

Type_p term_determine_type(Term_p term, Type_p type, TypeBank_p bank)
{
   int term_arity = ARG_NUM(term);
   if(type->arity-1 == term_arity)
   {
      return type->args[term_arity];
   }
   else if(type->arity-1 < term_arity)
   {
      return NULL;
   }
   else
   {
      int start = term_arity;
      Type_p* args = TypeArgArrayAlloc(type->arity - start);
      for(int i=0; i<type->arity-start; i++)
      {
         args[i] = type->args[i+start];
      }
      return TypeBankInsertTypeShared(bank,
                                      AllocArrowType(type->arity - start, args));
   }
}

/*-----------------------------------------------------------------------
//
// Function: infer_return_sort
//
//   infer the return sort of the given function symbol, given the signature.
//
// Global Variables: -
//
// Side Effects    : -
//
/----------------------------------------------------------------------*/



Type_p infer_return_sort(Sig_p sig, FunCode f_code)
{
   Type_p res;

   if(SigQueryProp(sig, f_code, FPIsInteger) &&
      (sig->distinct_props & FPIsInteger))
   {
      res = sig->type_bank->integer_type;
   }
   else if(SigQueryProp(sig, f_code, FPIsRational) &&
            (sig->distinct_props & FPIsRational))
   {
      res = sig->type_bank->rational_type;
   }
   else if(SigQueryProp(sig, f_code, FPIsFloat) &&
            (sig->distinct_props & FPIsFloat))
   {
      res = sig->type_bank->real_type;
   }
   else
   {
      res = SigDefaultSort(sig);
   }

   return res;
}


/*---------------------------------------------------------------------*/
/*                         Exported Functions                          */
/*---------------------------------------------------------------------*/


/*-----------------------------------------------------------------------
//
// Function: TypeCheckConsistent
//  recursively checks that the subterms of this term have a sort
//  that is consistent with the given signature.
//
//
// Global Variables: -
//
// Side Effects    : -
//
/----------------------------------------------------------------------*/

bool TypeCheckConsistent(Sig_p sig, Term_p term)
{
   bool res = true;
   Type_p type;

   PStack_p stack = PStackAlloc();
   PStackPushP(stack, term);

   while (!PStackEmpty(stack))
   {
      term = PStackPopP(stack);

      if (!TermIsVar(term))
      {
         /* check: same arity, same return sort, sort of arguments (pairwise)*/
         if(!SigIsPolymorphic(sig, term->f_code))
         {
            type = SigGetType(sig, term->f_code);

            assert(type);

            if(TypeIsArrow(type))
            {
               if((term->arity != type->arity-1)
                  || term->type != type->args[type->arity-1])
               {
                  res = false;
                  break;
               }
            }
            else
            {
               if(term->arity != 0 || term->type != type)
               {
                  // other kind of type constructor
                  res = false;
                  break;
               }
            }
            /* Check subterms recursively */
            for(int i=0; i < type->arity; i++)
            {
               PStackPushP(stack, term->args[i]);
               if(term->args[i]->type != type->args[i])
               {
                  res = false;
                  break;
               }
            }
         }
      }
   }
   PStackFree(stack);

   return res;
}

/*-----------------------------------------------------------------------
//
// Function: TypeInferSort()
//
//   Infer the sort of this term. It can either use the type of the
//   function symbol, if already known, or guess a type and add it
//   to the signature otherwise. By default terms are supposed not to
//   be atoms, unless the parser decides that they must be boolean.
//
// Global Variables: -
//
// Side Effects    : Modifies term and signature. May exit on type error.
//
/----------------------------------------------------------------------*/

#define TI_ERROR(msg) do{ \
   if(in)                                       \
   {\
      AktTokenError(in, msg, false);\
   }\
   else\
   {\
      Error(msg, SYNTAX_ERROR);\
   }}while(false)


void TypeInferSort(Sig_p sig, Term_p term, Scanner_p in)
{
   Type_p type = NULL;
   Type_p sort, *args;
   int i;


   if(TermIsVar(term))
   {
      if(!term->type)
      {
         term->type = SigDefaultSort(sig);
      }
   }
   else
   {
      if(TermIsPhonyApp(term))
      {
         type = term->args[0]->type;
      }
      else if(TermIsLambda(term))
      {
         // types have to be inferred only during parsing
         assert(term->f_code == SIG_NAMED_LAMBDA_CODE);
         assert(term->arity == 2);
         // type of lambda is 'type of variable' -> 'type of body'
         term->type = 
            TypeBankInsertTypeShared(sig->type_bank,
                                     ArrowTypeFlattened(&(term->args[0]->type), 1, 
                                     term->args[1]->type));
      }
      else if(term->f_code == sig->eqn_code || term->f_code == sig->neqn_code)
      {
         if(term->arity == 0)
         {
            AktTokenError(in, "Equality must have at least one argument", 
                          SYNTAX_ERROR);
         }
         Type_p arg_type = term->args[0]->type;
         Type_p eq_type_args[3] = {arg_type, arg_type, sig->type_bank->bool_type};
         type = TypeBankInsertTypeShared(sig->type_bank, 
                                         AllocArrowTypeCopyArgs(3, eq_type_args));
      }
      else if(term->f_code == sig->qex_code || term->f_code == sig->qall_code)
      {
         if(term->arity == 0)
         {
            AktTokenError(in, "Equality must have at least one argument", 
                          SYNTAX_ERROR);
         }
         assert(TermIsVar(term->args[0]));
         Type_p arg_type = term->args[0]->type;
         Type_p quant_type_args[3] = {arg_type, sig->type_bank->bool_type, sig->type_bank->bool_type};
         type = TypeBankInsertTypeShared(sig->type_bank, 
                                         AllocArrowTypeCopyArgs(3, quant_type_args));
      }
      else
      {
         type = SigGetType(sig, term->f_code);
      }

      /* Use type */
      if(type)
      {
         if(TypeIsArrow(type))
         {
            if(problemType == PROBLEM_FO && !app_encode
               && term->arity != type->arity-1)
            {
               fprintf(stderr, "Arity mismatch for ");
               TermPrint(stderr, term, sig, DEREF_NEVER);
               fprintf(stderr, " and type ");
               TypePrintTSTP(stderr, sig->type_bank, type);
               fprintf(stderr, "\n");
               TI_ERROR("Type error");
            }

            if(!TermIsPhonyApp(term))
            {
               for(i=0; SigIsFixedType(sig, term->f_code) && i < term->arity; i++)
               {
                  if(term->args[i]->type != type->args[i])
                  {
                     fprintf(stderr, "# Type mismatch in argument #%d of ", i+1);
                     TermPrint(stderr, term, sig, DEREF_NEVER);
                     fprintf(stderr, ": expected ");
                     TypePrintTSTP(stderr, sig->type_bank, type->args[i]);
                     fprintf(stderr, " but got ");
                     TypePrintTSTP(stderr, sig->type_bank, term->args[i]->type);
                     fprintf(stderr, "\n");
                     TI_ERROR("Type error");
                  }
               }
            }
            else
            {
               // probably can be unified with harder to understand code!
               for(i=1; i < term->arity; i++)
               {
                  assert(term->arity-1 < type->arity);

                  if(term->args[i]->type != type->args[i-1])
                  {
                     fprintf(stderr, "# Type mismatch in argument #%d of ", i+1);
                     TermPrint(stderr, term, sig, DEREF_NEVER);
                     fprintf(stderr, ": expected ");
                     TypePrintTSTP(stderr, sig->type_bank, type->args[i-1]);
                     fprintf(stderr, " but got ");
                     TypePrintTSTP(stderr, sig->type_bank, term->args[i]->type);
                     fprintf(stderr, "\n");
                     TI_ERROR("Type error");
                  }
               }
            }
            term->type = term_determine_type(term, type, sig->type_bank);
            if(term->type==NULL)
            {
               fprintf(stderr, "# too many arguments supplied for %s\n",
                       SigFindName(sig, term->f_code));
               assert(false);
               in?AktTokenError(in, "Type error", false):Error("Type error", SYNTAX_ERROR);
            }
         }
         else
         {
            if(term->arity != 0)
            {
               fprintf(stderr, "# Type mismatch for ");
               TermPrint(stderr, term, sig, DEREF_NEVER);
               fprintf(stderr, " and type ");
               TypePrintTSTP(stderr, sig->type_bank, type);
               fprintf(stderr, "\n");
               assert(false);
               TI_ERROR("Type error");
            }
            else
            {
               term->type = type;
            }
         }
      }
      else if(!TermIsLambda(term))
      {
         /* Infer type */
         sort = infer_return_sort(sig, term->f_code);
         args = term->arity ? TypeArgArrayAlloc(term->arity+1) : NULL;
         for(i=0; i < term->arity; i++)
         {
            args[i] = term->args[i]->type;
         }
         if(term->arity)
         {
            args[term->arity] = sort;
         }

         type = term->arity ?
            TypeBankInsertTypeShared(sig->type_bank,
                                     AllocArrowType(term->arity+1, args))
            : sort;

         /* Declare the inferred type */
         SigDeclareType(sig, term->f_code, type);
         term->type = sort;
      }
   }
}

/*-----------------------------------------------------------------------
//
// Function: TypeDeclareIsPredicate()
//
//   declare that the term has a role of predicate (occurs as a boolean atom)
//
// Global Variables: -
//
// Side Effects    : Modifies sig, modifies term's sort
//
/----------------------------------------------------------------------*/

void TypeDeclareIsPredicate(Sig_p sig, Term_p term)
{
   assert(!TermIsVar(term));

   SigDeclareIsPredicate(sig, term->f_code);
   term->type = sig->type_bank->bool_type;
}


/*-----------------------------------------------------------------------
//
// Function: TypeDeclareIsNotPredicate()
//
//   Declare that this term is not a boolean atom, because it ocurs in
//   an equation or is a subterm of another term.
//
// Global Variables: -
//
// Side Effects    : Modifies signature, update term's sort
//
/----------------------------------------------------------------------*/

void TypeDeclareIsNotPredicate(Sig_p sig, Term_p term, Scanner_p in)
{
   if(!TermIsVar(term))
   {
      TypeInferSort(sig, term, in);
      SigDeclareIsFunction(sig, term->f_code);
   }
}

/*---------------------------------------------------------------------*/
/*                        End of File                                  */
/*---------------------------------------------------------------------*/