diff --git a/Compiler/BackEnd/BackendEquation.mo b/Compiler/BackEnd/BackendEquation.mo index 77dec0d99cb..932b89fba73 100644 --- a/Compiler/BackEnd/BackendEquation.mo +++ b/Compiler/BackEnd/BackendEquation.mo @@ -76,19 +76,17 @@ algorithm rlen := intReal(len) * 1.4; arrsize := realInt(rlen); optarr := arrayCreate(arrsize, NONE()); - (size, optarr) := listEquation1(inEquationList, 1, 0, optarr); + (size, optarr) := listEquation1(inEquationList, optarr); outEquationArray := BackendDAE.EQUATION_ARRAY(size, len, arrsize, optarr); end listEquation; protected function listEquation1 input list inEquationList; - input Integer inPos "initially call this with 1"; - input Integer inSize "initially call this with 0"; input array> inEquOptArr; - output Integer outSize = inSize; + output Integer outSize = 0; output array> outEquOptArr = inEquOptArr; protected - Integer pos = inPos; + Integer pos = 1; algorithm for eq in inEquationList loop outSize := outSize + equationSize(eq); @@ -1482,13 +1480,12 @@ end removeRemovedEqs; public function setAtIndex "author: lochel Sets the n-th array element of an EquationArray. Please note: one-based indexing" - input BackendDAE.EquationArray inEquationArray; + input output BackendDAE.EquationArray inEquationArray; input Integer inPos "one-based indexing"; input BackendDAE.Equation inEquation; - output BackendDAE.EquationArray outEquationArray = inEquationArray; algorithm - outEquationArray.size := inEquationArray.size -equationOptSize(arrayGet(inEquationArray.equOptArr, inPos)) +equationSize(inEquation); - outEquationArray.equOptArr := arrayUpdate(inEquationArray.equOptArr, inPos, SOME(inEquation)); + inEquationArray.size := inEquationArray.size -equationOptSize(arrayGet(inEquationArray.equOptArr, inPos)) +equationSize(inEquation); + inEquationArray.equOptArr := arrayUpdate(inEquationArray.equOptArr, inPos, SOME(inEquation)); end setAtIndex; public function setAtIndexFirst "author: waurich @@ -1588,7 +1585,7 @@ algorithm case _ equation equOptArr = List.fold1r(inIndices, arrayUpdate, NONE(), inEquationArray.equOptArr); - eqnlst = equationDelete1(inEquationArray.arrSize, equOptArr, {}); + eqnlst = equationDelete1(inEquationArray.arrSize, equOptArr); then listEquation(eqnlst); else equation @@ -1602,23 +1599,16 @@ protected function equationDelete1 "author: Frenkel TUD 2012-09 helper for equationDelete." input Integer index; input array> equOptArr; - input list iAcc; - output list oAcc; + output list oAcc = {}; +protected + BackendDAE.Equation eqn; algorithm - oAcc := matchcontinue (index, equOptArr, iAcc) - local - BackendDAE.Equation eqn; - - case (0, _, _) - then iAcc; - - case (_, _, _) equation - SOME(eqn) = equOptArr[index]; - then equationDelete1(index-1, equOptArr, eqn::iAcc); - - case (_, _, _) - then equationDelete1(index-1, equOptArr, iAcc); - end matchcontinue; + for i in index:-1:1 loop + if isSome(equOptArr[i]) then + SOME(eqn) := equOptArr[i]; + oAcc := eqn::oAcc; + end if; + end for; end equationDelete1; public function equationRemove "author: Frenkel TUD 2012-09 @@ -1659,7 +1649,7 @@ algorithm case BackendDAE.EQUATION_ARRAY(numberOfElement=numberOfElement, equOptArr=equOptArr) equation outEquationArray = emptyEqnsSized(numberOfElement); - then compressEquations1(1, numberOfElement, equOptArr, outEquationArray); + then compressEquations1(numberOfElement, equOptArr, outEquationArray); else equation print("BackendEquation.compressEquations failed\n"); @@ -1668,39 +1658,18 @@ algorithm end compressEquations; protected function compressEquations1 "author: Frenkel TUD 2012-09" - input Integer index; input Integer nEqns; input array> equOptArr; - input BackendDAE.EquationArray iEqns; - output BackendDAE.EquationArray oEqns; + input output BackendDAE.EquationArray eqns; +protected + BackendDAE.Equation eqn; algorithm - oEqns := matchcontinue (index, nEqns, equOptArr, iEqns) - local - BackendDAE.Equation eqn; - BackendDAE.EquationArray eqns; - - // found element - case (_, _, _, _) equation - true = intLe(index, nEqns); - SOME(eqn) = equOptArr[index]; - eqns = addEquation(eqn, iEqns); - then compressEquations1(index+1, nEqns, equOptArr, eqns); - - // found non element - case (_, _, _, _) equation - true = intLe(index, nEqns); - NONE() = equOptArr[index]; - then compressEquations1(index+1, nEqns, equOptArr, iEqns); - - // at the end - case (_, _, _, _) equation - false = intLe(index, nEqns); - then iEqns; - - else equation - print("BackendEquation.compressEquations1 failed for index " + intString(index) + " and Number of Equations " + intString(nEqns) + "\n"); - then fail(); - end matchcontinue; + for index in 1:nEqns loop + _ := match equOptArr[index] + case SOME(eqn) equation eqns = addEquation(eqn, eqns); then (); + else (); + end match; + end for; end compressEquations1; public function equationToScalarResidualForm "author: Frenkel TUD 2012-06 @@ -2026,36 +1995,20 @@ public function equationOptSize algorithm size := match (oeqn) local BackendDAE.Equation eqn; - case (NONE()) then 0; case (SOME(eqn)) then equationSize(eqn); + else 0; end match; end equationOptSize; public function equationLstSize input list inEqns; - output Integer size; + output Integer size = 0; algorithm - size := equationLstSize_impl(inEqns, 0); + for eqn in inEqns loop + size := size + equationSize(eqn); + end for; end equationLstSize; -protected function equationLstSize_impl - input list inEqns; - input Integer isize; - output Integer size; -algorithm - size := match (inEqns, isize) - local - BackendDAE.Equation eqn; - list rest; - - case ({}, _) - then isize; - - case (eqn::rest, _) - then equationLstSize_impl(rest, isize+equationSize(eqn)); - end match; -end equationLstSize_impl; - public function generateEquation "author Frenkel TUD 2012-12 helper to generate an equation from lhs and rhs. the type of this function is determined by the lhs. @@ -2069,7 +2022,7 @@ protected DAE.Type ty; algorithm ty := Expression.typeof(lhs); - outEqn := matchcontinue () + outEqn := match () local Integer size; DAE.Dimensions dims; @@ -2077,23 +2030,24 @@ algorithm Boolean b1, b2; // complex types to complex equations - case () equation - true = DAEUtil.expTypeComplex(ty) or DAEUtil.expTypeTuple(ty); + case () guard + DAEUtil.expTypeComplex(ty) or DAEUtil.expTypeTuple(ty) + equation size = Expression.sizeOf(ty); then BackendDAE.COMPLEX_EQUATION(size, lhs, rhs, source, inEqAttr); // array types to array equations - case () equation - true = DAEUtil.expTypeArray(ty); + case () guard + DAEUtil.expTypeArray(ty) + equation dims = Expression.arrayDimension(ty); ds = Expression.dimensionsSizes(dims); then BackendDAE.ARRAY_EQUATION(ds, lhs, rhs, source, inEqAttr); // other types - case () equation - b1 = DAEUtil.expTypeComplex(ty); - b2 = DAEUtil.expTypeArray(ty); - false = b1 or b2; + case () guard + not DAEUtil.expTypeComplex(ty) and + not DAEUtil.expTypeArray(ty) then BackendDAE.EQUATION(lhs, rhs, source, inEqAttr); else equation @@ -2101,7 +2055,7 @@ algorithm true = Flags.isSet(Flags.FAILTRACE); Debug.traceln("- BackendEquation.generateEquation failed on: " + ExpressionDump.printExpStr(lhs) + " = " + ExpressionDump.printExpStr(rhs) + "\n"); then fail(); - end matchcontinue; + end match; end generateEquation; public function generateEQUATION "author: Frenkel TUD 2010-05" @@ -2747,30 +2701,30 @@ protected function aliasEquation2 "author Frenkel TUD 2011-04 input list> inTpls "(cr1, cr2, cr1=e2, cr2=e1, true if negated alias)"; output list> outTpls "(cr1, cr2, cr1=e2, cr2=e1, true if negated alias)"; algorithm - outTpls := matchcontinue (lhs, rhs, inTpls) + outTpls := match (lhs, rhs, inTpls) local list elst1, elst2; // {a1+b1, a2+b2, a3+b3, ..} = 0; - case (DAE.ARRAY(array = elst1), _, _) equation - true = Expression.isZero(rhs); + case (DAE.ARRAY(array = elst1), _, _) guard + Expression.isZero(rhs) then List.fold(elst1, aliasExpression, inTpls); // 0 = {a1+b1, a2+b2, a3+b3, ..}; - case (_, DAE.ARRAY(array = elst2), _) equation - true = Expression.isZero(lhs); + case (_, DAE.ARRAY(array = elst2), _) guard + Expression.isZero(lhs) then List.fold(elst2, aliasExpression, inTpls); // lhs = 0 - case (_, _, _) equation - true = Expression.isZero(rhs); + case (_, _, _) guard + Expression.isZero(rhs) then aliasExpression(lhs, inTpls); // 0 = rhs - case (_, _, _) equation - true = Expression.isZero(lhs); + case (_, _, _) guard + Expression.isZero(lhs) then aliasExpression(rhs, inTpls); - end matchcontinue; + end match; end aliasEquation2; protected function aliasRecord "author Frenkel TUD 2011-04 @@ -3132,10 +3086,9 @@ algorithm end markDifferentiated; protected function markDifferentiated2 - input BackendDAE.EquationAttributes inAttr; - output BackendDAE.EquationAttributes outAttr = inAttr; + input output BackendDAE.EquationAttributes attr; algorithm - outAttr.differentiated := true; + attr.differentiated := true; end markDifferentiated2; public function isDifferentiated diff --git a/Compiler/BackEnd/ResolveLoops.mo b/Compiler/BackEnd/ResolveLoops.mo index 5897aa9e4b3..8c9553950fc 100644 --- a/Compiler/BackEnd/ResolveLoops.mo +++ b/Compiler/BackEnd/ResolveLoops.mo @@ -1692,11 +1692,9 @@ protected function connectPaths "author:Waurich TUD 2014-02 output list> loopsOut; protected list path; - Integer last; algorithm - path := listDelete(pathIn, 1); - last := listLength(path); - path := listDelete(path, last); + _::path := pathIn; + path := List.stripLast(path); loopsOut := List.map1(closingPaths,listAppend,path); end connectPaths; diff --git a/Compiler/BackEnd/Tearing.mo b/Compiler/BackEnd/Tearing.mo index 440e74a7530..575401e67ab 100644 --- a/Compiler/BackEnd/Tearing.mo +++ b/Compiler/BackEnd/Tearing.mo @@ -462,60 +462,20 @@ end getUnsolvableVars; public function unsolvable " author: Frenkel TUD 2012-08" input BackendDAE.AdjacencyMatrixElementEnhanced elem; - output Boolean b; + output Boolean b = true; +protected + Integer e; + BackendDAE.Solvability s; algorithm - b := match(elem) - local - Integer e; - BackendDAE.AdjacencyMatrixElementEnhanced rest; - Boolean b1; - case ({}) then true; - case ((e,BackendDAE.SOLVABILITY_SOLVED(),_)::rest) - equation - b1 = intLe(e,0); - b1 = if b1 then unsolvable(rest) else false; - then - b1; - case ((e,BackendDAE.SOLVABILITY_CONSTONE(),_)::rest) - equation - b1 = intLe(e,0); - b1 = if b1 then unsolvable(rest) else false; - then - b1; - case ((e,BackendDAE.SOLVABILITY_CONST(),_)::rest) - equation - b1 = intLe(e,0); - b1 = if b1 then unsolvable(rest) else false; - then - b1; - case ((_,BackendDAE.SOLVABILITY_PARAMETER(b=false),_)::rest) - then - unsolvable(rest); - case ((e,BackendDAE.SOLVABILITY_PARAMETER(b=true),_)::rest) - equation - b1 = intLe(e,0); - b1 = if b1 then unsolvable(rest) else false; - then - b1; - case ((_,BackendDAE.SOLVABILITY_LINEAR(b=false),_)::rest) - then - unsolvable(rest); - case ((_,BackendDAE.SOLVABILITY_LINEAR(b=true),_)::rest) - then - unsolvable(rest); - case ((_,BackendDAE.SOLVABILITY_NONLINEAR(),_)::rest) - then - unsolvable(rest); - case ((_,BackendDAE.SOLVABILITY_UNSOLVABLE(),_)::rest) - then - unsolvable(rest); - case ((e,BackendDAE.SOLVABILITY_SOLVABLE(),_)::rest) - equation - b1 = intLe(e,0); - b1 = if b1 then unsolvable(rest) else false; - then - b1; - end match; + for el in elem loop + (e,s,_) := el; + if solvable(s) then + if e > 0 then + b := false; + return; + end if; + end if; + end for; end unsolvable; @@ -1424,6 +1384,7 @@ algorithm case BackendDAE.SOLVABILITY_NONLINEAR() then false; case BackendDAE.SOLVABILITY_UNSOLVABLE() then false; case BackendDAE.SOLVABILITY_SOLVABLE() then true; + else false; end match; end solvable; diff --git a/Compiler/FrontEnd/DAEUtil.mo b/Compiler/FrontEnd/DAEUtil.mo index 64705e900a2..63bf750acc0 100644 --- a/Compiler/FrontEnd/DAEUtil.mo +++ b/Compiler/FrontEnd/DAEUtil.mo @@ -489,7 +489,7 @@ algorithm case (DAE.DAE(elements), _) equation - elements = removeVariablesFromElements(elements, vars, {}); + elements = removeVariablesFromElements(elements, vars); then DAE.DAE(elements); @@ -501,49 +501,49 @@ protected function removeVariablesFromElements remove the variables that match for the element list" input list inElements; input list variableNames; - input list inAcc; - output list outElements; + output list outElements = {}; algorithm - outElements := match (inElements,variableNames,inAcc) - local - DAE.ComponentRef cr; - list rest, els, elist; - DAE.Element v; - String id; - DAE.ElementSource source "the origin of the element"; - Option cmt; - Boolean isEmpty; - - // empty case for vars - case(_,{},_) then inElements; - - // empty case for elements - case({},_,_) then listReverse(inAcc); - - // variable present, remove it - case((v as DAE.VAR(componentRef = cr))::rest, _, _) - equation - // variable is in the list! jump over it - isEmpty = listEmpty(List.select1(variableNames, ComponentReference.crefEqual, cr)); - els = removeVariablesFromElements(rest, variableNames, List.consOnTrue(isEmpty, v, inAcc)); - then els; + if listEmpty(variableNames) then + outElements := inElements; + return; + end if; + for el in inElements loop + _ := match el + local + DAE.ComponentRef cr; + list elist; + DAE.Element v; + String id; + DAE.ElementSource source "the origin of the element"; + Option cmt; + Boolean isEmpty; + + case (v as DAE.VAR(componentRef = cr)) + equation + // variable is in the list! jump over it + if listEmpty(List.select1(variableNames, ComponentReference.crefEqual, cr)) then + outElements = v::outElements; + end if; + then (); - // handle components - case(DAE.COMP(id,elist,source,cmt)::rest, _, _) - equation - elist = removeVariablesFromElements(elist, variableNames, {}); - els = removeVariablesFromElements(rest, variableNames, DAE.COMP(id,elist,source,cmt)::inAcc); - then els; + // handle components + case DAE.COMP(id,elist,source,cmt) + equation + elist = removeVariablesFromElements(elist, variableNames); + outElements = DAE.COMP(id,elist,source,cmt)::outElements; + then (); - // anything else, just keep it - case(v::rest, _, _) - equation - els = removeVariablesFromElements(rest, variableNames, v::inAcc); - then els; - end match; + // anything else, just keep it + else + equation + outElements = el::outElements; + then (); + end match; + end for; + outElements := MetaModelica.Dangerous.listReverseInPlace(outElements); end removeVariablesFromElements; -protected function removeVariable "Remove the variable from the DAE" +protected function removeVariable "Remove the variable from the DAE, UNUSED" input DAE.ComponentRef var; input DAE.DAElist dae; output DAE.DAElist outDae; @@ -589,10 +589,10 @@ public function removeInnerAttr "Remove the inner attribute from variable in the input DAE.DAElist dae; output DAE.DAElist outDae; algorithm - outDae := matchcontinue(var,dae) + outDae := match(var,dae) local DAE.ComponentRef cr,oldVar,newVar; - list elist,elist2,elist3; + list elist,elist2; DAE.Element e,v,u,o; String id; DAE.VarKind kind; DAE.VarParallelism prl; DAE.VarDirection dir; DAE.Type tp; @@ -609,19 +609,20 @@ algorithm These are named uniqly and renamed later in "instClass" */ case(_,DAE.DAE(DAE.VAR(oldVar,kind,dir,prl,prot,tp,bind,dim,ct,source,attr,cmt,(Absyn.INNER_OUTER()))::elist)) + guard + compareUniquedVarWithNonUnique(var,oldVar) equation - true = compareUniquedVarWithNonUnique(var,oldVar); newVar = nameInnerouterUniqueCref(oldVar); o = DAE.VAR(oldVar,kind,dir,prl,prot,tp,NONE(),dim,ct,source,attr,cmt,Absyn.OUTER()) "intact"; u = DAE.VAR(newVar,kind,dir,prl,prot,tp,bind,dim,ct,source,attr,cmt,Absyn.NOT_INNER_OUTER()) " unique'ified"; - elist3 = u::{o}; - elist= listAppend(elist3,elist); + elist= u::o::elist; then DAE.DAE(elist); case(_,DAE.DAE(DAE.VAR(cr,kind,dir,prl,prot,tp,bind,dim,ct,source,attr,cmt,io)::elist)) + guard + ComponentReference.crefEqualNoStringCompare(var,cr) equation - true = ComponentReference.crefEqualNoStringCompare(var,cr); io2 = removeInnerAttribute(io); then DAE.DAE(DAE.VAR(cr,kind,dir,prl,prot,tp,bind,dim,ct,source,attr,cmt,io2)::elist); @@ -636,7 +637,7 @@ algorithm equation DAE.DAE(elist)= removeInnerAttr(var,DAE.DAE(elist)); then DAE.DAE(e::elist); - end matchcontinue; + end match; end removeInnerAttr; protected function compareUniquedVarWithNonUnique " @@ -677,8 +678,7 @@ algorithm outCr := match(inCr) newChild = nameInnerouterUniqueCref(child); then ComponentReference.makeCrefQual(id,idt,subs,newChild); - -end match; + end match; end nameInnerouterUniqueCref; public function unNameInnerouterUniqueCref " @@ -1373,8 +1373,8 @@ protected algorithm DAE.DAE(elementLst = elements) := dae; (el1, el2) := findAllMatchingElements2(elements, cond1, cond2); - firstList := DAE.DAE(listReverse(el1)); - secondList := DAE.DAE(listReverse(el2)); + firstList := DAE.DAE(MetaModelica.Dangerous.listReverseInPlace(el1)); + secondList := DAE.DAE(MetaModelica.Dangerous.listReverseInPlace(el2)); end findAllMatchingElements; protected function findAllMatchingElements2 @@ -1860,30 +1860,12 @@ protected function getVariableList " input list inElementLst; output list outElementLst; algorithm - outElementLst := matchcontinue (inElementLst) - local - list res,lst; - DAE.Element x; - - /* adrpo: filter out records! */ - case ((DAE.VAR(ty = DAE.T_COMPLEX(complexClassType = ClassInf.RECORD(_))))::lst) - equation - res = getVariableList(lst); - then - (res); - - case ((x as DAE.VAR())::lst) - equation - res = getVariableList(lst); - then - (x::res); - case (_::lst) - equation - res = getVariableList(lst); - then - res; - case {} then {}; - end matchcontinue; + /* adrpo: filter out records! */ + outElementLst := list(e for e guard match e + case DAE.VAR(ty = DAE.T_COMPLEX(complexClassType = ClassInf.RECORD(_))) then false; + case DAE.VAR() then true; + else false; + end match in inElementLst); end getVariableList; public function getVariableType @@ -6554,7 +6536,7 @@ public function addSymbolicTransformation input DAE.SymbolicOperation op; output DAE.ElementSource outSource; algorithm - outSource := matchcontinue (source,op) + outSource := match (source,op) local SourceInfo info "the line and column numbers of the equations and algorithms this element came from"; list typeLst "the absyn type of the element" ; @@ -6567,9 +6549,10 @@ algorithm list comment; case (DAE.SOURCE(info, partOfLst, instanceOpt, connectEquationOptLst, typeLst, DAE.SUBSTITUTION(es1 as (h1::_),t1)::operations,comment),DAE.SUBSTITUTION(es2,t2)) - equation + guard // The tail of the new substitution chain is the same as the head of the old one... - true = Expression.expEqual(t2,h1); + Expression.expEqual(t2,h1) + equation // Reference equality would be fine as otherwise it is not really a chain... But replaceExp is stupid :( // true = referenceEq(t2,h1); es = listAppend(es2,es1); @@ -6577,7 +6560,7 @@ algorithm case (DAE.SOURCE(info, partOfLst, instanceOpt, connectEquationOptLst, typeLst, operations, comment),_) then DAE.SOURCE(info, partOfLst, instanceOpt, connectEquationOptLst, typeLst, op::operations,comment); - end matchcontinue; + end match; end addSymbolicTransformation; public function condAddSymbolicTransformation