/
report.tex
1654 lines (1293 loc) · 56.9 KB
/
report.tex
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
\documentclass[a4paper]{report}
\usepackage[english]{babel}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{verbatim}
%\usepackage{boxedminipage}
%\usepackage{lscape}
\usepackage{makeidx}
\author{David Kågedal}
\title{Blahonga Blahonga}
\makeindex
\newcommand{\code}[1]{\texttt{#1}}
\newcommand{\codebox}[1]{\fbox{\code{#1}}}
\newcommand{\filename}[1]{\texttt{#1}}
\newcommand{\term}[1]{\textit{\bfseries#1}}
\newcommand{\firstref}[1]{\term{#1}\index{#1}}
\newcommand{\note}[1]{[\textsc{#1}]}
\newcommand{\fixme}[1]{\mbox{}\marginpar{$\blacktriangleleft$}\note{#1}}
\newcommand{\unfinished}{\fixme{\ldots}}
\newtheorem{defth}{Definition}[chapter]
\newtheorem{example}{Example}[chapter]
\newenvironment{Def}[1]{\begin{defth}[#1]\index{#1|textbf}}{\end{defth}}
\newcommand{\codefont}{\normalfont\ttfamily\fontsize{8}{9}\selectfont}
\makeatletter
\newcommand{\sourcefile}[2]{%
\clearpage
\section{File \code{#1.rml}}
\label{src:#1}
\addcontentsline{stc}{sourcefile}{%
\protect\numberline{\thesection}\protect\makebox[8em][l]{\code{#1.rml}}#2}}
\newcommand{\listofsources}{\@starttoc{stc}}
\newcommand{\l@sourcefile}{%
\@dottedtocline{1}{2em}{3em}%
}
% Some magic from Tommy Pettersson
\def\dedu#1#2{{\setbox0=\hbox{\ensuremath{#1}}\dimen0=\wd0\setbox1=\hbox
to\dimen0{\hss\ensuremath{#2}\hss}\vbox{\box0\vspace{1mm}\hrule
width\dimen0\vspace{1mm}\box1}}}
% See the verbatim package for info about this
\def\boxedverbatim{\begingroup%
\parskip=0pt\topsep=0pt\partopsep=0pt%
\def\verbatim@processline{%
\setbox1=\hbox{\the\verbatim@line}%
\setbox0=\vbox{{\box0}{\box1}}%
}%
\def\verbatim@font{%
\codefont
\let\do\do@noligs
\verbatim@nolig@list}
\verbatim}
\def\endboxedverbatim{\endverbatim\framebox[\linewidth][l]{\box0}%
\endgroup\vspace{2ex}}
\makeatother
\begin{document}
\titlepage
\maketitle{}
\endtitlepage
\begin{abstract}
\label{abs}
Modelica is a new modeling language. It is an object-oriented,
equation-based languages that is designed to incorporate features from
several previous modeling languages and form a common ground for
modeling and simulation implementors. As a part of the design
process, the exact semantics of the language needs to be defined.
Formal descriptions of the syntax of programming languages has long
been accepted as a natural way of describing the syntactical form of
the language. Today everybody expects a BNF-like grammar for a new
language. But that is not the case when it comes to describing the
semantics of languages. Formalisms for the specifications of semantics
has been available for many years, but are not often used by others
than researchers.
One advantage of using a formal description of the semantics is of
course that the specification becomes more strict and unambiguous. But
there is also the possibility of using the semantic specification to
generate a language translator, or compiler, in an automatic way.
This report describes a partial formal semantics for Modelica. The
notation used enables it to be passed to a compiler generator that
generates a Modelica translator, that translates a Modelica model into
a simple set of equations.
The language used for the specification is called RML, and is based on
Natural semantics, an operational semantic specification formalism.
\end{abstract}
\tableofcontents
\chapter{Background}
\label{cha:bg}
\section{Introduction}
\label{sec:intro}
This chapter tries to give some background information outlining why
this work was done, and what the basic concepts used are.
\section{Overview of formal semantics}
\label{sec:semoverview}
Any new programming language needs to be described in one way or
another. The syntax of the language is conveniently described by a
grammar in a common format, such as BNF\cite{drakboken}. However,
when it comes to the semantics of a programming language, things are
not so straightforward.
The semantics of the language describes what any particular program
written in the language ``means'', i.e. what should happen when the
program is executed, or evaluated in some form.
Most programming language semantic definitions are given by a standard
text written in English which tries to give a complete an unambiguous
specification of the language. Unfortunately, this is often not
enough. Natural language is inherently not very exact, and it is hard
to make sure that all special cases are covered. One of the most
important requirements of a language specification is that two
different language implementors should be able to read the
specification and make implementations that interpret the
specification in the same way. This implies that the specification
must be exact and unambiguous.
For this reason formalisms for writing formal specifications of
languages have been invented\cite{pagan}. These formalisms
allow for a mathematical semantic description, which as a consequence
is exact, unambiguous and easier to verify.
Several approaches has been explored as to how the semantics should be
described formally. See \cite{pi} for a good introduction to the
various approaches.
\section{Natural semantics}
\label{sec:natsem}
\index{foobar}
The formalism that we use is called \firstref{Natural Semantics}.
The ``natural'' in Natural semantics is intended to indicate its
similarities to natural deduction, which is a way of constructing
proofs in formal logic systems. A specification in Natural semantics
is usually given a proof-theoretic interpretation, meaning that it is
used to construct proofs.
There are many different presentations of Natural semantics in the
literature, and I choose to present the simple form that RML uses.
Natural semantics uses \firstref{inference rules} to describe the
semantics. These rules consist of a \firstref{consequence} and a
number of \firstref{premises}.
$$
\dedu{ p_1 \Rightarrow q_1 \hspace{1em}
\ldots \hspace{1em} p_n \Rightarrow q_1 }
{ p \Rightarrow q }
$$
The number of premises may be zero, in which case we say that the rule
is an \firstref{axiom}.
$$
p \Rightarrow q
$$
\subsection{A simple expression evaluator}
The proof construction mechanism is described by a small example,
describing a very basic expression language, with numbers and addition
and multiplication operators. First, we define the abstract syntax of
the expression language:
\begin{gather}
n \in \text{Int} \\
e \in \text{Exp} ::= n \;|\; e + e \;|\; e \cdot e
\end{gather}
Then we give the rules for the evaluation of expressions to integer
values. \fixme{Numreringen av pilarna används inte}
\begin{gather}
\label{eq:int}
n => n \\
\label{eq:add}
\dedu{ e_1 \Rightarrow n_1 \hspace{1em}
e_2 \Rightarrow n_2 \hspace{1em}
n = n_1 + n_2 }
{ e_1 + e_2 \Rightarrow n } \\
\label{eq:mul}
\dedu{ e_1 \Rightarrow n_1 \hspace{1em}
e_2 \Rightarrow n_2 \hspace{1em}
n = n_1 \cdot n_2 }
{ e_1 \cdot e_2 \Rightarrow n }
\end{gather}
If we want to prove, using these rules, that the expression $3 \cdot
(4 + 5)$ has a value, we do this by finding a rule, whose consequence
can be instantiated to match this expression. Such an instance is
called a \firstref{sequent}.
The only rule that matches is rule \ref{eq:mul}, leading to the
following rule instance, where $3$ is substituted for $e_1$ and $(4 +
5)$ is for $e_2$:
$$
\dedu{ 3 \Rightarrow_2 n_1 \hspace{1em}
(4 + 5) \Rightarrow_3 n_2 \hspace{1em}
n = n_1 \cdot n_2 }
{ 3 \cdot (4 + 5) \Rightarrow_1 n }
$$
This is not a complete proof. We need to prove the sequent $3
\Rightarrow n_1$, and the only rule matching that is rule
\ref{eq:int}, leading to the following proof tree where $3$ is
substituted for $n_3$:
$$
\dedu{ 3 \Rightarrow_2 3 \hspace{1em}
(4 + 5) \Rightarrow_3 n_2 \hspace{1em}
n = 3 \cdot n_2 }
{ 3 \cdot (4 + 5) \Rightarrow_1 n }
$$
The sequent labeled with a $3$ also need to be proved, and this can be
done by instantiating rule \ref{eq:add}.
$$
\dedu{ 3 \Rightarrow_2 3 \hspace{1em}
\dedu{ 4 \Rightarrow_4 n_3 \hspace{1em}
5 \Rightarrow_5 n_4 \hspace{1em}
n_2 = n_3 + n_4 }
{ 4 + 5 \Rightarrow_3 n_2} \hspace{1em}
n = 3 \cdot n_2 }
{ 3 \cdot (4 + 5) \Rightarrow_1 n }
$$
Instantiating rule \ref{eq:int} twice leads to the following proof
tree:
$$
\dedu{ 3 \Rightarrow_2 3 \hspace{1em}
\dedu{ 4 \Rightarrow_4 4 \hspace{1em}
5 \Rightarrow_5 5 \hspace{1em}
n_2 = 4 + 5 }
{ 4 + 5 \Rightarrow_3 n_2} \hspace{1em}
n = 3 \cdot n_2 }
{ 3 \cdot (4 + 5) \Rightarrow_1 n }
$$
Now we can substitute $9$ for $n_2$, and consequently $27$ for $n$,
leading to the complete proof tree.
$$
\dedu{ 3 \Rightarrow_2 3 \hspace{1em}
\dedu{ 4 \Rightarrow_4 4 \hspace{1em}
5 \Rightarrow_5 5 \hspace{1em}
9 = 4 + 5 }
{ 4 + 5 \Rightarrow_3 9} \hspace{1em}
27 = 3 \cdot 9 }
{ 3 \cdot (4 + 5) \Rightarrow_1 27 }
$$
Not only does this prove that the initial expression $3 \cdot (4+5)$
has a value, it also computes the value $27$.
\section{RML}
\label{sec:rml}
One of the nice properties of Natural semantics is that it lends
itself to efficient implementation in a computer. The semantic
specification can be regarded as an operational description of how a
language is interpreted or translated.
This is what RML\cite{rml,petfrrml,pirml} is all about. The RML
language provides a simple text syntax for specifying rules and data
types. This specification can then be compiled into an executable
that can be used to interpret or translate any file in the described
language.
\subsection{RML syntax}
\label{sec:rmlsyn}
The RML syntax borrows elements from languages like SML\cite{sml} to
introduce a strict type system and a module system. The syntax for
rules looks like the normal Natural semantics rule layout, adjusted
for ASCII text.
The rules are grouped in \firstref{relations}. A relation is a set of
rules that have the same \firstref{signature}, meaning that they
perform the same operation, but are applicable at different times.
The type system includes basic types, such as \code{int}, \code{real}
and \code{bool}. User-defined algebraic types may be declared in a
SML-like syntax, as in the following example that declares the
expression type used in the previous section.
\begin{boxedverbatim}
datatype Exp = INT of int | ADD of Exp * Exp | MUL of Exp * Exp
\end{boxedverbatim}
With this type declaration, the expression $1*(2+3)$ is represented as
the RML value \codebox{MUL(INT(1),ADD(NUM(2),NUM(3)))}.
The signature of a relation can also be described as an RML type,
using the \code{=>} type constructor. A relation has a name, and each
rule has a number of input arguments on the left side of the arrow,
and a number of output arguments on the right side. The types of
these arguments form the signature. The rules in the example above
can be grouped in a relation that we call \code{eval}, with the
following signature:
\begin{boxedverbatim}
relation eval : Exp => int
\end{boxedverbatim}
The rules are placed inside the relation definition. It uses the
built-in relations \code{int\_add} and \code{int\_mul} that perform
integer addition an multiplication, respectively.
\begin{boxedverbatim}
relation eval : Exp => int =
axiom eval NUM(i) => i
rule eval e1 => n1 & eval e2 => n2 & int_add(n1,n2) => n
-------------------------------------------------------
eval ADD(e1,e2) => n
rule eval e1 => n1 & eval e2 => n2 & int_mul(n1,n2) => n
-------------------------------------------------------
eval MUL(e1,e2) => n
end
\end{boxedverbatim}
\section{Modelica}
\label{sec:modelicabg}
Modelica\cite{modelicawww} is an object-oriented language for modeling
of physical systems for the purpose of efficient simulation. The
language unifies and generalizes previous object-oriented modeling
languages designed by different companies and research institutions.
Compared with the widespread simulation languages previously
available, this language offers three important advances:
\begin{itemize}
\item Non-causal modeling based on differential and algebraic
equations.
\item Multi-domain modeling capability, i.e. it is possible to combine
electrical, mechanical, thermodynamic, hydraulic etc. model
components within the same application model
\item A general type system that unifies object-orientation, multiple
inheritance, and templates within a single class construct.
\end{itemize}
A Modelica model is defined in terms of classes containing equations
and definitions. The semantics of such a model is defined via
translation of classes, instances, connections and functions into a
flat set of constants, variables and equations. Equations are sorted
and converted to assignment statements when possible. Strongly
connected sets of equations are solved by using a symbolic or numeric
solver.
\fixme{More stuff}
\section{The Modelica design group}
\label{sec:designgroup}
The Modelica language is designed by a group of people. The work on
designing a new modeling language began in 1996 when a number of
experts formed a group to design a new multi-paradigm modeling
language for hybrid systems. Since the start, the group has expanded
to include many new members from around Europe.
A first version of the Modelica language definition was finalized in
September 1997, and there was much rejoicing. But no complete
implementation existed yet, and there was actually a lot more work
needed to make the specification comprehensive enough to be able to
build a implementation for it.
The work on the RML specification for Modelica began in November 1997.
\section{PELAB}
\label{sec:pela}
The Programming Environment Laboratory, PELAB, (Fritzson, professor)
is concerned with research in software engineering, i.e. tools and
methods for the specification, development and maintenance of computer
programs. Some examples are: programming languages, debuggers,
incremental programming environments and compilers, compiler
generators, tools for debugging and maintenance of distributed and
real-time systems, compilers and programming environments for parallel
computers, high-level environments and mathematical modeling languages
and systems for scientific computing, program transformation systems,
etc. The view of programming environment research is rather
pragmatic, and the primary interest is in developing and investigating
new methods and tools that have potential for practical applications,
e.g. in support systems for software specialists. Developing such
tools is very important, since most of the rising cost of computer
systems is due to development, debugging and maintenance of software.
\chapter{Goals, scope, motivation and stuff}
\label{cha:goals}
\fixme{Fixa rubriken}
This chapter describes the goals and the scope of this thesis.
\section{Goal}
The initial assignment was very simple in its formulation: ``Write a
formal semantic specification for Modelica using RML.'' There were at
least three important reasons for doing exactly this.
\subsection{A Modelica specification is needed}
The Modelica design process was, and still is, in dire need of a
formalized description of the language. The language specification
available at the time (version 1.0 from September 1997)\fixme{ref} did
not contain much about the semantics of the language. This meant that
nobody knew exactly how the language worked, and it was not certain
that everybody in the design group agreed on the semantical details,
since they were not clearly described anywhere.
Peter Fritzson is a member of the Modelica design group and there are
several projects at PELAB that concerns Modelica. This project would
be an important contribution to the Modelica design effort.
\subsection{Gaining experience with RML}
The RML language and compiler were developed at PELAB by Mikael
Pettersson. They were new, and only a few language specifications had
yet been written using it. More experience with various language
specifications was desired to gain experience with its advantages and
shortcomings.
\subsection{Natural semantics and equation-based\\ languages}
Modelica is not a programming language, such as C or LISP. Instead it
is a modeling language, with basically only static semantics. For
this reason it was interesting to see how a formalism like Natural
semantics, and more specificatlly RML, which is usually used to
specify programming languages, could be applied to it.
\section{Scope of the thesis}
\label{sec:scope}
\unfinished
\chapter{Development environment}
\label{cha:devenv}
\section{Compiling RML}
\label{sec:rmlc}
The RML compiler is currently at version 2.0. When the work on the
Modelica semantics began, the RML version was at 1.5. The new
compiler provided much improved error messages and freedom in writing
source files.
The RML compiler first translates the RML specification to a C
program. The RML compiler driver \code{rmlc} then compiles this,
using a C compiler. We have used the GNU C compiler (GCC), but most
modern C compilers should be usable.
\section{Parser}
\label{sec:parser}
The Modelica parser was generated by the PCCTS\fixme{ref} compiler
generation system. It generates a parser in C, which is linked with
the RML object files. The glue between the languages is a special RML
relation \code{parse}, which is implemented in C, rather than RML.
The parser only builds an abstract syntax tree (AST), and leaves the
rest of the transformation logic to the RML code. Therefore, the
parser itself is not further described in this report.
The parser generator in PCCTS is powerful, but it has a few problems,
and is currently not maintained very actively. Because of problems
with error handlers, the translator does not deal very well with
syntax errors. If an error occurs while parsing, an error message
will be printed, but the tranlator will try to translate the result of
parsing anyway. Since the resulting AST will be severely broken, this
will fail.
\section{The report}
\label{sec:report}
This report was written using the \LaTeX{}\cite{Lamport86} text
formatting system.
To produce the annotated semantics in chapter \ref{cha:formsem} a
small program was written to convert the RML source files with
comments to \LaTeX{} source with the comments converted to ordinary
text. This program is a quick'n'dirty Pike\cite{pike} hack, and is
provided in appendix \ref{cha:rmldoc}
\chapter{Informal semantics of Modelica}
\label{cha:semantics}
\section{Introduction}
\label{sec:semintro}
The Modelica specification\cite{modelica1f} contains an informal
description of the intended semantics of the language. Unfortunately,
the description lacks considerably in detail, which means that this
work needs to formalize things not covered by the specification. In
some cases, the semantics was easy to guess, but in other cases,
discussions with the authors and reading of design meeting minutes was
the needed to find out what was really intended.
This chapter complements the Modelica specification by filling in some
of the larger holes in the informal language semantics. It is
intended to help the understanding of the formal semantics in chapter
\ref{cha:formsem}.
\section{Terminology}
\label{sec:terminology}
There has been some confusion about the terminology regarding Modelica
language elements, and currently the language definition is not very
strict on this point. This will hopefully improve, but for now it is
necessary to include a short list of terms here to specify what they
mean.
\begin{Def}{Element}
The term \term{element} corresponds closely to the non-terminal
\texttt{element} in the grammar in the Modelica specification, which
is a part of a class declaration. It can be either a class
definition, a component declaration or an extends clause, that
appears inside a class definition.
\end{Def}
\begin{example}
The following class definition contains four elements. One
\code{extends} statement, one class definition, and two class
definitions. Note that the definitions of \code{width} and
\code{height} are separate elements, but the grammar only needs one
\code{element} rule to match them.
\begin{boxedverbatim}
class C
extends OtherClass;
type Length = Real(unit = "m");
Length width, height;
end C;
\end{boxedverbatim}
\end{example}
\begin{Def}{Instantiation}
The word \term{instantiate} is used in many language
descriptions, with slightly different meaning. I choose to use a
definition that actually differs from the one used in the Modelica
specification, but conforms more closely to what I consider normal
object-oriented use.
Instantiating a class means creating an object (or class instance)
from the description contained in the class definition, possibly
modified by any given modifiers. This instance is not a run-time
object that needs an allocated piece of memory, as in many dynamic
object-oriented languages, but the principle is the same.
For every component declaration in the class definition (possible
inherited from another class definitions using an \code{extends}
element), the component declarations class is also instantiated.
\fixme{Call this ``Component instantiation,'' and then define
``type instantiation?'' But what is that?}
\end{Def}
\begin{example}
The following Modelica example contains three class definitions. If
the last one (\code{C}) is instantiated, it means that the class
\code{B} needs to be instantiated twice to create the subcomponents
\code{b1} and \code{b2}.
\begin{boxedverbatim}
class B
Real x;
end B;
class C
B b1,b2;
end C;
\end{boxedverbatim}
\end{example}
\begin{Def}{Component}
The term \term{Component} refers to an class instance. Many
components are composed of several other components.
\end{Def}
\begin{Def}{Immediate Subcomponent}
If component $A$ is a structure which is composed of other
components, each of these components is an \term{immediate
subcomponent} of $A$.
\end{Def}
\begin{Def}{Subcomponent}
This is a recursive definition of \term{subcomponent}. A
subcomponent of a component $A$ is either
\begin{enumerate}
\item An immediate subcomponent of $A$.
\item A subcomponent of an immediate subcomponent of $A$.
\end{enumerate}
\end{Def}
\fixme{Example}
\section{Types}
\label{sec:types}
\subsection{Overview of the type system in Modelica}
\label{sec:typeoverview}
The type system is based on \firstref{classes}. A class is the basic
unit of modeling. It is used to modularize the model description and
to give the models a hierarchical structure. Another important
concept in the type system is that of \firstref{arrays}.
The type system used in Modelica is based on a type system described
by Luca Cardelli\cite{cardelli}.
The definition of what a type is has been the subject of discussion in
the Modelica group, but the following definition is used in this
specification.
\begin{Def}{Type}
A type is a property of components and expressions. It is defined
as one of the following:
\begin{enumerate}
\item A built-in type (Real, Integer, String or Boolean). These
correspond to the Modelica predefined classes \code{RealType},
\code{IntegerType}, \code{StringType} and \code{BooleanType}.
\item A structured type, containing a set of public components
$(N,T)$, where $N$ is an identifier and $T$ is a type. No two
components in the set can have the same identifier.
\item An array of a type, with a undefined or non-negative, integer
size.
\end{enumerate}
\end{Def}
Note that there is no special provision for multi-dimensional arrays.
They are regarded as equal to arrays of arrays. A Modelica
implementation might do it the other way around, regard arrays of
arrays as multidimensional arrays. The important thing is that they
are the same. Synonyms for two classes of arrays are used both in
this specification and the Modelica language definition.
\begin{Def}{Array synonyms}
The term \term{vector}\index{vector} is a synonym for a
one-dimensional array. The term \term{matrix}\index{matrix} is a
synonym for a two-dimensional array.
\end{Def}
\begin{Def}{Incomplete types}
If a type includes an undefined array size, it is an
\term{incomplete type}. If not, it is a \term{complete type}.
\end{Def}
All components must have a complete type. All expressions also have
complete types. Incomplete types only appear as the input argument
types of function. \fixme{But nobody known how this actually works}
\subsection{Type equivalence and subtypes}
\label{sec:typeq}
The concepts of equivalent types and subtypes appears in several
places in the specification.
\begin{Def}{Subtype}
A type $T_1$ is a \term{subtype} of another type $T_2$ if and
only if one of the following hold:
\begin{enumerate}
\item $T_1$ and $T_2$ are the same built-in type
\item $T_1$ and $T_2$ are structured types, where all public
components in $T_2$ appear in $T_1$. The type of the component in
$T_1$ must be a subtype of the type of the corresponding component
in $T_2$.
\item $T_1$ is an array of type $T_1'$ and $T_2$ is an array of type
$T_2'$, where $T_1'$ is a subtype of $T_2'$. Also either the size
of $T_2$ is undefined or the size of $T_1$ is equal to the size of
$T_2$.
\end{enumerate}
\end{Def}
\begin{Def}{Supertype}
If $T_1$ is a subtype of $T_2$, then $T_2$ is a \term{supertype}
of $T_1$.
\end{Def}
\begin{Def}{Equivalent types}
Two types $T_1$ and $T_2$ are said to be \term{equivalent types}
if and only if $T_1$ is a subtype of $T_2$ and $T_2$ is a subtype of
$T_1$.
\end{Def}
\subsection{Class restrictions}
\label{sec:clrestr}
A class can either be declared with the \code{class} keyword, or with
one of its restricted forms, listed below. The restricted forms are
used to indicate the intended use of the class, and to impose certain
restrictions on what the class definition can contain, and how it can
be used.
\begin{table}[htbp]
\begin{center}
\begin{tabular}{lp{8cm}}
\hline
\code{record} & No equations are allowed in the definition or in any
of its components. A \code{record} instance may not be used in
connections. \\
\hline
\code{type} & May only be declared as derived from one of the
predefined types, or as a matrix of a \code{type}. \\
\hline
\code{connector} & No equations are allowed in the definition or
in any of its components. \\
\hline
\code{model} & A \code{model} instance may not be used in
connections. \\
\hline
\code{block} & Each public component must be declared with one
of the modifiers \code{input} or \code{output}. \\
\hline
\code{package} & May only contain declarations of classes and
constants. \\
\hline
\code{function} & Each public component must be declared with one
of the modifiers \code{input} or \code{output}. No equations are
allowed and only one algorithm section is allowed. \\
\hline
\end{tabular}
\caption{Class restrictions}
\label{tab:clrestr}
\end{center}
\end{table}
A goal of the Modelica design is that a valid program can always be
transformed into another valid program by replacing all occurrences of
the restricted class keywords with the keyword \code{class}. For a
class declared with the keyword \code{class} to be able to be used as
if it was declared with a restricted keyword, it has to adhere to the
restrictions for that keyword.
As an example, this means that if a variable is used in a connection,
its type has to be either a class declared with \code{connector}, or a
class declared with \code{class} which contains no equations. Another
implication is that an invalid program can be transformed into a valid
program by replacing all restricted keywords with \code{class}, e.g.
when incorrectly trying to use a \code{record} in a connection,
because the keyword \code{record} has the same restrictions as the
\code{connector} keyword, except that \code{record} is explicitly not
allowed in connections, but \code{class} is, as long as it fulfills
the restrictions.
It is currently not known if this goal will be fully met. In most
cases it could be done, but things like \code{package} and
\code{function} might be problematic.
\section{Arrays and matrices}
\label{sec:arrays}
The concept of arrays in Modelica have not been thoroughly examined as
of writing. Most of the basic properties of the type system should be
clear, but there are some points worth noting.
\subsection{Overloaded array operations}
\label{sec:arrayop}
The exact meaning of multiplication where one or more of the operands
are arrays is currently being defined. Similarly for other
operations. The semantics specified here should not be regarded as an
absolute reference to what these operations should mean, but rather as
a description of how the overloading of operators works.
\subsection{Collapsing arrays}
\label{sec:collaps}
\fixme{This won't be implemented anyway}
\section{Functions}
\label{sec:functions}
\fixme{Who knows how functions work?}
\section{Connections}
\label{sec:connections}
Connections between objects are introduced by the \code{connect}
construct in the equation part of a class declaration. The
\code{connect} construct takes two references to connectors, each of
which is either an element of the same class as the \code{connect}
construct or an element of one of its components. The connectors are
connected to produce equation according to the rules below. Each
connector reference has either the syntactic form \code{c}, where
\code{c} is a connector instance in the class containing the
\code{connect} construct, or \code{m.c}, where \code{m} is the name of
a component or an array of components \fixme{Stämmer det?} of the class
containing the \code{connect} construct, and \code{c} is the name of a
connector variable in the component \code{m}.
\subsection{Connection sets}
\label{sec:connectsets}
To create equations from the \code{connect} constructs, the connected
variables are collected in \firstref{connection sets}.
\begin{Def}{Connection sets}
A connection set is a set $C$ of variables connected by means of
\code{connect} constructs. A connection set contains either flow or
non-flow variables, but never both.
\end{Def}
\begin{Def}{Inner and outer connectors}
In an object $M$, each connector element of that class is called an
\emph{outer connector} with respect to $M$. Each connector element of
elements of $M$ is called an \emph{inner connector} with respect to
$M$.
\end{Def}
When a \code{connect(a,b)} construct is encountered while
instantiating a class \code{C}, the component names \code{a} and
\code{b} are checked to be of the same type (see section
\ref{sec:typeq}). Then \code{a} and \code{b} are decomposed into their
public primitive components. Each subcomponent of \code{a} forms a
connection set with its corresponding subcomponent of \code{b}.
If several \code{connect(a,b)} constructs are encountered during
instantiation of a class, the resulting connection sets are merged to
a collection of connection sets $C$ so that if a component $x$ is
in two different connection sets $S_1$ and $S_2$, they the union $S_1
\cup S_2$ is a subset of one of the sets in $C$. Each component is in
exactly one of the sets in $C$.
\subsection{Equations}
\label{sec:coneq}
Each connection set is used to generate one or more equations. In the
case of variables declared with the \code{flow} type modifier, the
equation generated is a sum-to-zero equation, as in equation
\ref{eq:flow}. The coefficient $d_v$ is $1$ if the component $v$ was
added to the connection set from an inner connector, and $-1$ if it
was added from an outer connector. \fixme{Clear?}
\begin{equation}
\label{eq:flow}
\sum_{v \in C} d_v v = 0
\end{equation}
In the case of non-flow components a number of simple equations is
generated. If the connection set contains $n$ components $c_1 \ldots
c_n$, equations to the effect of $c_1=c_2=\cdots=c_n$ are generated,
as in \ref{eqn:nonflow}.
\begin{eqnarray}
\label{eqn:nonflow}
c_1 & = & c_2 \notag\\
c_2 & = & c_3 \notag\\
& \vdots & \notag\\
c_{n-1} & = & c_n
\end{eqnarray}
\subsection{Example}
\label{sec:csetex}
This section contains a short example of the use of connections in
Modelica, and shows the corresponding connection sets and generated
equations. The Modelica source is shown in figure \ref{fig:csetex1}
and declares a connector \code{C} and models \code{A} and \code{M}.
The model that is instantiated at the top level is \code{M}, as it is
the last model in the file.
\begin{figure}[htbp]
\begin{boxedverbatim}
connector C
Real x;
flow Real y;
end C;
model A
C con1, con2;
end A;
model M
C con;
A a1, a2;
Real y;
equation
connect(con,a1.con1);
connect(a1.con1,a2.con1);
connect(a1.con2,a2.con2);
y = con.y;
end M;
\end{boxedverbatim}
\caption{\code{connect} example}
\label{fig:csetex1}
\end{figure}
Connection sets are generated at two different levels in this example.
While instantiating the model \code{M}, the three \code{connect}
statements creates the connection sets described in figure
\ref{fig:csetex2}, and the two instances of the model \code{A}
(components \code{a1} and \code{a2} in model \code{M}) create the
connection sets in figure \ref{fig:csetex3}. Each component in the
sets are marked with a label indicating whether they were added from
an inner or an outer connector.
\newcommand{\couter}[1]{\code{#1}^{\text{outer}}}
\newcommand{\cinner}[1]{\code{#1}^{\text{inner}}}
\begin{figure}[htbp]
\begin{center}
$\left\{ \couter{con.x}, \cinner{a1.con1.x}, \cinner{a2.con1.x} \right\}$
$\left\{ \cinner{con.y}, \cinner{a1.con1.y}, \cinner{a2.con1.y} \right\}$
$\left\{ \cinner{a1.con2.x}, \cinner{a2.con2.x} \right\}$
$\left\{ \cinner{a1.con2.y}, \cinner{a2.con2.y} \right\}$
\caption{Connection sets from \code{M}}
\label{fig:csetex2}
\end{center}
\end{figure}
\begin{figure}[htbp]
\begin{center}
$\left\{ \couter{a1.con1.x}, \couter{a1.con2.x} \right\}$
$\left\{ \couter{a1.con1.y}, \couter{a1.con2.y} \right\}$
$\left\{ \couter{a2.con1.x}, \couter{a2.con2.x} \right\}$
$\left\{ \couter{a2.con1.y}, \couter{a2.con2.y} \right\}$
\caption{Connection sets from \code{A}}
\label{fig:csetex3}
\end{center}
\end{figure}
The equations generated from these connection sets, together with the
last equation in the model \code{M} is shown in figure
\ref{fig:csetex4}.
\begin{figure}[htbp]
\begin{center}
\begin{eqnarray*}
\code{con.x} &=& \code{a1.con1.x} \\
\code{a1.con1.x} &=& \code{a2.con1.x} \\
-\code{con.y} + \code{a1.con1.y} + \code{a2.con1.y} &=& 0 \\
\code{a1.con2.x} &=& \code{a2.con2.x} \\
\code{a1.con2.y} + \code{a2.con2.y} &=& 0 \\
\code{a1.con1.x} &=& \code{a1.con2.x} \\
-\code{a1.con1.y} - \code{a1.con2.x} &=& 0 \\
\code{a2.con1.x} &=& \code{a2.con2.x} \\
-\code{a2.con1.y} - \code{a2.con2.x} &=& 0
\end{eqnarray*}
\caption{Equations}
\label{fig:csetex4}
\end{center}
\end{figure}
\chapter{The Target language: Flat Modelica}
\label{cha:target}