primitives.rst 4.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. Functions
  2. =========
  3. The Modelverse distinguishes three types of function.
  4. For the modeller, it is transparent which type the function is, although it is not possible to dig down in primitive functions.
  5. We now present these three types of functions.
  6. Primitive functions
  7. -------------------
  8. The primitive functions are functions that are implemented directly for the platform, such as in Python.
  9. While the function is written in that language, there are still various bindings with the Modelverse, as the function makes the requested changes in the MvS directly.
  10. As such, the function also makes use of the yield infrastructure presented previously.
  11. For example, the *integer_addition* is one such function, which must be implemented in the platform.
  12. Its definition is as follows::
  13. def integer_addition(a, b, **remainder):
  14. if 'value' not in a:
  15. a['value'], = yield [("RV", [a['id']])]
  16. if 'value' not in b:
  17. b['value'], = yield [("RV", [b['id']])]
  18. yield [("RETURN", [{'value': a['value'] + b['value']}])]
  19. We see that there are two parameters: *a* and *b*.
  20. Additionally, a dictionary of other parameters is also passed, although these are not necessary most of the time.
  21. All alphabetic parameters are those originally passed to the function in the action language, and are represented as a dictionary containing a *value* and *id* key.
  22. The *value* key contains the value of the node, stored in a representation that is compatible with the platform.
  23. The *id* key stores the id of the element in the Modelverse.
  24. When a *value* is defined without an *id*, this means that the value was never stored in the Modelverse to begin with.
  25. When an *id* is defined without a *value*, this means that the value of the ID was never read out of the Modelverse.
  26. It is easy to see that we can switch between both.
  27. To create a value if only an id is present, we read the node::
  28. a['value'], = yield [("RV", [a['id']])]
  29. To create an id if only a value is present, we create the value in the Modelverse::
  30. a['id'], = yield [("CNV", [a['value']])]
  31. As such, the primitive functions will have to check which of the two representations is there, and if the required one is not there, it must be converted.
  32. This explains the first four lines of the function: the function checks whether the value is already read from the Modelverse, and reuses it where possible.
  33. Finally, we return an element of similar structure as the input values.
  34. Again, it is possible for us to just return a *value* entry, meaning that the value is not stored in the Modelverse yet.
  35. It is every functions responsibility to check for the correct type of input, and the output function should only contain either *value* or *id*.
  36. The primitive functions are hardcoded in the tool, and have no explicitly modelled counterpart.
  37. They are essentially to be seen as part of the specification.
  38. While a handle to these functions exists in the Modelverse, when the function is invoked, the instruction pointer is merely pointing at an empty node.
  39. During bootstrapping, however, this node was bound to the primitive python function.
  40. As this is the only option for these functions, all these functions **MUST** be ported if the Modelverse is to be ported to a different domain.
  41. Compiled functions
  42. ------------------
  43. In addition to the primitive functions, which must be implemented directly for the platform, some functions can also be precompiled by the user for performance reasons.
  44. Essentially, these functions are the same as the primitive functions, but their definition is stored in *kernel/modelverse_kernel/compiled.py*.
  45. The definition of these functions is identical, and they are automatically detected at startup.
  46. Nonetheless, they differ in that a Modelverse definition must also exist, which must have identical behaviour to the implemented function.
  47. When such a function is executed in the Modelverse, it is up to the Modelverse to decide which version is to be used.
  48. In case the platform provides a pre-compiled function, that one is normally used, otherwise the action language is directly executed.
  49. Through its JIT compiler, the Modelverse actually dynamically creates compiled functions for the platform all the time.
  50. As such, all functions are actually written out as platform code, and only sporadically is an actual piece of action language still directly executed.
  51. Normal functions
  52. ----------------
  53. All other functions are normal functions, but as mentioned before, these functions are often even compiled behind the scenes by the JIT compiler.