Code as data

(Redirected from Data as code)

In computer science, the expression code as data refers to the idea that source code written in a programming language can be manipulated as data, such as a sequence of characters or an abstract syntax tree (AST), and it has an execution semantics only in the context of a given compiler or interpreter.[1] The notion is often used in the context of Lisp-like languages that use S-expressions as their main syntax, as writing programs using nested lists of symbols makes the interpretation of the program as an AST quite transparent (a property known as homoiconicity).[2][3]

These ideas are generally used in the context of what is called metaprogramming, writing programs that treat other programs as their data.[4][5] For example, code-as-data allows the serialization of first-class functions in a portable manner.[6] Another use case is storing a program in a string, which is then processed by a compiler to produce an executable.[4] More often there is a reflection API that exposes the structure of a program as an object within the language, reducing the possibility of creating a malformed program.[7]

In computational theory, Kleene's second recursion theorem provides a form of code-is-data, by proving that a program can have access to its own source code.[8]

Code-as-data is also a principle of the Von Neumann architecture, since stored programs and data are both represented as bits in the same memory device.[4] This architecture offers the ability to write self-modifying code.[citation needed] It also opens the security risk of disguising a malicious program as user data and then using an exploit to direct execution to the malicious program.[9]

Data as Code

edit

In declarative programming, the Data as Code (DaC) principle refers to the idea that an arbitrary data structure can be exposed using a specialized language semantics or API. For example, a list of integers or a string is data, but in languages such as Lisp and Perl, they can be directly entered and evaluated as code.[1] Configuration scripts, domain-specific languages and markup languages are cases where program execution is controlled by data elements that are not clearly sequences of commands.[10][11]

References

edit
  1. ^ a b Poletto, Massimiliano A. (September 1999). Language and compiler support for dynamic code generation (PDF) (PhD). MIT. p. 20. until it is dynamically compiled, dynamic code is data. Similarly, lists in Lisp and strings in Perl are data, but they can be evaluated as code
  2. ^ Plusch, Mike (February 2004). "ConciseXML builds upon the important qualities of XML and S-Expressions". XML Journal. 5 (2). Gale Academic OneFile: 20+. Retrieved 14 January 2023. S-Expressions, or symbolic expressions, is the syntax behind Lisp-like languages, including Scheme. Basically, S-Expressions are nested lists of symbols. S-Expressions are used with languages that support the notion that code is data.
  3. ^ Riehl, Jonathan (22 October 2006). "Assimilating MetaBorg:: Embedding language tools in languages". Proceedings of the 5th international conference on Generative programming and component engineering. pp. 21–28. doi:10.1145/1173706.1173710. ISBN 1595932372. S2CID 11111101. The Lisp and Scheme communities are an exception, since they tend to hold closely to the idea that code is data, and implement a large portion of their language in a smaller core language.
  4. ^ a b c Klöckner, Andreas; Pinto, Nicolas; Lee, Yunsup; Catanzaro, Bryan; Ivanov, Paul; Fasih, Ahmed (March 2012). "PyCUDA and PyOpenCL: A Scripting-Based Approach to GPU Run-Time Code Generation". Parallel Computing. 38 (3): 157–174. arXiv:0911.3456. doi:10.1016/j.parco.2011.09.001. S2CID 18928397.
  5. ^ Wu, Chaur (2010). "Metaprogramming". Pro DLR in .NET 4. pp. 185–210. doi:10.1007/978-1-4302-3067-0_8. ISBN 978-1-4302-3066-3.
  6. ^ Tack, Guido; Kornstaedt, Leif; Smolka, Gert (March 2006). "Generic Pickling and Minimization". Electronic Notes in Theoretical Computer Science. 148 (2): 79–103. doi:10.1016/j.entcs.2005.11.041.
  7. ^ VanderHart, Luke; Sierra, Stuart (2010). "Macros and Metaprogramming". Practical Clojure. pp. 167–178. doi:10.1007/978-1-4302-7230-4_12. ISBN 978-1-4302-7231-1.
  8. ^ Panangaden, Prakash. "Notes on the recursion theorem" (PDF). COMP 330 Theory of Computation. McGill University. Retrieved 15 January 2023.
  9. ^ Bohme, Rainer; Moore, Tyler (26 August 2013). "A Brief Introduction to Information Security" (PDF).
  10. ^ https://arxiv.org/abs/2401.10603
  11. ^ https://github.com/shuttle-hq/synth