summaryrefslogtreecommitdiff
path: root/Source/AIFramework/Expr.cs
blob: 584735927eaba9c50a7aa79b59a20a46fb5152f5 (plain)
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
//-----------------------------------------------------------------------------
//
// Copyright (C) Microsoft Corporation.  All Rights Reserved.
//
//-----------------------------------------------------------------------------
// This file specifies the expression language used by the Abstract
// Interpretation Framework.
//
//   expressions   e  ::=  x              variables
//                      |  f(e1,...,en)   uninterpreted functions
//                      |  \x:t.e         lambda expressions
//
//   types         t  ::= b                   user-defined/built-in base types
//                      | t1 * ... * tn -> t' function type

namespace Microsoft.AbstractInterpretationFramework
{
    using System.Collections;
  using System;
    using System.Diagnostics.Contracts;

    //----------------------------- Expressions -----------------------------

    /// <summary>
    ///  An interface for expressions.  This expression language is specified
    ///  by interfaces to allow the client to be able to use their existing
    ///  AST nodes as AIF expressions.
    /// 
    ///  This only serves as a place for operations on expressions.  Clients
    ///  should implement directly IVariable, IFunApp, ...
    /// </summary>
    [ContractClass(typeof(IExprContracts))]
    public interface IExpr
    {
        /// <summary>
        /// Execute a visit over the expression.
        /// </summary>
        /// <param name="visitor">The expression visitor.</param>
        /// <returns>The result of the visit.</returns>
        [Pure] object DoVisit(ExprVisitor/*!*/ visitor);

        // TODO: Type checking of the expressions.
    }
  [ContractClassFor(typeof(IExpr))]
  public abstract class IExprContracts:IExpr{
  #region IExpr Members

public object  DoVisit(ExprVisitor visitor)
{
  Contract.Requires(visitor != null);
 	throw new System.NotImplementedException();
}

#endregion
}

    /// <summary>
    ///  An interface for variables.
    /// 
    ///  This interface should be implemented by the client.
    /// </summary>
    [ContractClass(typeof(IVariableContracts))]
    public interface IVariable : IExpr
    {
      string/*!*/ Name { get; }    // Each client must define the name for variables
    }
  [ContractClassFor(typeof(IVariable))]
  public abstract class IVariableContracts:IVariable{
    string IVariable.Name{get{Contract.Ensures(Contract.Result<string>() != null);throw new NotImplementedException();}

    }

    #region IExpr Members

    object IExpr.DoVisit(ExprVisitor visitor) {
      throw new NotImplementedException();
    }

    #endregion
  }

    /// <summary>
    ///  An interface for function applications.
    /// 
    ///  This interface should be implemented by the client.
    /// </summary>
    /// 
  [ContractClass(typeof(IFunAppContracts))]
    public interface IFunApp : IExpr
    {
        IFunctionSymbol/*!*/ FunctionSymbol { get; }
        IList/*<IExpr!>*//*!*/ Arguments
        {
            [Pure][Rep] get;
            
        }

        /// <summary>
        ///  Provides a method to create a new uninterpreted function
        ///  with the same function symbol but with the arguments with
        ///  args.
        /// </summary>
        /// <param name="args">The new arguments.</param>
        /// <returns>A copy of the function with the new arguments.</returns>
        IFunApp/*!*/ CloneWithArguments(IList/*<IExpr!>*//*!*/ args)
        //TODO  Contract.Requires(this.Arguments.Count == args.Count);
        ;
    }
  [ContractClassFor(typeof(IFunApp))]
public abstract class IFunAppContracts:IFunApp{

#region IFunApp Members

public IFunctionSymbol  FunctionSymbol
{
	get {Contract.Ensures(Contract.Result<IFunctionSymbol>() != null);
 throw new System.NotImplementedException(); }
}

public IList  Arguments
{
	get {Contract.Ensures(Contract.Result<IList>() != null);
    Contract.Ensures(Contract.Result<IList>().IsReadOnly);
 throw new System.NotImplementedException(); }
}

public IFunApp  CloneWithArguments(IList args)
{
  Contract.Requires(args != null);
  Contract.Ensures(Contract.Result<IFunApp>() != null);


 	throw new System.NotImplementedException();
}

#endregion

#region IExpr Members

object IExpr.DoVisit(ExprVisitor visitor) {
  throw new NotImplementedException();
}

#endregion
}

    /// <summary>
    ///  An interface for anonymous functions (i.e., lambda expressions)
    /// </summary>
    [ContractClass(typeof(IFunctionContracts))]
    public interface IFunction : IExpr
    {
        IVariable/*!*/ Param { get; }
        AIType/*!*/ ParamType { get; }
        IExpr/*!*/ Body { get; }

        IFunction/*!*/ CloneWithBody(IExpr/*!*/ body);
    }
  [ContractClassFor(typeof(IFunction))]
  public abstract class IFunctionContracts:IFunction{

    #region IFunction Members

    IVariable IFunction.Param {
      get {
        Contract.Ensures(Contract.Result<IVariable>() != null);
        throw new NotImplementedException();
      }
    }

    AIType IFunction.ParamType {
      get {
        Contract.Ensures(Contract.Result<AIType>() != null);
        throw new NotImplementedException();
      }
    }

    IExpr IFunction.Body {
      get {
        Contract.Ensures(Contract.Result<IExpr>() != null);
        throw new NotImplementedException();
      }
    }

    IFunction IFunction.CloneWithBody(IExpr body) {
      Contract.Requires(body != null);
      Contract.Ensures(Contract.Result<IFunction>() != null);
      throw new NotImplementedException();
    }

    #endregion

    #region IExpr Members

    object IExpr.DoVisit(ExprVisitor visitor) {
      throw new NotImplementedException();
    }

    #endregion
  }

    /// <summary>
    /// An interface representing an expression that at any moment could, in principle, evaluate
    /// to a different value.  That is, the abstract interpreter should treat these IExpr's
    /// as unknown values.  They are used when there is no other IExpr corresponding to the
    /// expression to be modeled.
    /// </summary>
    public interface IUnknown : IExpr {}
    
    /// <summary>
    /// An abstract class that provides an interface for expression visitors.
    /// </summary>
    [ContractClass(typeof(ExprVisitorContracts))]
    public abstract class ExprVisitor
    {
        public abstract object Default(IExpr/*!*/ expr);

        public virtual object VisitVariable(IVariable/*!*/ var){
Contract.Requires(var != null);
            return Default(var);
        }

        public virtual object VisitFunApp(IFunApp/*!*/ funapp){
Contract.Requires(funapp != null);
            return Default(funapp);
        }

        public virtual object VisitFunction(IFunction/*!*/ fun){
Contract.Requires(fun != null);
            return Default(fun);
        }
    }
  [ContractClassFor(typeof(ExprVisitor))]
  public abstract class ExprVisitorContracts:ExprVisitor{
    public override object  Default(IExpr expr)
{
 	Contract.Requires(expr != null); throw new NotImplementedException();
}}

    /// <summary>
    ///  A utility class for dealing with expressions.
    /// </summary>
    public sealed class ExprUtil
    {
        /// <summary>
        ///  Yield an expression that is 'inexpr' with 'var' replaced by 'subst'.
        /// </summary>
        /// <param name="subst">The expression to substitute.</param>
        /// <param name="var">The variable to substitute for.</param>
        /// <param name="inexpr">The expression to substitute into.</param>
        public static IExpr/*!*/ Substitute(IExpr/*!*/ subst, IVariable/*!*/ var, IExpr/*!*/ inexpr){
Contract.Requires(inexpr != null);
Contract.Requires(var != null);
Contract.Requires(subst != null);
Contract.Ensures(Contract.Result<IExpr>() != null);
            IExpr result = null;

            if (inexpr is IVariable)
            {
                result = inexpr.Equals(var) ? subst : inexpr;
            }
            else if (inexpr is IFunApp)
            {
                IFunApp/*!*/ funapp = (IFunApp/*!*/)cce.NonNull(inexpr);
                IList newargs = null;
              
              var x = new System.Collections.Generic.List<IExpr>();
              foreach (IExpr arg in funapp.Arguments){
                x.Add(Substitute(subst,var, arg));
              }
              newargs = new ArrayList(x);
                //newargs = new ArrayList{ IExpr/*!*/ arg in funapp.Arguments; Substitute(subst, var, arg) };
                result = funapp.CloneWithArguments(newargs);
            }
            else if (inexpr is IFunction)
            {
                IFunction/*!*/ fun = (IFunction/*!*/)cce.NonNull(inexpr);

                if (fun.Param.Equals(var))
                    result = fun;
                else
                    result = fun.CloneWithBody(Substitute(subst, var, fun.Body));
            }
            else if (inexpr is IUnknown)
            {
                result = inexpr;
            }
            else
            {
                {Contract.Assert(false);throw new cce.UnreachableException();}
            }

            return result;
        }
        
        
        //
        // Poor man's pattern matching.
        //
        // The methods below implement pattern matching for AI expressions.
        //
        // Example Usage:
        //   Match(e, Prop.Imp,
        //            (Matcher)delegate (IExpr e) { return Match(e, Prop.And, out x, out y); }
        //            out z)
        //   which sees if 'e' matches Prop.Imp(Prop.And(x,y),z) binding x,y,z to the subtrees.
        //
        public delegate bool Matcher(IExpr/*!*/ expr);
        
        private static IFunApp/*?*/ MatchFunctionSymbol(IExpr/*!*/ expr, IFunctionSymbol/*!*/ f){
Contract.Requires(f != null);
Contract.Requires(expr != null);
            IFunApp app = expr as IFunApp;
            if (app != null)
            {
                if (app.FunctionSymbol.Equals(f))
                    return app;
                else
                    return null;
            }
            else
                return null;
        }
        
        public static bool Match(IExpr/*!*/ expr, IFunctionSymbol/*!*/ f, params Matcher[]/*!*/ subs){
Contract.Requires(subs != null);
Contract.Requires(f != null);
Contract.Requires(expr != null);
            IFunApp app = MatchFunctionSymbol(expr,f);
            if (app != null)
            {
                int i = 0; // Note ***0***
                foreach(Matcher/*!*/ s in subs){
Contract.Assert(s != null);
                    if (!s(cce.NonNull((IExpr)app.Arguments[i]))) { return false; }
                    i++;
                }
                return true;
            }
            else { return false; }
        }
        
        // Unary Binding
        public static bool Match(IExpr/*!*/ expr, IFunctionSymbol/*!*/ f, out IExpr arg0, params Matcher[]/*!*/ subs){
Contract.Requires(subs != null);
Contract.Requires(f != null);
Contract.Requires(expr != null);
            arg0 = null;
        
            IFunApp app = MatchFunctionSymbol(expr,f);
            if (app != null)
            {
                arg0 = (IExpr/*!*/)cce.NonNull(app.Arguments[0]);
                
                int i = 1; // Note ***1***
                foreach(Matcher/*!*/ s in subs){
Contract.Assert(s != null);
                    if (!s(cce.NonNull((IExpr/*!*/)app.Arguments[i]))) { return false; }
                    i++;
                }
                return true;
            }
            else { return false; }
        }
        
        // Binary Binding       
        public static bool Match(IExpr/*!*/ expr, IFunctionSymbol/*!*/ f, Matcher/*!*/ sub0, out IExpr arg1, params Matcher[]/*!*/ subs){
Contract.Requires(subs != null);
Contract.Requires(sub0 != null);
Contract.Requires(f != null);
Contract.Requires(expr != null);
            arg1 = null;
        
            IFunApp app = MatchFunctionSymbol(expr,f);
            if (app != null)
            {
                if (!sub0(cce.NonNull((IExpr/*!*/)app.Arguments[0]))) { return false; }
            
                arg1 = (IExpr/*!*/)cce.NonNull(app.Arguments[1]);
                
                int i = 2; // Note ***2***
                foreach(Matcher/*!*/ s in subs){
Contract.Assert(s != null);
                    if (!s(cce.NonNull((IExpr)app.Arguments[i]))) { return false; }
                    i++;
                }
                return true;
            }
            else { return false; }
        }
        
        public static bool Match(IExpr/*!*/ expr, IFunctionSymbol/*!*/ f, out IExpr arg0, out IExpr arg1, params Matcher[]/*!*/ subs){
Contract.Requires(subs != null);
Contract.Requires(f != null);
Contract.Requires(expr != null);
            arg0 = null;
            arg1 = null;
        
            IFunApp app = MatchFunctionSymbol(expr,f);
            if (app != null)
            {
                arg0 = (IExpr/*!*/)cce.NonNull(app.Arguments[0]);
                arg1 = (IExpr/*!*/)cce.NonNull(app.Arguments[1]);
                
                int i = 2; // Note ***2***
                foreach(Matcher/*!*/ s in subs){
Contract.Assert(s != null);
                    if (!s(cce.NonNull((IExpr/*!*/)app.Arguments[i]))) { return false; }
                    i++;
                }
                return true;
            }
            else { return false; }
        }

        // Ternary Binding
        public static bool Match(IExpr/*!*/ expr, IFunctionSymbol/*!*/ f, out IExpr arg0, out IExpr arg1, out IExpr arg2, params Matcher[]/*!*/ subs){
Contract.Requires(subs != null);
Contract.Requires(f != null);
Contract.Requires(expr != null);
            arg0 = null;
            arg1 = null;
            arg2 = null;
        
            IFunApp app = MatchFunctionSymbol(expr,f);
            if (app != null)
            {
                arg0 = (IExpr/*!*/)cce.NonNull(app.Arguments[0]);
                arg1 = (IExpr/*!*/)cce.NonNull(app.Arguments[1]);
                arg2 = (IExpr/*!*/)cce.NonNull(app.Arguments[2]);
                
                int i = 3; // Note ***3***
                foreach(Matcher/*!*/ s in subs){
Contract.Assert(s != null);
                    if (!s(cce.NonNull((IExpr/*!*/)app.Arguments[i]))) { return false; }
                    i++;
                }
                return true;
            }
            else { return false; }
        }

        /// <summary>
        ///  Not intended to be instantiated.
        /// </summary>
        private ExprUtil() { }
    }

    //------------------------------ Symbols --------------------------------

    /// <summary>
    ///  An interface for function symbols.  Constants are represented by
    ///  0-ary function symbols.
    /// 
    ///  This interface should be implemented by abstract domains, but client
    ///  expressions need keep track of function symbols.
    /// </summary>
    [ContractClass(typeof(IFunctionSymbolContracts))]
    public interface IFunctionSymbol
    {
        AIType/*!*/ AIType { [Rep][ResultNotNewlyAllocated]
                         get; }
    }
  [ContractClassFor(typeof(IFunctionSymbol))]
  public abstract class IFunctionSymbolContracts:IFunctionSymbol{
    #region IFunctionSymbol Members

    AIType IFunctionSymbol.AIType {
      get {
        Contract.Ensures(Contract.Result<AIType>() != null);
        throw new NotImplementedException();
      }
    }

    #endregion
  }

    /// <summary>
    ///  The type of the arguments to ExprUtil.Match, a poor man's pattern
    ///  matching.
    /// </summary>
    public interface IMatchable
    {
    }

    //-------------------------------- Types --------------------------------

    /// <summary>
    ///  Types.
    /// </summary>
    public interface AIType
    {
    }

    /// <summary>
    ///  Function type constructor.
    /// </summary>
    public sealed class FunctionType : AIType
    {
        /*[Own]*/ private readonly IList/*<Type!>*//*!*/ argTypes;
        /*[Own]*/ private readonly AIType/*!*/ retType;
      [ContractInvariantMethod]
void ObjectInvariant() 
{
    Contract.Invariant(argTypes != null);
        Contract.Invariant(retType != null);
}


        public FunctionType(params AIType[]/*!*/ types){
Contract.Requires(types != null);
 Contract.Requires(types.Length >= 2);
            AIType type = types[types.Length-1];
            Contract.Assume(type != null);
            this.retType = type;
            ArrayList argTypes = new ArrayList();
            for (int i = 0; i < types.Length-1; i++)
            {
                 type = types[i];
                 Contract.Assume(type != null);
                 argTypes.Add(types);
            }
            this.argTypes = ArrayList.ReadOnly(argTypes);
        }

        public IList/*<AIType!>*//*!*/ Arguments
        {
            [Pure][Rep]
            get 
              {
              Contract.Ensures(Contract.Result<IList>() != null);
 Contract.Ensures(Contract.Result<IList>().IsReadOnly);
                return argTypes;
            }
        }

        public int Arity
        {
            get { return argTypes.Count; }
        }

        public AIType/*!*/ ReturnType
        {
            get {Contract.Ensures(Contract.Result<AIType>() != null); return retType; }
        }

        /* TODO Do we have the invariant that two functions are equal iff they're the same object.
        public override bool Equals(object o)
        {
            if (o != null && o is FunctionType)
            {
                FunctionType other = (FunctionType) o;
                
                if (Arity == other.Arity
                    && ReturnType.Equals(other.ReturnType))
                {
                    for (int i = 0; i < Arity; i++)
                    {
                        if (!argTypes[i].Equals(other.argTypes[i]))
                            return false;
                    }
                    return true;
                } 
                else
                    return false;
            }
            else
                return false;
        }
        */
    }

    //------------------------------ Queries -------------------------------

    public enum Answer { Yes, No, Maybe };
    
    /// <summary>
    ///  An interface that specifies a queryable object that can answer
    ///  whether a predicate holds.
    /// </summary>
    /// 
  [ContractClass(typeof(IQueryableContracts))]
    public interface IQueryable
    {
        /// <summary>
        ///  Answers the query whether the given predicate holds.
        /// </summary>
        /// <param name="pred">The given predicate.</param>
        /// <returns>Yes, No, or Maybe.</returns>
        Answer CheckPredicate(IExpr/*!*/ pred);

        /// <summary>
        ///  A simplified interface for disequalities.  One can always
        ///  implement this by calling CheckPredicate, but it may be
        ///  more efficient with this method.
        /// </summary>
        Answer CheckVariableDisequality(IVariable/*!*/ var1, IVariable/*!*/ var2);
    }
  [ContractClassFor(typeof(IQueryable))]
    public abstract class IQueryableContracts : IQueryable {
      #region IQueryable Members

      public Answer CheckPredicate(IExpr pred) {
        Contract.Requires(pred != null);
        throw new NotImplementedException();
      }

      public Answer CheckVariableDisequality(IVariable var1, IVariable var2) {
        Contract.Requires(var1 != null);
        Contract.Requires(var2 != null);
        throw new NotImplementedException();
      }

      #endregion
    }
    
    public static class QueryUtil
    {
        public static Answer Negate(Answer ans)
        {
            switch (ans)
            {
              case Answer.Yes:
                return Answer.No;
              case Answer.No:
                return Answer.Yes;
              default:
                return Answer.Maybe;
            }
        }
    }

    //----------------------------- Exceptions -----------------------------

    public class CheckedException : System.Exception {
    }
    public class TypeError : CheckedException
    {
    }
}