不思議な記述を見つけました。
An object in JSON is presented as a bunch of keys and values within curly braces, in Prolog I have used a function and a KV list like so:
obj([key1-value1, key2-value2, ...]).
Present your terms like that and everything should be fine. See the source file json_encode.pl for a full explanation and some examples that should hopefully explain it all.
https://github.com/emacstheviking/gnuprolog-json
Prolog に “key-value
” といった記法があったけ? と数日考え込んだのですが、Prolog では明示的に評価するまで記述した内容がそのまま値として扱われるということを利用しているということに思い至りました。
$ gprolog
| ?- A = 2 - 1.
A = 2-1
変数 A
の値は 1
ではなく 2-1
であることがわかります。
2-1
は 1
とはマッチしません。
| ?- 1 = 2 - 1.
no
2-1
を明示的に評価することで値として 1
がえられます。
| ?- A is 2 - 1.
A = 1
yes
| ?- A is 2 - 1, 1 = A.
A = 1
yes
と、いうことは。評価前であれば演算子 -
も含めた形で変数にマッチさせることができます。
| ?- A - B = 2 - 1.
A = 2
B = 1
yes
-
の演算は評価されないので、数値でなくても構いません。
| ?- A - B = abc - def.
A = abc
B = def
yes
評価すると当然のようにエラーになります。
| ?- A is abc - def.
uncaught exception: error(type_error(evaluable,abc/0),(is)/2)
これが “key-value
” の正体だったようです。
これらを踏まえて。
key_value.pro を用意。
find(_, [], not_found) :- !.
find(K, [K-V|_], V) :- !.
find(K, [_|KVs], V) :- find(K, KVs, V).
GNU-Prolog で実行。
$ gprolog
| ?- ['key_value.pro'].
yes
| ?- find(a, [a-1, b-2, c-3], V).
V = 1
yes
| ?- find(b, [a-1, b-2, c-3], V).
V = 2
yes
| ?- find(c, [a-1, b-2, c-3], V).
V = 3
yes
| ?- find(d, [a-1, b-2, c-3], V).
V = not_found
yes
ペアが作れればよいので [ [a, 1], [b, 2], [c, 3] ]
でもよいのですが、見やすさ書きやすさ誤った形式の排除という点で key-value
という記述のメリットはありそうです。
ちなみに。
評価されなければよいので演算子は -
でなくても構いません。
| ?- A + B = abc + def.
A = abc
B = def
yes
| ?- A / B = abc / def.
A = abc
B = def
yes
| ?- A : B = abc : def.
A = abc
B = def
yes
いつか読むはずっと読まない:青春ものだった
私はサンリオSF文庫は読んだことはないのですが、こうして復刊されたものを読むと良書ぞろいだったのだろうなぁと思わずにはいられません。