-
Notifications
You must be signed in to change notification settings - Fork 10
Open
Description
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!
Iwijn
Metadata
Metadata
Assignees
Labels
No labels