Symbolic Logic:Programming:Program Model
A "Program Model" is a representation of a program as objects.
In Object Oriented Programming the domain we wish to describe is modeled as objects. This gives an object representation of the domain we choose. For example, applying OOP to the trading domain gives a Trading Model. Orders, Executions and Trades are modelled as objects.
Applying Object Oriented Programming to programs gives a Program Model. Classes, Functions, Function Calls, and Variables are modelled as objects.
Contents |
[edit] History
When a program is compiled, internally the compiler must implicitly construct a model of the program. However this model is thrown away. All the compiler and linker finally produces is the executable. The programmer only wants the program to execute correctly. The programmers target audience is the user running the program.
The programmer makes compromises in writing the program between really general solutions that would run slowly, and specific solutions that run fast, but have limitted applicability.
[edit] Purpose
The purpose of the Program Model is to add another level of processing, before the program is turned into an executable. This level performs analysis on the program prior to generating the executable. The most easy to implement analysis is partial evaluation. There are many other kinds of analysis that may be done.
[edit] Easy Implementation
Starting with a basic set of classes to represent the program objects,
Name | Description | Inherits | Methods |
---|---|---|---|
PMClass | A class that represents a class | PMObject | IsA, HasFunction |
PMFunction | A class that represents a function | PMObject | Parameter, SetBody. |
PMCall | A class that represents a call to a function | PMValue | SetFunction, Parameter. |
PMVariable | A class that represents a variable | PMValue | |
PMConstant | A class that represents a constant expression (like 1 or "some text"). | PMValue | |
PMValue | A class that represents a value. | PMObject | The pure operators (+, -, *, /). |
In a language like C++ that has operators it is possible to write expressions that look like the calculation of a value, which instead return a Program Model Object that represents the calculation.
[edit] Class Constants
Constants of type PMClass. ???? How high level should these be (e.g precision) ????
- PMInteger
- PMReal
[edit] Built in functions
- PMIf
[edit] Example
PMClass *math = new PMClass("math"); PMVariable *n = new PMVariable(PMInteger); PMFunction *factorial = math->HasFunction("factorial", PMInteger)->Parameter(n) ->SetBody( PMIf(n == 0, new PMConstant(1), n * factorial->parameter(n-1)) );