Scryer-prolog: tabling issue

Created on 27 Jun 2020  路  7Comments  路  Source: mthom/scryer-prolog

The following happens:

$ cd /tmp
$ git clone https://github.com/mthom/scryer-prolog &> /dev/null
$ cd scryer-prolog
$ cargo install --path . &> /dev/null
$ cd ..
$ rm -rf scryer-prolog
$ scryer-prolog
?- use_module(library(lists)).
   true.
?- use_module(library(tabling)).
caught: error(existence_error(source_sink,'/tmp/scryer-prolog/src/lib/tabling/double_linked_list.pl'),use_module/1)
?- 

Is everything inside the binary? This issue is related to #262 .

Most helpful comment

extern crate indexmap;

use std::env;
use std::fs;
use std::fs::File;
use std::io::Write;
use std::path::Path;

fn find_prolog_files(libraries: &mut File, prefix: &str, current_dir: &Path) {
    let entries = match current_dir.read_dir() {
        Ok(entries) => entries,
        Err(_) => return,
    };
    for entry in entries.filter_map(Result::ok).map(|e| e.path()) {
        if entry.is_dir() {
            if let Some(file_name) = entry.file_name() {
                let new_prefix =
                    prefix.to_owned() + file_name.to_str().unwrap() + "/";
                find_prolog_files(libraries, &new_prefix, &entry);
            }
        } else if entry.is_file() {
            let ext = std::ffi::OsStr::new("pl");
            if entry.extension() == Some(ext) {
                let contain =
                    String::from_utf8(fs::read(&entry).unwrap()).unwrap();
                let name = entry.file_stem().unwrap().to_str().unwrap();
                let line = format!(
                    "        m.insert(\"{}\",\n{:?});\n",
                    prefix.to_owned() + name,
                    contain
                );

                libraries.write_all(line.as_bytes()).unwrap();
            }
        }
    }
}

fn main() {
    let out_dir = env::var("OUT_DIR").unwrap();
    let dest_path = Path::new(&out_dir).join("libraries.rs");

    let mut libraries = File::create(&dest_path).unwrap();
    let lib_path = Path::new("src/lib");

    libraries
        .write_all(
            b"ref_thread_local! {
    pub static managed LIBRARIES: IndexMap<&'static str, &'static str> = {
        let mut m = IndexMap::new();\n",
        )
        .unwrap();
    find_prolog_files(&mut libraries, "", &lib_path);
    libraries.write_all(b"\n        m\n    };\n}\n").unwrap();
}

This code could be used to solve the issue but other files need changes.

All 7 comments

The paths are baked into the binary at compile time, yes.

I can't share a binary application with that issue, it isn't independent of the build.

I looked at build.rs and libraries.rs, maybe it could be:

m.insert("tabling/double_linked_list", ...);

The path wouldn't need to be encoded in the application.

Why isn't ListingSource::User used for builtins.pl and the others?

This might be a mistake.

Definitely looks like it! Thanks.

extern crate indexmap;

use std::env;
use std::fs;
use std::fs::File;
use std::io::Write;
use std::path::Path;

fn find_prolog_files(libraries: &mut File, prefix: &str, current_dir: &Path) {
    let entries = match current_dir.read_dir() {
        Ok(entries) => entries,
        Err(_) => return,
    };
    for entry in entries.filter_map(Result::ok).map(|e| e.path()) {
        if entry.is_dir() {
            if let Some(file_name) = entry.file_name() {
                let new_prefix =
                    prefix.to_owned() + file_name.to_str().unwrap() + "/";
                find_prolog_files(libraries, &new_prefix, &entry);
            }
        } else if entry.is_file() {
            let ext = std::ffi::OsStr::new("pl");
            if entry.extension() == Some(ext) {
                let contain =
                    String::from_utf8(fs::read(&entry).unwrap()).unwrap();
                let name = entry.file_stem().unwrap().to_str().unwrap();
                let line = format!(
                    "        m.insert(\"{}\",\n{:?});\n",
                    prefix.to_owned() + name,
                    contain
                );

                libraries.write_all(line.as_bytes()).unwrap();
            }
        }
    }
}

fn main() {
    let out_dir = env::var("OUT_DIR").unwrap();
    let dest_path = Path::new(&out_dir).join("libraries.rs");

    let mut libraries = File::create(&dest_path).unwrap();
    let lib_path = Path::new("src/lib");

    libraries
        .write_all(
            b"ref_thread_local! {
    pub static managed LIBRARIES: IndexMap<&'static str, &'static str> = {
        let mut m = IndexMap::new();\n",
        )
        .unwrap();
    find_prolog_files(&mut libraries, "", &lib_path);
    libraries.write_all(b"\n        m\n    };\n}\n").unwrap();
}

This code could be used to solve the issue but other files need changes.

I'll look at adding that function to build.rs tomorrow, @notoria. Thanks!

The issue has been fixed, closing.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

srenatus picture srenatus  路  4Comments

triska picture triska  路  4Comments

notoria picture notoria  路  3Comments

UWN picture UWN  路  3Comments

notoria picture notoria  路  4Comments