database/
database.rs

1use mini_kanren::database::Database;
2use mini_kanren::prelude::*;
3use mini_kanren::{db_facts, db_rel, disj, run};
4use std::sync::Arc;
5
6// declare relations
7db_rel! {
8    food(f);
9    drink(d);
10    goes_well_with(f, d);
11    is_vegetarian(f, k);
12}
13
14fn main() {
15    // Construct an empty database
16    // and add some facts about the world.
17    let mut db = Database::new();
18    db_facts! {
19        db {
20            food("Beef");
21            food("Fish");
22            food("Hamburger");
23            food("Pizza");
24            food("Salad");
25            food("Stone soup");
26            food("Veggies");
27
28            drink("Beer");
29            drink("Red wine");
30            drink("Water");
31            drink("White wine");
32
33            goes_well_with("Beef", "Beer");
34            goes_well_with("Beef", "Red wine");
35            goes_well_with("Fish", "White wine");
36            goes_well_with("Hamburger", "Beer");
37            goes_well_with("Pizza", "Red wine");
38            goes_well_with("Stone soup", "Water");
39            goes_well_with("Veggies", "White wine");
40
41            is_vegetarian("Beef", false);
42            is_vegetarian("Fish", "some say so");
43            is_vegetarian("Hamburger", "canbe");
44            is_vegetarian("Pizza", "canbe");
45            is_vegetarian("Salad", true);
46            is_vegetarian("Stone soup", true);
47            is_vegetarian("Veggies", true);
48        }
49    }
50
51    // Share database
52    let db = Arc::new(db);
53
54    // run a simple query
55    let food_: Vec<_> = run!(q, food(&db, q)).collect();
56    println!("All the food we like: {:?}", food_);
57
58    // run another simple query
59    let veggie: Vec<_> = run!(q, is_vegetarian(&db, q, true)).collect();
60    println!("Vegetarian food: {:?}", veggie);
61
62    // run a combined query
63    let wine_and_veggie: Vec<_> = run!(
64        q,
65        is_vegetarian(&db, q, true),
66        goes_well_with(&db, q, "White wine"),
67    )
68    .collect();
69    println!(
70        "Vegetarian food that goes well with white wine: {:?}",
71        wine_and_veggie
72    );
73
74    // run a query with alternatives
75    let any_wine: Vec<_> = run!(
76        q,
77        disj! {
78            goes_well_with(&db, q, "Red wine");
79            goes_well_with(&db, q, "White wine")
80        }
81    )
82    .collect();
83    println!("Food that goes well with wine: {:?}", any_wine);
84}