In the usual, both imperative and functional programming, the whole program basically reduces to a huge number of equalities
x = ... , where the variable on the left is, and on the right is an expression dependent on variables. And programming languages provide syntactic sugar to write this set as compact as possible. These are almost equations, only trivial.
Some difficulties are found in lazy languages:
fib = 1:1:(zipWith (+) fib (tail fib))
')
Here fib is present in both the left and the right side, that is, we get a complete equation, the solution of which is an infinite sequence of Fibonacci numbers. Since topology is naturally determined on infinite sequences, Haskell can solve this equation using simple iterations (via the fixed-point operator).
Further along the path of the equations went Prolog. Each predicate defines an equation on sets, although it is written without an equal sign. Such equations are solved by almost a search and the possibility of using arithmetic in them is very limited.
But there is a language in which the equations are the most important part - this is
Modelica .
model Point Real x,y; equation end Point; model Line parameter Real len; Point p1,p2; equation len = (p1.x-p2.x)^2 + (p1.y-p2.y)^2; end Line;
Here the segment is defined as the coordinates of the ends and is long, and the length and coordinates are connected by an obvious equation.
(Unfortunately, the source does not know such a language, and it is impossible to insert a colored pygment by html.)
Naturally, the equations can be combined into a system:
model Hand Point ph,pl; parameter Real l1,h1,l2,h2; Line ll1(len = l1),lh1(len = h1),ll2(len = l2),lh2(len = h2); Real arcl,arch; Point p; equation ll1.p1 = pl; lh1.p1 = ph; ll1.p2.x = pl.x + l1 * arcl; lh1.p2.x = ph.x + h1 * arch; ll1.p2 = ll2.p1; lh1.p2 = lh2.p1; p = ll2.p2; p = lh2.p2; end Hand;
Here is described a manipulator consisting of four rails connected by hinges controlled by two servo drives.
As the name suggests, Modelica is designed for modeling complex, heterogeneous systems. Its implementation is at the famous
Wolfram , a manufacturer of CAD (not to be confused, and "drawing programs")
CATIA . There are also free implementations, such as
OpenModelica .
Since simulated objects are rarely described by algebraic equations, Modelica can also solve ordinary differential equations. Unfortunately, it has not yet mastered partial differential equations, but usually it is possible to break the system into finite elements and generate ordinary difures in a cycle.
As an example, I will give a model of a
very complex hybrid electromechanical system - Gaussgan:
model Gauss extends Modelica.Electrical.Analog.Interfaces.OnePort; Real x (start = -1); Real sp (start = 0); Real co; Real pw (start = 0); Real ke; Real se; Real pe; equation der(pw) = i*v; ke = sp^2/2; se = i^2/2; pe = pw - ke - se; co = (if abs(x) > 0.2 then (abs(x)*(x^(-5))/3.0) else (x/((0.2^5)*3.0))); der(sp) = -co * i; der(x) = sp; der(i) = v + (co * sp); end Gauss; model Main Modelica.Electrical.Analog.Basic.Ground g; Modelica.Electrical.Analog.Sources.ConstantVoltage ps; Modelica.Electrical.Analog.Basic.Resistor rps; Gauss gun; equation connect(ps.p, gp); connect(ps.p, gun.p); connect(ps.n, rps.p); connect(rps.n, gun.n); end Main;
Here x is the coordinate of the projectile, v is the voltage, i is the current. For debugging, the total energy pe is calculated - its graph will be a horizontal straight line.
In addition to the textual representation of the program, Modelica is standardized and graphical (although I personally prefer to work with text). For code reuse, inheritance familiar to many people using OOP is used. There are extensive libraries in electrical engineering, heat engineering, mechanics, and even biochemistry.
Modelik developers do not intend to cover all with one system - most implementations can be modeled using the
Functional Mock-up Interface . If desired, the same interface can be used to interact with the outside world, but this is still an open area.