% --------------------------------------------------------------- % Euler Yap Engine - EYE looking through N3 glasses -- Jos De Roo % --------------------------------------------------------------- version_info('$Id: euler.yap 5343 2012-05-16 15:25:18Z josd $'). help_info('Usage: eye * * * eye\n\ \ java -jar Euler.jar [--swipl] [--no-install]\n\ \ yap -q -f euler.yap -g main --\n\ \ swipl -q -f euler.yap -g main --\n\ \\n\ \ --nope no proof explanation\n\ \ --no-branch no branch engine\n\ \ --no-qvars no quantified variables in output\n\ \ --no-qnames no qnames in output\n\ \ --no-span no span control\n\ \ --quiet incomplete e:falseModel explanation\n\ \ --quick-false do not prove all e:falseModel\n\ \ --quick-possible do not prove all e:possibleModel\n\ \ --quick-answer do not prove all answers\n\ \ --think generate e:consistentGives\n\ \ --ances generate e:ancestorModel\n\ \ --plugin plugin yap_resource\n\ \ --wcache to tell that uri is cached as file\n\ \ --ignore-syntax-error do not halt in case of syntax error\n\ \ --pcl output PCL code\n\ \ --strings output log:outputString objects\n\ \ --warn output warning info\n\ \ --debug output debug info\n\ \ --profile output profile info\n\ \ --version show version info\n\ \ --help show help info\n\ \\n\ \ n3 facts and rules\n\ \\n\ \ --query output filtered with filter rules\n\ \ --pass output deductive closure\n\ \ --pass-all output deductive closure plus rules'). :- use_module(library(lists)). :- use_module(library(gensym)). :- use_module(library(system)). :- use_module(library(terms)). :- use_module(library('url.pl')). :- use_module(library(charsio)). :- if(current_prolog_flag(dialect,swi)). :- use_module(library(when),[when/2]). :- endif. :- if(\+current_predicate(date_time_stamp/2)). :- load_foreign_files(['pl-tai'],[],install). :- endif. :- if(current_predicate(set_stream/2)). :- initialization(set_stream(user_output,encoding(utf8))). :- else. :- set_prolog_flag(encoding,utf8). :- endif. :- set_prolog_flag(float_format,'%.16g'). :- dynamic(answer/1). :- dynamic(answers/2). :- dynamic(base_uri/1). :- dynamic(bcnd/2). :- dynamic(bgot/3). :- dynamic(brake/0). :- dynamic(branch/0). :- dynamic(bref/2). :- dynamic(bstep/3). :- dynamic(bvar/1). :- dynamic(evar/3). :- dynamic(fact/1). :- dynamic(false/1). :- dynamic(fd/2). :- dynamic(flag/1). :- dynamic(fm/1). :- dynamic(fs/1). :- dynamic(goal/0). :- dynamic(got_dq/0). :- dynamic(graph/1). :- dynamic(hash_value/2). :- dynamic(intern/1). :- dynamic(keywords/1). :- dynamic(mtime/2). :- dynamic(nodepth/0). :- dynamic(ns/2). :- dynamic(pcl/3). :- dynamic(pfx/2). :- dynamic(pi/3). :- dynamic(possibles/1). :- dynamic(qevar/3). :- dynamic(quvar/3). :- dynamic(semantics/1). :- dynamic(span/1). :- dynamic(steps/5). :- dynamic(table/2). :- dynamic(tmpfile/1). :- dynamic(tuple/2). :- dynamic(wcache/2). :- dynamic(wpfx/1). :- dynamic(wtcache/2). :- dynamic(''/2). :- dynamic(''/2). :- dynamic(''/2). :- dynamic(''/2). :- dynamic(''/2). :- dynamic(''/2). :- dynamic(''/2). :- dynamic(''/2). :- dynamic(''/2). :- dynamic(''/2). :- dynamic(''/2). :- dynamic(''/2). main :- version_info(V), sub_atom(V,1,_,2,Version), format(user_error,'~w~n',[Version]), flush_output(user_error), prolog_flag(version,PVersion), format(user_error,'~w~n',[PVersion]), flush_output(user_error), statistics(runtime,[T0,_]), statistics(walltime,[T1,_]), format(user_error,'starting ~w [msec cputime] ~w [msec walltime]~n',[T0,T1]), flush_output(user_error), nb_setval(indentation,0), nb_setval(possibles,not_started), nb_setval(scope,[]), nb_setval(limit,-1), nb_setval(bnet,not_done), nb_setval(fnet,not_done), nb_setval(table,-1), nb_setval(tuple,-1), nb_setval(fdepth,0), nb_setval(fvar,evar), current_prolog_flag(argv,Argv), ( append(_,['--'|Argu],Argv) -> true ; Argu = Argv ), ( Argu = ['--wget-path',Path|Args] -> assertz(flag('wget-path'(Path))) ; Args = Argu ), ( ( Args = [] ; Args = ['--swipl'] ) -> args(['--help']) ; opts(Args), args(Args) ), ( flag('no-blank') -> assertz(flag('no-qvars')) ; true ), findall([Dlen,Dsort,pcl(Prem,dn(D),Src)], ( retract(pcl(Prem,dn(D),Src)), length(D,Dlen), cflat([Prem|D],Df), labelvars(Df,0,_), sort(Df,Dsort) ), E ), sort(E,F), forall( ( member([_,_,G],F) ), ( assertz(G) ) ), ( \+pcl(answer(_),goal,'<>') -> assertz(pcl(answer(_),goal,'<>')) ; ( \+flag('quick-answer') -> retract(pcl(answer(_),goal,'<>')), assertz(pcl(answer(_),goal,'<>')) ; true ) ), ( flag(profile) -> ( current_predicate(profon/0) -> yap_flag(profiling,on), profinit, profon ; true ) ; true ), statistics(runtime,[T2,_]), statistics(walltime,[T3,_]), T20 is T2-T0, T31 is T3-T1, format(user_error,'networking ~w [msec cputime] ~w [msec walltime]~n',[T20,T31]), flush_output(user_error), ( flag(pcl) -> halt ; true ), ( flag(strings) -> true ; format('#Processed by ~w~n',[Version]), ( flag(kgb) -> true ; format('#eye',[]), forall( ( member(Arg,Args) ), ( format(' ~w',[Arg]) ) ), nl ), nl ), ( pfx('var:',_) -> true ; assertz(pfx('var:','')) ), ( pfx('e:',_) -> true ; assertz(pfx('e:','')) ), ( pfx('r:',_) -> true ; assertz(pfx('r:','')) ), ( pfx('n3:',_) -> true ; assertz(pfx('n3:','')) ), nb_setval(tc,0), nb_setval(tp,0), nb_setval(bc,0), nb_setval(bp,0), nb_setval(wn,0), nb_setval(pm,0), nb_setval(cm,0), nb_setval(fm,0), catch(yasam(0),Exc, ( format(user_error,'** ERROR ** yasam ** ~w~n',[Exc]), flush_output(user_error) ) ), nb_getval(tc,TC), nb_getval(tp,TP), nb_getval(bc,BC), nb_getval(bp,BP), nb_getval(wn,_), nb_getval(pm,PM), nb_getval(cm,CM), nb_getval(fm,FM), TM is PM+CM, AM is PM+CM+FM, ( AM > 0, Ev is TM/AM, Ap is PM/AM -> ( flag(strings) -> true ; write('[ '), ( TM > 0, Ind is PM/TM -> wp(''), write(' '), write(Ind), write('; ') ; true ), wp(''), write(' '), write(Ev), write('; '), wp(''), write(' '), write(Ap), write('; '), wp(''), write(' '), write(PM), write('; '), wp(''), write(' '), write(CM), write('; '), wp(''), write(' '), write(FM), write('; '), wp(''), write(' '), write(AM), write('].'), nl, nl ) ; true ), format(user_error,'TC=~w TP=~w BC=~w BP=~w PM=~w CM=~w FM=~w AM=~w~n',[TC,TP,BC,BP,PM,CM,FM,AM]), flush_output(user_error), statistics(runtime,[T4,_]), statistics(walltime,[T5,_]), T42 is T4-T2, T53 is T5-T3, format(user_error,'reasoning ~w [msec cputime] ~w [msec walltime]~n~n',[T42,T53]), flush_output(user_error), ( flag(strings) -> findall([Key,Str], ( ''(Key,Str) ), KS ), sort(KS,KT), forall( ( member([_,MT],KT), ( MT = literal(LT,_) -> true ; atomic(MT), ( number(MT) -> number_codes(MT,LT) ; atom_codes(MT,LT) ) ) ), ( atom_codes(ST,LT), wt(ST) ) ) ; format('#ENDS ~w [msec] TC=~w TP=~w BC=~w BP=~w PM=~w CM=~w FM=~w AM=~w~n',[T42,TC,TP,BC,BP,PM,CM,FM,AM]), nl ), ( flag(profile) -> findall([Cnt,Rule], ( table(RuleName,Rule), nb_getval(RuleName,Cnt) ), CntRl ), sort(CntRl,CntRs), reverse(CntRs,CntRr), format(user_error,'>>> P histogram <<<~n',[]), forall( ( member([Cnt,Rule],CntRr) ), ( format(user_error,'P=~w for rule ~w~n',[Cnt,Rule]) ) ), format(user_error,'~n',[]), ( current_predicate(profon/0) -> profoff, showprofres, format(user_error,'~n',[]) ; true ), statistics ; true ), halt. opts([]) :- !. opts(['--wcache',Arg,File|T]) :- !, ( wcache(Arg,_) -> true ; assertz(wcache(Arg,File)) ), opts(T). opts([_|T]) :- opts(T). args([]) :- !. args(['--trules',Arg|T]) :- !, n3_pcl(Arg,trules), atomic_list_concat(['<',Arg,'>'],R), nb_getval(scope,S), nb_setval(scope,[R|S]), args(T). args(['--tquery',Arg|T]) :- !, assertz(flag(tquery)), n3_pcl(Arg,tquery), assertz(pcl(answer(_),goal,'<>')), args(T). args(['--query',Arg|T]) :- !, n3_pcl(Arg,query), assertz(pcl(answer(_),goal,'<>')), args(T). args(['--pass'|T]) :- !, assertz(pcl(varpred(S,P,O),answer(varpred(S,P,O)),'<>')), assertz(pcl(answer(_),goal,'<>')), args(T). args(['--pass-all'|T]) :- !, assertz(pcl(varpred(S,P,O),answer(varpred(S,P,O)),'<>')), assertz(pcl(''(S,O),answer(''(S,O)),'<>')), assertz(pcl(answer(_),goal,'<>')), args(T). args(['--version'|_]) :- !, halt. args(['--help'|_]) :- !, help_info(Help), format(user_error,'~w~n~n',[Help]), flush_output(user_error), halt. args(['--step',_|T]) :- !, args(T). args(['--quick'|T]) :- !, assertz(flag('quick-false')), args(T). args(['--plugin',Arg|T]) :- sub_atom(Arg,_,14,0,'rif-plugin.yap'), !, args(T). args(['--plugin',Arg|T]) :- !, ( wcache(Arg,File) -> format(user_error,'GET ~w FROM ~w~n',[Arg,File]) ; format(user_error,'GET ~w~n',[Arg]) ), flush_output(user_error), ( wcache(Arg,File) -> true ; ( ( sub_atom(Arg,0,5,_,'http:') -> true ; sub_atom(Arg,0,6,_,'https:') ) -> tmp_file('eye',File), assertz(tmpfile(File)), ( flag('wget-path'(Path)) -> true ; Path = '' ), atomic_list_concat([Path,'wget --header="Cache-Control: max-age=3600" -q "',Arg,'" -O ',File],Cmd), catch(exec(Cmd,_),Exc, ( format(user_error,'** ERROR ** ~w ** ~w~n',[Arg,Exc]), flush_output(user_error), ( retract(tmpfile(File)) -> delete_file(File) ; true ), halt ) ) ; ( sub_atom(Arg,0,5,_,'file:') -> parse_url(Arg,Parts), memberchk(path(File),Parts) ; File = Arg ) ) ), catch(consult(File),Exc, ( format(user_error,'** ERROR ** ~w ** ~w~n',[Arg,Exc]), flush_output(user_error), ( retract(tmpfile(File)) -> delete_file(File) ; true ), halt ) ), ( retract(tmpfile(File)) -> delete_file(File) ; true ), args(T). args(['--wcache',Arg,File|T]) :- !, ( wcache(Arg,_) -> true ; assertz(wcache(Arg,File)) ), args(T). args([Arg|T]) :- sub_atom(Arg,0,2,_,'--'), !, sub_atom(Arg,2,_,0,Opt), assertz(flag(Opt)), args(T). args([Arg|T]) :- n3_pcl(Arg,data), args(T), atomic_list_concat(['<',Arg,'>'],R), nb_getval(scope,S), nb_setval(scope,[R|S]). % ------------------ % N3 to PCL compiler % ------------------ n3_pcl(Argument,Mode) :- nb_setval(cmod,Mode), resolve_slash(Argument,Arg), tmp_file('eye',Tmp), ( flag('ignore-syntax-error') -> Ise = 'IGNORED' ; Ise = 'ERROR' ), ( wcache(Arg,File) -> true ; ( ( sub_atom(Arg,0,5,_,'http:') -> true ; sub_atom(Arg,0,6,_,'https:') ) -> File = Tmp, assertz(tmpfile(File)), ( flag('wget-path'(Path)) -> true ; Path = '' ), atomic_list_concat([Path,'wget --header="Accept: text/*" --header="Cache-Control: max-age=3600" -q "',Arg,'" -O ',File],Cmd), catch(exec(Cmd,_),Exc, ( format(user_error,'** ERROR ** ~w ** ~w~n',[Arg,Exc]), flush_output(user_error), ( retract(tmpfile(File)) -> delete_file(File) ; true ), halt ) ) ; ( sub_atom(Arg,0,5,_,'file:') -> parse_url(Arg,Parts), memberchk(path(File),Parts) ; File = Arg ) ) ), open(File,read,In,[encoding(utf8)]), nb_setval(line_number,1), catch(turtle_tokens(In,Tokens),Exc, ( format(user_error,'** ~w ** ~w ** ~w~n',[Ise,Arg,Exc]), flush_output(user_error), fail ) ), ( flag(debug) -> format(user_error,'>>> ~w tokens <<<~n~w~n~n',[Arg,Tokens]), flush_output(user_error) ; true ), close(In), ( retract(tmpfile(File)) -> delete_file(File) ; true ), ( flag(profile) -> statistics(runtime,[_,T0]), statistics(walltime,[_,T1]), format(user_error,'tokenizing ~w [msec cputime] ~w [msec walltime]~n',[T0,T1]) ; true ), retractall(base_uri(_)), assertz(base_uri(Arg)), retractall(ns(_,_)), atomic_list_concat([Arg,'#'],D), assertz(ns('',D)), retractall(keywords(_)), retractall(quvar(_,_,_)), retractall(qevar(_,_,_)), retractall(evar(_,_,_)), catch(phrase(document(Triples),Tokens),Exc, ( format(user_error,'** ~w ** ~w ** ~w~n',[Ise,Arg,Exc]), flush_output(user_error), fail ) ), ( flag(debug) -> format(user_error,'>>> ~w triples <<<~n~w~n~n',[Arg,Triples]), flush_output(user_error) ; true ), ( flag(profile) -> statistics(runtime,[_,T2]), statistics(walltime,[_,T3]), format(user_error,'parsing ~w [msec cputime] ~w [msec walltime]~n',[T2,T3]) ; true ), ( nb_getval(cmod,data) -> atomic_list_concat(['<',Arg,'>'],Src) ; atomic_list_concat(['\'<',Arg,'>\''],Src) ), assertz(tmpfile(Tmp)), nb_setval(sc,0), ( Mode = semantics -> open(Tmp,write,Ws,[encoding(utf8)]), tell(Ws), forall( ( member(Triple,Triples) ), ( write(graph(Triple,Src)), writeln('.') ) ), told, open(Tmp,read,Rs,[encoding(utf8)]), repeat, read(Rs,Rt), ( Rt = end_of_file -> true ; assertz(Rt), cnt(sc), fail ), close(Rs), ( retract(tmpfile(Tmp)) -> delete_file(Tmp) ; true ) ; ( flag(debug) -> format(user_error,'>>> ~w pcl code <<<~n',[Arg]), flush_output(user_error) ; true ), ( nb_getval(cmod,data) -> n3_pcl(Triples,Src,Mode) ; open(Tmp,write,Ws,[encoding(utf8)]), tell(Ws), n3_pcl(Triples,Src,Mode), told, open(Tmp,read,Rs,[encoding(utf8)]), repeat, read(Rs,Rt), ( Rt = end_of_file -> true ; ( ground(Rt) -> term_hash(Rt,Rh), ( hash_value(Rh,Rt) -> true ; assertz(hash_value(Rh,Rt)) ), ( call(Rt) -> true ; assertz(Rt), cnt(sc), ( flag(pcl) -> format('~q.~n',[Rt]) ; true ), ( flag(debug) -> format(user_error,'~w~n',[Rt]), flush_output(user_error) ; true ) ) ; ( Rt = steps(Ct,Pt,It,Mt,St) -> ( nonvar(It) -> copy_term(It,Ic) ; Ic = It ), ( steps(Ct,Pt,_,_,St) -> true ; assertz(steps(Ct,Pt,Ic,Mt,St)), ( flag(pcl) -> format('~q.~n',[steps(Ct,Pt,Ic,Mt,St)]) ; true ), ( flag(debug) -> format(user_error,'~w~n',[steps(Ct,Pt,Ic,Mt,St)]), flush_output(user_error) ; true ) ) ; ( Rt = ':-'(Ci,Pi) -> ( Ci = true -> call(Pi) ; atomic_list_concat(['<',Arg,'>'],Si), copy_term(''(Pi,Ci),Ri), cn_conj(Pi,Pn), ( flag(nope) -> Pj = Pn ; ( Pi = when(Ai,Bi) -> c_append(Bi,istep(Si,Pi,Ci,Ri),Bj), Pj = when(Ai,Bj) ; c_append(Pn,istep(Si,Pi,Ci,Ri),Pj) ) ), assertz(':-'(Ci,Pj)), cnt(sc), ( flag(pcl) -> format('~q.~n',[':-'(Ci,Pj)]) ; true ), ( flag(debug) -> format(user_error,'~w~n',[':-'(Ci,Pj)]), flush_output(user_error) ; true ) ) ; assertz(Rt), cnt(sc), ( flag(pcl) -> format('~q.~n',[Rt]) ; true ), ( flag(debug) -> format(user_error,'~w~n',[Rt]), flush_output(user_error) ; true ) ) ) ), fail ), retractall(hash_value(_,_)), close(Rs), ( retract(tmpfile(Tmp)) -> delete_file(Tmp) ; true ), ( flag(debug) -> format(user_error,'~n',[]), flush_output(user_error) ; true ) ) ), ( flag(profile) -> statistics(runtime,[_,T4]), statistics(walltime,[_,T5]), format(user_error,'compiling ~w [msec cputime] ~w [msec walltime]~n',[T4,T5]) ; true ), nb_getval(sc,SC), ( wcache(Arg,File) -> format(user_error,'GET ~w FROM ~w SC=~w~n',[Arg,File,SC]) ; format(user_error,'GET ~w SC=~w~n',[Arg,SC]) ), flush_output(user_error), !. n3_pcl(_,_) :- ( retract(tmpfile(File)) -> delete_file(File) ; true ), ( flag('ignore-syntax-error') -> true ; nl, halt ). n3_pcl([],_,_) :- !. n3_pcl(['\'\''(X,Y)|Z],Src,trules) :- !, ( clast(X,'\'\''(_,T)) -> true ; T = 1.0 ), clist(L,X), tr_pcl(L,K,M), clist(K,N), write(pcl(N,'\'\''([Y|M],T),Src)), writeln('.'), n3_pcl(Z,Src,trules). n3_pcl(['\'\''(X,Y)|Z],Src,tquery) :- !, clist(L,X), tr_pcl(L,K,M), append(K,['\'\''([Y|M],T)],J), clist(J,N), write(pcl(N,answer('\'\''([Y|M],T)),Src)), writeln('.'), n3_pcl(Z,Src,tquery). n3_pcl(['\'\''(X,Y)|Z],Src,query) :- !, write(pcl(X,answer(Y),Src)), writeln('.'), n3_pcl(Z,Src,query). n3_pcl([X|Z],Src,query) :- !, write(pcl(true,answer(X),Src)), writeln('.'), n3_pcl(Z,Src,query). n3_pcl(['\'\''(X,Y)|Z],Src,Mode) :- !, write(pcl(X,Y,Src)), writeln('.'), n3_pcl(Z,Src,Mode). n3_pcl([':-'(Conc,Prem)|Z],Src,Mode) :- !, write(':-'(Conc,Prem)), writeln('.'), n3_pcl(Z,Src,Mode). n3_pcl([X|Z],Src,Mode) :- ( nb_getval(cmod,data) -> ( call(X) -> true ; assertz(X), cnt(sc), ( flag(pcl) -> format('~q.~n',[X]) ; true ), ( flag(debug) -> format(user_error,'~w~n',[X]), flush_output(user_error) ; true ), assertz(steps(X,true,_,forward,Src)), ( flag(pcl) -> format('~q.~n',[steps(X,true,_,forward,Src)]) ; true ), ( flag(debug) -> format(user_error,'~w~n',[steps(X,true,_,forward,Src)]), flush_output(user_error) ; true ) ) ; write(X), writeln('.'), write(steps(X,true,_,forward,Src)), writeln('.') ), n3_pcl(Z,Src,Mode). tr_pcl([],[],[]) :- !. tr_pcl([A|B],C,[A|D]) :- functor(A,'\'\'',_), !, tr_pcl(B,C,D). tr_pcl([A|B],C,D) :- functor(A,'\'\'',_), !, tr_pcl(B,C,D). tr_pcl([A|B],[A|C],D) :- tr_pcl(B,C,D). % ------------------------------------------- % YASAM (Yet Another Skolem Abstract Machine) % ------------------------------------------- yasam(Span) :- ( ( flag(debug) -> format(user_error,'yasam/1 enter trunk span ~w~n',[Span]), flush_output(user_error) ; true ), pcl(Prem,Conc,Src), Conc \= dn(_), Conc \= goal, Conc \= false, ( flag(nope) -> true ; copy_term(''(Prem,Conc),Rule) ), \+''(''(Prem,Conc),_), ( flag(debug) -> format(user_error,'. yasam/1 selecting rule ~w~n',[pcl(Prem,Conc,Src)]), flush_output(user_error) ; true ), call(Prem), ( Conc = answer(_) -> true ; term_variables(Prem,Pvars), variables_within_term(Pvars,Conc,[]) ), ( flag(profile) -> lookup(RuleName,Rule), cnt(RuleName) ; true ), cnt(tp), ( \+call(Conc) -> cnt(tc) ; ( flag(debug), flag(warn) -> format(user_error,'.. yasam/1 euler path so do not step in your own step ~w~n',[Conc]), flush_output(user_error), fail ) ), nb_getval(wn,W), copy_term(Conc,Cc), labelvars(Conc,W,N), nb_setval(wn,N), ( flag(debug) -> format(user_error,'... yasam/1 assert step ~w~n',[Conc]), flush_output(user_error) ; true ), clist(La,Conc), clist(Lb,Cc), couple(La,Lb,Lc), findall([F,E], ( member([D,E],Lc), unify(D,F), \+call(F) ), Ld ), couple(Le,Lf,Ld), clist(Le,Concl), clist(Lf,Clc), astep(Src,Prem,Concl,Clc,Rule), retract(brake), fail ; brake, ( S is Span+1, assertz(span(S)), nb_getval(limit,Limit), Span < Limit, yasam(S) ; ( flag(strings) -> true ; w3(trunk) ), \+flag('no-branch'), assertz(branch), yasam([],0,[]), ( nb_getval(cm,CM), CM > 0 -> true ; forall( ( possibles(Ans) ), ( wt(Ans), write('.'), nl ) ), ( possibles(_) -> nl ; true ) ) ; true ), ! ; assertz(brake), ( answer(_), flag('quick-answer') -> ( flag(strings) -> true ; w3(trunk) ) ; yasam(Span) ) ). % Coherent Logic inspired by http://www.cs.vu.nl/~diem/research/ht/CL.pl yasam(Grd,Pnum,Env) :- ( flag(debug) -> format(user_error,'yasam/3 enter branch ~w~n',[Env]), flush_output(user_error) ; true ), pcl(Prem,Conc,Src), ( flag(nope) -> true ; copy_term(''(Prem,Conc),Rule) ), ( \+''(''(Prem,Conc),_) -> Gnew = Grd ; ( ''(''(Prem,Conc),[Grd,Gnew]) -> true ; ( flag(debug) -> format(user_error,'. yasam/3 mismatch with current guard ~w for rule ~w~n',[Grd,pcl(Prem,Conc,Src)]), flush_output(user_error), fail ) ) ), ( flag(debug) -> format(user_error,'. yasam/3 selecting rule ~w~n',[pcl(Prem,Conc,Src)]), flush_output(user_error) ; true ), call(Prem), ( Conc = answer(_) -> true ; term_variables(Prem,Pvars), variables_within_term(Pvars,Conc,[]) ), ( flag(profile) -> lookup(RuleName,Rule), cnt(RuleName) ; true ), cnt(bp), ( flag('quick-possible') -> ( \+call(Conc) -> cnt(bc) ; ( flag(debug), flag(warn) -> format(user_error,'.. yasam/3 euler path so do not step in your own step ~w~n',[Conc]), flush_output(user_error), fail ) ) ; true ), ( Conc = false -> \+false(Prem), C = false(Prem) ; C = Conc ), ( ( C = goal -> true ; C = false(_), flag('quick-false') ) -> ances(Env), ( flag(strings) -> true ; end(C,Env) ) ; ( C = dn(D) -> ( flag('quick-possible'), Prem = true -> retract(pcl(Prem,Conc,Src)) ; true ), forall( ( member(E,D) ), ( memo(Src,Prem,Gnew,Pnum,E,[E|Env],Rule) ) ), ( flag('quick-possible'), Prem = true -> assertz(pcl(Prem,Conc,Src)) ; true ) ; memo(Src,Prem,Gnew,Pnum,C,Env,Rule) ) ). memo(Src,Prem,Grd,Pnum,Conc,Env,Rule) :- ( flag('quick-possible') -> true ; ( \+call(Conc) -> cnt(bc) ; ( flag(debug), flag(warn) -> format(user_error,'.. memo/7 euler path so do not step in your own step ~w~n',[Conc]), flush_output(user_error), fail ) ) ), copy_term(Conc,Cc), ( Conc = false(_) -> Pnew = Pnum ; labelvars(Conc,Pnum,Pnew) ), ( flag(debug) -> format(user_error,'... memo/7 assert step ~w~n',[Conc]), flush_output(user_error) ; true ), clist(La,Conc), clist(Lb,Cc), couple(La,Lb,Lc), findall([F,E], ( member([D,E],Lc), unify(D,F), \+call(F) ), Ld ), couple(Le,Lf,Ld), clist(Le,Concl), clist(Lf,Clc), astep(Src,Prem,Concl,Clc,Rule), ( yasam(Grd,Pnew,Env) -> true ; ances(Env), ( flag(strings) -> true ; end(countermodel,Env) ) ), ( flag(debug) -> format(user_error,'... memo/7 retract step ~w~n',[Conc]), flush_output(user_error) ; true ), dstep(Src,Prem,Concl,Rule). istep(Src,Prem,Conc,Rule) :- ( \+steps(Conc,Prem,_,_,Src) -> nb_getval(wn,W), labelvars([Prem,Conc],W,N), nb_setval(wn,N), assertz(steps(Conc,Prem,Rule,backward,Src)), ( branch -> ( \+bstep(Src,Prem,Conc) -> assertz(bstep(Src,Prem,Conc)) ; true ) ; true ) ; true ). astep(A,B,C,Cc,Rule) :- ( C = cn([D|E]), Cc = cn([Dc|Ec]) -> ( flag(think), Dc = ''(Prem,Conc) -> asserta(pcl(Prem,Conc,A)) ; true ), assertz(D), assertz(steps(D,B,Rule,forward,A)), ( E = [F], Ec = [Fc] -> true ; F = cn(E), Fc = cn(Ec) ), astep(A,B,F,Fc,Rule) ; ( C = true -> true ; ( flag(think), Cc = ''(Prem,Conc) -> asserta(pcl(Prem,Conc,A)) ; true ), assertz(C), assertz(steps(C,B,Rule,forward,A)) ) ). dstep(A,B,C,Rule) :- ( C = cn([D|E]) -> ( flag(think), D = ''(Prem,Conc) -> retract(pcl(Prem,Conc,A)) ; true ), retract(D), retract(steps(D,B,_,_,A)), ( E = [F] -> true ; F = cn(E) ), dstep(A,B,F,Rule) ; ( C = true -> true ; ( flag(think), C = ''(Prem,Conc) -> retract(pcl(Prem,Conc,A)) ; true ), retract(C), retract(steps(C,B,_,_,A)) ) ), forall( ( retract(bstep(Sr,Pr,Cn)) ), ( retract(steps(Cn,Pr,_,_,Sr)) ) ). ancestor(A,B) :- steps(D,C,_,_,_), C \= true, D \= false(_), D \= answer(_), unify(B,D), cmember(E,C), ( unify(A,E) ; ancestor(A,E) ). cgives(A,B) :- ( \+steps(B,_,_,_,_), ! ; steps(B,C,_,_,_), \+( ( cmember(D,C), cmember(E,A), ( unify(E,D) ; \+cgives(E,D) ) ) ) ). ances(Env) :- ( flag(ances) -> write('[ '), wp(''), write(' '), clist(Env,G), wg(G), nl, ( steps(answer(_),D,_,_,_), findall(X, ( ancestor(X,D) ), T ), distinct(T,U), clist(U,V), write('; '), wp(''), write(' [ '), wp(''), write(' '), wg(D), nl, write(' ; '), wp(''), write(' '), wg(V), nl, write(' ]'), nl, fail ; write('].'), nl ) ; true ). end(goal,Env) :- \+false(_), !, ( Env = [] -> ( flag(strings) -> true ; w3(knot) ) ; write('[ '), wp(''), write(' '), clist(Env,G), indentation(2), wg(G), nl, write('; '), wp(''), write(' {'), nl, retractall(answers(_,branch)), indentation(1), ( flag(strings) -> true ; w3(branch) ), indentation(-1), indent, write('}'), nl, write('].'), indentation(-2), nl, nl, ( flag('quick-possible') -> true ; ( ''(_,_) -> true ; ( nb_getval(possibles,started) -> forall( ( possibles(Ans) ), ( ( answers(Ans,branch) -> true ; retract(possibles(Ans)) ) ) ) ; nb_setval(possibles,started), forall( ( answers(Ans,branch) ), ( assertz(possibles(Ans)) ) ) ) ) ), cnt(pm) ). end(countermodel,Env) :- \+false(_), !, write('[ '), wp(''), write(' '), clist(Env,G), indentation(2), wg(G), indentation(-2), nl, write('].'), nl, nl, cnt(cm). end(End,Env) :- write('[ '), wp(''), write(' '), clist(Env,G), indentation(2), wg(G), nl, retractall(fd(_,_)), ( ( End = false(F) ; false(F) ), write('; '), wp(''), write(' [ '), wp(''), write(' {'), labelvars(F,0,_,allv), wg(F), write(' '), wp(''), write(' false}'), ( cmember(A,F), nl, write(' ; '), wp(''), write(' [ '), wp(''), write(' '), wg(A), nl, ( flag(quiet) -> true ; findall(X, ( ancestor(X,A) ), L ), distinct(L,Ls), clist(Ls,U), findall(X, ( ancestor(A,X) ), M ), distinct(M,Ms), clist(Ms,V), findall(X, ( false(Y), cmember(X,Y) ), I ), distinct(I,Is), clist(Is,Q), findall(X, ( cmember(X,U), cmember(Y,Q), unify(X,Y) ), J ), distinct(J,Js), clist(Js,R), write(' ; '), wp(''), write(' '), indentation(4), wg(R), nl, findall(X, ( cmember(X,V), cmember(Y,Q), unify(X,Y) ), K ), distinct(K,Ks), clist(Ks,S), write(' ; '), wp(''), write(' '), wg(S), nl, findall(X, ( ancestor(X,A), steps(Y,true,_,_,_), unify(X,Y) ), T ), distinct(T,Ts), clist(Ts,Ua), write(' ; '), wp(''), write(' [ '), wp(''), write(' '), wg(Ua), nl, ( cmember(Ca,Ua), write(' ; '), wp(''), write(' [ '), wp(''), write(' '), wg(Ca), nl, findall(X, ( ancestor(Ca,X) ), D ), distinct(D,Ds), clist(Ds,Va), write(' ; '), wp(''), write(' '), indentation(4), wg(Va), indentation(-4), nl, write(' ]'), nl, fail ; true ), write(' ]'), nl, ( fd(A,_) -> true ; ''([R,S],Fd), assertz(fd(A,Fd)) ), ( flag(think) -> findall(X, ( steps(X,_,_,_,_), X \= false(_), X \= answer(_), \+unify(X,A), cgives(A,X) ), N ), distinct(N,Ns), clist(Ns,W), write(' ; '), wp(''), write(' '), wg(W), nl ; true ), indentation(-4) ), write(' ]'), fail ; true ), nl, write(' ]'), nl, fail ; true ), ( flag(think) -> findall(X, ( steps(X,_,_,_,_), X \= false(_), X \= answer(_), forall( ( false(H), cmember(B,H) ), ( \+unify(X,B), cgives(B,X) ) ) ), O ), distinct(O,Os), clist(Os,Z), write('; '), wp(''), write(' '), wg(Z), nl ; true ), ( flag('quick-false') -> true ; findall(X, ( false(X) ), Ir ), ''(Ir,It), findall([X,Y], ( cmember(Y,It), findall(1, ( false(Ig), cmember(Y,Ig) ), Im ), length(Im,X) ), In ), sort(In,Io), findall(X, ( member([_,X],Io) ), Ip ), reverse(Ip,Iq), write('; '), wp(''), write(' '), wt(Iq), nl, ( flag(quiet) -> true ; findall([X,Y], ( cmember(Y,It), fd(Y,Ig), clist(Ih,Ig), length(Ih,X) ), Iu ), sort(Iu,Iv), findall(X, ( member([_,X],Iv) ), Iw ), write('; '), wp(''), write(' '), wt(Iw), nl, findall([X,Y], ( cmember(Y,It), findall(1, ( false(Ig), cmember(Y,Ig) ), Im ), length(Im,Xa), fd(Y,Jg), clist(Jh,Jg), length(Jh,Xb), X is Xb-Xa ), Ju ), sort(Ju,Jv), findall(X, ( member([_,X],Jv) ), Jw ), write('; '), wp(''), write(' '), wt(Jw), nl ), write('; '), wp(''), write(' {'), nl, retractall(answers(_,branch)), indentation(1), ( flag(strings) -> true ; w3(branch) ), indentation(-1), indent, write('}') ), write('].'), indentation(-2), nl, nl, ( Env = [] -> throw(empty_false_model) ; true ), cnt(fm). % ------------ % proof output % ------------ w3(U) :- ( flag('no-qnames') -> true ; nb_setval(wpfx,false), forall( ( pfx(A,B), \+wpfx(A) ), ( format('@prefix ~w ~w.~n',[A,B]), assertz(wpfx(A)), nb_setval(wpfx,true) ) ), ( nb_getval(wpfx,true) -> nl ; true ) ), flag(nope), !, ( steps(answer(C),_,_,_,_), \+answers(C,_), assertz(answers(C,U)), indent, wt(C), ws(C), write('.'), nl, fail ; ( U = branch -> true ; nl ) ). w3(U):- ( steps(answer(X),_,_,_,_), \+answers(X,_), !, write('[ '), wp(''), write(' '), wp(''), write(', '), wp(''), write(';'), indentation(1), nl, indent, ( steps(answer(C),B,R,_,A), R =.. [P,S,answer(O)], Rule =.. [P,S,O], \+answers(C,_), assertz(answers(C,U)), wp(''), write(' '), wi(A,B,C,Rule), write(';'), nl, nl, indent, fail ; true ), wp(''), write(' {'), indentation(1), ( answers(C,U), nl, indent, getvars(C,D), ( C = ''(_,_) -> Q = allv ; Q = some ), wq(D,Q), wt(C), ws(C), write('.'), fail ; true ), indentation(-1), nl, indent, write('}].'), indentation(-1), nl, nl ; true ). wi(A,true,C,_) :- !, write('[ '), wp(''), write(' '), wp(''), write('; '), wp(''), write(' {'), labelvars([A,C],0,_,avar), getvars(C,D), ( C = ''(_,_) -> Q = allv ; Q = some ), wq(D,Q), wt(C), write('};'), nl, indentation(1), indent, wp(''), write(' [ '), wp(''), write(' '), wp(''), write('; '), wp(''), write(' '), wt(A), write(']]'), indentation(-1). wi(A,B,C,Rule) :- ( Rule = ''(Prem,Conc), unifiable(Prem,B,Bs), sort(Bs,Bindings), term_variables(Prem,PVars), term_variables(Conc,CVars), labelvars([A,B,C],0,_,some), labelvars([Rule,PVars,CVars],0,_,avar), findall(V, ( member(V,CVars), \+member(V,PVars) ), EVars ), write('[ '), wp(''), write(' '), wp(''), write('; '), wp(''), write(' {'), getvars(C,D), ( C = ''(_,_) -> Q = allv ; Q = some ), wq(D,Q), wt(C), write('}; '), wp(''), write(' ('), indentation(1), wr(B), write(');'), nl, indent, wb(Bindings), wp(''), write(' [ '), wp(''), write(' '), wp(''), write('; '), wp(''), write(' {'), wq(PVars,allv), wq(EVars,some), wt(Rule), write('};'), nl, indentation(1), indent, wp(''), write(' [ '), wp(''), write(' '), wp(''), write('; '), wp(''), write(' '), wt(A), write(']]]'), indentation(-2), fail ; true ). wr(varpred(S,P,O)) :- !, U =.. [P,S,O], wr(U). wr(cn([X])) :- !, wr(X). wr(cn([X|Y])) :- !, wr(X), ( Y = [Z] -> true ; Z = cn(Y) ), wr(Z). wr(Z) :- steps(Z,Y,Rule,_,X), !, nl, indent, wi(X,Y,Z,Rule). wr(Y) :- nl, indent, write('[ '), wp(''), write(' '), wp(''), write('; '), wp(''), write(' {'), labelvars(Y,0,_,avar), getvars(Y,Z), wq(Z,some), wt(Y), write('}]'). wt(X) :- number(X), !, write(X). wt(cn([X])) :- !, wt(X). wt(cn([X|Y])) :- !, wt(X), ws(X), write('.'), ( flag(nope) -> write(' ') ; nl, indent ), ( Y = [Z] -> true ; Z = cn(Y) ), wt(Z). wt(dn(X)) :- !, wt(X), write('!'), wp(''). wt([]) :- !, write('()'). wt([X|Y]) :- !, ( Y = avar(_) -> write('[ '), wt(''), write(' '), wg(X), write('; '), wt(''), write(' '), wt(Y), write(']') ; write('('), wg(X), wl(Y), write(')') ). wt(X) :- functor(X,_,A), ( A = 0, !, wt0(X) ; A = 1, !, wt1(X) ; A = 2, !, wt2(X) ; wtn(X) ). wt0(!) :- !, write('() '), wp(!), write(' true'). wt0(X) :- flag(nope), \+flag('no-qvars'), sub_atom(X,0,22,_,' write('?') ; write('_:') ), write(Y). wt0(X) :- ( wtcache(X,W) -> true ; ( \+flag('no-qnames'), atom(X), sub_atom(X,I,1,J,'#'), J > 1, sub_atom(X,0,I,_,C), atom_concat(C,'#>',D), pfx(E,D), K is J-1, sub_atom(X,_,K,1,F), atom_codes(F,G), regex("^[A-Z_a-z][\\-0-9A-Z_a-z]*$",G,_) -> atom_concat(E,F,W) ; ( \+flag(strings), atom(X), \+sub_atom(X,0,1,_,'<'), X \= true, X \= false -> atom_codes(X,Y), W = literal(Y,type('')) ; W = X ) ), assertz(wtcache(X,W)) ), ( W = literal(Y,type('')) -> wt2(W) ; write(W) ). wt1(some(X)) :- !, ( \+flag('no-qvars') -> write('_:sk'), write(X) ; atom_number(Y,X), atomic_list_concat([''],Z), wt(Z) ). wt1(allv(X)) :- !, ( \+flag('no-qvars') -> write('?U'), write(X) ; atom_number(Y,X), atomic_list_concat([''],Z), wt(Z) ). wt1(avar(X)) :- !, atom_number(Y,X), atomic_list_concat([''],Z), wt(Z). wt1(semantics(X)) :- !, findall(T, ( graph(T,X) ), L ), clist(L,Y), labelvars(Y,0,_,some), wg(Y). wt1(X) :- X =.. [B|C], wt(C), write(' '), wp(B), write(' true'). wt2(literal(X,lang(Y))) :- !, escape_string(X,Z), atom_codes(A,Z), write('"'), write(A), write('"@'), write(Y). wt2(literal(X,type(Y))) :- !, escape_string(X,Z), atom_codes(A,Z), write('"'), write(A), write('"^^'), wt(Y). wt2(literal(X,void)) :- !, escape_string(X,Z), atom_codes(A,Z), write('"'), write(A), write('"'). wt2(''([X|Y],Z)) :- flag(nope), flag(tquery), !, ''(Y,U), write('{'), wt(U), write('. _: '), wp(''), write(' '), wt(Z), write('} '), wp(''), write(' {'), wt(X), write('}'). wt2(''([X|Y],Z)) :- flag(nope), flag(tquery), !, ''(Y,U), write('{'), wt(U), write('. _: '), wp(''), write(' '), wt(Z), write('} '), wp(''), write(' {'), wt(X), write('}'). wt2(''(X,Y)) :- !, nb_setval(fvar,uvar), ( catch(clause(Y,X),_,fail) -> wg(Y), write(' <= '), wg(X) ; wg(X), write(' => '), wg(Y) ), nb_setval(fvar,evar). wt2(is(O,T)) :- !, ( number(T), T < 0 -> P = -, Q is -T, S = [Q] ; T =.. [P|S] ), wg(S), write(' '), wp(P), write(' '), wg(O). wt2(prolog:X) :- !, ( X = '\'C\'' -> Y = 'C' ; ( X = '\';\'' -> Y = disjunction ; prolog_sym(Y,X,_) ) ), atomic_list_concat([''],Z), wt0(Z). wt2(X) :- X =.. [P,S,O], ( prolog_sym(_,P,_) -> wt([S,O]), write(' '), wp(P), write(' true') ; wg(S), write(' '), wp(P), write(' '), wg(O) ). wtn(varpred(S,P,O)) :- !, wg(S), write(' '), wp(P), write(' '), wg(O). wtn(X) :- X =.. [B|C], ( atom(B), \+sub_atom(B,0,1,_,'<'), \+prolog_sym(_,B,_), X \= true, X \= false -> wt([B|C]), write('^'), wp('') ; wt(C), write(' '), wp(B), write(' true') ). wg(X) :- functor(X,F,A), ( ( F = varpred, ! ; F = cn, ! ; prolog_sym(_,F,_), F \= true, F \= false, F \= '-', ! ; A = 2, F \= '.', F \= ':', F \= literal ) -> write('{'), indentation(1), nb_getval(fdepth,D), E is D+1, nb_setval(fdepth,E), wt(X), nb_setval(fdepth,D), indentation(-1), write('}') ; wt(X) ). wp('') :- \+flag('no-qnames'), !, write('a'). wp('') :- \+flag('no-qnames'), !, write('=>'). wp(X) :- ( prolog_sym(Y,X,_) -> atomic_list_concat([''],Z), wt(Z) ; wt(X) ). wk([]) :- !. wk([X|Y]) :- write(', '), wt(X), wk(Y). wl([]) :- !. wl([X|Y]) :- write(' '), wg(X), wl(Y). wq([],_) :- !. wq([X|Y],allv) :- !, write('@forAll '), wt(X), wk(Y), write('. '). wq([X|Y],some) :- ( \+flag('no-qvars') -> write('@forSome '), wt(X), wk(Y), write('. ') ; true ). wb([]) :- !. wb([X = Y|Z]) :- wp(''), write(' [ '), wp(''), write(' '), wv(X), write('; '), wp(''), write(' '), wv(Y), write('];'), nl, indent, wb(Z). wv(avar(I)) :- !, write('[ '), wp(''), write(' "http://localhost/var#x'), write(I), write('"]'). wv(some(I)) :- !, write('[ '), wp(''), write(' '), wp(''), write('; '), wp(''), write(' "_:sk'), write(I), write('"]'). wv(X) :- atom(X), sub_atom(X,0,22,_,''), write(' '), wp(''), write('; '), wp(''), write(' "http://localhost/var#'), sub_atom(X,22,_,1,Q), write(Q), write('"]'). wv(X) :- atom(X), sub_atom(X,1,_,1,U), atomic_list_concat(['<',U,'>'],X), !, write('[ '), wp(''), write(' "'), write(U), write('"]'). wv(X) :- wg(X). ws(cn(X)) :- !, last(X,Y), ws(Y). ws(X) :- X =.. Y, ( flag(tquery) -> true ; last(Y,Z), ( number(Z) -> write(' ') ; true ) ). indent:- nb_getval(indentation,A), tab(A). indentation(C) :- nb_getval(indentation,A), B is A+C, nb_setval(indentation,B). % -------- % builtins % -------- ''(A,B) :- within_scope(_), steps(C,D,_,_,_), unify(A,C), ( D = true -> B = true ; findall(X, ( ancestor(X,A) ), L ), clist(L,B) ). ''(A,B) :- within_scope(_), steps(C,D,_,_,_), unify(A,C), ( D = true -> B = true ; findall(X, ( ancestor(X,A), steps(Y,true,_,_,_), unify(X,Y) ), L ), clist(L,B) ). ''(A,B) :- steps(B,true,_,_,_), unify(A,B). ''(A,B) :- within_scope(_), steps(C,_,_,_,_), unify(A,C), findall(X, ( ancestor(A,X) ), L ), clist(L,B). ''([''(A,B)|C],D) :- within_scope(_), ( nb_getval(bnet,done) -> true ; bnet, nb_setval(bnet,done) ), bvar(A), bval(B), bcon([''(A,B)],C,D). ''(A,B) :- getnumber(A,C), ( C =:= 0.0 -> B is 0.0 ; ( C =:= 1.0 -> B is 0.0 ; B is -(C*log(C)+(1-C)*log(1-C))/log(2) ) ). ''(Sc,A) :- within_scope(Sc), call(A). ''(A,B) :- findall(C, ( cartesian(A,C) ), B ). ''(Sc,A) :- within_scope(Sc), steps(A,_,_,_,_). ''(A,B) :- when( ( nonvar(A) ), ( distinct(A,B) ) ). ''(Sc,[A,B,C]) :- within_scope(Sc), \+is_list(B), findall(A, B, E ), ( flag(warn) -> copy_term([A,B,E],[Ac,Bc,Ec]), labelvars([Ac,Bc,Ec],0,_), ( fact(''(Sc,[Ac,Bc,F])) -> ( E \= F -> format(user_error,'** WARNING ** conflicting_findall_answers ~w VERSUS ~w~n',[[A,B,F],[A,B,E]]) ; true ) ; assertz(fact(''(Sc,[Ac,Bc,Ec]))) ) ; true ), E = C. ''([literal(X,void)|Y],literal(Z,void)) :- when( ( ground([X,Y]) ), ( preformat(Y,F), format_to_chars(X,F,Z) ) ). ''(X,Y) :- nonvar(X), difference(X,Y). ''(X,Y) :- nonvar(X), intersection(X,Y). ''(A,B) :- clistflat(B,A). ''(A,B) :- when( ( nonvar(A) ), ( ( atom(A) -> sub_atom(A,0,22,_,''(A,B) :- when( ( nonvar(A) ), ( length(A,B) ) ). ''(A,B) :- when( ( nonvar(A) ), ( bmax(A,B) ) ). ''(A,B) :- when( ( nonvar(A) ), ( bmin(A,B) ) ). ''(A,B) :- when( ( nonvar(A) ), ( \+''(A,B) ) ). ''(A,B) :- when( ( nonvar(A) ), ( getnumber(A,B) ) ). ''(Sc,A) :- within_scope(Sc), optional(A). ''(A,[B,C]) :- ( ''(A,[B,C]) ; ''(A,[C,B]) ). ''([A],[B,C]) :- !, D =.. [A,B,C], call(D). ''([A|B],[C,D]) :- E =.. [A,C,F], call(E), ''(B,[F,D]). ''(A,B) :- reverse(A,B). ''(St,[Sen,Asp]) :- getnumber(St,K), ( getnumber(Sen,S) -> Asp is 1-(1-exp(-K*(S-1)))*(1+exp(K))/(1+exp(-K*(S-1)))/(1-exp(K)) ; getnumber(Asp,A), Sen is (1-exp(-K*A))*(1+exp(-K))/(1+exp(-K*A))/(1-exp(-K)) ). ''(A,B) :- getnumber(A,C), B is 1/(1+exp(-C)). ''(A,B) :- when( ( nonvar(A) ), ( quicksort(A,B) ) ). ''(literal(X,Y),literal(Z,Y)) :- when( ( ground(X) ), ( escape_string(X,Z) ) ). ''(A,B) :- when( ( nonvar(A) ), ( append(C,_,A), append(_,B,C) ) ). ''(X,Y) :- ignore(get_time(X)), ( flag(strings) -> true ; write('#TRACE '), copy_term_nat(Y,Z), labelvars(Z,0,_,avar), wg(Z), nl ). ''(_,A) :- when( ( nonvar(A) ), ( A =:= 1.0 ) ). ''(X,Y) :- ( tuple(X,Y) -> true ; var(X), nb_getval(tuple,M), N is M+1, nb_setval(tuple,N), atom_number(A,N), atomic_list_concat([''],X), assertz(tuple(X,Y)) ). ''(literal(X,void),literal(Y,void)) :- when( ( ground(X) ; ground(Y) ), ( ground(X) -> atom_codes(A,X), www_form_encode(A,B), atom_codes(B,Y) ; ( ground(Y) -> atom_codes(B,Y), www_form_encode(A,B), atom_codes(A,X) ) ) ). ''([literal(A,void),literal(B,void)],literal(C,void)) :- when( ( ground([A,B]) ), ( atom_codes(U,A), atom_codes(V,B), resolve_uri(U,V,W), atom_codes(W,C) ) ). ''([literal(A,_),B|C],literal(D,void)) :- when( ( ground([A,B,C]) ), ( ( C = [] -> length(A,E), F is E-B ; C = [F] ), atom_codes(U,A), sub_atom(U,B,F,_,V), atom_codes(V,D) ) ). ''([literal(A,void),literal(B,void)],literal(C,void)) :- when( ( ground([A,B]) ), ( atom_codes(U,A), atom_codes(V,B), sub_atom(U,_,_,W,V), sub_atom(U,_,W,0,X), atom_codes(X,C) ) ). ''([literal(A,void),literal(B,void)],literal(C,void)) :- when( ( ground([A,B]) ), ( atom_codes(U,A), atom_codes(V,B), sub_atom(U,W,_,_,V), sub_atom(U,0,W,_,X), atom_codes(X,C) ) ). ''(A,B) :- when( ( nonvar(A) ), ( getlist(A,C), append(C,B) ) ). ''(A,B) :- when( ( nonvar(A) ), ( A = [B|C], nonvar(C) ) ). ''(A,B) :- when( ( nonvar(B) ), ( getlist(B,C), member(A,C) ) ). ''(A,B) :- when( ( nonvar(A) ), ( getlist(A,C), last(C,B) ) ). ''(A,B) :- when( ( nonvar(A) ), ( getlist(A,C), member(B,C) ) ). ''(A,B) :- when( ( nonvar(A) ), ( A = [_|B] ) ). ''(X,Y) :- when( ( nonvar(X) ), ( conjoin(X,0,_), findall(Z, ( graph(Z) ), L ), clist(L,Y), retractall(graph(_)) ) ). ''(A,B) :- when( ( ground(A) ; nonvar(B) ), ( ( nonvar(B) -> dtlit(A,B) ; A = [literal(C,void),D], B = literal(C,type(D)) ) ) ). ''(X,Y) :- unify(X,Y). ''(X,Y) :- pcl(X,Y,_), \+steps(''(X,Y),_,_,_,_), X \= true, Y \= answer(_), Y \= goal. ''(X,Y) :- when( ( nonvar(X), nonvar(Y) ), ( ( X = semantics(Z) -> qgraph(Y,Z) ; includes(X,Y) ) ) ). ''(X,Y) :- when( ( ground([X,Y]) ), ( \+''(X,Y) ) ). ''(X,Y) :- when( ( nonvar(X), nonvar(Y) ), ( \+''(X,Y) ) ). ''(X,semantics(X)) :- when( ( nonvar(X) ), ( nonvar(X), ( semantics(X) -> true ; sub_atom(X,1,_,1,Z), n3_pcl(Z,semantics), assertz(semantics(X)) ) ) ). ''(X,literal(Y,void)) :- when( ( nonvar(X) ; nonvar(Y) ), ( atom(X), sub_atom(X,1,_,1,U), atomic_list_concat(['<',U,'>'],X), !, atom_codes(U,Y) ; nonvar(Y), atom_codes(U,Y), atomic_list_concat(['<',U,'>'],X) ) ). ''(X,Z) :- when( ( ground(X) ), ( getnumber(X,U), Z is abs(U) ) ). ''([X,Y],Z) :- when( ( ground([X,Y]) ), ( getnumber(X,U), getnumber(Y,V), Z is atan(U/V) ) ). ''(X,Z) :- when( ( ground(X) ; ground(Z) ), ( getnumber(X,U), Z is cos(U), ! ; getnumber(Z,W), X is acos(W) ) ). ''(X,Z) :- when( ( ground(X) ; ground(Z) ), ( getnumber(X,U), Z is cosh(U), ! ; getnumber(Z,W), X is acosh(W) ) ). ''(X,Z) :- when( ( ground(X) ; ground(Z) ), ( getnumber(X,U), Z is U*180/pi, ! ; getnumber(Z,W), X is W*pi/180 ) ). ''([X,Y],Z) :- when( ( ground([X,Y]) ), ( getnumber(X,U), getnumber(Y,V), Z is U-V) ). ''(X,Y) :- when( ( ground([X,Y]) ), ( getnumber(X,U), getnumber(Y,V), U =:= V ) ). ''([X,Y],Z) :- when( ( ground([X,Y]) ; ground([X,Z]) ), ( getnumber(X,U), ( getnumber(Y,V), Z is U**V, ! ; getnumber(Z,W), W =\= 0, U =\= 0, Y is log(W)/log(U) ) ) ). ''(X,Y) :- when( ( ground([X,Y]) ), ( getnumber(X,U), getnumber(Y,V), U > V ) ). ''([X,Y],Z) :- when( ( ground([X,Y]) ), ( getnumber(X,U), getnumber(Y,V), ( V =\= 0 -> Z is round(floor(U/V)) ; throw(zero_division(''([X,Y],Z))) ) ) ). ''(X,Y) :- when( ( ground([X,Y]) ), ( getnumber(X,U), getnumber(Y,V), U < V ) ). ''(X,Y) :- when( ( nonvar(X) ), ( length(X,Y) ) ). ''(X,Z) :- when( ( ground(X) ; ground(Z) ), ( getnumber(X,U), Z is -U, ! ; getnumber(Z,W), X is -W ) ). ''(X,Y) :- when( ( ground([X,Y]) ), ( getnumber(X,U), getnumber(Y,V), U =\= V ) ). ''(X,Y) :- when( ( ground([X,Y]) ), ( getnumber(X,U), getnumber(Y,V), U =< V ) ). ''(X,Y) :- when( ( ground([X,Y]) ), ( getnumber(X,U), getnumber(Y,V), U >= V ) ). ''(X,Z) :- when( ( ground(X) ), ( product(X,Z) ) ). ''([X,Y],Z) :- when( ( ground([X,Y]) ), ( getnumber(X,U), getnumber(Y,V), ( V =\= 0 -> Z is U/V ; throw(zero_division(''([X,Y],Z))) ) ) ). ''([X,Y],Z) :- when( ( ground([X,Y]) ), ( getnumber(X,U), getnumber(Y,V), ( V =\= 0 -> Z is U-V*round(floor(U/V)) ; throw(zero_division(''([X,Y],Z))) ) ) ). ''(X,Z) :- when( ( ground(X) ), ( getnumber(X,U), Z is round(round(U)) ) ). ''(X,Z) :- when( ( ground(X) ; ground(Z) ), ( getnumber(X,U), Z is sin(U), ! ; getnumber(Z,W), X is asin(W) ) ). ''(X,Z) :- when( ( ground(X) ; ground(Z) ), ( getnumber(X,U), Z is sinh(U), ! ; getnumber(Z,W), X is asinh(W) ) ). ''(X,Z) :- when( ( ground(X) ), ( sum(X,Z) ) ). ''(X,Z) :- when( ( ground(X) ; ground(Z) ), ( getnumber(X,U), Z is tan(U), ! ; getnumber(Z,W), X is atan(W) ) ). ''(X,Z) :- when( ( ground(X) ; ground(Z) ), ( getnumber(X,U), Z is tanh(U), ! ; getnumber(Z,W), X is atanh(W) ) ). ''(X,Y) :- when( ( nonvar(X) ), ( X = [Y|Z], nonvar(Z) ) ). ''(X,Y) :- when( ( nonvar(X) ), ( X = [_|Y] ) ). ''(X,literal(Y,void)) :- when( ( ground(X) ), ( findall(S, ( member(A,X), ( A = literal(S,_) -> true ; atomic(A), ( number(A) -> number_codes(A,S) ; atom_codes(A,S) ) ) ), Z ), flatten(Z,Y) ) ). ''(literal(X,void),literal(Y,void)) :- when( ( ground([X,Y]) ), ( append(Z,_,X), append(_,Y,Z) ) ). ''(literal(X,_),literal(Y,void)) :- when( ( ground([X,Y]) ), ( downcase_string(X,U), downcase_string(Y,V), append(Z,_,U), append(_,V,Z) ) ). ''(literal(X,void),literal(Y,void)) :- when( ( ground([X,Y]) ), ( append(_,Y,X) ) ). ''(literal(X,_),literal(Y,void)) :- when( ( ground([X,Y]) ), ( downcase_string(X,U), downcase_string(Y,U) ) ). ''(X,Y) :- when( ( ground([X,Y]) ), ( X @> Y ) ). ''(X,Y) :- when( ( ground([X,Y]) ), ( X @< Y ) ). ''(literal(X,void),literal(Y,void)) :- when( ( ground([X,Y]) ), ( regex(Y,X,_) ) ). ''(X,Y) :- when( ( ground([X,Y]) ), ( \+''(X,Y) ) ). ''(X,Y) :- when( ( ground([X,Y]) ), ( X @=< Y ) ). ''(X,Y) :- when( ( ground([X,Y]) ), ( X @>= Y ) ). ''(X,Y) :- when( ( ground([X,Y]) ), ( \+''(X,Y) ) ). ''([literal(X,void),literal(Y,void)],literal(Z,void)) :- when( ( ground([X,Y]) ), ( regex(Y,X,[Z|_]) ) ). ''([literal(X,void),literal(Y,void)],Z) :- when( ( ground([X,Y]) ), ( regex(Y,X,L), findall(literal(M,void), ( member(M,L) ), Z ) ) ). ''(literal(X,void),literal(Y,void)) :- when( ( ground([X,Y]) ), ( append(Y,_,X) ) ). ''(literal(X,_),literal(Y,void)) :- when( ( ground(X) ), ( atom_codes(U,X), sub_atom(U,8,2,_,V), atom_codes(V,Y) ) ). ''(literal(X,_),literal(Y,void)) :- when( ( ground(X) ), ( atom_codes(U,X), sub_atom(U,5,2,_,V), atom_codes(V,Y) ) ). ''(literal(X,_),literal(Y,void)) :- when( ( ground(X) ), ( atom_codes(U,X), sub_atom(U,0,4,_,V), atom_codes(V,Y) ) ). % ----------------------------------------------------------------------------- % RIF builtins % according to RIF Datatypes and Built-Ins 1.0 -- http://www.w3.org/TR/rif-dtb/ % ----------------------------------------------------------------------------- % 4.1.1.1 pred:literal-not-identical ''([literal(A,B),literal(C,B)],D) :- when( ( ground([A,B,C]) ), ( A \== C -> D = true ; D = false ) ). % 4.4.4 pred:iri-string ''([A,literal(B,void)],C) :- when( ( nonvar(A) ; nonvar(B) ), ( atom(A), sub_atom(A,1,_,1,U), atomic_list_concat(['<',U,'>'],A), !, ( atom_codes(U,B) -> C = true ; C = false ) ; nonvar(B), atom_codes(U,B), ( atomic_list_concat(['<',U,'>'],A) -> C = true ; C = false ) ) ). % 4.5.1 Numeric Functions ''([A,B],C) :- when( ( ground([A,B]) ), ( sum([A,B],C) ) ). ''([A,B],C) :- when( ( ground([A,B]) ), ( getnumber(A,U), getnumber(B,V), C is U-V ) ). ''([A,B],C) :- when( ( ground([A,B]) ), ( getnumber(A,U), getnumber(B,V), C is U*V ) ). ''([A,B],C) :- when( ( ground([A,B]) ), ( getnumber(A,U), getnumber(B,V), ( V =\= 0 -> C is U/V ; throw(zero_division(''([A,B],C))) ) ) ). ''([A,B],C) :- when( ( ground([A,B]) ), ( getnumber(A,U), getnumber(B,V), ( V =\= 0 -> C is integer(floor(U/V)) ; throw(zero_division(''([A,B],C))) ) ) ). ''([A,B],C) :- when( ( ground([A,B]) ), ( getnumber(A,U), getnumber(B,V), ( V =\= 0 -> C is U-V*integer(floor(U/V)) ; throw(zero_division(''([A,B],C))) ) ) ). % 4.5.2.1 pred:numeric-equal ''([A,B],C) :- when( ( ground([A,B]) ), ( getnumber(A,U), getnumber(B,V), ( U =:= V -> C = true ; C = false ) ) ). % 4.5.2.2 pred:numeric-less-than ''([A,B],C) :- when( ( ground([A,B]) ), ( getnumber(A,U), getnumber(B,V), ( U < V -> C = true ; C = false ) ) ). % 4.5.2.3 pred:numeric-greater-than ''([A,B],C) :- when( ( ground([A,B]) ), ( getnumber(A,U), getnumber(B,V), ( U > V -> C = true ; C = false ) ) ). % 4.5.2.4 pred:numeric-not-equal ''([A,B],C) :- when( ( ground([A,B]) ), ( getnumber(A,U), getnumber(B,V), ( U =\= V -> C = true ; C = false ) ) ). % 4.5.2.5 pred:numeric-less-than-or-equal ''([A,B],C) :- when( ( ground([A,B]) ), ( getnumber(A,U), getnumber(B,V), ( U =< V -> C = true ; C = false ) ) ). % 4.5.2.6 pred:numeric-greater-than-or-equal ''([A,B],C) :- when( ( ground([A,B]) ), ( getnumber(A,U), getnumber(B,V), ( U >= V -> C = true ; C = false ) ) ). % 4.6.1.1 func:not ''([A],B) :- when( ( ground(A) ), ( getbool(A,U), ( ground(B) -> getbool(B,V) ; V = B ), inv(U,V) ) ). % 4.6.2.1 pred:boolean-equal ''([A,B],C) :- when( ( ground([A,B]) ), ( getbool(A,U), getbool(B,U) -> C = true ; C = false ) ). % 4.6.2.2 pred:boolean-less-than ''([A,B],C) :- when( ( ground([A,B]) ), ( getbool(A,false), getbool(B,true) -> C = true ; C = false ) ). % 4.6.2.3 pred:boolean-greater-than ''([A,B],C) :- when( ( ground([A,B]) ), ( getbool(A,true), getbool(B,false) -> C = true ; C = false ) ). % 4.7.1.1 func:compare @@partial implementation: no collation ''([literal(A,B),literal(C,B)],D) :- !, ( A @< C -> D = -1 ; ( A == C -> D = 0 ; ( A @> C -> D = 1 ) ) ). % 4.7.1.2 func:concat ''(A,literal(B,void)) :- when( ( ground(A) ), ( findall(S, ( member(literal(S,void),A) ), C ), flatten(C,B) ) ). % 4.7.1.3 func:string-join ''([A,literal(B,void)],literal(C,void)) :- when( ( ground([A,B]) ), ( findall([S,B], ( member(literal(S,void),A) ), D ), flatten(D,C) ) ). % 4.7.1.4 func:substring ''([literal(A,_),B,C],literal(D,void)) :- !, when( ( ground([A,B,C]) ), ( getint(B,I), getint(C,J), ( I < 1 -> G is 0, H is J+I-1 ; G is I-1, H is J ), ( H < 0 -> D = "" ; atom_codes(U,A), sub_atom(U,G,H,_,V), atom_codes(V,D) ) ) ). ''([literal(A,_),B],literal(D,void)) :- when( ( ground([A,B]) ), ( getint(B,I), length(A,E), J is E-I+1, ( I < 1 -> G is 0, H is J+I-1 ; G is I-1, H is J ), ( H < 0 -> D = "" ; atom_codes(U,A), sub_atom(U,G,H,_,V), atom_codes(V,D) ) ) ). % 4.7.1.5 func:string-length ''([literal(A,_)],B) :- when( ( ground(A) ), ( length(A,B) ) ). % 4.7.1.6 func:upper-case ''([literal(A,B)],literal(C,B)) :- when( ( ground([A,B]) ), ( upcase_string(A,C) ) ). % 4.7.1.7 func:lower-case ''([literal(A,B)],literal(C,B)) :- when( ( ground([A,B]) ), ( downcase_string(A,C) ) ). % 4.7.1.8 func:encode-for-uri ''([literal(A,B)],literal(C,B)) :- when( ( ground([A,B]) ), ( atom_codes(D,A), www_form_encode(D,E), atom_codes(E,C) ) ). % 4.7.1.11 func:substring-before @@partial implementation: no collation ''([literal(A,void),literal(B,void)],literal(C,void)) :- when( ( ground([A,B]) ), ( atom_codes(U,A), atom_codes(V,B), sub_atom(U,W,_,_,V), sub_atom(U,0,W,_,X), atom_codes(X,C) ) ). % 4.7.1.12 func:substring-after @@partial implementation: no collation ''([literal(A,void),literal(B,void)],literal(C,void)) :- when( ( ground([A,B]) ), ( atom_codes(U,A), atom_codes(V,B), sub_atom(U,_,_,W,V), sub_atom(U,_,W,0,X), atom_codes(X,C) ) ). % 4.7.2.1 pred:contains @@partial implementation: no collation ''([literal(A,void),literal(B,void)],C) :- when( ( ground([A,B]) ), ( append(D,_,A), append(_,B,D) -> C = true ; C = false ) ). % 4.7.2.2 pred:starts-with @@partial implementation: no collation ''([literal(A,void),literal(B,void)],C) :- when( ( ground([A,B]) ), ( append(B,_,A) -> C = true ; C = false ) ). % 4.7.2.3 pred:ends-with @@partial implementation: no collation ''([literal(A,void),literal(B,void)],C) :- when( ( ground([A,B]) ), ( append(_,B,A) -> C = true ; C = false ) ). % 4.7.2.4 pred:matches @@partial implementation: no flags ''([literal(A,void),literal(B,void)],C) :- when( ( ground([A,B]) ), ( regex(B,A,_) -> C = true ; C = false ) ). % 4.8.1.1 func:year-from-dateTime ''([literal(A,type(''))],B) :- when( ( ground(A) ), ( phrase(datetime(C,_,_,_,_,_,_),A), ( nonvar(B) -> C =:= B ; C = B ) ) ). % 4.8.1.2 func:month-from-dateTime ''([literal(A,type(''))],B) :- when( ( ground(A) ), ( phrase(datetime(_,C,_,_,_,_,_),A), ( nonvar(B) -> C =:= B ; C = B ) ) ). % 4.8.1.3 func:day-from-dateTime ''([literal(A,type(''))],B) :- when( ( ground(A) ), ( phrase(datetime(_,_,C,_,_,_,_),A), ( nonvar(B) -> C =:= B ; C = B ) ) ). % 4.8.1.4 func:hours-from-dateTime ''([literal(A,type(''))],B) :- when( ( ground(A) ), ( phrase(datetime(_,_,_,C,_,_,_),A), ( nonvar(B) -> C =:= B ; C = B ) ) ). % 4.8.1.5 func:minutes-from-dateTime ''([literal(A,type(''))],B) :- when( ( ground(A) ), ( phrase(datetime(_,_,_,_,C,_,_),A), ( nonvar(B) -> C =:= B ; C = B ) ) ). % 4.8.1.6 func:seconds-from-dateTime ''([literal(A,type(''))],B) :- when( ( ground(A) ), ( phrase(datetime(_,_,_,_,_,C,_),A), ( nonvar(B) -> C =:= B ; C = B ) ) ). % 4.8.1.7 func:year-from-date ''([literal(A,type(''))],B) :- when( ( ground(A) ), ( phrase(date(C,_,_,_),A), ( nonvar(B) -> C =:= B ; C = B ) ) ). % 4.8.1.8 func:month-from-date ''([literal(A,type(''))],B) :- when( ( ground(A) ), ( phrase(date(_,C,_,_),A), ( nonvar(B) -> C =:= B ; C = B ) ) ). % 4.8.1.9 func:day-from-date ''([literal(A,type(''))],B) :- when( ( ground(A) ), ( phrase(date(_,_,C,_),A), ( nonvar(B) -> C =:= B ; C = B ) ) ). % 4.8.1.10 func:hours-from-time ''([literal(A,type(''))],B) :- when( ( ground(A) ), ( phrase(time(C,_,_,_),A), ( nonvar(B) -> C =:= B ; C = B ) ) ). % 4.8.1.11 func:minutes-from-time ''([literal(A,type(''))],B) :- when( ( ground(A) ), ( phrase(time(_,C,_,_),A), ( nonvar(B) -> C =:= B ; C = B ) ) ). % 4.8.1.12 func:seconds-from-time ''([literal(A,type(''))],B) :- when( ( ground(A) ), ( phrase(time(_,_,C,_),A), ( nonvar(B) -> C =:= B ; C = B ) ) ). % 4.8.1.13 func:years-from-duration ''([literal(_,type(''))],0) :- !. ''([literal(A,type(''))],B) :- when( ( ground(A) ), ( phrase(yearmonthduration(C),A), D is C//12, ( nonvar(B) -> D =:= B ; D = B ) ) ). % 4.8.1.14 func:months-from-duration ''([literal(_,type(''))],0) :- !. ''([literal(A,type(''))],B) :- when( ( ground(A) ), ( phrase(yearmonthduration(C),A), D is C-(C//12)*12, ( nonvar(B) -> D =:= B ; D = B ) ) ). % 4.8.1.15 func:days-from-duration ''([literal(_,type(''))],_) :- !. ''([literal(A,type(''))],B) :- when( ( ground(A) ), ( phrase(daytimeduration(C),A), D is integer(C)//86400, ( nonvar(B) -> D =:= B ; D = B ) ) ). % 4.8.1.16 func:hours-from-duration ''([literal(_,type(''))],_) :- !. ''([literal(A,type(''))],B) :- when( ( ground(A) ), ( phrase(daytimeduration(C),A), D is (integer(C)-(integer(C)//86400)*86400)//3600, ( nonvar(B) -> D =:= B ; D = B ) ) ). % 4.8.1.17 func:minutes-from-duration ''([literal(_,type(''))],_) :- !. ''([literal(A,type(''))],B) :- when( ( ground(A) ), ( phrase(daytimeduration(C),A), D is (integer(C)-(integer(C)//3600)*3600)//60, ( nonvar(B) -> D =:= B ; D = B ) ) ). % 4.8.1.18 func:seconds-from-duration ''([literal(_,type(''))],_) :- !. ''([literal(A,type(''))],B) :- when( ( ground(A) ), ( phrase(daytimeduration(C),A), D is C-(integer(C)//60)*60, ( nonvar(B) -> D =:= B ; D = B ) ) ). % 4.8.1.19 func:timezone-from-dateTime ''([literal(A,type(''))], literal(B,type(''))) :- when( ( ground(A) ), ( phrase(datetime(_,_,_,_,_,_,C),A), ( ground(B) -> phrase(daytimeduration(D),B), D =:= C ; daytimeduration(C,B) ) ) ). % 4.8.1.20 func:timezone-from-date ''([literal(A,type(''))], literal(B,type(''))) :- when( ( ground(A) ), ( phrase(date(_,_,_,C),A), ( ground(B) -> phrase(daytimeduration(D),B), D =:= C ; daytimeduration(C,B) ) ) ). % 4.8.1.21 func:timezone-from-time ''([literal(A,type(''))], literal(B,type(''))) :- when( ( ground(A) ), ( phrase(time(_,_,_,C),A), ( ground(B) -> phrase(daytimeduration(D),B), D =:= C ; daytimeduration(C,B) ) ) ). % 4.8.1.22 func:subtract-dateTimes ''([literal(A,type('')), literal(B,type(''))],literal(C,type(''))) :- when( ( ground([A,B]) ), ( phrase(datetime(D),A), phrase(datetime(E),B), F is D-E, ( ground(C) -> phrase(daytimeduration(G),C), G =:= F ; daytimeduration(F,C) ) ) ). % 4.8.1.23 func:subtract-dates ''([literal(A,type('')), literal(B,type(''))],literal(C,type(''))) :- when( ( ground([A,B]) ), ( phrase(date(D),A), phrase(date(E),B), F is D-E, ( ground(C) -> phrase(daytimeduration(G),C), G =:= F ; daytimeduration(F,C) ) ) ). % 4.8.1.24 func:subtract-times ''([literal(A,type('')), literal(B,type(''))],literal(C,type(''))) :- when( ( ground([A,B]) ), ( phrase(time(D),A), phrase(time(E),B), F is D-E, ( ground(C) -> phrase(daytimeduration(G),C), G =:= F ; daytimeduration(F,C) ) ) ). % 4.8.1.25 func:add-yearMonthDurations ''([literal(A,type('')), literal(B,type(''))],literal(C,type(''))) :- when( ( ground([A,B]) ), ( phrase(yearmonthduration(D),A), phrase(yearmonthduration(E),B), F is D+E, ( ground(C) -> phrase(yearmonthduration(G),C), G =:= F ; yearmonthduration(F,C) ) ) ). % 4.8.1.26 func:subtract-yearMonthDurations ''([literal(A,type('')), literal(B,type(''))],literal(C,type(''))) :- when( ( ground([A,B]) ), ( phrase(yearmonthduration(D),A), phrase(yearmonthduration(E),B), F is D-E, ( ground(C) -> phrase(yearmonthduration(G),C), G =:= F ; yearmonthduration(F,C) ) ) ). % 4.8.1.27 func:multiply-yearMonthDuration ''([literal(A,type('')),B], literal(C,type(''))) :- when( ( ground([A,B]) ), ( phrase(yearmonthduration(D),A), getnumber(B,E), F is integer(round(D*E-1)+1), ( ground(C) -> phrase(yearmonthduration(G),C), G =:= F ; yearmonthduration(F,C) ) ) ). % 4.8.1.28 func:divide-yearMonthDuration ''([literal(A,type('')),B], literal(C,type(''))) :- when( ( ground([A,B]) ), ( phrase(yearmonthduration(D),A), getnumber(B,E), F is integer(round(D/E-1)+1), ( ground(C) -> phrase(yearmonthduration(G),C), G =:= F ; yearmonthduration(F,C) ) ) ). % 4.8.1.29 func:divide-yearMonthDuration-by-yearMonthDuration ''([literal(A,type('')), literal(B,type(''))],C) :- when( ( ground([A,B]) ), ( phrase(yearmonthduration(D),A), phrase(yearmonthduration(E),B), F is D/E, ( ground(C) -> C =:= F ; C = F ) ) ). % 4.8.1.30 func:add-dayTimeDurations ''([literal(A,type('')), literal(B,type(''))],literal(C,type(''))) :- when( ( ground([A,B]) ), ( phrase(daytimeduration(D),A), phrase(daytimeduration(E),B), F is D+E, ( ground(C) -> phrase(daytimeduration(G),C), G =:= F ; daytimeduration(F,C) ) ) ). % 4.8.1.31 func:subtract-dayTimeDurations ''([literal(A,type('')), literal(B,type(''))],literal(C,type(''))) :- when( ( ground([A,B]) ), ( phrase(daytimeduration(D),A), phrase(daytimeduration(E),B), F is D-E, ( ground(C) -> phrase(daytimeduration(G),C), G =:= F ; daytimeduration(F,C) ) ) ). % 4.8.1.32 func:multiply-dayTimeDuration ''([literal(A,type('')),B], literal(C,type(''))) :- when( ( ground([A,B]) ), ( phrase(daytimeduration(D),A), getnumber(B,E), F is integer(round(D*E-1)+1), ( ground(C) -> phrase(daytimeduration(G),C), G =:= F ; daytimeduration(F,C) ) ) ). % 4.8.1.33 func:divide-dayTimeDuration ''([literal(A,type('')),B], literal(C,type(''))) :- when( ( ground([A,B]) ), ( phrase(daytimeduration(D),A), getnumber(B,E), F is integer(round(D/E-1)+1), ( ground(C) -> phrase(daytimeduration(G),C), G =:= F ; daytimeduration(F,C) ) ) ). % 4.8.1.34 func:divide-dayTimeDuration-by-dayTimeDuration ''([literal(A,type('')), literal(B,type(''))],C) :- when( ( ground([A,B]) ), ( phrase(daytimeduration(D),A), phrase(daytimeduration(E),B), F is D/E, ( ground(C) -> C =:= F ; C = F ) ) ). % 4.8.1.35 func:add-yearMonthDuration-to-dateTime ''([literal(A,type('')), literal(B,type(''))],literal(C,type(''))) :- when( ( ground([A,B]) ), ( phrase(datetime(D,E,F,G,H,I,J),A), phrase(yearmonthduration(K),B), L is E+K-1, Q is D+integer(floor(L/12)), R is L-integer(floor(L/12))*12+1, memotime(datime(Q,R,F,G,H,0),M), memotime(datime(1971,1,1,0,0,0),N), O is M+I+31536000-N-J, ( ground(C) -> phrase(datetime(P),C), O =:= P ; Offset is -J, stamp_date_time(O,date(Year,Month,Day,Hour,Minute,Second,_,_,_),Offset), datetime(Year,Month,Day,Hour,Minute,Second,Offset,C) ) ) ). % 4.8.1.36 func:add-yearMonthDuration-to-date ''([literal(A,type('')), literal(B,type(''))],literal(C,type(''))) :- when( ( ground([A,B]) ), ( phrase(date(D,E,F,G),A), phrase(yearmonthduration(K),B), L is E+K-1, Q is D+integer(floor(L/12)), R is L-integer(floor(L/12))*12+1, memotime(datime(Q,R,F,0,0,0),M), memotime(datime(1971,1,1,0,0,0),N), O is (integer(floor(M+31536000-N-G))//60)*60, ( ground(C) -> phrase(date(P),C), O =:= P ; date(O,C) ) ) ). % 4.8.1.37 func:add-dayTimeDuration-to-dateTime ''([literal(A,type('')), literal(B,type(''))],literal(C,type(''))) :- when( ( ground([A,B]) ), ( phrase(datetime(D,E,F,G,H,I,J),A), phrase(daytimeduration(K),B), L is I+K, memotime(datime(D,E,F,G,H,0),M), memotime(datime(1971,1,1,0,0,0),N), O is M+L+31536000-N-J, ( ground(C) -> phrase(datetime(P),C), O =:= P ; Offset is -J, stamp_date_time(O,date(Year,Month,Day,Hour,Minute,Second,_,_,_),Offset), datetime(Year,Month,Day,Hour,Minute,Second,Offset,C) ) ) ). % 4.8.1.38 func:add-dayTimeDuration-to-date ''([literal(A,type('')), literal(B,type(''))],literal(C,type(''))) :- when( ( ground([A,B]) ), ( phrase(date(D,E,F,G),A), phrase(daytimeduration(K),B), L is integer(K), memotime(datime(D,E,F,0,0,0),M), memotime(datime(1971,1,1,0,0,0),N), O is (integer(floor(M+L+31536000-N))//86400)*86400-G, ( ground(C) -> phrase(date(P),C), O =:= P ; date(O,C) ) ) ). % 4.8.1.39 func:add-dayTimeDuration-to-time ''([literal(A,type('')), literal(B,type(''))],literal(C,type(''))) :- when( ( ground([A,B]) ), ( phrase(time(D,E,F,G),A), phrase(daytimeduration(K),B), L is F+K, memotime(datime(1972,12,31,D,E,0),M), memotime(datime(1971,1,1,0,0,0),N), Z is M+L+31536000-N-G, O is Z-86400*integer(floor(Z/86400)), ( ground(C) -> phrase(time(P),C), O =:= P-86400*integer(floor(P/86400)) ; Offset is -G, stamp_date_time(O,date(_,_,_,Hour,Minute,Second,_,_,_),Offset), time(Hour,Minute,Second,Offset,C) ) ) ). % 4.8.1.40 func:subtract-yearMonthDuration-from-dateTime ''([literal(A,type('')), literal(B,type(''))],literal(C,type(''))) :- when( ( ground([A,B]) ), ( phrase(datetime(D,E,F,G,H,I,J),A), phrase(yearmonthduration(K),B), L is E-K-1, Q is D+integer(floor(L/12)), R is L-integer(floor(L/12))*12+1, memotime(datime(Q,R,F,G,H,0),M), memotime(datime(1971,1,1,0,0,0),N), O is M+I+31536000-N-J, ( ground(C) -> phrase(datetime(P),C), O =:= P ; Offset is -J, stamp_date_time(O,date(Year,Month,Day,Hour,Minute,Second,_,_,_),Offset), datetime(Year,Month,Day,Hour,Minute,Second,Offset,C) ) ) ). % 4.8.1.41 func:subtract-yearMonthDuration-from-date ''([literal(A,type('')), literal(B,type(''))],literal(C,type(''))) :- when( ( ground([A,B]) ), ( phrase(date(D,E,F,G),A), phrase(yearmonthduration(K),B), L is E-K-1, Q is D+integer(floor(L/12)), R is L-integer(floor(L/12))*12+1, memotime(datime(Q,R,F,0,0,0),M), memotime(datime(1971,1,1,0,0,0),N), O is (integer(floor(M+31536000-N-G))//60)*60, ( ground(C) -> phrase(date(P),C), O =:= P ; date(O,C) ) ) ). % 4.8.1.42 func:subtract-dayTimeDuration-from-dateTime ''([literal(A,type('')), literal(B,type(''))],literal(C,type(''))) :- when( ( ground([A,B]) ), ( phrase(datetime(D,E,F,G,H,I,J),A), phrase(daytimeduration(K),B), L is I-integer(K), memotime(datime(D,E,F,G,H,0),M), memotime(datime(1971,1,1,0,0,0),N), O is M+L+31536000-N-J, ( ground(C) -> phrase(datetime(P),C), O =:= P ; Offset is -J, stamp_date_time(O,date(Year,Month,Day,Hour,Minute,Second,_,_,_),Offset), datetime(Year,Month,Day,Hour,Minute,Second,Offset,C) ) ) ). % 4.8.1.43 func:subtract-dayTimeDuration-from-date ''([literal(A,type('')), literal(B,type(''))],literal(C,type(''))) :- when( ( ground([A,B]) ), ( phrase(date(D,E,F,G),A), phrase(daytimeduration(K),B), L is -integer(K), memotime(datime(D,E,F,0,0,0),M), memotime(datime(1971,1,1,0,0,0),N), O is (integer(floor(M+L+31536000-N))//86400)*86400-G, ( ground(C) -> phrase(date(P),C), O =:= P ; date(O,C) ) ) ). % 4.8.1.44 func:subtract-dayTimeDuration-from-time ''([literal(A,type('')), literal(B,type(''))],literal(C,type(''))) :- when( ( ground([A,B]) ), ( phrase(time(D,E,F,G),A), phrase(daytimeduration(K),B), L is F-K, memotime(datime(1972,12,31,D,E,0),M), memotime(datime(1971,1,1,0,0,0),N), Z is M+L+31536000-N-G, O is Z-86400*integer(floor(Z/86400)), ( ground(C) -> phrase(time(P),C), O =:= P-86400*integer(floor(P/86400)) ; Offset is -G, stamp_date_time(O,date(_,_,_,Hour,Minute,Second,_,_,_),Offset), time(Hour,Minute,Second,Offset,C) ) ) ). % 4.8.2.1 pred:dateTime-equal ''([literal(A,type('')), literal(B,type(''))],C) :- when( ( ground([A,B]) ), ( phrase(datetime(D),A), phrase(datetime(E),B), ( D =:= E -> C = true ; C = false ) ) ). % 4.8.2.2 pred:dateTime-less-than ''([literal(A,type('')), literal(B,type(''))],C) :- when( ( ground([A,B]) ), ( phrase(datetime(D),A), phrase(datetime(E),B), ( D < E -> C = true ; C = false ) ) ). % 4.8.2.3 pred:dateTime-greater-than ''([literal(A,type('')), literal(B,type(''))],C) :- when( ( ground([A,B]) ), ( phrase(datetime(D),A), phrase(datetime(E),B), ( D > E -> C = true ; C = false ) ) ). % 4.8.2.4 pred:date-equal ''([literal(A,type('')), literal(B,type(''))],C) :- when( ( ground([A,B]) ), ( phrase(date(D),A), phrase(date(E),B), ( D =:= E -> C = true ; C = false ) ) ). % 4.8.2.5 pred:date-less-than ''([literal(A,type('')), literal(B,type(''))],C) :- when( ( ground([A,B]) ), ( phrase(date(D),A), phrase(date(E),B), ( D < E -> C = true ; C = false ) ) ). % 4.8.2.6 pred:date-greater-than ''([literal(A,type('')), literal(B,type(''))],C) :- when( ( ground([A,B]) ), ( phrase(date(D),A), phrase(date(E),B), ( D > E -> C = true ; C = false ) ) ). % 4.8.2.7 pred:time-equal ''([literal(A,type('')), literal(B,type(''))],C) :- when( ( ground([A,B]) ), ( phrase(time(D),A), phrase(time(E),B), ( D =:= E -> C = true ; C = false ) ) ). % 4.8.2.8 pred:time-less-than ''([literal(A,type('')), literal(B,type(''))],C) :- when( ( ground([A,B]) ), ( phrase(time(D),A), phrase(time(E),B), ( D < E -> C = true ; C = false ) ) ). % 4.8.2.9 pred:time-greater-than ''([literal(A,type('')), literal(B,type(''))],C) :- when( ( ground([A,B]) ), ( phrase(time(D),A), phrase(time(E),B), ( D > E -> C = true ; C = false ) ) ). % 4.8.2.10 pred:duration-equal ''([literal(A,type('')), literal(B,type(''))],C) :- when( ( ground([A,B]) ), ( phrase(duration(D),A), phrase(duration(E),B), ( D =:= E -> C = true ; C = false ) ) ). % 4.8.2.11 pred:dayTimeDuration-less-than ''([literal(A,type('')), literal(B,type(''))],C) :- when( ( ground([A,B]) ), ( phrase(daytimeduration(D),A), phrase(daytimeduration(E),B), ( D < E -> C = true ; C = false ) ) ). % 4.8.2.12 pred:dayTimeDuration-greater-than ''([literal(A,type('')), literal(B,type(''))],C) :- when( ( ground([A,B]) ), ( phrase(daytimeduration(D),A), phrase(daytimeduration(E),B), ( D > E -> C = true ; C = false ) ) ). % 4.8.2.13 pred:yearMonthDuration-less-than ''([literal(A,type('')), literal(B,type(''))],C) :- when( ( ground([A,B]) ), ( phrase(yearmonthduration(D),A), phrase(yearmonthduration(E),B), ( D < E -> C = true ; C = false ) ) ). % 4.8.2.14 pred:yearMonthDuration-greater-than ''([literal(A,type('')), literal(B,type(''))],C) :- when( ( ground([A,B]) ), ( phrase(yearmonthduration(D),A), phrase(yearmonthduration(E),B), ( D > E -> C = true ; C = false ) ) ). % 4.8.2.15 pred:dateTime-not-equal ''([literal(A,type('')), literal(B,type(''))],C) :- when( ( ground([A,B]) ), ( phrase(datetime(D),A), phrase(datetime(E),B), ( D =\= E -> C = true ; C = false ) ) ). % 4.8.2.16 pred:dateTime-less-than-or-equal ''([literal(A,type('')), literal(B,type(''))],C) :- when( ( ground([A,B]) ), ( phrase(datetime(D),A), phrase(datetime(E),B), ( D =< E -> C = true ; C = false ) ) ). % 4.8.2.17 pred:dateTime-greater-than-or-equal ''([literal(A,type('')), literal(B,type(''))],C) :- when( ( ground([A,B]) ), ( phrase(datetime(D),A), phrase(datetime(E),B), ( D >= E -> C = true ; C = false ) ) ). % 4.8.2.18 pred:date-not-equal ''([literal(A,type('')), literal(B,type(''))],C) :- when( ( ground([A,B]) ), ( phrase(date(D),A), phrase(date(E),B), ( D =\= E -> C = true ; C = false ) ) ). % 4.8.2.19 pred:date-less-than-or-equal ''([literal(A,type('')), literal(B,type(''))],C) :- when( ( ground([A,B]) ), ( phrase(date(D),A), phrase(date(E),B), ( D =< E -> C = true ; C = false ) ) ). % 4.8.2.20 pred:date-greater-than-or-equal ''([literal(A,type('')), literal(B,type(''))],C) :- when( ( ground([A,B]) ), ( phrase(date(D),A), phrase(date(E),B), ( D >= E -> C = true ; C = false ) ) ). % 4.8.2.21 pred:time-not-equal ''([literal(A,type('')), literal(B,type(''))],C) :- when( ( ground([A,B]) ), ( phrase(time(D),A), phrase(time(E),B), ( D =\= E -> C = true ; C = false ) ) ). % 4.8.2.22 pred:time-less-than-or-equal ''([literal(A,type('')), literal(B,type(''))],C) :- when( ( ground([A,B]) ), ( phrase(time(D),A), phrase(time(E),B), ( D =< E -> C = true ; C = false ) ) ). % 4.8.2.23 pred:time-greater-than-or-equal ''([literal(A,type('')), literal(B,type(''))],C) :- when( ( ground([A,B]) ), ( phrase(time(D),A), phrase(time(E),B), ( D >= E -> C = true ; C = false ) ) ). % 4.8.2.24 pred:duration-not-equal ''([literal(A,type('')), literal(B,type(''))],C) :- when( ( ground([A,B]) ), ( phrase(duration(D),A), phrase(duration(E),B), ( D =\= E -> C = true ; C = false ) ) ). % 4.8.2.25 pred:dayTimeDuration-less-than-or-equal ''([literal(A,type('')), literal(B,type(''))],C) :- when( ( ground([A,B]) ), ( phrase(daytimeduration(D),A), phrase(daytimeduration(E),B), ( D =< E -> C = true ; C = false ) ) ). % 4.8.2.26 pred:dayTimeDuration-greater-than-or-equal ''([literal(A,type('')), literal(B,type(''))],C) :- when( ( ground([A,B]) ), ( phrase(daytimeduration(D),A), phrase(daytimeduration(E),B), ( D >= E -> C = true ; C = false ) ) ). % 4.8.2.27 pred:yearMonthDuration-less-than-or-equal ''([literal(A,type('')), literal(B,type(''))],C) :- when( ( ground([A,B]) ), ( phrase(yearmonthduration(D),A), phrase(yearmonthduration(E),B), ( D =< E -> C = true ; C = false ) ) ). % 4.8.2.28 pred:yearMonthDuration-greater-than-or-equal ''([literal(A,type('')), literal(B,type(''))],C) :- when( ( ground([A,B]) ), ( phrase(yearmonthduration(D),A), phrase(yearmonthduration(E),B), ( D >= E -> C = true ; C = false ) ) ). % 4.10.1.1 func:PlainLiteral-from-string-lang ''([literal(A,void)],literal(A,void)) :- !. ''([literal(A,void),literal(B,void)],literal(A,lang(C))) :- downcase_string(B,D), atom_codes(C,D). % 4.10.1.2 func:string-from-PlainLiteral ''([literal(A,void)],literal(A,void)) :- !. ''([literal(A,lang(_))],literal(A,void)). % 4.10.1.3 func:lang-from-PlainLiteral ''([literal(_,void)],literal("",void)) :- !. ''([literal(_,lang(A))],literal(B,void)) :- atom_codes(A,B). % 4.10.1.4 func:PlainLiteral-compare @@partial implementation: no collation ''([literal(A,void),literal(C,void)],D) :- !, ( A @< C -> D = -1 ; ( A == C -> D = 0 ; ( A @> C -> D = 1 ) ) ). ''([literal(A,lang(B)),literal(C,lang(B))],D) :- ( A @< C -> D = -1 ; ( A == C -> D = 0 ; ( A @> C -> D = 1 ) ) ). % 4.10.1.5 func:PlainLiteral-length ''([literal(A,void)],C) :- !, length(A,C). ''([literal(A,lang(_))],C) :- length(A,C). % 4.10.2.1 pred:matches-language-range @@partial implementation: no false results ''([literal(A,lang(B)),literal(C,void)],true) :- A \= "", atom_codes(B,D), regexp_wildcard(C,E), append("^",E,F), downcase_string(F,G), downcase_string(D,H), regex(G,H,_). % 4.11.3.1 pred:is-list ''([A],B) :- ( is_list(A) -> B = true ; B = false ). % 4.11.3.2 pred:list-contains ''([A,B],C) :- when( ( nonvar(A) ), ( member(B,A) -> C = true ; C = false ) ). % 4.11.4.1 func:make-list ''(A,A). % 4.11.4.2 func:count ''([A],B) :- when( ( nonvar(A) ), ( length(A,B) ) ). % 4.11.4.3 func:get ''([A,B],C) :- when( ( nonvar(A), ground(B) ), ( getnumber(B,U), nth0(U,A,C) ) ). % 4.11.4.4 func:sublist ''([A,B,C],D) :- !, when( ( nonvar(A), ground([B,C]) ), ( getint(B,U), getint(C,V), length(A,W), ( U < 0 -> I is W+U ; I is U ), ( V < 0 -> J is W+V ; J is V ), append(E,F,A), length(E,I), append(D,G,F), K is J-I, ( length(D,K) -> true ; G = [] ), ! ) ). ''([A,B],C) :- when( ( nonvar(A), ground(B) ), ( getint(B,U), length(A,W), ( U < 0 -> I is W+U ; I is U ), append(E,C,A), length(E,I), ! ) ). % 4.11.4.5 func:append ''([A|B],C) :- append(A,B,C). % 4.11.4.6 func:concatenate ''(A,B) :- when( ( nonvar(A) ), ( append(A,B) ) ). % 4.11.4.7 func:insert-before ''([A,B,C],D) :- when( ( nonvar(A), ground([B,C]) ), ( getint(B,U), length(A,W), ( U < 0 -> I is W+U ; I is U ), append(G,H,A), length(G,I), append([G,[C],H],D) ) ). % 4.11.4.8 func:remove ''([A,B],C) :- when( ( nonvar(A), ground(B) ), ( getint(B,U), length(A,W), ( U < 0 -> I is W+U ; I is U ), append(G,[_|T],A), length(G,I), append(G,T,C) ) ). % 4.11.4.9 func:reverse ''([A],B) :- reverse(A,B). % 4.11.4.10 func:index-of ''([A,B],C) :- when( ( nonvar(A), ground(B) ), ( findall(I, ( nth0(I,A,B) ), C ) ) ). % 4.11.4.11 func:union ''(A,B) :- when( ( nonvar(A) ), ( append(A,C), distinct(C,B) ) ). % 4.11.4.12 func:distinct-values ''([A],B) :- when( ( nonvar(A) ), ( distinct(A,B) ) ). % 4.11.4.13 func:intersect ''([A,B],C) :- when( ( ground(A), ground(B) ), ( findall(I, ( member(I,A), member(I,B) ), C ) ) ). % 4.11.4.14 func:except ''([A,B],C) :- when( ( ground(A), ground(B) ), ( findall(I, ( member(I,A), \+member(I,B) ), C ) ) ). % ------- % support % ------- def_pfx('math:',''). def_pfx('e:',''). def_pfx('list:',''). def_pfx('xsd:',''). def_pfx('log:',''). def_pfx('r:',''). def_pfx('rdfs:',''). def_pfx('fn:',''). def_pfx('time:',''). def_pfx('rdf:',''). def_pfx('var:',''). def_pfx('str:',''). def_pfx('owl:',''). def_pfx('n3:',''). put_pfx(_,URI) :- atomic_list_concat(['<',URI,'>'],U), pfx(_,U), !. put_pfx(_,URI) :- atomic_list_concat(['<',URI,'>'],U), def_pfx(Pf,U), !, assertz(pfx(Pf,U)). put_pfx(Pf,URI) :- atomic_list_concat(['<',URI,'>'],U), fresh_pf(Pf,Pff), assertz(pfx(Pff,U)). fresh_pf(Pf,Pfx) :- atom_concat(Pf,':',Pfx), \+pfx(Pfx,_), !. fresh_pf(_,Pfx) :- gensym(ns,Pfn), fresh_pf(Pfn,Pfx). cnt(A) :- catch(nb_getval(A,B),_,B = 0), C is B+1, nb_setval(A,C). within_scope([A,B]) :- ( var(B) -> B = 1 ; true ), ( ( flag('no-span') ; B = 0 ) -> brake ; nb_getval(limit,C), ( C < B -> nb_setval(limit,B) ; true ), span(B) ), nb_getval(scope,A). varpred(S,P,O) :- ( atom(P) -> U =.. [P,S,O], call(U) ; steps(U,_,_,forward,_), U =.. [P,S,O], P \= false ). unify(A,B) :- nonvar(A), A = varpred(S,P,O), tpred(P,Q), unify(S,T), unify(O,R), ( ( nonvar(B) ; nonvar(Q) ) -> B =.. [Q,T,R] ), !. unify(A,B) :- nonvar(B), B = varpred(S,P,O), tpred(P,Q), unify(S,T), unify(O,R), ( ( nonvar(A) ; nonvar(Q) ) -> A =.. [Q,T,R] ), !. unify(A,A). tpred(A,A) :- var(A), !. tpred(some(I),B) :- !, atom_number(A,I), ( \+flag('no-qvars') -> atom_concat('_:sk',A,B) ; atomic_list_concat([''],B) ). tpred(A,A). dn([A|B]) :- ( call(A) ; ( B = [C] -> true ; C = dn(B) ), call(C) ). dlist([],false) :- !. dlist([A],A) :- A \= dn(_), !. dlist(A,dn(A)). cn([A|B]) :- call(A), ( B = [C] -> true ; C = cn(B) ), call(C). clist([],true) :- !. clist([A],A) :- A \= cn(_), !. clist(A,cn(A)). clistflat(true,[]) :- !. clistflat([A],A) :- A \= cn(_), !. clistflat(A,cn(B)) :- ( nonvar(A) -> cflat(A,C), distinct(C,B) ; distinct(B,A) ). cflat([],[]) :- !. cflat([A|B],C) :- cflat(B,D), copy_term(A,E), ( E = cn(F) -> append(F,D,C) ; ( E = true -> C = D ; C = [E|D] ) ). cmember(A,cn(B)) :- member(A,B). cmember(A,A) :- A \= cn(_). clast(cn(A),B) :- !, last(A,B). clast(A,A). cn_conj(A,B) :- clist(C,A), c_d(C,D), c_list(D,B). c_d([],[]) :- !. c_d([(A;B)|C],[(D;E)|F]) :- !, cn_conj(A,D), cn_conj(B,E), c_d(C,F). c_d([A|B],[A|C]) :- c_d(B,C). c_list([],true) :- !. c_list([A],A) :- !. c_list([A|B],(A,C)) :- c_list(B,C). c_append((A,B),C,(A,D)) :- c_append(B,C,D), !. c_append(A,B,(A,B)). couple([],[],[]). couple([A|B],[C|D],[[A,C]|E]) :- couple(B,D,E). conjoin([],_,_) :- !. conjoin([true|Y],I,J) :- !, conjoin(Y,I,J). conjoin([X|Y],I,K) :- copy_term_nat(X,Z), labelvars(Z,I,J,some), agraph(Z), conjoin(Y,J,K). agraph(cn([X|Y])) :- !, ( \+graph(X) -> assertz(graph(X)) ; true ), ( Y = [Z] -> true ; Z = cn(Y) ), agraph(Z). agraph(X) :- ( \+graph(X) -> assertz(graph(X)) ; true ). qgraph(cn([X|Y]),U) :- !, ( X = varpred(_,_,_) -> graph(T,U), unify(T,X) ; graph(X,U) ), ( Y = [Z] -> true ; Z = cn(Y) ), qgraph(Z,U). qgraph(X,U) :- ( X = varpred(_,_,_) -> graph(T,U), unify(T,X) ; graph(X,U) ). includes(_,true) :- !. includes(X,Y) :- X \= cn(_), unify(X,Y), !. includes(cn([X|Y]),Z) :- ( unify(X,Z) ; ( Y = [U] -> true ; U = cn(Y) ), includes(U,Z) ). includes(X,cn([Y|Z])) :- includes(X,Y), ( Z = [U] -> true ; U = cn(Z) ), includes(X,U). difference([true,_],true) :- !. difference([X,true],X) :- !. difference([X,Y],Z) :- findall(U, ( cmember(U,X), \+( ( cmember(V,Y), unify(U,V) ) ) ), W ), ( W = [] -> Z = true ; clist(W,G), Z = G ). intersection([X],X) :- !. intersection([true|_],true) :- !. intersection([X|Y],Z) :- intersection(Y,I), ( I = true -> Z = true ; findall(U, ( cmember(U,X), cmember(V,I), unify(U,V) ), W ), clist(W,Z) ). cartesian([],[]). cartesian([A|B],[C|D]) :- member(C,A), cartesian(B,D). distinct(A,B) :- ( ground(A) -> distinct_hash(A,B) ; distinct_value(A,B) ). distinct_hash([],[]) :- retractall(hash_value(_,_)). distinct_hash([A|B],C) :- term_hash(A,D), ( hash_value(D,E) -> ( unify(A,E) -> C = F ; C = [A|F] ) ; assertz(hash_value(D,A)), C = [A|F] ), distinct_hash(B,F). distinct_value([],[]). distinct_value([A|B],[A|D]) :- del(B,A,E), distinct_value(E,D). del([],_,[]). del([A|B],C,D) :- unify(A,C), !, del(B,C,D). del([A|B],C,[A|D]) :- del(B,C,D). subst(_,[],_,[]). subst(A,[B|C],D,[E|F]) :- ( A == B -> E = D ; E = B ), subst(A,C,D,F). quicksort([],[]). quicksort([A|B],C) :- split(A,B,D,E), quicksort(D,F), quicksort(E,G), append(F,[A|G],C). split(_,[],[],[]). split(A,[B|C],[B|D],E) :- sort([A,B],[B,A]), !, split(A,C,D,E). split(A,[B|C],D,[B|E]) :- split(A,C,D,E). sum([],0) :- !. sum([A|B],C) :- getnumber(A,X), sum(B,D), C is X+D. product([],1) :- !. product([A|B],C) :- getnumber(A,X), product(B,D), C is X*D. rms(A,B) :- findall(C, ( member(D,A), getnumber(D,E), C is E*E ), F ), sum(F,G), length(F,H), B is sqrt(G/H). bmax([A|B],C) :- getnumber(A,X), bmax(B,X,C). bmax([],A,A). bmax([A|B],C,D) :- getnumber(A,X), ( X > C -> bmax(B,X,D) ; bmax(B,C,D) ). bmin([A|B],C) :- getnumber(A,X), bmin(B,X,C). bmin([],A,A). bmin([A|B],C,D) :- getnumber(A,X), ( X < C -> bmin(B,X,D) ; bmin(B,C,D) ). inconsistent([''(A,'')|B]) :- memberchk(''(A,''),B), !. inconsistent([''(A,'')|B]) :- memberchk(''(A,''),B), !. inconsistent([_|B]) :- inconsistent(B). inverse(''(A,''), ''(A,'')) :- !. inverse(''(A,''), ''(A,'')). bnet :- ( ''([A|B],_), sort(B,C), findall(Y, ( ''([A|X],Y), sort(X,C) ), L ), sum(L,S), length(L,N), Z is S/N, \+bcnd([A|B],_), assertz(bcnd([A|B],Z)), inverse(A,D), \+bcnd([D|B],_), E is 1-Z, assertz(bcnd([D|B],E)), fail ; bcnd([''(A,_)|B],_), ( \+bvar(A), assertz(bvar(A)) ; true ), member(''(C,_),B), \+bref(C,A), assertz(bref(C,A)), \+bvar(C), assertz(bvar(C)), fail ; true ). bval(''). bval(''). brel(''(A,_),''(B,_)) :- bref(A,B), !. brel(A,''(B,_)) :- bref(C,B), brel(A,''(C,_)). bpar([],[]) :- !. bpar([''(A,_)|B],[A|C]) :- bpar(B,C). bget(A,B,1.0) :- memberchk(A,B), !. bget(''(A,''),B,0.0) :- memberchk(''(A,''),B), !. bget(''(A,''),B,C) :- ( memberchk(''(A,''),B), !, C is 0.0 ; !, bget(''(A,''),B,D), C is 1-D ). bget(A,B,C) :- ( bgot(A,B,C) -> true ; ( member(X,B), brel(A,X), member(G,B), findall(Y, ( member(Z,[A|B]), brel(G,Z) ), [] ), del(B,G,H), !, bget(G,[A|H],U), bget(A,H,V), bget(G,H,W), ( W < 1e-15 -> C is 0.5 ; E is U*V/W, bmin([E,1.0],C) ) ; findall([Z,Y], ( bcnd([A|O],P), bcon(O,B,Q), Z is P*Q, bpar(O,Y) ), L ), findall(Z, ( member([_,Z],L) ), N ), distinct(N,I), findall(Z, ( member(Y,I), findall(P, ( member([P,Y],L) ), Q ), sum(Q,R), length(Q,S), length(Y,T), ( Q = [] -> Z is 0.0 ; D is 2**(T-ceiling(log(S)/log(2))), ( D < 1 -> Z is R*D ; Z is R ) ) ), J ), ( J = [] -> C is 0.0 ; bmax(J,C) ) ), assertz(bgot(A,B,C)) ). bcon([],_,1.0) :- !. bcon(_,B,0.5) :- inconsistent(B), !. bcon([A|B],C,D) :- bget(A,C,E), bcon(B,[A|C],F), D is E*F. exec(A,B) :- ( shell(A,B) -> true ; system(A,B) ), ( B =:= 0 -> true ; throw(exec_error(A)) ). optional(A) :- call(A). optional(_). if_then_else(A,B,C) :- ( call(A) -> call(B) ; call(C) ). soft_cut(A,B,C) :- ( call(A) *-> call(B) ; call(C) ). inv(false,true). inv(true,false). +(A,B,C) :- plus(A,B,C). lookup(A,B) :- table(A,B), !. lookup(A,B) :- var(A), nb_getval(table,M), N is M+1, nb_setval(table,N), atom_number(I,N), atomic_list_concat(['table_entry_',I],A), assertz(table(A,B)). upcase_string([],[]). upcase_string([A|B],[C|D]) :- code_type(A,to_lower(C)), upcase_string(B,D). downcase_string([],[]). downcase_string([A|B],[C|D]) :- code_type(A,to_upper(C)), downcase_string(B,D). escape_string([],[]) :- !. escape_string([0'\n|A],[0'\\,0'n|B]) :- !, escape_string(A,B). escape_string([0'\"|A],[0'\\,0'"|B]) :- !, escape_string(A,B). escape_string([0'\\|A],[0'\\,0'\\|B]) :- !, escape_string(A,B). escape_string([0'\t|A],[0'\\,0't|B]) :- !, escape_string(A,B). escape_string([0'\r|A],[0'\\,0'r|B]) :- !, escape_string(A,B). escape_string([A|B],[A|C]) :- escape_string(B,C). quant(''(_,_),allv) :- !. quant(answer(''(_,_)),allv) :- !. quant(answer(''(_,_)),allv) :- !. quant(answer(varpred(_,P,_)),allv) :- P == '', !. quant(_,some). labelvars(cn([A|B]),C,D) :- !, quant(A,Q), labelvars(A,C,E,Q), ( B = [F] -> true ; F = cn(B) ), labelvars(F,E,D). labelvars(A,B,C) :- quant(A,Q), labelvars(A,B,C,Q). :- if(current_predicate(numbervars/4)). labelvars(A,B,C,D) :- numbervars(A,B,C,[functor_name(D),attvar(bind)]). :- else. labelvars(some(A),A,B,some) :- !, B is A+1. labelvars(allv(A),A,B,allv) :- !, B is A+1. labelvars(avar(A),A,B,avar) :- !, B is A+1. labelvars(A,B,B,_) :- atomic(A), !. labelvars([A|B],C,D,Q) :- !, labelvars(A,C,E,Q), labelvars(B,E,D,Q). labelvars(A,B,C,Q) :- nonvar(A), functor(A,_,D), labelvars(0,D,A,B,C,Q). labelvars(A,A,_,B,B,_) :- !. labelvars(A,B,C,D,E,Q) :- F is A+1, arg(F,C,G), labelvars(G,D,H,Q), labelvars(F,B,C,H,E,Q). :- endif. :- if(\+current_predicate(terms:variables_within_term/3)). variables_within_term(A,B,C) :- term_variables(B,D), copy_term_nat([A,D],[E,F]), labelvars([E,F],0,_), findall(G, ( member(G,E), member(G,F) ), C ). :- endif. getvars(A,B) :- atomic(A), !, ( atom(A), sub_atom(A,0,22,_,' B = [A] ; B = [] ). getvars([],[]) :- !. getvars([A|B],C) :- getvars(A,D), getvars(B,E), append(D,E,F), !, distinct(F,C). getvars(semantics(A),B) :- !, findall(T, ( graph(T,A) ), L ), clist(L,C), getvars(C,B). getvars(A,B) :- A =.. C, getvars(C,B). getnumber(A,A) :- number(A), !. getnumber(A,epsilon) :- nonvar(A), A = '', !. getnumber(literal(A,type('')),B) :- !, ground(A), phrase(datetime(B),A). getnumber(literal(A,type('')),B) :- !, ground(A), phrase(date(B),A). getnumber(literal(A,type('')),B) :- !, ground(A), phrase(time(B),A). getnumber(literal(A,type('')),B) :- !, ground(A), phrase(duration(B),A). getnumber(literal(A,type('')),B) :- !, ground(A), phrase(yearmonthduration(B),A). getnumber(literal(A,type('')),B) :- !, ground(A), phrase(daytimeduration(B),A). getnumber(literal(A,_),B) :- ground(A), numeral(A,C), number_codes(B,C). getint(A,B) :- getnumber(A,C), B is integer(round(C)). getbool(literal("false",type('')),false). getbool(literal("true",type('')),true). getbool(false,false). getbool(true,true). getlist(A,A) :- var(A), !. getlist('',[]) :- !. getlist([],[]) :- !. getlist([A|B],[C|D]) :- getlist(A,C), !, getlist(B,D). getlist([A|B],[A|D]) :- !, getlist(B,D). getlist(A,[B|C]) :- ''(A,B), ''(A,D), getlist(D,C). preformat([],[]) :- !. preformat([literal(A,void)|B],[C|D]) :- !, atom_codes(C,A), preformat(B,D). preformat([A|B],[A|D]) :- preformat(B,D). numeral([0'-,0'.|A],[0'-,0'0,0'.|A]) :- !. numeral([0'+,0'.|A],[0'+,0'0,0'.|A]) :- !. numeral([0'.|A],[0'0,0'.|A]) :- !. numeral(A,B) :- append([C,[0'.,0'e],D],A), append([C,[0'.,0'0,0'e],D],B), !. numeral(A,B) :- append([C,[0'.,0'E],D],A), append([C,[0'.,0'0,0'E],D],B), !. numeral(A,B) :- last(A,0'.), append(A,[0'0],B), !. numeral(A,A). dtlit([literal(A,void),''],B) :- ( number(B) -> datetime(B,A) ; B = date(Year,Month,Day,Hour,Minute,Second,Offset,_,_), datetime(Year,Month,Day,Hour,Minute,Second,Offset,A) ), !. dtlit([literal(A,void),''],B) :- ( number(B) -> date(B,A) ; B = date(Year,Month,Day,_,_,_,Offset,_,_), date(Year,Month,Day,Offset,A) ), !. dtlit([literal(A,void),''],B) :- ( number(B) -> time(B,A) ; B = date(_,_,_,Hour,Minute,Second,Offset,_,_), time(Hour,Minute,Second,Offset,A) ), !. dtlit([literal(A,void),''],B) :- number(B), !, daytimeduration(B,A). dtlit([literal(A,void),''],B) :- number(B), !, yearmonthduration(B,A). dtlit([literal(A,void),''],B) :- number(B), !, daytimeduration(B,A). dtlit([literal(A,void),''],B) :- atomic(B), \+sub_atom(B,0,1,_,'<'), !, atom_codes(B,A). dtlit([literal(A,void),B],literal(A,type(B))). :- if(\+current_predicate(get_time/1)). get_time(A) :- datime(B), mktime(B,C), A is C*1.0. :- endif. memotime(datime(A,B,C,D,E,F),G) :- ( mtime(datime(A,B,C,D,E,F),G) -> true ; date_time_stamp(date(A,B,C,D,E,F,0,-,-),G), assertz(mtime(datime(A,B,C,D,E,F),G)) ). datetime(A) --> int(B), "-", int(C), "-", int(D), "T", int(E), ":", int(F), ":", decimal(G), timezone(H), { I is -H, date_time_stamp(date(B,C,D,E,F,G,I,-,-),A) }. datetime(A,B,C,D,E,F,G) --> int(A), "-", int(B), "-", int(C), "T", int(D), ":", int(E), ":", decimal(F), timezone(G). date(A) --> int(B), "-", int(C), "-", int(D), timezone(H), { I is -H, date_time_stamp(date(B,C,D,0,0,0,I,-,-),A) }. date(A,B,C,D) --> int(A), "-", int(B), "-", int(C), timezone(D). time(A) --> int(B), ":", int(C), ":", decimal(D), timezone(E), { B = 24 -> A is C*60+D-E ; A is B*3600+C*60+D-E }. time(A,B,C,D) --> int(A), ":", int(B), ":", decimal(C), timezone(D). duration(A) --> dsign(B), "P", years(C), months(D), days(E), dtime(F), { A is B*(C*31556952+D*2629746+E*86400.0+F) }. yearmonthduration(A) --> dsign(B), "P", years(C), months(D), { A is B*(C*12+D) }. daytimeduration(A) --> dsign(B), "P", days(C), dtime(D), { A is B*(C*86400.0+D) }. timezone(A) --> int(B), !, ":", int(C), { A is B*3600+C*60 }. timezone(0) --> "Z", !. timezone(0) --> []. dsign(1) --> "+". dsign(-1) --> "-". dsign(1) --> []. dtime(A) --> "T", !, hours(B), minutes(C), seconds(D), { A is B*3600+C*60+D }. dtime(0) --> []. years(A) --> int(A), "Y". years(0) --> []. months(A) --> int(A), "M". months(0) --> []. days(A) --> int(A), "D". days(0) --> []. hours(A) --> int(A), "H". hours(0) --> []. minutes(A) --> int(A), "M". minutes(0) --> []. seconds(A) --> decimal(A), "S". seconds(0) --> []. int(A) --> sgn(B), digit(C), digits(D), { number_codes(A,[B,C|D]) }. decimal(A) --> sgn(B), digit(C), digits(D), fraction(E), { append([B,C|D],E,F), number_codes(G,F), A is G*1.0 }. sgn(0'+) --> "+". sgn(0'-) --> "-". sgn(0'+) --> []. fraction([0'.,A|B]) --> ".", !, digit(A), digits(B). fraction([]) --> []. digits([A|B]) --> digit(A), digits(B). digits([]) --> []. digit(A) --> [A], { code_type(A,digit) }. datetime(A,B) :- stamp_date_time(A,date(Year,Month,Day,Hour,Minute,Second,_,_,_),0), number_codes(Year,C), ncodes(Month,D), ncodes(Day,E), ncodes(Hour,F), ncodes(Minute,G), ncodes(Second,H), append([C,"-",D,"-",E,"T",F,":",G,":",H,"Z"],B). datetime(Year,Month,Day,Hour,Minute,Second,Offset,B) :- number_codes(Year,C), ncodes(Month,D), ncodes(Day,E), ncodes(Hour,F), ncodes(Minute,G), ncodes(Second,H), ( Offset =:= 0 -> append([C,"-",D,"-",E,"T",F,":",G,":",H,"Z"],B) ; ( Offset > 0 -> I = "-", OHour is Offset//3600 ; I = "+", OHour is -Offset//3600 ), ncodes(OHour,J), OMinute is (Offset mod 3600)//60, ncodes(OMinute,K), append([C,"-",D,"-",E,"T",F,":",G,":",H,I,J,":",K],B) ). ncodes(A,B) :- number_codes(A,D), ( A < 10 -> append(["0",D],B) ; B = D ). date(A,B) :- N is A+3600*12, stamp_date_time(N,date(Year,Month,Day,_,_,_,_,_,_),0), number_codes(Year,C), ncodes(Month,D), ncodes(Day,E), Offset is (round(floor(N)) mod 86400) - 3600*12, ( Offset =:= 0 -> append([C,"-",D,"-",E,"Z"],B) ; ( Offset > 0 -> I = "-", OHour is Offset//3600 ; I = "+", OHour is -Offset//3600 ), ncodes(OHour,J), OMinute is (Offset mod 3600)//60, ncodes(OMinute,K), append([C,"-",D,"-",E,I,J,":",K],B) ). date(Year,Month,Day,Offset,B) :- number_codes(Year,C), ncodes(Month,D), ncodes(Day,E), ( Offset =:= 0 -> append([C,"-",D,"-",E,"Z"],B) ; ( Offset > 0 -> I = "-", OHour is Offset//3600 ; I = "+", OHour is -Offset//3600 ), ncodes(OHour,J), OMinute is (Offset mod 3600)//60, ncodes(OMinute,K), append([C,"-",D,"-",E,I,J,":",K],B) ). time(A,B) :- stamp_date_time(A,date(_,_,_,Hour,Minute,Second,_,_,_),0), ncodes(Hour,F), ncodes(Minute,G), ncodes(Second,H), append([F,":",G,":",H,"Z"],B). time(Hour,Minute,Second,Offset,B) :- ncodes(Hour,F), ncodes(Minute,G), ncodes(Second,H), ( Offset =:= 0 -> append([F,":",G,":",H,"Z"],B) ; ( Offset > 0 -> I = "-", OHour is Offset//3600 ; I = "+", OHour is -Offset//3600 ), ncodes(OHour,J), OMinute is (Offset mod 3600)//60, ncodes(OMinute,K), append([F,":",G,":",H,I,J,":",K],B) ). yearmonthduration(A,B) :- ( A < 0 -> C = "-" ; C = "" ), D is abs(A), E is D//12, number_codes(E,Years), F is D-(D//12)*12, number_codes(F,Months), append([C,"P",Years,"Y",Months,"M"],B). daytimeduration(A,B) :- AInt is round(floor(A)), AFrac is A-AInt, ( AInt < 0 -> C = "-" ; C = "" ), D is abs(AInt), E is D//86400, number_codes(E,Days), F is (D-(D//86400)*86400)//3600, number_codes(F,Hours), G is (D-(D//3600)*3600)//60, number_codes(G,Minutes), H is D-(D//60)*60+AFrac, number_codes(H,Seconds), append([C,"P",Days,"DT",Hours,"H",Minutes,"M",Seconds,"S"],B). resolve_slash(A,B) :- atom_codes(A,C), resolve_slash_codes(C,D), atom_codes(B,D). resolve_slash_codes([],[]). resolve_slash_codes([A|B],[C|D]) :- ( A = 0'\\ -> C = 0'/ ; C = A ), resolve_slash_codes(B,D). resolve_uri(A,_,A) :- sub_atom(A,_,1,_,':'), !. resolve_uri('',A,A) :- !. resolve_uri('#',A,B) :- !, atomic_list_concat([A,'#'],B). resolve_uri(A,B,A) :- \+sub_atom(B,_,1,_,':'), !. resolve_uri(A,B,C) :- so_uri(U), atom_length(U,V), sub_atom(A,0,1,_,'#'), sub_atom(B,0,V,_,U), !, atomic_list_concat([B,A],C). resolve_uri(A,B,C) :- so_uri(U), atom_length(U,V), sub_atom(A,0,1,_,'/'), sub_atom(B,0,V,D,U), sub_atom(B,V,D,_,E), sub_atom(E,F,1,_,'/'), !, sub_atom(E,0,F,_,G), atomic_list_concat([U,G,A],C). resolve_uri(A,B,C) :- so_uri(U), atom_length(U,V), sub_atom(B,0,V,D,U), sub_atom(B,V,D,_,E), sub_atom(E,F,1,G,'/'), sub_atom(E,_,G,0,H), \+sub_atom(H,_,_,_,'/'), sub_atom(E,0,F,_,I), !, atomic_list_concat([U,I,'/',A],C). resolve_uri(A,_,_) :- nb_getval(line_number,Ln), throw(unresolvable_relative_uri(A,after_line(Ln))). so_uri('http://'). so_uri('https://'). so_uri('ftp://'). so_uri('file://'). prolog_sym(abolish,abolish,rel). prolog_sym(abort,abort,rel). prolog_sym(abs,abs,func). prolog_sym(absolute_file_name,absolute_file_name,rel). prolog_sym(acos,acos,func). prolog_sym(acosh,acosh,func). prolog_sym(acyclic_term,acyclic_term,rel). prolog_sym(add_to_array_element,add_to_array_element,rel). prolog_sym(alarm,alarm,rel). prolog_sym(all,all,rel). prolog_sym(always_prompt_user,always_prompt_user,rel). prolog_sym(append,append,rel). prolog_sym(arg,arg,rel). prolog_sym(arithmetic_equal,=:=,rel). prolog_sym(arithmetic_greater_than,>,rel). prolog_sym(arithmetic_greater_than_or_equal,>=,rel). prolog_sym(arithmetic_less_than,<,rel). prolog_sym(arithmetic_less_than_or_equal,=<,rel). prolog_sym(arithmetic_not_equal,=\=,rel). prolog_sym(array,array,rel). prolog_sym(array_element,array_element,rel). prolog_sym(asin,asin,func). prolog_sym(asinh,asinh,func). prolog_sym(assert,assert,rel). prolog_sym(assert_static,assert_static,rel). prolog_sym(asserta,asserta,rel). prolog_sym(asserta_static,asserta_static,rel). prolog_sym(assertz,assertz,rel). prolog_sym(assertz_static,assertz_static,rel). prolog_sym(at_end_of_stream,at_end_of_stream,rel). prolog_sym(atan,atan,func). prolog_sym(atan2,atan2,func). prolog_sym(atanh,atanh,func). prolog_sym(atom,atom,rel). prolog_sym(atom_chars,atom_chars,rel). prolog_sym(atom_codes,atom_codes,rel). prolog_sym(atom_concat,atom_concat,rel). prolog_sym(atom_length,atom_length,rel). prolog_sym(atom_number,atom_number,rel). prolog_sym(atomic,atomic,rel). prolog_sym(atomic_concat,atomic_concat,rel). prolog_sym(atomic_list_concat,atomic_list_concat,rel). prolog_sym(b_getval,b_getval,rel). prolog_sym(b_setval,b_setval,rel). prolog_sym(bagof,bagof,rel). prolog_sym(bb_delete,bb_delete,rel). prolog_sym(bb_get,bb_get,rel). prolog_sym(bb_put,bb_put,rel). prolog_sym(bb_update,bb_update,rel). prolog_sym(between,between,rel). prolog_sym(break,break,rel). prolog_sym('C','C',rel). prolog_sym(call,call,rel). prolog_sym(call_count,call_count,rel). prolog_sym(call_count_data,call_count_data,rel). prolog_sym(call_count_reset,call_count_reset,rel). prolog_sym(call_residue,call_residue,rel). prolog_sym(call_residue_vars,call_residue_vars,rel). prolog_sym(call_with_args,call_with_args,rel). prolog_sym(callable,callable,rel). prolog_sym(catch,catch,rel). prolog_sym(cd,cd,rel). prolog_sym(char_code,char_code,rel). prolog_sym(char_conversion,char_conversion,rel). prolog_sym(char_type,char_type,rel). prolog_sym(character_count,character_count,rel). prolog_sym(ceiling,ceiling,func). prolog_sym(clause,clause,rel). prolog_sym(close,close,rel). prolog_sym(close_static_array,close_static_array,rel). prolog_sym(code_type,code_type,rel). prolog_sym(compare,compare,rel). prolog_sym(compound,compound,rel). prolog_sym(conjunction,',',rel). prolog_sym(copy_term,copy_term,rel). prolog_sym(cos,cos,func). prolog_sym(cosh,cosh,func). prolog_sym(cputime,cputime,func). prolog_sym(create_mutable,create_mutable,rel). prolog_sym(create_prolog_flag,create_prolog_flag,rel). prolog_sym(current_atom,current_atom,rel). prolog_sym(current_char_conversion,current_char_conversion,rel). prolog_sym(current_host,current_host,rel). prolog_sym(current_input,current_input,rel). prolog_sym(current_key,current_key,rel). prolog_sym(current_line_number,current_line_number,rel). prolog_sym(current_module,current_module,rel). prolog_sym(current_op,current_op,rel). prolog_sym(current_output,current_output,rel). prolog_sym(current_predicate,current_predicate,rel). prolog_sym(current_prolog_flag,current_prolog_flag,rel). prolog_sym(current_stream,current_stream,rel). prolog_sym(cut,!,rel). prolog_sym(cyclic_term,cyclic_term,rel). prolog_sym(date_time_stamp,date_time_stamp,rel). prolog_sym(date_time_value,date_time_value,rel). prolog_sym(day_of_the_week,day_of_the_week,rel). prolog_sym(db_reference,db_reference,rel). prolog_sym(delete,delete,rel). prolog_sym(dif,dif,rel). prolog_sym(disjunction,;,rel). prolog_sym(display,display,rel). prolog_sym(div,div,func). prolog_sym(duplicate_term,duplicate_term,rel). prolog_sym(dynamic,dynamic,rel). prolog_sym(dynamic_predicate,dynamic_predicate,rel). prolog_sym(e,e,func). prolog_sym(environ,environ,rel). prolog_sym(epsilon,epsilon,func). prolog_sym(erase,erase,rel). prolog_sym(eraseall,eraseall,rel). prolog_sym(erased,erased,rel). prolog_sym(erf,erf,func). prolog_sym(erfc,erfc,func). prolog_sym(exception,exception,rel). prolog_sym(exists,exists,rel). prolog_sym(exp,exp,func). prolog_sym(fail,fail,rel). prolog_sym(false,false,rel). prolog_sym(file_base_name,file_base_name,rel). prolog_sym(file_name_extension,file_name_extension,rel). prolog_sym(fileerrors,fileerrors,rel). prolog_sym(findall,findall,rel). prolog_sym(flatten,flatten,rel). prolog_sym(float,float,func). prolog_sym(float_fractional_part,float_fractional_part,func). prolog_sym(float_integer_part,float_integer_part,func). prolog_sym(floor,floor,func). prolog_sym(flush_output,flush_output,rel). prolog_sym(forall,forall,rel). prolog_sym(format,format,rel). prolog_sym(format_time,format_time,rel). prolog_sym(freeze,freeze,rel). prolog_sym(frozen,frozen,rel). prolog_sym(functor,functor,rel). prolog_sym(garbage_collect,garbage_collect,rel). prolog_sym(garbage_collect_atoms,garbage_collect_atoms,rel). prolog_sym(gc,gc,rel). prolog_sym(gcd,gcd,func). prolog_sym(get,get,rel). prolog_sym(get_byte,get_byte,rel). prolog_sym(get_char,get_char,rel). prolog_sym(get_code,get_code,rel). prolog_sym(get_mutable,get_mutable,rel). prolog_sym(get_time,get_time,rel). prolog_sym(get_value,get_value,rel). prolog_sym(get0,get0,rel). prolog_sym(getcwd,getcwd,rel). prolog_sym(global,global,func). prolog_sym(ground,ground,rel). prolog_sym(grow_heap,grow_heap,rel). prolog_sym(grow_stack,grow_stack,rel). prolog_sym(halt,halt,rel). prolog_sym(heapused,heapused,func). prolog_sym(hostname_address,hostname_address,rel). prolog_sym(if,soft_cut,rel). prolog_sym(if_then,->,rel). prolog_sym(if_then_else,if_then_else,rel). prolog_sym(ignore,ignore,rel). prolog_sym(incore,incore,rel). prolog_sym(inf,inf,func). prolog_sym(initialization,initialization,rel). prolog_sym(instance,instance,rel). prolog_sym(integer,integer,func). prolog_sym(integer_conjunction,/\,func). prolog_sym(integer_disjunction,\/,func). prolog_sym(integer_exclusive_disjunction,#,func). prolog_sym(integer_left_logical_shift,<<,func). prolog_sym(integer_negation,\,func). prolog_sym(integer_power,^,func). prolog_sym(integer_quotient,//,func). prolog_sym(integer_right_logical_shift,>>,func). prolog_sym(is,is,rel). prolog_sym(is_list,is_list,rel). prolog_sym(is_mutable,is_mutable,rel). prolog_sym(is_stream,is_stream,rel). prolog_sym(keysort,keysort,rel). prolog_sym(key_statistics,key_statistics,rel). prolog_sym(last,last,rel). prolog_sym(length,length,rel). prolog_sym(lgamma,lgamma,func). prolog_sym(line_count,line_count,rel). prolog_sym(line_position,line_position,rel). prolog_sym(list_concat,list_concat,rel). prolog_sym(listing,listing,rel). prolog_sym(local,local,func). prolog_sym(log,log,func). prolog_sym(log10,log10,func). prolog_sym(lsb,lsb,func). prolog_sym(max,max,func). prolog_sym(max_list,max_list,rel). prolog_sym(member,member,rel). prolog_sym(memberchk,memberchk,rel). prolog_sym(message_to_string,message_to_string,rel). prolog_sym(min,min,func). prolog_sym(min_list,min_list,rel). prolog_sym(minus,-,func). prolog_sym(mmapped_array,mmapped_array,rel). prolog_sym(mod,mod,func). prolog_sym(msb,msb,func). prolog_sym(name,name,rel). prolog_sym(nan,nan,func). prolog_sym(nb_current,nb_current,rel). prolog_sym(nb_delete,nb_delete,rel). prolog_sym(nb_getval,nb_getval,rel). prolog_sym(nb_linkarg,nb_linkarg,rel). prolog_sym(nb_linkval,nb_linkval,rel). prolog_sym(nb_setarg,nb_setarg,rel). prolog_sym(nb_set_shared_arg,nb_set_shared_arg,rel). prolog_sym(nb_set_shared_val,nb_set_shared_val,rel). prolog_sym(nb_setval,nb_setval,rel). prolog_sym(new_variables_in_term,new_variables_in_term,rel). prolog_sym(nl,nl,rel). prolog_sym(nofileerrors,nofileerrors,rel). prolog_sym(nogc,nogc,rel). prolog_sym(nonvar,nonvar,rel). prolog_sym(not_provable,\+,rel). prolog_sym(not_unifiable,\=,rel). prolog_sym(nth,nth,rel). prolog_sym(nth_clause,nth_clause,rel). prolog_sym(nth_instance,nth_instance,rel). prolog_sym(nth0,nth0,rel). prolog_sym(nth1,nth1,rel). prolog_sym(number,number,rel). prolog_sym(number_atom,number_atom,rel). prolog_sym(number_chars,number_chars,rel). prolog_sym(number_codes,number_codes,rel). prolog_sym(numbervars,numbervars,rel). prolog_sym(numlist,numlist,rel). prolog_sym(on_signal,on_signal,rel). prolog_sym(once,once,rel). prolog_sym(op,op,rel). prolog_sym(open,open,rel). prolog_sym(parse_time,parse_time,rel). prolog_sym(peek_byte,peek_byte,rel). prolog_sym(peek_char,peek_char,rel). prolog_sym(peek_code,peek_code,rel). prolog_sym(permutation,permutation,rel). prolog_sym(phrase,phrase,rel). prolog_sym(pi,pi,func). prolog_sym(plus,+,func). prolog_sym(popcount,popcount,func). prolog_sym(portray_clause,portray_clause,rel). prolog_sym(power,**,func). prolog_sym(predicate_property,predicate_property,rel). prolog_sym(predsort,predsort,rel). prolog_sym(primitive,primitive,rel). prolog_sym(print,print,rel). prolog_sym(print_message,print_message,rel). prolog_sym(print_message_lines,print_message_lines,rel). prolog_sym(product,*,func). prolog_sym(profile_data,profile_data,rel). prolog_sym(profile_reset,profile_reset,rel). prolog_sym(profinit,profinit,rel). prolog_sym(profoff,profoff,rel). prolog_sym(profon,profon,rel). prolog_sym(prolog_flag,prolog_flag,rel). prolog_sym(prolog_initialization,prolog_initialization,rel). prolog_sym(prolog_load_context,prolog_load_context,rel). prolog_sym(prompt,prompt,rel). prolog_sym(put,put,rel). prolog_sym(put_byte,put_byte,rel). prolog_sym(put_char,put_char,rel). prolog_sym(put_code,put_code,rel). prolog_sym(putenv,putenv,rel). prolog_sym(quotient,/,func). prolog_sym(random,random,func). prolog_sym(rational,rational,func). prolog_sym(rationalize,rationalize,func). prolog_sym(read,read,rel). prolog_sym(read_term,read_term,rel). prolog_sym(recorda,recorda,rel). prolog_sym(recorda_at,recorda_at,rel). prolog_sym(recordaifnot,recordaifnot,rel). prolog_sym(recorded,recorded,rel). prolog_sym(recordz,recordz,rel). prolog_sym(recordz_at,recordz_at,rel). prolog_sym(recordzifnot,recordzifnot,rel). prolog_sym(rem,rem,func). prolog_sym(remove_duplicates,remove_duplicates,rel). prolog_sym(rename,rename,rel). prolog_sym(repeat,repeat,rel). prolog_sym(reset_static_array,reset_static_array,rel). prolog_sym(resize_static_array,resize_static_array,rel). prolog_sym(retract,retract,rel). prolog_sym(retractall,retractall,rel). prolog_sym(reverse,reverse,rel). prolog_sym(round,round,func). prolog_sym(same_length,same_length,rel). prolog_sym(see,see,rel). prolog_sym(seeing,seeing,rel). prolog_sym(seen,seen,rel). prolog_sym(select,select,rel). prolog_sym(selectchk,selectchk,rel). prolog_sym(set_input,set_input,rel). prolog_sym(set_output,set_output,rel). prolog_sym(set_prolog_flag,set_prolog_flag,rel). prolog_sym(set_stream_position,set_stream_position,rel). prolog_sym(set_value,set_value,rel). prolog_sym(setarg,setarg,rel). prolog_sym(setof,setof,rel). prolog_sym(sh,sh,rel). prolog_sym(showprofres,showprofres,rel). prolog_sym(sign,sign,func). prolog_sym(simple,simple,rel). prolog_sym(sin,sin,func). prolog_sym(sinh,sinh,func). prolog_sym(skip,skip,rel). prolog_sym(socket,socket,rel). prolog_sym(socket_accept,socket_accept,rel). prolog_sym(socket_bind,socket_bind,rel). prolog_sym(socket_buffering,socket_buffering,rel). prolog_sym(socket_close,socket_close,rel). prolog_sym(socket_connect,socket_connect,rel). prolog_sym(socket_listen,socket_listen,rel). prolog_sym(socket_select,socket_select,rel). prolog_sym(sort,sort,rel). prolog_sym(source_file,source_file,rel). prolog_sym(source_location,source_location,rel). prolog_sym(sqrt,sqrt,func). prolog_sym(srandom,srandom,rel). prolog_sym(stamp_date_time,stamp_date_time,rel). prolog_sym(static_array,static_array,rel). prolog_sym(static_array_location,static_array_location,rel). prolog_sym(static_array_properties,static_array_properties,rel). prolog_sym(static_array_to_term,static_array_to_term,rel). prolog_sym(statistics,statistics,rel). prolog_sym(stream_position,stream_position,rel). prolog_sym(stream_position_data,stream_position_data,rel). prolog_sym(stream_property,stream_property,rel). prolog_sym(stream_select,stream_select,rel). prolog_sym(sub_atom,sub_atom,rel). prolog_sym(sublist,sublist,rel). prolog_sym(subsumes,subsumes,rel). prolog_sym(subsumes_chk,subsumes_chk,rel). prolog_sym(subsumes_term,subsumes_term,rel). prolog_sym(succ,succ,rel). prolog_sym(suffix,suffix,rel). prolog_sym(sum_list,sum_list,rel). prolog_sym(sumlist,sumlist,rel). prolog_sym(system,system,rel). prolog_sym(system_predicate,system_predicate,rel). prolog_sym(tab,tab,rel). prolog_sym(tan,tan,func). prolog_sym(tanh,tanh,func). prolog_sym(tell,tell,rel). prolog_sym(telling,telling,rel). prolog_sym(term_greater_than,@>,rel). prolog_sym(term_greater_than_or_equal,@>=,rel). prolog_sym(term_hash,term_hash,rel). prolog_sym(term_identical,==,rel). prolog_sym(term_less_than,@<,rel). prolog_sym(term_less_than_or_equal,@=<,rel). prolog_sym(term_not_identical,\==,rel). prolog_sym(term_variables,term_variables,rel). prolog_sym(throw,throw,rel). prolog_sym(time,time,rel). prolog_sym(time_file,time_file,rel). prolog_sym(told,told,rel). prolog_sym(true,true,rel). prolog_sym(truncate,truncate,func). prolog_sym(ttyget,ttyget,rel). prolog_sym(ttyget0,ttyget0,rel). prolog_sym(ttynl,ttynl,rel). prolog_sym(ttyput,ttyput,rel). prolog_sym(ttyskip,ttyskip,rel). prolog_sym(ttytab,ttytab,rel). prolog_sym(unifiable,unifiable,rel). prolog_sym(unify,=,rel). prolog_sym(unify_with_occurs_check,unify_with_occurs_check,rel). prolog_sym(univ,=..,rel). prolog_sym(unix,unix,rel). prolog_sym(unknown,unknown,rel). prolog_sym(update_array,update_array,rel). prolog_sym(update_mutable,update_mutable,rel). prolog_sym(var,var,rel). prolog_sym(variable_in_term,variable_in_term,rel). prolog_sym(variables_within_term,variables_within_term,rel). prolog_sym(variant,variant,rel). prolog_sym(version,version,rel). prolog_sym(when,when,rel). prolog_sym(with_output_to,with_output_to,rel). prolog_sym(write,write,rel). prolog_sym(write_canonical,write_canonical,rel). prolog_sym(write_depth,write_depth,rel). prolog_sym(write_term,write_term,rel). prolog_sym(writeln,writeln,rel). prolog_sym(writeq,writeq,rel). % Regular Expressions inspired by http://www.cs.sfu.ca/~cameron/Teaching/384/99-3/regex-plg.html regex(RE,Input,Output) :- re(Parsed_RE,RE,[]), ( RE = [0'^|_] -> Bos = true ; Bos = false ), tokenize2(Parsed_RE,Input,Output,Bos), !. tokenize2(_P_RE,[],[],true). tokenize2(P_RE,Input,Output,Bos) :- ( rematch1(P_RE,Input,_,Output) -> true ; Bos = false, Input = [_|Inp], tokenize2(P_RE,Inp,Output,Bos) ). rematch1(union(RE1,_RE2),S,U,Selected) :- rematch1(RE1,S,U,Selected). rematch1(union(_RE1,RE2),S,U,Selected) :- rematch1(RE2,S,U,Selected). rematch1(conc(RE1,RE2),S,U,Selected) :- rematch1(RE1,S,U1,Sel1), rematch1(RE2,U1,U,Sel2), append(Sel1,Sel2,Selected). rematch1(star(RE),S,U,Selected) :- rematch1(RE,S,U1,Sel1), rematch1(star(RE),U1,U,Sel2), append(Sel1,Sel2,Selected). rematch1(star(_RE),S,S,[]). rematch1(plus(RE),S,U,Selected) :- rematch1(RE,S,U1,Sel1), rematch1(star(RE),U1,U,Sel2), append(Sel1,Sel2,Selected). rematch1(group(RE),S,U,Selected) :- rematch1(RE,S,U,Sel1), append(P,U,S), append(Sel1,[P],Selected). rematch1(any,[_C1|U],U,[]). rematch1(char(C),[C|U],U,[]). rematch1(bos,S,S,[]). rematch1(eos,[],[],[]). rematch1(negSet(Set),[C|U],U,[]) :- \+charSetMember(C,Set). rematch1(posSet(Set),[C|U],U,[]) :- charSetMember(C,Set). charSetMember(C,[char(C)|_]). charSetMember(C,[range(C1,C2)|_]) :- C1 =< C, C =< C2. charSetMember(C,[_|T]) :- charSetMember(C,T). re(Z) --> basicRE(W), reTail(W,Z). reTail(W,Z) --> "|", basicRE(X), reTail(union(W,X),Z). reTail(W,W) --> []. basicRE(Z) --> simpleRE(W), basicREtail(W,Z). basicREtail(W,Z) --> simpleRE(X), basicREtail(conc(W,X),Z). basicREtail(W,W) --> []. simpleRE(Z) --> elementalRE(W), simpleREtail(W,Z). simpleREtail(W,star(W)) --> "*". simpleREtail(W,plus(W)) --> "+". simpleREtail(W,W) --> []. elementalRE(any) --> ".". elementalRE(group(X)) --> "(", re(X), ")". elementalRE(bos) --> "^". elementalRE(eos) --> "$". elementalRE(char(C)) --> [C], { \+re_metachar([C]) }. elementalRE(char(C)) --> "\\", [C], { re_metachar([C]) }. elementalRE(negSet(X)) --> "[^", !, setItems(X), "]". elementalRE(posSet(X)) --> "[", setItems(X), "]". re_metachar("\\"). re_metachar("\|"). re_metachar("*"). re_metachar("+"). re_metachar("\."). re_metachar("["). re_metachar("$"). re_metachar("("). re_metachar(")"). setItems([Item1|MoreItems]) --> setItem(Item1), setItems(MoreItems). setItems([Item1]) --> setItem(Item1). setItem(char(C)) --> [C], { \+set_metachar([C]) }. setItem(char(C)) --> "\\", [C], { set_metachar([C]) }. setItem(range(A,B)) --> setItem(char(A)), "-", setItem(char(B)). set_metachar("\\"). set_metachar("]"). set_metachar("-"). regexp_wildcard([],[]) :- !. regexp_wildcard([0'*|A],[0'.,0'*|B]) :- !, regexp_wildcard(A,B). regexp_wildcard([A|B],[A|C]) :- regexp_wildcard(B,C). % --------------------------------------------------------------- % N3 Parser % according to http://www.w3.org/2000/10/swap/grammar/n3-ietf.txt % inspired by http://code.google.com/p/km-rdf/wiki/Henry % --------------------------------------------------------------- barename(BareName) --> [name(BareName)]. barename_csl([BareName|Tail]) --> barename(BareName), !, barename_csl_tail(Tail). barename_csl([]) --> []. barename_csl_tail([BareName|Tail]) --> [','], !, barename(BareName), barename_csl_tail(Tail). barename_csl_tail([]) --> []. boolean(true) --> ['@',name('true')], !. boolean(true) --> [name('true')], !. boolean(false) --> ['@',name('false')], !. boolean(false) --> [name('false')], !. boolean(Boolean) --> literal(Literal,type(T)), { ( nb_getval(cmod,data) -> T = '' ; T = '\'\'' ), memberchk([Boolean,Literal],[[true,"true"],[true,"1"],[false,"false"],[false,"0"]]) }. declaration --> ['@',name(base)], !, explicituri(U), { base_uri(V), resolve_uri(U,V,URI), retractall(base_uri(_)), assertz(base_uri(URI)) }. declaration --> ['@',name(keywords)], !, barename_csl(List), { retractall(keywords(_)), assertz(keywords(List)) }. declaration --> ['@',name(prefix)], prefix(Prefix), explicituri(U), { base_uri(V), resolve_uri(U,V,URI), retractall(ns(Prefix,_)), assertz(ns(Prefix,URI)), put_pfx(Prefix,URI) }. document(Triples) --> statements_optional(Triples), !. document([]) --> { nb_getval(line_number,Ln), throw(invalid_document(after_line(Ln))) }. dtlang(lang(Lang)) --> ['@'], !, langcode(Lang). dtlang(type(Datatype)) --> ['^','^'], !, symbol(Datatype). dtlang(void) --> []. existential --> ['@',name(forSome)], !, symbol_csl(Symbols), { nb_getval(fdepth,D), forall( ( member(S,Symbols) ), ( gensym(qe,Q), asserta(qevar(S,Q,D)) ) ) }. explicituri(ExplicitURI) --> [relative_uri(ExplicitURI)]. expression(Node,T) --> pathitem(N1,T1), pathtail(N1,P,N2,T2), { append(T1,T2,T3), ( ( nb_getval(cmod,data) -> P = '' ; P = '\'\'' ) -> distinct(N1,Distinct), dlist(Distinct,Node), T = [] ; ( ( nb_getval(cmod,data) -> P = '' ; P = '\'\'' ) -> Node = answer(N1), T = [] ; Node = N2, T = T3 ) ), ( keywords(List), memberchk(Node,List) -> nb_getval(line_number,Ln), throw(invalid_keyword_use(Node,after_line(Ln))) ; true ) }. formulacontent(Formula) --> statementlist(List), { distinct(List,Distinct), clist(Distinct,Formula) }. langcode(Langcode) --> [name(Name)], { ( nb_getval(cmod,data) -> Name = Langcode ; atomic_list_concat(['\'',Name,'\''],Langcode) ) }. literal(Literal,DtLang) --> string(Literal), dtlang(DtLang). numericliteral(Num) --> [numeric(_,NumC)], { number_codes(Num,NumC) }. object(Node,Triples) --> expression(Node,Triples). objecttail(Subject,Verb,[Triple|T]) --> [','], !, object(Object,Triples), objecttail(Subject,Verb,Tail), { append(Triples,Tail,T), ( atom(Verb) -> true ; ( Verb = isof(_) -> true ; nb_getval(line_number,Ln), throw(unexpected_atom(Verb,after_line(Ln))) ) ), ( Verb = isof(V) -> ( \+sub_atom(V,0,1,_,'_') -> Triple =.. [V,Object,Subject] ; Triple = varpred(Object,V,Subject) ) ; ( \+sub_atom(Verb,0,1,_,'_') -> Triple =.. [Verb,Subject,Object] ; Triple = varpred(Subject,Verb,Object) ) ) }. objecttail(_,_,[]) --> []. pathitem([],[]) --> symbol(S), { ( nb_getval(cmod,data) -> S = '' ; S = '\'\'' ) }, !. pathitem(Name,[]) --> symbol(S), !, { ( ( quvar(S,N,0), ! ; qevar(S,N,1) ) -> ( nb_getval(fdepth,0) -> ( nb_getval(cmod,data) -> atomic_list_concat([''],Name) ; atomic_list_concat(['\'\''],Name) ) ; atom_concat('_',N,Name) ) ; ( atom(S), ( nb_getval(cmod,data) -> atom_concat('',A) ; atom_concat('\'\'',A) ) -> ( B = 'C' -> Pred = '\'C\'' ; ( B = disjunction -> Pred = '\';\'' ; ( prolog_sym(B,Pred,_) -> true ; nb_getval(line_number,Ln), throw(invalid_prolog_builtin(B,after_line(Ln))) ) ) ), Name = prolog:Pred ; ( intern(S) -> true ; assertz(intern(S)), ( sub_atom(S,0,1,_,'\'') -> sub_atom(S,1,_,1,B) ; B = S ), ( current_predicate(B/2) -> true ; dynamic(B/2) ) ), Name = S ) ) }. pathitem(VarID,[]) --> [uvar(Var)], !, { atom_codes(Var,VarCodes), subst(0'-,VarCodes,0'_,VarTidy), atom_codes(VarID,[0'_|VarTidy]) }. pathitem(Number,[]) --> numericliteral(Number), !. pathitem(Boolean,[]) --> boolean(Boolean), !. pathitem(Atom,[]) --> literal(Literal,type(T)), { ( nb_getval(cmod,data) -> T = '' ; T = '\'\'' ) }, !, { atom_codes(A,Literal), ( nb_getval(cmod,data) -> A = Atom ; atomic_list_concat(['\'',A,'\''],Atom) ) }. pathitem(literal(Literal,DtLang),[]) --> literal(Literal,DtLang), !. pathitem(BNode,Triples) --> ['['], !, { gensym(e,S), ( nb_getval(fdepth,0) -> ( nb_getval(cmod,data) -> atomic_list_concat([''],BN) ; atomic_list_concat(['\'\''],BN) ) ; atom_concat('_',S,BN) ) }, propertylist(BN,T), { ( ( nb_getval(cmod,data) -> memberchk(''(_,Head),T), memberchk(''(_,Tail),T) ; memberchk('\'\''(_,Head),T), memberchk('\'\''(_,Tail),T) ) -> BNode = [Head|Tail], Triples = [] ; BNode = BN, Triples = T ) }, [']']. pathitem(List,Triples) --> ['('], !, pathlist(List,Triples), [')']. pathitem(Node,[]) --> ['{'], { nb_getval(fdepth,I), J is I+1, nb_setval(fdepth,J) }, formulacontent(Node), { retractall(quvar(_,_,J)), retractall(qevar(_,_,J)), retractall(evar(_,_,J)), nb_setval(fdepth,I) }, ['}']. pathlist([Node|Rest],Triples) --> expression(Node,T), !, pathlist(Rest,Tail), { append(T,Tail,Triples) }. pathlist([],[]) --> []. pathtail(Node,Verb,PNode,[Triple|Triples]) --> ['!'], !, pathitem(Verb,Triples2), { gensym(e,S), ( nb_getval(fdepth,0) -> ( nb_getval(cmod,data) -> atomic_list_concat([''],BNode) ; atomic_list_concat(['\'\''],BNode) ) ; atom_concat('_',S,BNode) ), ( atom(Verb) -> true ; ( Verb = isof(_) -> true ; ( Verb = prolog:Pred -> true ; nb_getval(line_number,Ln), throw(unexpected_atom(Verb,after_line(Ln))) ) ) ), ( Verb = isof(V) -> ( \+sub_atom(V,0,1,_,'_') -> Triple =.. [V,BNode,Node] ; Triple = varpred(BNode,V,Node) ) ; ( Verb = prolog:Pred -> ( BNode = true -> Triple =.. [Pred|Node] ; ( BNode = false -> T =.. [Pred|Node], Triple = \+(T) ; ( prolog_sym(_,Pred,func) -> T =.. [Pred|Node], Triple = is(BNode,T) ; Triple =.. [Pred,Node,BNode] ) ) ) ; ( \+sub_atom(Verb,0,1,_,'_') -> Triple =.. [Verb,Node,BNode] ; Triple = varpred(Node,Verb,BNode) ) ) ) }, pathtail(BNode,_,PNode,Tail), { append(Triples2,Tail,Triples) }. pathtail(Node,Verb,PNode,[Triple|Triples]) --> ['^'], !, pathitem(Verb,Triples2), { gensym(e,S), ( nb_getval(fdepth,0) -> ( nb_getval(cmod,data) -> atomic_list_concat([''],BNode) ; atomic_list_concat(['\'\''],BNode) ) ; atom_concat('_',S,BNode) ), ( atom(Verb) -> true ; ( Verb = isof(_) -> true ; ( Verb = prolog:Pred -> true ; nb_getval(line_number,Ln), throw(unexpected_atom(Verb,after_line(Ln))) ) ) ), ( Verb = isof(V) -> ( \+sub_atom(V,0,1,_,'_') -> Triple =.. [V,Node,BNode] ; Triple = varpred(Node,V,BNode) ) ; ( Verb = prolog:Pred -> ( Node = true -> Triple =.. [Pred|BNode] ; ( Node = false -> T =.. [Pred|BNode], Triple = \+(T) ; ( prolog_sym(_,Pred,func) -> T =.. [Pred|BNode], Triple = is(Node,T) ; Triple =.. [Pred,BNode,Node] ) ) ) ; ( \+sub_atom(Verb,0,1,_,'_') -> Triple =.. [Verb,BNode,Node] ; Triple = varpred(BNode,Verb,Node) ) ) ) }, pathtail(BNode,_,PNode,Tail), { append(Triples2,Tail,Triples) }. pathtail(Node,void,Node,[]) --> []. prefix(Prefix) --> [Prefix:'']. propertylist(Subject,[Triple|Triples]) --> verb(Verb,Triples1), !, object(Object,Triples2), objecttail(Subject,Verb,Triples3), propertylisttail(Subject,Triples4), { append(Triples1,Triples2,Triples12), append(Triples12,Triples3,Triples123), append(Triples123,Triples4,Triples), ( atom(Verb) -> true ; ( Verb = isof(_) -> true ; ( Verb = prolog:Pred -> true ; nb_getval(line_number,Ln), throw(unexpected_atom(Verb,after_line(Ln))) ) ) ), ( Verb = isof(V) -> ( \+sub_atom(V,0,1,_,'_') -> Triple =.. [V,Object,Subject] ; Triple = varpred(Object,V,Subject) ) ; ( Verb = prolog:Pred -> ( Object = true -> Triple =.. [Pred|Subject] ; ( Object = false -> T =.. [Pred|Subject], Triple = \+(T) ; ( prolog_sym(_,Pred,func) -> T =.. [Pred|Subject], Triple = is(Object,T) ; Triple =.. [Pred,Subject,Object] ) ) ) ; ( \+sub_atom(Verb,0,1,_,'_') -> Triple =.. [Verb,Subject,Object] ; Triple = varpred(Subject,Verb,Object) ) ) ) }. propertylist(_,[]) --> []. propertylisttail(Subject,Triples) --> [';'], !, propertylist(Subject,Triples). propertylisttail(_,[]) --> []. qname(URI) --> [NS:Name], { ( ns(NS,Base) -> ( nb_getval(cmod,data) -> atomic_list_concat(['<',Base,Name,'>'],URI) ; atomic_list_concat(['\'<',Base,Name,'>\''],URI) ) ; nb_getval(line_number,Ln), throw(no_prefix_directive(NS,after_line(Ln))) ) }, !. simpleStatement(Triples) --> subject(Subject,Triples1), ( { Subject = (D1;D2) } -> { Triples = [(D1;D2)] } ; propertylist(Subject,Triples2), { append(Triples1,Triples2,Triples) } ). statement([]) --> declaration, !. statement([]) --> universal, !. statement([]) --> existential, !. statement(Statement) --> simpleStatement(Statement). statementlist(Triples) --> statement(Tr), !, statementtail(T), { append(Tr,T,Triples) }. statementlist([]) --> []. statements_optional(Triples) --> statement(Tr), [dot(Ln)], !, { nb_setval(line_number,Ln) }, statements_optional(T), { append(Tr,T,Triples) }. statements_optional([]) --> []. statementtail(T) --> [dot(Ln)], !, { nb_setval(line_number,Ln) }, statementlist(T). statementtail([]) --> []. string(Literal) --> [literal(Literal)]. subject(Node,Triples) --> expression(Node,Triples). symbol(Name) --> explicituri(U), !, { base_uri(V), resolve_uri(U,V,W), ( nb_getval(cmod,data) -> atomic_list_concat(['<',W,'>'],Name) ; atomic_list_concat(['\'<',W,'>\''],Name) ) }. symbol(Name) --> qname(Name), !. symbol(Name) --> [name(N)], !, { ( keywords(List) -> ( memberchk(N,List) -> Name = N ; ns('',Base), ( nb_getval(cmod,data) -> atomic_list_concat(['<',Base,N,'>'],Name) ; atomic_list_concat(['\'<',Base,N,'>\''],Name) ) ) ; ( memberchk(N,[true,false]) -> Name = N ; nb_getval(line_number,Ln), throw(invalid_keyword(N,after_line(Ln))) ) ) }. symbol(Name) --> [bnode(N)], { nb_getval(fdepth,D), ( evar(N,S,D) -> true ; atom_concat(N,'_',M), gensym(M,S), assertz(evar(N,S,D)) ), ( nb_getval(fdepth,0) -> ( nb_getval(cmod,data) -> atomic_list_concat([''],Name) ; atomic_list_concat(['\'\''],Name) ) ; atom_concat('_',S,Name) ) }. symbol_csl([Symbol|Tail]) --> symbol(Symbol), !, symbol_csl_tail(Tail). symbol_csl([]) --> []. symbol_csl_tail([Symbol|T]) --> [','], !, symbol(Symbol), symbol_csl_tail(T). symbol_csl_tail([]) --> []. universal --> ['@',name(forAll)], !, symbol_csl(Symbols), { nb_getval(fdepth,D), forall( ( member(S,Symbols) ), ( gensym(qu,Q), asserta(quvar(S,Q,D)) ) ) }. verb(V,[]) --> ['=','>'], !, { ( nb_getval(cmod,data) -> V = '' ; V = '\'\'' ) }. verb(V,[]) --> ['='], !, { ( nb_getval(cmod,data) -> V = '' ; V = '\'\'' ) }. verb(':-',[]) --> ['<','='], !. verb(V,[]) --> ['@',name(a)], !, { ( nb_getval(cmod,data) -> V = '' ; V = '\'\'' ) }. verb(V,[]) --> [name(a)], !, { ( nb_getval(cmod,data) -> V = '' ; V = '\'\'' ) }. verb(Node,Triples) --> ['@',name(has)], !, expression(Node,Triples). verb(isof(Node),Triples) --> ['@',name(is)], !, expression(Node,Triples), ['@',name(of)]. verb(isof(Node),Triples) --> [name(is)], !, expression(Node,Triples), [name(of)]. verb(Node,Triples) --> expression(Node,Triples). % tokenizer turtle_tokens(In,List) :- get_code(In,C0), turtle_token(C0,In,C1,Tok1), ( Tok1 == end_of_file -> List = [] ; List = [Tok1|Tokens], turtle_tokens(C1,In,Tokens) ). turtle_tokens(C0,In,List) :- ( turtle_token(C0,In,C1,H) -> true ; nb_getval(line_number,Ln), char_code(Char,C0), throw(illegal_token(char_code(Char,C0),after_line(Ln))) ), ( H == end_of_file -> List = [] ; List = [H|T], turtle_tokens(C1,In,T) ). turtle_token(-1,_,-1,end_of_file) :- !. turtle_token(0'.,In,C,dot(Ln)) :- !, nb_getval(line_number,Ln), get_code(In,C). turtle_token(0'#,In,C,Token) :- !, get_code(In,C1), skip_line(C1,In,C2), turtle_token(C2,In,C,Token). turtle_token(WS,In,C,Token) :- turtle_ws(WS), !, get_code(In,C1), turtle_token(C1,In,C,Token). turtle_token(C0,In,C,Number) :- 0'0 =< C0, C0 =< 0'9, !, turtle_number(C0,In,C,Number). turtle_token(0'-,In,C,Number) :- !, turtle_number(0'-,In,C,Number). turtle_token(0'+,In,C,Number) :- !, turtle_number(0'+,In,C,Number). turtle_token(0'",In,C,literal(Codes)) :- !, ( peek_code(In,0'") -> get_code(In,0'"), ( peek_code(In,0'") -> get_code(In,0'"), get_code(In,C1), turtle_dq_string(C1,In,C,Codes) ; get_code(In,C), Codes = [] ) ; get_code(In,C1), turtle_string(C1,In,C,Codes) ). turtle_token(0'?,In,C,uvar(Name)) :- !, get_code(In,C0), ( name(C0,In,C,Name) -> true ; C = C0, nb_getval(line_number,Ln), throw(empty_quickvar_name(after_line(Ln))) ), nb_setval(cmod,rule). turtle_token(0'_,In,C,bnode(Name)) :- peek_code(In,0':), !, get_code(In,_), get_code(In,C0), ( name(C0,In,C,Name) -> true ; C = C0, Name = '' ). turtle_token(0'<,In,C,relative_uri(URI)) :- peek_code(In,C1), C1 \== 0'=, !, get_code(In,C1), uri_chars(C1,In,C,Codes), atom_codes(URI,Codes). turtle_token(0':,In,C,Token) :- !, get_code(In,C0), ( name(C0,In,C,Name) -> Token = '':Name ; Token = '':'', C = C0 ). turtle_token(C0,In,C,Token) :- name(C0,In,C1,Name), !, ( C1 == 0': -> get_code(In,C2), ( name(C2,In,C,Name2) -> Token = (Name:Name2) ; Token = (Name:''), C = C2 ) ; Token = name(Name), C = C1 ). turtle_token(Punct,In,C,P) :- punctuation(Punct,P), ( P = '{' -> nb_setval(cmod,rule) ; true ), !, get_code(In,C). turtle_number(0'-,In,CN,numeric(T,[0'-|Codes])) :- !, get_code(In,C0), turtle_number_nn(C0,In,CN,numeric(T,Codes)). turtle_number(0'+,In,CN,numeric(T,[0'+|Codes])) :- !, get_code(In,C0), turtle_number_nn(C0,In,CN,numeric(T,Codes)). turtle_number(C0,In,CN,Value) :- turtle_number_nn(C0,In,CN,Value). turtle_number_nn(C,In,CN,numeric(Type,Codes)) :- turtle_integer_codes(C,In,CN0,Codes,T0), ( CN0 == 0'., peek_code(In,C0), 0'0 =< C0, C0 =< 0'9 -> T0 = [CN0|T1], get_code(In,C1), turtle_integer_codes(C1,In,CN1,T1,T2), ( exponent(CN1,In,CN,T2) -> Type = double ; CN = CN1, T2 = [], Type = decimal ) ; ( exponent(CN0,In,CN,T0) -> Type = double ; T0 = [], CN = CN0, Type = integer ) ). turtle_integer_codes(C0,In,CN,[C0|T0],T) :- 0'0 =< C0, C0 =< 0'9, !, get_code(In,C1), turtle_integer_codes(C1,In,CN,T0,T). turtle_integer_codes(CN,_,CN,T,T). exponent(C0,In,CN,[C0|T0]) :- e(C0), !, get_code(In,C1), optional_sign(C1,In,CN0,T0,T1), turtle_integer_codes(CN0,In,CN,T1,[]). optional_sign(C0,In,CN,[C0|T],T) :- sign(C0), !, get_code(In,CN). optional_sign(CN,_,CN,T,T). e(0'e). e(0'E). sign(0'-). sign(0'+). turtle_dq_string(-1,_,_,[]) :- !, nb_getval(line_number,Ln), throw(unexpected_end_of_input(after_line(Ln))). turtle_dq_string(0'",In,C,[]) :- ( retract(got_dq) -> true ; peek_code(In,0'"), get_code(In,_) ), ( retract(got_dq) -> assertz(got_dq) ; assertz(got_dq), peek_code(In,0'"), get_code(In,_), assertz(got_dq) ), \+peek_code(In,0'"), !, retractall(got_dq), get_code(In,C). turtle_dq_string(0'\\,In,C,[H|T]) :- ( retract(got_dq) -> C1 = 0'" ; get_code(In,C1) ), !, string_escape(C1,In,C2,H), turtle_dq_string(C2,In,C,T). turtle_dq_string(C0,In,C,[C0|T]) :- ( retract(got_dq) -> C1 = 0'" ; get_code(In,C1) ), turtle_dq_string(C1,In,C,T). turtle_string(-1,_,_,[]) :- !, nb_getval(line_number,Ln), throw(unexpected_end_of_input(after_line(Ln))). turtle_string(0'",In,C,[]) :- !, get_code(In,C). turtle_string(0'\\,In,C,[H|T]) :- get_code(In,C1), !, string_escape(C1,In,C2,H), turtle_string(C2,In,C,T). turtle_string(C0,In,C,[C0|T]) :- get_code(In,C1), turtle_string(C1,In,C,T). string_escape(0'n,In,C,0'\n) :- !, get_code(In,C). string_escape(0'",In,C,0'") :- !, get_code(In,C). string_escape(0'\\,In,C,0'\\) :- !, get_code(In,C). string_escape(0't,In,C,0'\t) :- !, get_code(In,C). string_escape(0'r,In,C,0'\r) :- !, get_code(In,C). string_escape(0'u,In,C,Code) :- !, get_hhhh(In,Code), get_code(In,C). string_escape(0'U,In,C,Code) :- !, get_hhhh(In,Code0), get_hhhh(In,Code1), Code is Code0 << 16 + Code1, get_code(In,C). string_escape(C,_,_,_) :- nb_getval(line_number,Ln), atom_codes(A,[0'\\,C]), throw(illegal_escape_sequence(A,after_line(Ln))). get_hhhh(In,Code) :- get_code(In,C1), code_type(C1,xdigit(D1)), get_code(In,C2), code_type(C2,xdigit(D2)), get_code(In,C3), code_type(C3,xdigit(D3)), get_code(In,C4), code_type(C4,xdigit(D4)), Code is D1<<12+D2<<8+D3<<4+D4. language(C0,In,C,[C0|Codes]) :- code_type(C0,lower), get_code(In,C1), lwr_word(C1,In,C2,Codes,Tail), sub_langs(C2,In,C,Tail,[]). lwr_word(C0,In,C,[C0|T0],T) :- code_type(C0,lower), !, get_code(In,C1), lwr_word(C1,In,C,T0,T). lwr_word(C,_,C,T,T). sub_langs(0'-,In,C,[0'-,C1|Codes],T) :- get_code(In,C1), lwrdig(C1), !, get_code(In,C2), lwrdigs(C2,In,C3,Codes,Tail), sub_langs(C3,In,C,Tail,T). sub_langs(C,_,C,T,T). lwrdig(C) :- code_type(C,lower), !. lwrdig(C) :- code_type(C,digit). lwrdigs(C0,In,C,[C0|T0],T) :- lwrdig(C0), !, get_code(In,C1), lwr_word(C1,In,C,T0,T). lwrdigs(C,_,C,T,T). uri_chars(0'>,In,C,[]) :- !, get_code(In,C). uri_chars(0'\\,In,C,[H|T]) :- !, get_code(In,C1), string_escape(C1,In,C2,H), uri_chars(C2,In,C,T). uri_chars(0'',In,C,[0'',0''|T]) :- !, get_code(In,C1), uri_chars(C1,In,C,T). uri_chars(-1,_,_,_) :- !, fail. uri_chars(C0,In,C,[C0|T]) :- get_code(In,C1), uri_chars(C1,In,C,T). name(C0,In,C,Atom) :- name_start_char(C0), get_code(In,C1), name_chars(C1,In,C,T), atom_codes(Atom,[C0|T]). name_chars(C0,In,C,[C0|T]) :- name_char(C0), !, get_code(In,C1), name_chars(C1,In,C,T). name_chars(C,_,C,[]). name_start_char(C) :- code_type(C,csym), !. name_start_char(C) :- 0xC0 =< C, C =< 0xD6, !. name_start_char(C) :- 0xD8 =< C, C =< 0xF6, !. name_start_char(C) :- 0xF8 =< C, C =< 0x2FF, !. name_start_char(C) :- 0x370 =< C, C =< 0x37D, !. name_start_char(C) :- 0x37F =< C, C =< 0x1FFF, !. name_start_char(C) :- 0x200C =< C, C =< 0x200D, !. name_start_char(C) :- 0x2070 =< C, C =< 0x218F, !. name_start_char(C) :- 0x2C00 =< C, C =< 0x2FEF, !. name_start_char(C) :- 0x3001 =< C, C =< 0xD7FF, !. name_start_char(C) :- 0xF900 =< C, C =< 0xFDCF, !. name_start_char(C) :- 0xFDF0 =< C, C =< 0xFFFD, !. name_start_char(C) :- 0x10000 =< C, C =< 0xEFFFF. name_char(C) :- name_start_char(C), !. name_char(0'-) :- !. name_char(D) :- code_type(D,digit), !. name_char(0xB7) :- !. name_char(C) :- 0x0300 =< C, C =< 0x036F, !. name_char(C) :- 0x203F =< C, C =< 0x2040. punctuation(0'(,'('). punctuation(0'),')'). punctuation(0'[,'['). punctuation(0'],']'). punctuation(0',,','). punctuation(0'@,'@'). punctuation(0':,':'). punctuation(0';,';'). punctuation(0'{,'{'). punctuation(0'},'}'). punctuation(0'?,'?'). punctuation(0'!,'!'). punctuation(0'^,'^'). punctuation(0'=,'='). punctuation(0'<,'<'). punctuation(0'>,'>'). skip_line(-1,_,-1) :- !. skip_line(0xA,In,C) :- !, cnt(line_number), get_code(In,C). skip_line(0xD,In,C) :- !, get_code(In,C). skip_line(_,In,C) :- get_code(In,C1), skip_line(C1,In,C). turtle_ws(0x9). turtle_ws(0xA) :- cnt(line_number). turtle_ws(0xD). turtle_ws(0x20).