同図像性

出典: フリー百科事典『ウィキペディア(Wikipedia)』

同図像性(どうずぞうせい、: homoiconicity)は、一部のプログラミング言語が持つ特性である 。 ある言語のコードをその言語で操作できる場合、その言語は同図像性があるという。 「プログラムコードをデータとして扱う」と呼ばれることもある。

同図像性はデータモデルを表現するための言語とプログラム言語が深く関連しているか、同一であることを意味する。 同図像性はメタプログラミングを容易にする。

同図像性を持つ代表的なプログラミング言語はLispである。Lispの構造は階層化されたリストの形式をとるS式によって与えられる。その結果、プログラムは実行中にある関数にアクセスでき、プログラムによってその場でその関数を変更できる。

実装された言語[編集]

多くの Lisp (Common LispSchemeClojure等)、 RacketS-expressionsに加えて次のものがある。

Lispの例[編集]

LispS式をデータとコードの外部表現として使用する。 S式は、プリミティブのLisp関数READで読み取ることができる。 READはLispデータを返す:リスト、 シンボル 、数値、文字列。 プリミティブのLisp関数EVALは、Lispデータとして表されたLispコードを使用し、副作用を計算して結果を返す。 結果は、Lispデータから外部S式を作成するプリミティブ関数PRINTによって出力される。

((:name "john" :age 20) (:name "mary" :age 18) (:name "alice" :age 22))

Lispコード。 この例では、リスト、記号、数字を使用する。

(* (sin 1.1) (cos 2.03))   ; in infix: sin(1.1)*cos(2.03)

Lisp関数LISTを使用して上記の式を作成し、変数EXPRESSIONを結果に格納する。

(setf expression (list '* (list 'sin 1.1) (list 'cos 2.03)) ) 
-> (* (SIN 1.1) (COS 2.03))  ; Lisp returns and prints the result

(third expression)  ; the third element of the expression
-> (COS 2.03)

COS関数SIN関数に変更する。

(setf (first (third expression)) 'SIN)
; The expression is now (* (SIN 1.1) (SIN 2.03)).

式を評価する。

(eval expression)
-> 0.7988834

式を文字列に出力する。

(print-to-string expression)
-> "(* (SIN 1.1) (SIN 2.03))"

文字列から式を読み取る。

(read-from-string "(* (SIN 1.1) (SIN 2.03))")
-> (* (SIN 1.1) (SIN 2.03))   ; returns a list of lists, numbers and symbols

参考文献[編集]

  1. ^ Why we created Julia”. julialang.org. 2020年8月13日閲覧。 “We want a language that’s homoiconic, with true macros like Lisp, but with obvious, familiar mathematical notation like Matlab.”
  2. ^ metaprogramming”. docs.julialang.org. 2020年8月13日閲覧。 “Like Lisp, Julia represents its own code as a data structure of the language itself.”
  3. ^ Homoiconic languages (archived), in true Blue blog at Oracle
  4. ^ Ramsay, S.; Pytlik-Zillig, B. (2012). “Code-Generation Techniques for XML Collections Interoperability”. dh2012 Digital Humanities Conference Proceedings. http://www.dh2012.uni-hamburg.de/conference/programme/abstracts/code-generation-techniques-for-xml-collections-interoperability/ 
  5. ^ Notes for Programming Language Experts”. Wolfram Language. Wolfram (2017年). 2020年8月13日閲覧。
  6. ^ Metaprogramming in mathematica”. Stack Exchange. 2020年8月13日閲覧。 “Mathematica is [...] Homoiconic language (programs written in own data structures - Mathematica expressions. This is code-as-data paradigm, like Lisp which uses lists for this)”