Skip to content

Is there a way to load an external pl file and run it from rust with swipl-rs?Β #44

@tumluliu

Description

@tumluliu

Hi there, thanks for making such a crate. I am wondering if there is such a use case supported by swipl-rs? Or it's totally not designed for that? I have asked the same question already to GPT-4, but it's still providing unusable code snippets even after more than 10 iterates.

Here is the latest version it gave me:

extern crate swipl;

use swipl::prelude::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let input_data = "john"; // The variable data we want to pass to Prolog program

    // Initialize SWI-Prolog environment
    swipl::init::initialize_swipl()?;
    let activation = swipl::init::activate_engine()?;

    // Consult Prolog program
    let consult_result = {
        let functor = swipl::functor::Functor::new("consult", 1)?;
        let module = swipl::module::Module::new("user")?;
        let pred = swipl::predicate::Predicate::new(functor, module);
        let context = swipl::context::Context::new(activation);
        let term = term!(context, "simple_swipl.pl");
        let mut query = swipl::callable::OpenQuery::new_with_term(activation, &pred, &[term])?;
        query.next()?
    };

    if consult_result.is_none() {
        eprintln!("Failed to consult Prolog file");
        return Ok(());
    }

    // Define Prolog query: grandparent(john, Grandchild)
    let query_string = format!("grandparent('{}', Grandchild)", input_data);
    let call_functor = swipl::functor::Functor::new("call", 1)?;
    let module = swipl::module::Module::new("user")?;
    let pred = swipl::predicate::Predicate::new(call_functor, module);
    let context = swipl::context::Context::new(activation);
    let query_term = term!(context, &query_string);
    let mut query = swipl::callable::OpenQuery::new_with_term(activation, &pred, &[query_term])?;

    // Run the query
    match query.next()? {
        Some(_) => {
            // Get the result
            let grandchild = query.get("Grandchild")?;
            let grandchild_str = grandchild.as_string()?.to_string_lossy().into_owned();

            println!("SWI-Prolog output: {}", grandchild_str);
        }
        None => {
            println!("No result found");
        }
    }

    Ok(())
}

where there are multiple apparent errors. I can fix some of them, but for the statement like let term = term!(context, "simple_swipl.pl");, I have no idea if it makes any sense.

And the minimal sample simple_swipl.pl:

% simple_swipl.pl
parent(john, jim).
parent(jane, jim).
parent(jim, ann).

grandparent(X, Z) :- parent(X, Y), parent(Y, Z).

Looking forward to hearing suggestions from you experts. Thanks a lot!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions