Directories in Prolog
% Our first program will demonstrate working with directories in Prolog.
% We'll use the SWI-Prolog's built-in predicates for file system operations.
% Helper predicate to handle errors
check_error(Error) :-
( Error == true
-> true
; throw(Error)
).
% Main predicate
main :-
% Create a new sub-directory in the current working directory
make_directory('subdir'),
% When creating temporary directories, it's good practice to clean them up afterwards
catch(
( % Create files and subdirectories
create_empty_file('subdir/file1'),
make_directory_path('subdir/parent/child'),
create_empty_file('subdir/parent/file2'),
create_empty_file('subdir/parent/file3'),
create_empty_file('subdir/parent/child/file4'),
% List directory contents
writeln('Listing subdir/parent'),
directory_files('subdir/parent', Files),
maplist(print_file_info('subdir/parent'), Files),
% Change current working directory
working_directory(_, 'subdir/parent/child'),
% List contents of current directory
writeln('Listing subdir/parent/child'),
directory_files('.', ChildFiles),
maplist(print_file_info('.'), ChildFiles),
% Change back to the original directory
working_directory(_, '../../..'),
% Visit directory recursively
writeln('Visiting subdir'),
directory_walk('subdir', visit)
),
Error,
(check_error(Error), delete_directory_and_contents('subdir'))
),
delete_directory_and_contents('subdir').
% Helper predicate to create an empty file
create_empty_file(Name) :-
open(Name, write, Stream),
close(Stream).
% Helper predicate to print file information
print_file_info(Dir, File) :-
(File \= '.' , File \= '..' ->
atomic_list_concat([Dir, '/', File], FullPath),
(exists_directory(FullPath) ->
format(' ~w true~n', [File])
;
format(' ~w false~n', [File])
)
; true).
% Predicate to visit files and directories recursively
visit(Path) :-
(exists_directory(Path) ->
format(' ~w true~n', [Path])
;
format(' ~w false~n', [Path])
).
% Predicate to walk through a directory recursively
directory_walk(Dir, Predicate) :-
directory_files(Dir, Files),
maplist(process_file(Dir, Predicate), Files).
process_file(Dir, Predicate, File) :-
(File \= '.' , File \= '..' ->
atomic_list_concat([Dir, '/', File], FullPath),
call(Predicate, FullPath),
(exists_directory(FullPath) ->
directory_walk(FullPath, Predicate)
; true)
; true).
% Helper predicate to delete a directory and its contents
delete_directory_and_contents(Dir) :-
(exists_directory(Dir) ->
directory_files(Dir, Files),
maplist(delete_file_or_dir(Dir), Files),
delete_directory(Dir)
; true).
delete_file_or_dir(Dir, File) :-
(File \= '.' , File \= '..' ->
atomic_list_concat([Dir, '/', File], FullPath),
(exists_directory(FullPath) ->
delete_directory_and_contents(FullPath)
;
delete_file(FullPath)
)
; true).To run this Prolog program:
- Save the code in a file named
directories.pl. - Open SWI-Prolog console.
- Load the file with
[directories]. - Run the main predicate with
main.
The output will be similar to:
Listing subdir/parent
child true
file2 false
file3 false
Listing subdir/parent/child
file4 false
Visiting subdir
subdir true
subdir/file1 false
subdir/parent true
subdir/parent/child true
subdir/parent/child/file4 false
subdir/parent/file2 false
subdir/parent/file3 falseThis Prolog program demonstrates various directory operations:
- Creating directories and files.
- Listing directory contents.
- Changing the current working directory.
- Walking through a directory structure recursively.
- Cleaning up temporary directories and files.
Note that Prolog doesn’t have built-in functions for all these operations, so we’ve used SWI-Prolog’s file system predicates and created helper predicates to mimic the functionality of the original Go program.
The main/0 predicate wraps all operations in a catch/3 to handle errors and ensure cleanup. The delete_directory_and_contents/1 predicate is used to remove the temporary directory and its contents at the end of the program.
Remember that Prolog is a logic programming language, so the approach to file system operations is quite different from imperative languages like Go. However, this example shows how similar functionality can be achieved in Prolog.
Comments powered by Disqus