1:- module(misc, [
2 compile_pred_word/4
3 , random_choice/2
4 , collect_subterm/3
5 , collect_subterm/4
6 , completing_optional_args/3
7 , cs/2
8 , desugaring/2
9 , dir_minus/3
10 , dir_plus/3
11 , directory_files/3
12 , directory_remove_ymhms/1
13 , drop3zw/2
14 , find_exportables_from_calls/2
15 , ignore/2
16 , insert/3
17 , insert_pause/2
18 , kanji/3
19 , list/2
20 , map_directory_files/2
21 , matrix_paths/2
22 , parse_time/3
23 , parse_utf8/2
24 , predicate_arity/2
25 , predicate_arity_for_export/2
26 , rename_directory_suffix/3
27 , set/2
28 , split_by_filler/3
29 , string/2
30 , shell_string/2, shell_string/3, qshell_string/2
31 , texadjust/2
32 , texuncomment/2
33 , token_and_split/3
34 , try2problem/2
35 , expand_sgn_brace/2 ]). 36
37:- use_module(pac(basic)). 38:- use_module(util(file)). 39:- use_module(util('meta2')). 40:- use_module(util(math)). 41:- use_module(pac('expand-pac')). 42:- use_module(pac('expand-word')). 43:- use_module(pac(op)). 44
45term_expansion --> pac:expand_pac.
47
48
49 52
53new_names([V|Vs], [A=V|Eqs], N, Prefix, As):-
54 new_name(N, As, A, Prefix, K),
55 new_names(Vs, Eqs, K, Prefix, As).
56new_names([], [], _, _, _).
57
59new_name(N, As, B, Prx, K):- atom_concat(Prx, N, B),
60 \+ memberchk(B, As),
61 !,
62 succ(N, K).
63new_name(N, As, A, Prx, K):- succ(N, N1),
64 new_name(N1, As, A, Prx, K).
65
67subtractq([], _, []).
68subtractq([A|As], B, C):- memq(A, B), !,
69 subtractq(As, B, C).
70subtractq([A|As], B, [A|C]):- subtractq(As, B, C).
71
72%
73expand_clause_slim(X, Y):-
74 expand_clause(X, [], Y0),
75 maplist(pred([X:-true, X] & [C, C]), Y0, Y).
77compile_pred_word(X-->X0, Eqs, H0, R0):-!,
78 maplist(pred([A=P, A, P]), Eqs, As, Vs),
79 expand_clause_slim(X-->X0, [H|R]),
80 term_variables(H, HVs),
81 subtractq(HVs, Vs, SVs),
82 new_names(SVs, Eqs0, 1, 'A', As),
83 append(Eqs0, Eqs, Eqs1),
84 term_string(H, H0, [variable_names(Eqs1),
85 quoted(true)]),
86 maplist(pred(([U, [V,".\n"]] :-
87 numbervars(U, 0, _),
88 term_string(U, V, [ numbervars(true),
89 quoted(true)]))),
90 R, R0).
91compile_pred_word(X, Eqs, H0, R0):-
92 expand_clause_slim(X, [H|R]),
93 term_string(H, H0, [variable_names(Eqs),
94 quoted(true)]),
95 maplist(pred(([U, [V,".\n"]] :-
96 numbervars(U, 0, _),
97 term_string(U, V, [ numbervars(true),
98 quoted(true)]))),
99 R, R0).
100
101
102
116
129
130shell_infix(L / R, L, /, R).
131shell_infix(L ; R, L, ;, R).
132shell_infix('|'(L,R), L, '|', R).
133shell_infix(L > R, L, >, R).
134shell_infix(L >> R, L, >>, R).
135shell_infix(L << R, L, <<, R).
136shell_infix(&(L, R), L, &, R).
137shell_infix(&&(L, R), L, &&, R).
139qshell_string(X, Y):- shell_string(X, " > /dev/null 2>&1", Y).
141shell_string(X, Y):- shell_string(X, "", Y).
143shell_string([], Z, Z):-!.
144shell_string([X], Y, Z):-!, shell_string(X, Y, Z).
145shell_string([X,Y|Z], U, V):-!, shell_string([Y|Z], U, W),
146 string_concat(" ", W, W0),
147 shell_string(X, W0, V).
148shell_string(A+B, X, Y):-!, shell_string(B, X, X0),
149 shell_string(A, X0, Y).
150shell_string(-A, X, Y):-!, shell_string("-" + A, X, Y).
151shell_string(--(A), X, Y):-!, shell_string("--" + A, X, Y).
152shell_string(A, X, Y):- shell_infix(A, L, F, R), !,
153 shell_string(R, X, X0),
154 string_concat(F, X0, X1),
155 shell_string(L, X1, Y).
156shell_string({A}, X, Y):-!, shell_string("( " + A + " )", X, Y).
157shell_string(shell(A), X, Y):-!, shell_string({A}, X, Y).
158shell_string(A, X, Y):- compound(A), !,
159 A =.. B,
160 shell_string(B, X, Y).
161shell_string(A, X, Y):- atomic(A), string_concat(A, X, Y).
162
163
164 167
177completing_optional_args(X, Y, Z):-
178 completing_optional_args(X, Y, Z, []).
179
184
185completing_optional_args([], R, R, []):-!.
186completing_optional_args([A|As], Bs, [B|R], R0):- (A = A0:_ ; A0= A), !,
187 functor(A0, F, 1),
188 functor(B, F, 1),
189 ( select(B, Bs, Cs); Cs = Bs ), !,
190 unify_one(A, B),
191 completing_optional_args(As, Cs, R, R0).
192
195:- set_prolog_flag(open_dict, false). 196unify_default({G}, _):-!, once(G).
197unify_default(X, X).
198:- set_prolog_flag(open_dict, true). 199
201unify_one(A:Val, B):- !, functor(A, F, 1),
202 functor(B, F, 1),
203 arg(1, A, U),
204 arg(1, B, V),
205 ( var(V) -> U = Val, 206 V = U
207 ; var(U) -> U = V
208 ; true
209 ).
210unify_one(A, A):-!.
211unify_one(_, _).
212
213
222
223set(emptyset,[]):-! .
224set(X,X):-listp(X),! .
225set(singleton(A),[B]):-!,set(A,B) .
226set(A+B,A1):-!,(set(A,A2),set(B,A3)),union(A2,A3,A1) .
227set(plus(A,B),A1):-!,set(A+B,A1) .
228set(cup(A),A1):-!,(set(A,A2),set(append(A2),A3)),sort(A3,A1) .
229set(cap(A),A1):-!,set(A,A2),math:bigcap(A2,A1) .
230set(++(A,B),A1):-!,(set(A,A2),set(B,A3)),math:direct_sum(A2,A3,A1) .
231set(A*B,A1):-!,(set(A,A2),set(B,A3)),math:product(A2,A3,A1) .
232set(A-B,A1):-!,(set(A,A2),set(B,A3)),
233 pac_meta:pac_product(misc:set_aux,A2,A3,A1) .
234set(\(A,B),A1):-!,(set(A,A2),set(B,A3)),subtract(A2,A3,A1) .
235set(#(A,B),A1):-!,(set(A,A2),set(B,A3)),scramble_cons(A2,A3,A1) .
236set(&(A,B),A1):-!,(set(A,A2),set(B,A3)),math:intersection(A2,A3,A1) .
237set(pow(A),A1):-!,set(A,A2), math:powerset(A2,A1) .
238set(zip(A,B),A1):-!,(set(A,A2),set(B,A3)),zip(A2,A3,A1) .
239set(sort(A),A1):-!,set(A,A2),sort(A2,A1) .
240set((A->B),A1):-!,(set(A,A2),set(B,A3)),math:mapset(A2,A3,A1) .
241set(..(I,J),A1):-(I0 is I,J0 is J),!,numlist(I0,J0,A1) .
242set(in(X,Y),A1):-!,(set(X,A2),set(Y,A3)),truth(memberchk,A2,A3,A1) .
243set(X=Y,A1):-!,(set(X,A2),set(Y,A3)),truth(==,A2,A3,A1) .
244set(X=<Y,A1):-!,(set(X,A2),set(Y,A3)),truth(subset,A2,A3,A1) .
245set(X>=Y,A1):-!,set(Y=<X,A1) .
246set(X<Y,A1):-!,(set(X,A2),set(Y,A3)),truth(math:proper_subset,A2,A3,A1) .
247set(X>Y,A1):-set(Y<X,A1) .
248
250set_aux(A,B,A-B).
257insert(M, [A|P], [A|Q]):- foldl(pred(M, [B, [M, B| U], U]), P, Q, []).
258insert(_, [], []).
259
261truth(X, true) :- call(X), !.
262truth(_, false).
263
264truth(X, Y, true) :- call(X, Y), !.
265truth(_, _, false).
266
267truth(X, Y, Z, true) :- call(X, Y, Z), !.
268truth(_, _, _, false).
275
276desugaring(X-Y,A1+ -1*A2):-!,desugaring(X,A1),desugaring(Y,A2).
277desugaring(+X,A1):-!,desugaring(X,A1).
278desugaring(-X,-1*A1):-!,desugaring(X,A1).
279desugaring(X/Y,A1 rdiv A2):-!,desugaring(X,A1),desugaring(Y,A2).
280desugaring(X,X).
281
282insert_pause(cs(item),[cs(pause),cs(item)]):-! .
283insert_pause(X,A1):-(listp(X),maplist(insert_pause,X,Y)),!,insert_pause(Y,A1) .
284insert_pause(X,A1):-(X=..[F|As],maplist(insert_pause,As,Bs),Z=..[F|Bs]),!,
285 insert_pause(Z,A1).
286
287texadjust([cs(item)|X],A1):-texadjust(X,Y),!,texadjust([10,cs(item)|Y],A1) .
288
289cs(cs(N),[N]):-!.
290cs(A,A1):-listp(A),!,maplist(cs,A,A1) .
291cs(A,A1):-A=..[_A2|As], maplist(cs,As,A1) .
292
(comment(_A1),[]).
294
295try2problem(env(try,Body),A1):-try2problem(Body,A2),try2problem(env(problem,A2),A1).
296
297drop3zw(ddol(E),[cs(noindent),cs(skip),"3zw",env(coronamath,[dol([cs(displaystyle)," ",E])])]).
298
299
300
304
307
310
316
329
330matrix_paths([], [[]]).
331matrix_paths([X|Y], Z):- matrix_paths(Y, PY),
332 foldr(pred(PY, [J, P, Q]:-
333 foldr(pred(J, [K, N, [[J|K]|N]]), PY, P, Q)),
334 X, [], Z).
335
336
340
359
360random_choice([L|R], [A|Q]):- length(L, N), I is random(N),
361 nth0(I, L, A),
362 random_choice(R, Q).
363random_choice([], []).
375
376list(A+B,A1):-!, list(A,A2),
377 list(B,A3),
378 append(A2,A3,A1).
379list(\(A,B),Z):-!, list(A,X),list(B,Y), append(X,Z,Y) .
380list(A/B,Z):-!, list(A,X),list(B,Y), append(Z,Y,X) .
381list(E^L,A1):-N is L,!,list(E,A2),times(N,A2,A1) .
382list(^^(E,L),C):-N is L,!,list(E,A),math:nlist(A,N,C) .
383list(X,X):-listp(X),! .
384list(+A,A1):-!,list(A,A2), append(A2,A1) .
385list(flat(A),A1):-list(A,A2),flatten(A2,A1) .
398
399string(A+B,A1):-!, string(A,A2),
400 string(B,A3),
401 string_concat(A2,A3,A1).
402string(\(A,B),Z):-!, string(A,X),
403 string(B,Y),
404 string_concat(X,Z,Y).
405string(A/B,Z):-!, string(A,X),
406 string(B,Y),
407 string_concat(Z,Y,X).
408string(E^L,A1):-N is L,!,string(E,A2),
409 string_times(N,A2,A1) .
410string(reverse(X),B):-!, string(X,A1),
411 string_codes(A1,A2),
412 reverse(A2,A),
413 string_codes(B,A).
414string(+A,A1):-!, string(A,A2),
415 list(A2,A3),
416 string_list_concat(A3,A1).
417string(X,X):-(string(X);atom(X)),!.
418
420edit_skk_line(X, A, B):-
421 ( skk_null_jisyo_entry(X, []) -> B = A
422 ; A = [X|B]
423 ).
425skk_null_jisyo_entry --> wl("[^/]*//[\n\s\r\t]*"), end_of_line.
427end_of_line([], _).
428
430split_by_filler(X) --> filler, token_and_split(X).
432token_and_split([]) --> current([]).
433token_and_split([A|X]) --> wl("[^\s\t\r\n]+", A, []), split_by_filler(X).
434
436filler --> filler(_, _).
438filler(X, Y) --> wl("[\s\t\n\r]*", X, Y).
440delimiter_plus --> wl(+("[\t\r\n]" | "\s\s+"), _, _).
442delimiter_plus(X, Y) --> wl(+("[\t\r\n]" | "\s\s+"), X, Y).
443
444
445
460
461split_plus(X) --> split_plus(X, []).
463split_plus(X, Y) --> filler, !, words(X, Y).
465words(X, X) --> current([]), !.
466words([[C|W]|Xs], Y) --> [C], word_tail(W), words(Xs, Y).
468word_tail([]) --> filler, current([]), !.
469word_tail([]) --> delimiter_plus, !.
470word_tail([C|W]) --> [C], word_tail(W).
471
477
478pred_split(Filler, Delimiter, X) -->
479 { pac_word:let_wl(F, Filler),
480 pac_word:let_wl(D, Delimiter)
481 },
482 pred_split(F, D, X, []).
484pred_split(F, D, X, Y) --> call(F), !, words(F, D, X, Y).
486words(_, _, X, X) --> current([]), !.
487words(F, D, [[C|W]|Xs], Y) --> [C], word_tail(F, D, W), words(F, D, Xs, Y).
489word_tail(F, _, []) --> call(F), current([]), !.
490word_tail(_, D, []) --> call(D), !.
491word_tail(F, D, [C|W]) --> [C], word_tail(F, D, W).
492
493
525
531
533:- meta_predicate directory_files(2,?,?). 534directory_files(E, Ds, L):-
535 maplist(pred([D, D0]:-expand_file_name(D, [D0])), Ds, Es),
536 maplist(directory_files, Es, Ls),
537 call(E, Ls, L).
538
539:- meta_predicate map_directory_files(1, ?). 540map_directory_files(F, D):- directory_files(D, Fs),
541 working_directory(D0, D),
542 maplist(ignore(F), Fs),
543 working_directory(_, D0).
544
545ignore(F, X):- ignore(call(F, X)).
546
549directory_remove_ymhms(D):- expand_file_name(D, [D0]),
550 map_directory_files(pred(([X]:-
551 atom_codes(X, X0),
552 parse_time_me(".pdf", X0, Y0),
553 atom_codes(Y,Y0),
554 rename_file(X, Y) )),
555 D0).
556
559
560parse_time(Ext) -->
561 w(*(char(digit)), Year), "å¹´",
562 w(*(char(digit)), Month), "æ",
563 w(*(char(digit)), Day), "æ¥",
564 w(*(char(digit)), Hour), "æ",
565 w(*(char(digit)), Minute), "å",
566 w(*(char(digit)), Second), "ç§",
567 ".",
568 w(*(.), Ext),
569 current([]),
570 { append([Year, Month, Day, Hour, Minute, Second, `.`, Ext], Y) },
571 peek(Y).
572
576dir_minus(X,Y,Z):- directory_files(fun([[A,B]]-> (set::(A\B))), [X, Y], Z).
577dir_plus(X,Y,Z):- directory_files(fun([[A,B]]-> (set::(A+B))), [X, Y], Z).
578
580rename_directory_suffix(Suffix0, Suffix, Dir):-
581 expand_file_name(Dir, [Dir0]),
582 map_directory_files(
583 pred([Suffix0,Suffix], [X]:- ( atom_concat(X0, Suffix0, X),
584 atom_concat(X0, Suffix, Y),
585 rename_file(X, Y)
586 )),
587 Dir0).
588
589:- meta_predicate collect_subterm(1,?,?). 590
591collect_subterm(F, X, Y):- collect_subterm(X, Y0, [], F), sort(Y0, Y).
592
594collect_subterm(X, [X|V], V, F):- call(F, X), !.
595collect_subterm(X, V, W, F):- compound(X), !,
596 X=..[_|As],
597 foldr( pred(F, [A, P, Q] :- collect_subterm(A, P, Q, F), As, V, W)).
598collect_subterm(_, V, V, _).
599
610
615
617predicate_arity(F, L):-
618 setof(P/N,
619 A^X^( predicate_property(A:X, file(F)),
620 functor(X, P, N)
621 ),
622 L),
623 insert(", ", L, L0),
624 smash(["[", L0, "]"]).
625
627predicate_arity_for_export(F, L):-
628 writeln('start ...'),
629 assert_call_graph,
630 writeln('call graph done'),
631 find_exportables_from_calls(F, L).
632
634
635
637
638find_exportables_from_calls(Loc, Exp):-
639 predicate_arity(Loc, L0),
640 setof(F/N, P^X^(
641 member(F/N, L0),
642 functor(P, F, N),
643 once(calls(X, user:P)),
644 \+ predicate_property(X, file(Loc))
645 ),
646 Exp),
647 insert(",\n", Exp, E),
648 maplist(write, E).
649
650
653
654residue(X-Y,V):-!, residue(X,A),
655 residue(Y,U),
656 append(U,V,A).
657residue(X,X).
658
659% ?- parse_utf8(`My name is åäº å½æ.`, R), smash(R).
660parse_utf8 --> sed(kanji(A), =(A)),
661 maplist(pred(([X, Y] :- listp(X), string_codes(Y, X))
662 &
663 ([X, Y] :- char_code(Y, X)))).
664
665kanji(A) --> w(char(utf8b), A, B), wl(*char(utf8c), B).
666
669
678
690
696repeat_line(V, In, Out):- read_line_to_codes(In, A),
697 A \== end_of_file,
698 cgi_bin_name_edit(V, A, B),
699 maplist(put_code(Out), B),
700 put_code(Out, 0'\n), 701 !,
702 repeat_line(V, In, Out).
703repeat_line(_,_,_).
704
705
708expand_sgn_brace(sgn([A,A0|B]),(A1;A2)):-!,
709 expand_sgn_brace(sgn([A]),A1),
710 expand_sgn_brace(sgn([A0|B]),A2).
711expand_sgn_brace(sgn([A]),A1):-!,expand_sgn_brace(A,A1).
712expand_sgn_brace((A,B),(A1,A2)):-!,expand_sgn_brace(A,A1),
713 expand_sgn_brace(B,A2).
714expand_sgn_brace((A;B),(A1;A2)):-!,expand_sgn_brace(A,A1),
715 expand_sgn_brace(B,A2).
716expand_sgn_brace(\+A,\+A1):-!,expand_sgn_brace(A,A1).
717expand_sgn_brace(A/N,A0/N):-!,expand_sgn_brace(A,A0).
718expand_sgn_brace(X,A1):-is_list(X),!,
719 maplist(expand_sgn_brace,X,A1).
720expand_sgn_brace(X,X).
721
723
726
727list_concat(X+Y,A1):-list_concat(X,A2),
728 list_cocnat(Y,A3),
729 append(A2,A3,A1) .
730list_concat(X,X).
731
732
735my_add(X+Y,A1):- my_add(X,A2),
736 my_add(Y,A3),
737 plus(A2,A3,A1).
738my_add(X,X).
739
744
749
753
762
764
773
775
779split_list_at_nth1(Nth1, Long, Start, End) :-
780 ( nonvar(Nth1) -> must_be(nonneg, Nth1), Once = true
781 ; is_list(Long), once(is_list(Start) ; is_list(End)) -> Once = true
782 ; is_list(End), is_list(Start) -> Once = true
783 ; Once = false
784 ),
785 split_list_at_nth1_(Long, 0, Nth1, Once, Start, End).
786
787split_list_at_nth1_(L, N, N, Once, [], L) :-
788 (Once == true -> ! ; true).
789split_list_at_nth1_([H|T], N, Nth1, Once, [H|Upto], End) :-
790 N1 is N + 1,
791 split_list_at_nth1_(T, N1, Nth1, Once, Upto, End)