1 /********************************************************************************
2 *
3 * Copyright (c) 2008 Fujitsu Services Ltd.
4 *
5 * Author: Nick Battle
6 *
7 * This file is part of VDMJ.
8 *
9 * VDMJ is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * VDMJ is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with VDMJ. If not, see <http://www.gnu.org/licenses/>.
21 *
22 ******************************************************************************/
23
24 package org.overturetool.vdmj.expressions;
25
26 import org.overturetool.vdmj.lex.LexLocation;
27 import org.overturetool.vdmj.pog.NonEmptySetObligation;
28 import org.overturetool.vdmj.pog.POContextStack;
29 import org.overturetool.vdmj.pog.ProofObligationList;
30 import org.overturetool.vdmj.runtime.Context;
31 import org.overturetool.vdmj.runtime.ValueException;
32 import org.overturetool.vdmj.typechecker.Environment;
33 import org.overturetool.vdmj.typechecker.NameScope;
34 import org.overturetool.vdmj.types.SetType;
35 import org.overturetool.vdmj.types.Type;
36 import org.overturetool.vdmj.types.TypeList;
37 import org.overturetool.vdmj.types.UnknownType;
38 import org.overturetool.vdmj.values.SetValue;
39 import org.overturetool.vdmj.values.Value;
40 import org.overturetool.vdmj.values.ValueSet;
41
42 public class DistIntersectExpression extends UnaryExpression
43 {
44 private static final long serialVersionUID = 1L;
45
46 public DistIntersectExpression(LexLocation location, Expression exp)
47 {
48 super(location, exp);
49 }
50
51 @Override
52 public String toString()
53 {
54 return "(dinter " + exp + ")";
55 }
56
57 @Override
58 public Type typeCheck(Environment env, TypeList qualifiers, NameScope scope)
59 {
60 Type arg = exp.typeCheck(env, null, scope);
61
62 if (arg.isSet())
63 {
64 SetType set = arg.getSet();
65
66 if (set.empty || set.setof.isSet())
67 {
68 return set.setof;
69 }
70 }
71
72 report(3076, "Argument of 'dinter' is not a set of sets");
73 return new UnknownType(location);
74 }
75
76 @Override
77 public Value eval(Context ctxt)
78 {
79 breakpoint.check(location, ctxt);
80
81 try
82 {
83 ValueSet setset = exp.eval(ctxt).setValue(ctxt);
84 ValueSet result = null;
85
86 for (Value v: setset)
87 {
88 if (result == null)
89 {
90 result = new ValueSet(v.setValue(ctxt));
91 }
92 else
93 {
94 result.retainAll(v.setValue(ctxt));
95 }
96 }
97
98 return new SetValue(result);
99 }
100 catch (ValueException e)
101 {
102 return abort(e);
103 }
104 }
105
106 @Override
107 public ProofObligationList getProofObligations(POContextStack ctxt)
108 {
109 ProofObligationList obligations = super.getProofObligations(ctxt);
110 obligations.add(new NonEmptySetObligation(exp, ctxt));
111 return obligations;
112 }
113
114 @Override
115 public String kind()
116 {
117 return "dinter";
118 }
119 }