# Code as data

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Code_as_data
> Markdown URL: https://mediated.wiki/source/Code_as_data.md
> Source: https://en.wikipedia.org/wiki/Code_as_data
> Source revision: 1332157772
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

{{Short description|Principle of interchangeability of data and code}}
In computer science, the expression '''code as data''' refers to the idea that [source code](/source/source_code) written in a [programming language](/source/programming_language) can be manipulated as data, such as a sequence of characters or an [abstract syntax tree](/source/abstract_syntax_tree) (AST), and it has an [execution](/source/Execution_(computing)) semantics only in the context of a given [compiler](/source/compiler) or [interpreter](/source/Interpreter_(computing)).<ref name="Poletto">{{cite thesis|type=PhD |last1=Poletto |first1=Massimiliano A. |title=Language and compiler support for dynamic code generation |date=September 1999 |page=20 |publisher = MIT|url=https://dspace.mit.edu/bitstream/handle/1721.1/80592/44265672-MIT.pdf?sequence=2 |language=EN |quote=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}}</ref> The notion is often used in the context of [Lisp](/source/Lisp_(programming_language))-like languages that use [S-expressions](/source/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](/source/homoiconicity)).<ref>{{cite journal|last1=Plusch|first1=Mike|title=ConciseXML builds upon the important qualities of XML and S-Expressions|journal=XML Journal|volume=5|number=2|date=February 2004|pages=20+|publisher=Gale Academic OneFile|url=http://link.gale.com/apps/doc/A113563363/AONE|access-date=14 January 2023|quote=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.}}</ref><ref>{{cite book |last1=Riehl |first1=Jonathan |title=Proceedings of the 5th international conference on Generative programming and component engineering |chapter=Assimilating MetaBorg:: Embedding language tools in languages |date=22 October 2006 |pages=21–28 |doi=10.1145/1173706.1173710 |isbn=1595932372 |s2cid=11111101 |quote=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.}}</ref>

These ideas are generally used in the context of what is called [metaprogramming](/source/metaprogramming), writing programs that treat other programs as their data.<ref name=PyCUDA>{{cite journal |last1=Klöckner |first1=Andreas |last2=Pinto |first2=Nicolas |last3=Lee |first3=Yunsup |last4=Catanzaro |first4=Bryan |last5=Ivanov |first5=Paul |last6=Fasih |first6=Ahmed |title=PyCUDA and PyOpenCL: A Scripting-Based Approach to GPU Run-Time Code Generation |journal=Parallel Computing |date=March 2012 |volume=38 |issue=3 |pages=157–174 |doi=10.1016/j.parco.2011.09.001 |arxiv=0911.3456 |s2cid=18928397 }}</ref><ref>{{cite book |last1=Wu |first1=Chaur |title=Pro DLR in .NET 4 |chapter=Metaprogramming |date=2010 |pages=185–210 |doi=10.1007/978-1-4302-3067-0_8|isbn=978-1-4302-3066-3 }}</ref> For example, code-as-data allows the [serialization](/source/serialization) of [first-class functions](/source/first-class_functions) in a portable manner.<ref>{{cite journal |last1=Tack |first1=Guido |last2=Kornstaedt |first2=Leif |last3=Smolka |first3=Gert |title=Generic Pickling and Minimization |journal=Electronic Notes in Theoretical Computer Science |date=March 2006 |volume=148 |issue=2 |pages=79–103 |doi=10.1016/j.entcs.2005.11.041|doi-access=free }}</ref> Another use case is storing a program in a string, which is then processed by a compiler to produce an executable.<ref name=PyCUDA/> More often there is a [reflection](/source/Reflection_(computer_programming)) API that exposes the structure of a program as an object within the language, reducing the possibility of creating a malformed program.<ref>{{cite book |last1=VanderHart |first1=Luke |last2=Sierra |first2=Stuart |chapter=Macros and Metaprogramming |title=Practical Clojure |date=2010 |pages=167–178 |doi=10.1007/978-1-4302-7230-4_12|isbn=978-1-4302-7231-1 }}</ref>

In [computational theory](/source/computational_theory), [Kleene's second recursion theorem](/source/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.<ref>{{cite web |last1=Panangaden |first1=Prakash |title=Notes on the recursion theorem |url=https://www.cs.mcgill.ca/~prakash/Courses/Comp330/Notes/recursion_thm.pdf |website=COMP 330 Theory of Computation |publisher=McGill University |access-date=15 January 2023}}</ref>

Code-as-data is also a principle of the [Von Neumann architecture](/source/Von_Neumann_architecture), since [stored programs](/source/stored_programs) and data are both represented as bits in the same memory device.<ref name=PyCUDA/> This architecture offers the ability to write [self-modifying code](/source/self-modifying_code).{{cn|date=January 2023}} It also opens the security risk of disguising a malicious program as user data and then using an [exploit](/source/Exploit_(computer_security)) to direct execution to the malicious program.<ref>{{cite web |last1=Bohme |first1=Rainer |last2=Moore |first2=Tyler |title=A Brief Introduction to Information Security |url=http://www.cs.yale.edu/homes/jf/BohmeInfoSecIntroduction.pdf |date=26 August 2013}}</ref>

== Data as Code ==

In [declarative programming](/source/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.<ref name="Poletto"/> [Configuration scripts](/source/Configuration_scripts), [domain-specific language](/source/domain-specific_language)s and [markup language](/source/markup_language)s are cases where program execution is controlled by data elements that are not clearly sequences of commands.<ref>{{cite arXiv | eprint=2401.10603 | last1=Zills | first1=Fabian | last2=Schäfer | first2=Moritz | last3=Tovey | first3=Samuel | last4=Kästner | first4=Johannes | last5=Holm | first5=Christian | title=ZnTrack -- Data as Code | date=2024 | class=cs.SE }}</ref><ref>{{cite web | url=https://github.com/shuttle-hq/synth | title=Shuttle-hq/Synth | website=[GitHub](/source/GitHub) }}</ref>

== References ==
{{reflist}}

Category:Programming language topics

{{Comp-sci-stub}}
{{Compu-prog-stub}}

---
Adapted from the Wikipedia article [Code as data](https://en.wikipedia.org/wiki/Code_as_data) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Code_as_data?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
