This repository has been archived by the owner on May 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
/
NFCeval.mo
2583 lines (2281 loc) · 75.6 KB
/
NFCeval.mo
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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-2014, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR
* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
* RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
* ACCORDING TO RECIPIENTS CHOICE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from OSMC, either from the above address,
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/
encapsulated package NFCeval
import NFBinding.Binding;
import ComponentRef = NFComponentRef;
import Error;
import NFComponent.Component;
import Expression = NFExpression;
import NFInstNode.InstNode;
import Operator = NFOperator;
import NFOperator.Op;
import Typing = NFTyping;
import NFCall.Call;
import Dimension = NFDimension;
import Type = NFType;
import NFTyping.ExpOrigin;
import ExpressionSimplify;
import NFPrefixes.Variability;
import NFClassTree.ClassTree;
import ComplexType = NFComplexType;
import Subscript = NFSubscript;
protected
import NFFunction.Function;
import EvalFunction = NFEvalFunction;
import List;
import System;
import ExpressionIterator = NFExpressionIterator;
import MetaModelica.Dangerous.*;
import NFClass.Class;
public
uniontype EvalTarget
record DIMENSION
InstNode component;
Integer index;
Expression exp;
SourceInfo info;
end DIMENSION;
record ATTRIBUTE
Binding binding;
end ATTRIBUTE;
record RANGE
SourceInfo info;
end RANGE;
record CONDITION
SourceInfo info;
end CONDITION;
record GENERIC
SourceInfo info;
end GENERIC;
record IGNORE_ERRORS end IGNORE_ERRORS;
function isRange
input EvalTarget target;
output Boolean isRange;
algorithm
isRange := match target
case RANGE() then true;
else false;
end match;
end isRange;
function hasInfo
input EvalTarget target;
output Boolean hasInfo;
algorithm
hasInfo := match target
case DIMENSION() then true;
case ATTRIBUTE() then true;
case RANGE() then true;
case CONDITION() then true;
else false;
end match;
end hasInfo;
function getInfo
input EvalTarget target;
output SourceInfo info;
algorithm
info := match target
case DIMENSION() then target.info;
case ATTRIBUTE() then Binding.getInfo(target.binding);
case RANGE() then target.info;
case CONDITION() then target.info;
end match;
end getInfo;
end EvalTarget;
function evalExp
input output Expression exp;
input EvalTarget target = EvalTarget.IGNORE_ERRORS();
algorithm
exp := match exp
local
InstNode c;
Binding binding;
Expression exp1, exp2, exp3;
list<Expression> expl = {};
Call call;
Component comp;
Option<Expression> oexp;
ComponentRef cref;
Dimension dim;
ExpOrigin.Type exp_origin;
case Expression.CREF()
then evalCref(exp.cref, exp, target);
case Expression.TYPENAME()
then evalTypename(exp.ty, exp, target);
case Expression.ARRAY()
algorithm
exp.elements := list(evalExp(e, target) for e in exp.elements);
then
exp;
case Expression.RANGE()
algorithm
exp1 := evalExp(exp.start, target);
oexp := evalExpOpt(exp.step, target);
exp3 := evalExp(exp.stop, target);
then
if EvalTarget.isRange(target) then
Expression.RANGE(exp.ty, exp1, oexp, exp3) else evalRange(exp1, oexp, exp3);
case Expression.TUPLE()
algorithm
exp.elements := list(evalExp(e, target) for e in exp.elements);
then
exp;
case Expression.RECORD()
algorithm
exp.elements := list(evalExp(e, target) for e in exp.elements);
then
exp;
case Expression.CALL()
then evalCall(exp.call, target);
case Expression.SIZE(dimIndex = SOME(exp1))
algorithm
dim := listGet(Type.arrayDims(Expression.typeOf(exp.exp)), Expression.toInteger(evalExp(exp1, target)));
then
if Dimension.isKnown(dim) then Expression.INTEGER(Dimension.size(dim)) else exp;
case Expression.SIZE()
algorithm
expl := list(Dimension.sizeExp(d) for d in Type.arrayDims(Expression.typeOf(exp.exp)));
dim := Dimension.INTEGER(listLength(expl), Variability.PARAMETER);
then
Expression.ARRAY(Type.ARRAY(Type.INTEGER(), {dim}), expl);
case Expression.BINARY()
algorithm
exp1 := evalExp(exp.exp1, target);
exp2 := evalExp(exp.exp2, target);
then
evalBinaryOp(exp1, exp.operator, exp2);
case Expression.UNARY()
algorithm
exp1 := evalExp(exp.exp, target);
then
evalUnaryOp(exp1, exp.operator);
case Expression.LBINARY()
then evalLogicBinaryOp(exp.exp1, exp.operator, exp.exp2, target);
case Expression.LUNARY()
algorithm
exp1 := evalExp(exp.exp, target);
then
evalLogicUnaryOp(exp1, exp.operator);
case Expression.RELATION()
algorithm
exp1 := evalExp(exp.exp1, target);
exp2 := evalExp(exp.exp2, target);
then
evalRelationOp(exp1, exp.operator, exp2);
case Expression.IF() then evalIfExp(exp.condition, exp.trueBranch, exp.falseBranch, target);
case Expression.CAST()
algorithm
exp1 := evalExp(exp.exp, target);
then
evalCast(exp1, exp.ty);
case Expression.UNBOX()
algorithm
exp1 := evalExp(exp.exp, target);
then Expression.UNBOX(exp1, exp.ty);
case Expression.SUBSCRIPTED_EXP()
then evalSubscriptedExp(exp.exp, exp.subscripts, target);
case Expression.TUPLE_ELEMENT()
algorithm
exp1 := evalExp(exp.tupleExp, target);
then
Expression.tupleElement(exp1, exp.ty, exp.index);
case Expression.MUTABLE()
algorithm
exp1 := evalExp(Mutable.access(exp.exp), target);
then
exp1;
else exp;
end match;
end evalExp;
function evalExpOpt
input output Option<Expression> oexp;
input EvalTarget target;
algorithm
oexp := match oexp
local
Expression e;
case SOME(e) then SOME(evalExp(e, target));
else oexp;
end match;
end evalExpOpt;
function evalCref
input ComponentRef cref;
input Expression defaultExp;
input EvalTarget target;
output Expression exp;
protected
InstNode c;
algorithm
exp := match cref
// TODO: Rewrite this, we need to take all subscripts into account and not
// just the ones on the last identifier.
case ComponentRef.CREF(node = c as InstNode.COMPONENT_NODE(),
origin = NFComponentRef.Origin.CREF)
algorithm
exp := evalComponentBinding(c, defaultExp, target);
then
Expression.applySubscripts(list(evalSubscript(s, target) for s in cref.subscripts), exp);
else defaultExp;
end match;
end evalCref;
function evalComponentBinding
input InstNode node;
input Expression defaultExp "The expression returned if the binding couldn't be evaluated";
input EvalTarget target;
output Expression exp;
protected
ExpOrigin.Type exp_origin;
Component comp;
Binding binding;
algorithm
exp_origin := if Class.isFunction(InstNode.getClass(InstNode.parent(node)))
then ExpOrigin.FUNCTION else ExpOrigin.CLASS;
Typing.typeComponentBinding(node, exp_origin);
comp := InstNode.component(node);
binding := Component.getBinding(comp);
if Binding.isUnbound(binding) then
binding := makeComponentBinding(comp, node, Expression.toCref(defaultExp), target);
end if;
exp := match binding
case Binding.TYPED_BINDING()
algorithm
exp := evalExp(binding.bindingExp, target);
if not referenceEq(exp, binding.bindingExp) then
binding.bindingExp := exp;
comp := Component.setBinding(binding, comp);
InstNode.updateComponent(comp, node);
end if;
then
exp;
case Binding.CEVAL_BINDING() then binding.bindingExp;
case Binding.UNBOUND()
algorithm
printUnboundError(target, defaultExp);
then
defaultExp;
else
algorithm
Error.addInternalError(getInstanceName() + " failed on untyped binding", sourceInfo());
then
fail();
end match;
end evalComponentBinding;
function makeComponentBinding
input Component component;
input InstNode node;
input ComponentRef cref;
input EvalTarget target;
output Binding binding;
protected
ClassTree tree;
array<InstNode> comps;
list<Expression> fields;
Type ty;
InstNode rec_node;
Expression exp;
ComponentRef rest_cr;
algorithm
binding := matchcontinue (component, cref)
// A record component without an explicit binding, create one from its children.
case (Component.TYPED_COMPONENT(ty = Type.COMPLEX(complexTy = ComplexType.RECORD(rec_node))), _)
algorithm
exp := makeRecordBindingExp(component.classInst, rec_node, component.ty, cref);
binding := Binding.CEVAL_BINDING(exp);
InstNode.updateComponent(Component.setBinding(binding, component), node);
then
binding;
// A record array component without an explicit binding, create one from its children.
case (Component.TYPED_COMPONENT(ty = ty as Type.ARRAY(elementType =
Type.COMPLEX(complexTy = ComplexType.RECORD(rec_node)))), _)
algorithm
exp := makeRecordBindingExp(component.classInst, rec_node, component.ty, cref);
exp := splitRecordArrayExp(exp);
binding := Binding.CEVAL_BINDING(exp);
InstNode.updateComponent(Component.setBinding(binding, component), node);
then
binding;
// A record field without an explicit binding, evaluate the parent's binding
// if it has one and fetch the binding from it instead.
case (_, ComponentRef.CREF(restCref = rest_cr as ComponentRef.CREF(ty = ty)))
guard Type.isRecord(Type.arrayElementType(ty))
algorithm
exp := evalCref(rest_cr, Expression.EMPTY(ty), target);
exp := makeComponentBinding2(exp, InstNode.name(node));
binding := Binding.CEVAL_BINDING(exp);
// TODO: If the cref has subscripts we can't cache the binding, since it
// will have been evaluated with regards to the subscripts. We
// should create the complete binding and cache it first, then
// subscript it.
if not ComponentRef.hasSubscripts(cref) then
InstNode.updateComponent(Component.setBinding(binding, component), node);
end if;
then
binding;
else NFBinding.EMPTY_BINDING;
end matchcontinue;
end makeComponentBinding;
function makeComponentBinding2
input Expression exp;
input String name;
output Expression result;
algorithm
result := match exp
local
list<Expression> expl;
Type ty;
Dimension dim;
case Expression.RECORD() then Expression.lookupRecordField(name, exp);
// An empty array of records will still be empty, only the type needs to be changed.
case Expression.ARRAY(elements = {})
algorithm
exp.ty := Type.lookupRecordFieldType(name, exp.ty);
then
exp;
// For a non-empty array of records, look up the field in each record and
// create an array from them.
// TODO: Optimize this, the index of the field will be the same for each
// element of the array so we only need to do lookup once.
case Expression.ARRAY(ty = Type.ARRAY(dimensions = dim :: _))
algorithm
expl := list(makeComponentBinding2(e, name) for e in exp.elements);
ty := Type.liftArrayLeft(Expression.typeOf(listHead(expl)), dim);
then
Expression.ARRAY(ty, expl);
end match;
end makeComponentBinding2;
function makeRecordBindingExp
input InstNode typeNode;
input InstNode recordNode;
input Type recordType;
input ComponentRef cref;
output Expression exp;
protected
ClassTree tree;
array<InstNode> comps;
list<Expression> fields;
Type ty;
InstNode c;
ComponentRef cr;
algorithm
tree := Class.classTree(InstNode.getClass(typeNode));
comps := ClassTree.getComponents(tree);
fields := {};
for i in arrayLength(comps):-1:1 loop
c := comps[i];
ty := InstNode.getType(c);
cr := ComponentRef.CREF(c, {}, ty, NFComponentRef.Origin.CREF, cref);
fields := Expression.CREF(ty, cr) :: fields;
end for;
exp := Expression.RECORD(InstNode.scopePath(recordNode), recordType, fields);
exp := evalExp(exp);
end makeRecordBindingExp;
function splitRecordArrayExp
input output Expression exp;
protected
Absyn.Path path;
Type ty;
list<Expression> expl;
algorithm
Expression.RECORD(path, ty, expl) := exp;
exp := Expression.RECORD(path, Type.arrayElementType(ty), expl);
exp := Expression.fillType(ty, exp);
end splitRecordArrayExp;
function evalTypename
input Type ty;
input Expression originExp;
input EvalTarget target;
output Expression exp;
protected
list<Expression> lits;
algorithm
// Only expand the typename into an array if it's used as a range, and keep
// them as typenames when used as e.g. dimensions.
if not EvalTarget.isRange(target) then
exp := originExp;
else
exp := match ty
case Type.ARRAY(elementType = Type.BOOLEAN())
then Expression.ARRAY(ty, {Expression.BOOLEAN(false), Expression.BOOLEAN(true)});
case Type.ARRAY(elementType = Type.ENUMERATION())
algorithm
lits := Expression.makeEnumLiterals(ty.elementType);
then
Expression.ARRAY(ty, lits);
else
algorithm
Error.addInternalError(getInstanceName() + " got invalid typename", sourceInfo());
then
fail();
end match;
end if;
end evalTypename;
function evalRange
input Expression start;
input Option<Expression> optStep;
input Expression stop;
output Expression exp;
protected
Expression step;
list<Expression> expl;
Type ty;
list<String> literals;
Integer istep;
algorithm
if isSome(optStep) then
SOME(step) := optStep;
(ty, expl) := match (start, step, stop)
case (Expression.INTEGER(), Expression.INTEGER(istep), Expression.INTEGER())
algorithm
// The compiler decided to randomly dislike using step.value here, hence istep.
expl := list(Expression.INTEGER(i) for i in start.value:istep:stop.value);
then
(Type.INTEGER(), expl);
case (Expression.REAL(), Expression.REAL(), Expression.REAL())
algorithm
expl := list(Expression.REAL(r) for r in start.value:step.value:stop.value);
then
(Type.REAL(), expl);
else
algorithm
printWrongArgsError(getInstanceName(), {start, step, stop}, sourceInfo());
then
fail();
end match;
else
(ty, expl) := match (start, stop)
case (Expression.INTEGER(), Expression.INTEGER())
algorithm
expl := list(Expression.INTEGER(i) for i in start.value:stop.value);
then
(Type.INTEGER(), expl);
case (Expression.REAL(), Expression.REAL())
algorithm
expl := list(Expression.REAL(r) for r in start.value:stop.value);
then
(Type.REAL(), expl);
case (Expression.BOOLEAN(), Expression.BOOLEAN())
algorithm
expl := list(Expression.BOOLEAN(b) for b in start.value:stop.value);
then
(Type.BOOLEAN(), expl);
case (Expression.ENUM_LITERAL(ty = ty as Type.ENUMERATION()), Expression.ENUM_LITERAL())
algorithm
expl := list(Expression.ENUM_LITERAL(ty, listGet(ty.literals, i), i) for i in start.index:stop.index);
then
(ty, expl);
else
algorithm
printWrongArgsError(getInstanceName(), {start, stop}, sourceInfo());
then
fail();
end match;
end if;
exp := Expression.ARRAY(Type.ARRAY(ty, {Dimension.fromInteger(listLength(expl))}), expl);
end evalRange;
function printFailedEvalError
input String name;
input Expression exp;
input SourceInfo info;
algorithm
Error.addInternalError(name + " failed to evaluate ‘" + Expression.toString(exp) + "‘", info);
end printFailedEvalError;
function evalBinaryOp
input Expression exp1;
input Operator op;
input Expression exp2;
output Expression exp;
algorithm
exp := match op.op
case Op.ADD then evalBinaryAdd(exp1, exp2);
case Op.SUB then evalBinarySub(exp1, exp2);
case Op.MUL then evalBinaryMul(exp1, exp2);
case Op.DIV then evalBinaryDiv(exp1, exp2);
case Op.POW then evalBinaryPow(exp1, exp2);
case Op.ADD_SCALAR_ARRAY then evalBinaryScalarArray(exp1, exp2, evalBinaryAdd);
case Op.ADD_ARRAY_SCALAR then evalBinaryArrayScalar(exp1, exp2, evalBinaryAdd);
case Op.SUB_SCALAR_ARRAY then evalBinaryScalarArray(exp1, exp2, evalBinarySub);
case Op.SUB_ARRAY_SCALAR then evalBinaryArrayScalar(exp1, exp2, evalBinarySub);
case Op.MUL_SCALAR_ARRAY then evalBinaryScalarArray(exp1, exp2, evalBinaryMul);
case Op.MUL_ARRAY_SCALAR then evalBinaryArrayScalar(exp1, exp2, evalBinaryMul);
case Op.MUL_VECTOR_MATRIX then evalBinaryMulVectorMatrix(exp1, exp2);
case Op.MUL_MATRIX_VECTOR then evalBinaryMulMatrixVector(exp1, exp2);
case Op.SCALAR_PRODUCT then evalBinaryScalarProduct(exp1, exp2);
case Op.MATRIX_PRODUCT then evalBinaryMatrixProduct(exp1, exp2);
case Op.DIV_SCALAR_ARRAY then evalBinaryScalarArray(exp1, exp2, evalBinaryDiv);
case Op.DIV_ARRAY_SCALAR then evalBinaryArrayScalar(exp1, exp2, evalBinaryDiv);
case Op.POW_SCALAR_ARRAY then evalBinaryScalarArray(exp1, exp2, evalBinaryPow);
case Op.POW_ARRAY_SCALAR then evalBinaryArrayScalar(exp1, exp2, evalBinaryPow);
case Op.POW_MATRIX then evalBinaryPowMatrix(exp1, exp2);
else
algorithm
Error.addInternalError(getInstanceName() + ": unimplemented case for " +
Expression.toString(Expression.BINARY(exp1, op, exp2)), sourceInfo());
then
fail();
end match;
end evalBinaryOp;
function evalBinaryAdd
input Expression exp1;
input Expression exp2;
output Expression exp;
algorithm
exp := match (exp1, exp2)
case (Expression.INTEGER(), Expression.INTEGER())
then Expression.INTEGER(exp1.value + exp2.value);
case (Expression.REAL(), Expression.REAL())
then Expression.REAL(exp1.value + exp2.value);
case (Expression.STRING(), Expression.STRING())
then Expression.STRING(exp1.value + exp2.value);
case (Expression.ARRAY(), Expression.ARRAY())
guard listLength(exp1.elements) == listLength(exp2.elements)
then Expression.ARRAY(exp1.ty,
list(evalBinaryAdd(e1, e2) threaded for e1 in exp1.elements, e2 in exp2.elements));
else
algorithm
exp := Expression.BINARY(exp1, Operator.makeAdd(Type.UNKNOWN()), exp2);
printFailedEvalError(getInstanceName(), exp, sourceInfo());
then
fail();
end match;
end evalBinaryAdd;
function evalBinarySub
input Expression exp1;
input Expression exp2;
output Expression exp;
algorithm
exp := match (exp1, exp2)
case (Expression.INTEGER(), Expression.INTEGER())
then Expression.INTEGER(exp1.value - exp2.value);
case (Expression.REAL(), Expression.REAL())
then Expression.REAL(exp1.value - exp2.value);
case (Expression.ARRAY(), Expression.ARRAY())
guard listLength(exp1.elements) == listLength(exp2.elements)
then Expression.ARRAY(exp1.ty,
list(evalBinarySub(e1, e2) threaded for e1 in exp1.elements, e2 in exp2.elements));
else
algorithm
exp := Expression.BINARY(exp1, Operator.makeSub(Type.UNKNOWN()), exp2);
printFailedEvalError(getInstanceName(), exp, sourceInfo());
then
fail();
end match;
end evalBinarySub;
function evalBinaryMul
input Expression exp1;
input Expression exp2;
output Expression exp;
algorithm
exp := match (exp1, exp2)
case (Expression.INTEGER(), Expression.INTEGER())
then Expression.INTEGER(exp1.value * exp2.value);
case (Expression.REAL(), Expression.REAL())
then Expression.REAL(exp1.value * exp2.value);
case (Expression.ARRAY(), Expression.ARRAY())
guard listLength(exp1.elements) == listLength(exp2.elements)
then Expression.ARRAY(exp1.ty,
list(evalBinaryMul(e1, e2) threaded for e1 in exp1.elements, e2 in exp2.elements));
else
algorithm
exp := Expression.BINARY(exp1, Operator.makeMul(Type.UNKNOWN()), exp2);
printFailedEvalError(getInstanceName(), exp, sourceInfo());
then
fail();
end match;
end evalBinaryMul;
function evalBinaryDiv
input Expression exp1;
input Expression exp2;
output Expression exp;
algorithm
exp := match (exp1, exp2)
case (Expression.REAL(), Expression.REAL())
then Expression.REAL(exp1.value / exp2.value);
case (Expression.ARRAY(), Expression.ARRAY())
guard listLength(exp1.elements) == listLength(exp2.elements)
then Expression.ARRAY(exp1.ty,
list(evalBinaryDiv(e1, e2) threaded for e1 in exp1.elements, e2 in exp2.elements));
else
algorithm
exp := Expression.BINARY(exp1, Operator.makeDiv(Type.UNKNOWN()), exp2);
printFailedEvalError(getInstanceName(), exp, sourceInfo());
then
fail();
end match;
end evalBinaryDiv;
function evalBinaryPow
input Expression exp1;
input Expression exp2;
output Expression exp;
algorithm
exp := match (exp1, exp2)
case (Expression.REAL(), Expression.REAL())
then Expression.REAL(exp1.value ^ exp2.value);
case (Expression.ARRAY(), Expression.ARRAY())
guard listLength(exp1.elements) == listLength(exp2.elements)
then Expression.ARRAY(exp1.ty,
list(evalBinaryPow(e1, e2) threaded for e1 in exp1.elements, e2 in exp2.elements));
else
algorithm
exp := Expression.BINARY(exp1, Operator.makePow(Type.UNKNOWN()), exp2);
printFailedEvalError(getInstanceName(), exp, sourceInfo());
then
fail();
end match;
end evalBinaryPow;
function evalBinaryScalarArray
input Expression scalarExp;
input Expression arrayExp;
input FuncT opFunc;
output Expression exp;
partial function FuncT
input Expression exp1;
input Expression exp2;
output Expression exp;
end FuncT;
algorithm
exp := match arrayExp
case Expression.ARRAY()
then Expression.ARRAY(arrayExp.ty,
list(evalBinaryScalarArray(scalarExp, e, opFunc) for e in arrayExp.elements));
else opFunc(scalarExp, arrayExp);
end match;
end evalBinaryScalarArray;
function evalBinaryArrayScalar
input Expression arrayExp;
input Expression scalarExp;
input FuncT opFunc;
output Expression exp;
partial function FuncT
input Expression exp1;
input Expression exp2;
output Expression exp;
end FuncT;
algorithm
exp := match arrayExp
case Expression.ARRAY()
then Expression.ARRAY(arrayExp.ty,
list(evalBinaryArrayScalar(e, scalarExp, opFunc) for e in arrayExp.elements));
else opFunc(arrayExp, scalarExp);
end match;
end evalBinaryArrayScalar;
function evalBinaryMulVectorMatrix
input Expression vectorExp;
input Expression matrixExp;
output Expression exp;
protected
list<Expression> expl;
Dimension m;
Type ty;
algorithm
exp := match Expression.transposeArray(matrixExp)
case Expression.ARRAY(Type.ARRAY(ty, {m, _}), expl)
algorithm
expl := list(evalBinaryScalarProduct(vectorExp, e) for e in expl);
then
Expression.ARRAY(Type.ARRAY(ty, {m}), expl);
else
algorithm
exp := Expression.BINARY(vectorExp, Operator.makeMul(Type.UNKNOWN()), matrixExp);
printFailedEvalError(getInstanceName(), exp, sourceInfo());
then
fail();
end match;
end evalBinaryMulVectorMatrix;
function evalBinaryMulMatrixVector
input Expression matrixExp;
input Expression vectorExp;
output Expression exp;
protected
list<Expression> expl;
Dimension n;
Type ty;
algorithm
exp := match matrixExp
case Expression.ARRAY(Type.ARRAY(ty, {n, _}), expl)
algorithm
expl := list(evalBinaryScalarProduct(e, vectorExp) for e in expl);
then
Expression.ARRAY(Type.ARRAY(ty, {n}), expl);
else
algorithm
exp := Expression.BINARY(matrixExp, Operator.makeMul(Type.UNKNOWN()), vectorExp);
printFailedEvalError(getInstanceName(), exp, sourceInfo());
then
fail();
end match;
end evalBinaryMulMatrixVector;
function evalBinaryScalarProduct
input Expression exp1;
input Expression exp2;
output Expression exp;
algorithm
exp := match (exp1, exp2)
local
Type elem_ty;
Expression e2;
list<Expression> rest_e2;
case (Expression.ARRAY(ty = Type.ARRAY(elem_ty)), Expression.ARRAY())
guard listLength(exp1.elements) == listLength(exp2.elements)
algorithm
exp := Expression.makeZero(elem_ty);
rest_e2 := exp2.elements;
for e1 in exp1.elements loop
e2 :: rest_e2 := rest_e2;
exp := evalBinaryAdd(exp, evalBinaryMul(e1, e2));
end for;
then
exp;
else
algorithm
exp := Expression.BINARY(exp1, Operator.makeMul(Type.UNKNOWN()), exp2);
printFailedEvalError(getInstanceName(), exp, sourceInfo());
then
fail();
end match;
end evalBinaryScalarProduct;
function evalBinaryMatrixProduct
input Expression exp1;
input Expression exp2;
output Expression exp;
protected
Expression e2;
list<Expression> expl1, expl2;
Type elem_ty, row_ty, mat_ty;
Dimension n, p;
algorithm
e2 := Expression.transposeArray(exp2);
exp := match (exp1, e2)
case (Expression.ARRAY(Type.ARRAY(elem_ty, {n, _}), expl1),
Expression.ARRAY(Type.ARRAY(_, {p, _}), expl2))
algorithm
mat_ty := Type.ARRAY(elem_ty, {n, p});
if listEmpty(expl2) then
exp := Expression.makeZero(mat_ty);
else
row_ty := Type.ARRAY(elem_ty, {p});
expl1 := list(Expression.ARRAY(row_ty, list(evalBinaryScalarProduct(r, c) for c in expl2)) for r in expl1);
exp := Expression.ARRAY(mat_ty, expl1);
end if;
then
exp;
else
algorithm
exp := Expression.BINARY(exp1, Operator.makeMul(Type.UNKNOWN()), exp2);
printFailedEvalError(getInstanceName(), exp, sourceInfo());
then
fail();
end match;
end evalBinaryMatrixProduct;
function evalBinaryPowMatrix
input Expression matrixExp;
input Expression nExp;
output Expression exp;
protected
Integer n;
algorithm
exp := match (matrixExp, nExp)
case (Expression.ARRAY(), Expression.INTEGER(value = 0))
algorithm
n := Dimension.size(listHead(Type.arrayDims(matrixExp.ty)));
then
Expression.makeIdentityMatrix(n, Type.REAL());
case (_, Expression.INTEGER(value = n))
then evalBinaryPowMatrix2(matrixExp, n);
else
algorithm
exp := Expression.BINARY(matrixExp, Operator.makePow(Type.UNKNOWN()), nExp);
printFailedEvalError(getInstanceName(), exp, sourceInfo());
then
fail();
end match;
end evalBinaryPowMatrix;
function evalBinaryPowMatrix2
input Expression matrix;
input Integer n;
output Expression exp;
algorithm
exp := match n
// A^1 = A
case 1 then matrix;
// A^2 = A * A
case 2 then evalBinaryMatrixProduct(matrix, matrix);
// A^n = A^m * A^m where n = 2*m
case _ guard intMod(n, 2) == 0
algorithm
exp := evalBinaryPowMatrix2(matrix, intDiv(n, 2));
then
evalBinaryMatrixProduct(exp, exp);
// A^n = A * A^(n-1)
else
algorithm
exp := evalBinaryPowMatrix2(matrix, n - 1);
then
evalBinaryMatrixProduct(matrix, exp);
end match;
end evalBinaryPowMatrix2;
function evalUnaryOp
input Expression exp1;
input Operator op;
output Expression exp;
algorithm
exp := match op.op
case Op.UMINUS then evalUnaryMinus(exp1);
else
algorithm
Error.addInternalError(getInstanceName() + ": unimplemented case for " +
Expression.toString(Expression.UNARY(op, exp1)), sourceInfo());
then
fail();
end match;
end evalUnaryOp;
function evalUnaryMinus
input Expression exp1;
output Expression exp;
algorithm
exp := match exp1
case Expression.INTEGER() then Expression.INTEGER(-exp1.value);
case Expression.REAL() then Expression.REAL(-exp1.value);
case Expression.ARRAY()
algorithm
exp1.elements := list(evalUnaryMinus(e) for e in exp1.elements);
then
exp1;
else
algorithm