Mercurial > hg > dml-open-cliopatria
comparison cpack/dml/lib/jpath.pl @ 0:718306e29690 tip
commiting public release
author | Daniel Wolff |
---|---|
date | Tue, 09 Feb 2016 21:05:06 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:718306e29690 |
---|---|
1 /* Part of DML (Digital Music Laboratory) | |
2 Copyright 2014-2015 Samer Abdallah, University of London | |
3 | |
4 This program is free software; you can redistribute it and/or | |
5 modify it under the terms of the GNU General Public License | |
6 as published by the Free Software Foundation; either version 2 | |
7 of the License, or (at your option) any later version. | |
8 | |
9 This program is distributed in the hope that it will be useful, | |
10 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 GNU General Public License for more details. | |
13 | |
14 You should have received a copy of the GNU General Public | |
15 License along with this library; if not, write to the Free Software | |
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
17 */ | |
18 | |
19 :- module(jpath, [ jpath/2, op(600,xfy,#) ]). | |
20 :- use_module(library(dcg_core)). | |
21 :- use_module(library(insist)). | |
22 | |
23 % :- op(600,fx,#). | |
24 :- op(600,xfy,#). | |
25 | |
26 % harvest(M,Tree) :- harvest(M,just(Tree),nothing). | |
27 % harvest(\X,just(X),nothing). | |
28 % harvest(@F,just(X),just(Y)) :- Y=X.F. | |
29 % harvest(#N,just(X),just(Y)) :- nth1(N,X,Y). | |
30 % harvest(F*G) --> harvest(F), harvest(G). | |
31 % harvest((F//G)) --> harvest(F) // harvest(G). | |
32 | |
33 % dec(id,X,X). | |
34 % dec([],_,[]). | |
35 % dec([D|Ds],X,[Y|Ys]) :- dec(D,X,Y), dec(Ds,X,Ys). | |
36 % dec(@F,X,Y) :- Y=X.F. | |
37 % dec(F*G,X,Z) :- dec(F,X,Y), dec(G,Y,Z). | |
38 % dec(swap,(X,Y),(Y,X)). | |
39 % dec(dup,X,(X,X)). | |
40 % dec(fst(F),(X,Y),(Z,Y)) :- dec(F,X,Z). | |
41 % dec((F,G),X,(Y,Z)) :- dec(F,X,Y), dec(G,X,Z). | |
42 % dec(item,X,Y) :- member(Y,X). | |
43 | |
44 % decode(F, R, X) :- atomic(F), X=R.F. | |
45 % decode(item(D), R, X) :- decode(D,R,Y), member(X,Y). | |
46 % decode(F/G, R, X) :- decode(F,R,Y), decode(G,Y,X). | |
47 | |
48 jpath(Y,X) :- | |
49 (complex(X) -> (match(Y,X); Y=(_,_), multi_match(Y,[],X)) ; Y=X). | |
50 | |
51 complex(X) :- is_list(X). | |
52 complex(X) :- is_dict(X). | |
53 | |
54 match(\X,X). | |
55 match(Y,X) :- var(Y), !, | |
56 ( is_dict(X) -> match_dict(Y,X) | |
57 ; is_list(X) -> match_list(Y,X) | |
58 ). | |
59 match(dict(X),X) :- must_be(dict,X). | |
60 match(list(X),X) :- must_be(list,X). | |
61 match(N#Y,X) :- must_be(list,X), match_member(N,Y,X). | |
62 match(F:Y,X) :- must_be(dict,X), match_field(F,Y,X). | |
63 | |
64 match_dict(dict(X),X). | |
65 match_dict(F:V,X) :- match_field(F,V,X). | |
66 | |
67 match_list(list(X),X). | |
68 match_list(N#Y,X) :- match_member(N,Y,X). | |
69 | |
70 match_field(F,Y,X) :- | |
71 ( var(F) -> get_dict(F,X,Z) | |
72 ; insist(get_dict(F,X,Z),field_not_present(F,X)) | |
73 ), | |
74 jpath(Y,Z). | |
75 | |
76 match_member(N,Y,X) :- | |
77 ( var(N) -> nth1(N,X,Z) | |
78 ; nth1(N,X,Z) % insist(...),index_out_of_range(N,X)) | |
79 ), | |
80 jpath(Y,Z). | |
81 | |
82 % multi_match([],_,_). | |
83 % multi_match([S|Ss],Prev,X) :- match1(S,X), \+member(S,Prev), multi_match(Ss,[S|Prev],X). | |
84 multi_match(S,Prev,X) :- match(S,X), \+member(S,Prev). | |
85 multi_match((S,Ss),Prev,X) :- match(S,X), \+member(S,Prev), multi_match(Ss,[S|Prev],X). |