34
35:- module(infer_alias,
36 [infer_alias/3,
37 fastest_alias/2,
38 library_alias/2,
39 smallest_alias/2,
40 current_alias/2,
41 pretty_path/2
42 ]). 43
44:- use_module(library(apply)). 45:- use_module(library(lists)). 46:- use_module(library(option)). 47:- use_module(library(term_size)). 48
49infer_alias(File, CAlias, Options) :-
50 select_option(sort(SortL), Options, _, []),
51 findall(SortTerm-Alias,
52 ( current_alias(File, Alias),
53 Alias =.. [AName, _],
54 maplist(sort_field(Alias, AName), SortL, SortTerm)
55 ), SA),
56 sort(SA, [_-CAlias|_]).
57
58sort_field(_, A, alias(L), N) :-
59 ( nth0(N, L, A)
60 ->true
61 ; length(L, N)
62 ).
63sort_field(_, A, sols, N) :-
64 findall(A, user:file_search_path(A, _), L),
65 length(L, N).
66sort_field(Alias, _, size, S) :-
67 term_size(Alias, S).
68sort_field(Alias, _, length, N) :-
69 term_to_atom(Alias, Atom),
70 atom_length(Atom, N).
71
72fastest_alias(File, Alias) :-
73 infer_alias(File, Alias, [sort([sols, size, length])]).
74
75library_alias(File, Alias) :-
76 infer_alias(File, Alias, [sort([alias([library]), sols, size, length])]).
77
78smallest_alias(File, Alias) :-
79 infer_alias(File, Alias, [sort([length, size, sols])]).
80
81current_alias(File, Alias) :-
82 user:file_search_path(A1, ADir),
83 A1 \= autoload,
84 absolute_file_name(ADir, Dir, [file_type(directory), solutions(all)]),
85 directory_file_path(Dir, Base, File),
86 file_name_extension(Name, _Ext, Base),
87 pretty_path(Name, Path),
88 Alias =.. [A1, Path].
89
90pretty_path(Name1, Path/F) :-
91 directory_file_path(Dir, F, Name1),
92 Dir \= '.',
93 !,
94 pretty_path(Dir, Path).
95pretty_path(Name, Name)