Skip to content

Commit

Permalink
Fix the type of list/listReverse comprehensions with multiple iterators
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@22436 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Sep 25, 2014
1 parent 6c7ffee commit 487c23d
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 28 deletions.
49 changes: 25 additions & 24 deletions Compiler/FrontEnd/Ceval.mo
Expand Up @@ -5430,6 +5430,7 @@ algorithm
(cache, value, st) = ceval(cache, env, exp, impl, st,msg,numIter+1);
// print("cevalReductionEval: " +& ExpressionDump.printExpStr(exp) +& " => " +& ValuesUtil.valString(value) +& "\n");
(cache, result, st) = cevalReductionFold(cache, env, opPath, curValue, value, foldName, resultName, foldExp, exprType, impl, st,msg,numIter);
// print("cevalReductionEval => " +& Util.applyOptionOrDefault(result, ValuesUtil.valString, "") +& "\n");
then (cache, result, st);
end match;
end cevalReductionEvalAndFold;
Expand Down Expand Up @@ -5525,24 +5526,6 @@ algorithm
end match;
end valueCons;

protected function lookupReductionOp
"Looks up a reduction function based on it's name."
input DAE.Ident reductionName;
output ReductionOperator op;

partial function ReductionOperator
input Values.Value v1;
input Values.Value v2;
output Values.Value res;
end ReductionOperator;
algorithm
op := match(reductionName)
case "array" then valueArrayCons;
case "list" then valueCons;
case "listReverse" then valueCons;
end match;
end lookupReductionOp;

protected function cevalReductionIterators
input Env.Cache inCache;
input Env.Env inEnv;
Expand Down Expand Up @@ -5661,7 +5644,17 @@ algorithm
case (_,_,value,{_}) then value;
case (Absyn.IDENT("array"),Absyn.COMBINE(),Values.ARRAY(valueLst=vals),_)
equation
value = backpatchArrayReduction3(vals,listReverse(dims));
value = backpatchArrayReduction3(vals,listReverse(dims),ValuesUtil.makeArray);
// print(ValuesUtil.valString(value));print("\n");
then value;
case (Absyn.IDENT("list"),Absyn.COMBINE(),Values.LIST(vals),_)
equation
value = backpatchArrayReduction3(vals,listReverse(dims),ValuesUtil.makeList);
// print(ValuesUtil.valString(value));print("\n");
then value;
case (Absyn.IDENT("listReverse"),Absyn.COMBINE(),Values.LIST(vals),_)
equation
value = backpatchArrayReduction3(vals,listReverse(dims),ValuesUtil.makeList);
// print(ValuesUtil.valString(value));print("\n");
then value;
else inValue;
Expand All @@ -5671,25 +5664,33 @@ end backpatchArrayReduction;
protected function backpatchArrayReduction3
input list<Values.Value> inVals;
input list<Integer> inDims;
input Func makeSequence;
output Values.Value outValue;
partial function Func
input list<Values.Value> inVals;
output Values.Value outVal;
end Func;
algorithm
outValue := match (inVals,inDims)
outValue := match (inVals,inDims,makeSequence)
local
Integer dim;
list<list<Values.Value>> valMatrix;
Values.Value value;
list<Values.Value> vals;
list<Integer> dims;

case (vals,{_}) then ValuesUtil.makeArray(vals);
case (vals,dim::dims)
case (vals,{_},_)
equation
value = makeSequence(vals);
then value;
case (vals,dim::dims,_)
equation
// Split into the smallest of the arrays
// print("into sublists of length: " +& intString(dim) +& " from length=" +& intString(listLength(vals)) +& "\n");
valMatrix = List.partition(vals,dim);
// print("output has length=" +& intString(listLength(valMatrix)) +& "\n");
vals = List.map(valMatrix,ValuesUtil.makeArray);
value = backpatchArrayReduction3(vals,dims);
vals = List.map(valMatrix,makeSequence);
value = backpatchArrayReduction3(vals,dims,makeSequence);
then value;
end match;
end backpatchArrayReduction3;
Expand Down
4 changes: 2 additions & 2 deletions Compiler/FrontEnd/Static.mo
Expand Up @@ -1470,13 +1470,13 @@ algorithm
case (cache,_,Absyn.IDENT(name = "list"), exp, ty, _, _, _, _)
equation
(exp,ty) = Types.matchType(exp, ty, DAE.T_METABOXED_DEFAULT, true);
ty = DAE.T_METALIST(ty, DAE.emptyTypeSource);
ty = List.foldr(dims,Types.liftList,ty);
then (cache,exp,ty,ty,SOME(Values.LIST({})),fn);

case (cache,_,Absyn.IDENT(name = "listReverse"), exp, ty, _, _, _, _)
equation
(exp,ty) = Types.matchType(exp, ty, DAE.T_METABOXED_DEFAULT, true);
ty = DAE.T_METALIST(ty, DAE.emptyTypeSource);
ty = List.foldr(dims,Types.liftList,ty);
then (cache,exp,ty,ty,SOME(Values.LIST({})),fn);

case (cache,_,Absyn.IDENT("min"),exp, ty, DAE.T_REAL(varLst = _),_,_,_)
Expand Down
9 changes: 9 additions & 0 deletions Compiler/FrontEnd/Types.mo
Expand Up @@ -1882,6 +1882,15 @@ algorithm
outType := DAE.T_ARRAY(inType, {inDimension}, DAE.emptyTypeSource);
end liftArray;

public function liftList "This function turns a type into a list of that type.
If the type already is a list, another dimension is simply added."
input DAE.Type inType;
input DAE.Dimension inDimension;
output DAE.Type outType;
algorithm
outType := DAE.T_METALIST(inType, DAE.emptyTypeSource);
end liftList;

public function liftArrayListDims "
This function turns a type into an array of that type."
input DAE.Type inType;
Expand Down
10 changes: 8 additions & 2 deletions Compiler/FrontEnd/ValuesUtil.mo
Expand Up @@ -1915,14 +1915,20 @@ algorithm
v := Values.STRING(s);
end makeString;

public function makeTuple
"Construct an tuple of a list of Values."
public function makeTuple "Construct a tuple of a list of Values."
input list<Values.Value> inValueLst;
output Values.Value outValue;
algorithm
outValue := Values.TUPLE(inValueLst);
end makeTuple;

public function makeList "Construct a list from a list of Values."
input list<Values.Value> inValueLst;
output Values.Value outValue;
algorithm
outValue := Values.LIST(inValueLst);
end makeList;

public function makeArray "
Construct an array of a list of Values.
"
Expand Down

0 comments on commit 487c23d

Please sign in to comment.