Method: FuncComposeObligation(ACompBinaryExp, ILexNameToken, ILexNameToken, IPOContextStack, IPogAssistantFactory)

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.overture.pog.obligation;
25:
26: import java.util.List;
27: import java.util.Vector;
28:
29: import org.overture.ast.analysis.AnalysisException;
30: import org.overture.ast.expressions.ACompBinaryExp;
31: import org.overture.ast.expressions.AForAllExp;
32: import org.overture.ast.expressions.APreExp;
33: import org.overture.ast.expressions.PExp;
34: import org.overture.ast.factory.AstExpressionFactory;
35: import org.overture.ast.intf.lex.ILexNameToken;
36: import org.overture.ast.types.PType;
37: import org.overture.pog.pub.IPOContextStack;
38: import org.overture.pog.pub.IPogAssistantFactory;
39: import org.overture.pog.pub.POType;
40:
41: public class FuncComposeObligation extends ProofObligation
42: {
43:         private static final long serialVersionUID = 8813166638915813635L;
44:
45:         // add last parameter to pass assistantFactory to the method.
46:         public FuncComposeObligation(ACompBinaryExp exp, ILexNameToken pref1,
47:                         ILexNameToken pref2, IPOContextStack ctxt,
48:                         IPogAssistantFactory assistantFactory) throws AnalysisException
49:         {
50:                 super(exp, POType.FUNC_COMPOSE, ctxt, exp.getLocation(), assistantFactory);
51:
52:                 // Function composition must be of two functions with a single parameter each.
53:                 // The obligation depends on whether the left/right expressions of the "comp"
54:                 // are not directly functions (pref is null) or whether they are functions
55:                 // that do or do not have preconditions (pref is "" or a name like "pre_FN").
56:                 //
57:                 // Given f1(a:A), f2(b:B), the obligation for "f1 comp f2" is then:
58:                 //
59:                 // forall arg:A & pre_f2(arg) => pre_f1(f2(arg))
60:                 //
61:                 // And for exp1 comp exp2 (where the arguments evaluate to functions):
62:                 //
63:                 // forall arg:A & pre_(exp2, arg) => pre_(exp1, exp2(arg))
64:                 //
65:                 // Similarly for mixtures of functions and expressions. If f2 is a function without
66:                 // a precondition, there is not LHS to the =>, only the RHS.
67:
68:                 ILexNameToken arg = getUnique("arg");
69:
70:                 AForAllExp forallExp = new AForAllExp();
71:                 PType leftPType = assistantFactory.createPTypeAssistant().getFunction(exp.getLeft().getType()).getParameters().get(0).clone();
72:                 forallExp.setBindList(getMultipleTypeBindList(leftPType, arg));
73:                 PExp firstPart = null;
74:
75:•                if (pref2 == null || pref2.getFullName() != "") // An expression or a function with a precondition
76:                 {
77:•                        if (pref2 != null)
78:                         {
79:                                 // pref2(arg) =>
80:                                 firstPart = getApplyExp(getVarExp(pref2), getVarExp(arg));
81:                         } else
82:                         {
83:                                 // pre_(exp.getRight(), arg) =>
84:                                 APreExp preExp = new APreExp();
85:                                 preExp.setFunction(exp.getRight().clone());
86:                                 List<PExp> args = new Vector<PExp>();
87:                                 args.add(getVarExp(arg));
88:                                 preExp.setArgs(args);
89:
90:                                 firstPart = preExp;
91:                         }
92:                 }
93:
94:                 PExp secondPart = null;
95:
96:•                if (pref1 != null)
97:                 {
98:                         // pref1(exp.getRight()(arg))
99:                         secondPart = getApplyExp(getVarExp(pref1), getApplyExp(exp.getRight(), getVarExp(arg)));
100:                 } else
101:                 {
102:                         // pre_(exp.getLeft(), exp.getRight()(arg))
103:                         APreExp preExp = new APreExp();
104:                         preExp.setFunction(exp.getLeft().clone());
105:                         List<PExp> args = new Vector<PExp>();
106:                         args.add(getApplyExp(exp.getRight().clone(), getVarExp(arg)));
107:                         preExp.setArgs(args);
108:                         secondPart = preExp;
109:                 }
110:
111:•                if (firstPart == null)
112:                 {
113:                         forallExp.setPredicate(secondPart);
114:                 } else
115:                 {
116:                         forallExp.setPredicate(AstExpressionFactory.newAImpliesBooleanBinaryExp(firstPart, secondPart));
117:                 }
118:
119:                 stitch = forallExp.clone();
120:                 valuetree.setPredicate(ctxt.getPredWithContext(forallExp));
121:         }
122: }