From e3b592abf8911b33d70f360c6a6781229b984ffc Mon Sep 17 00:00:00 2001 From: J Pratt Date: Tue, 31 Aug 2021 17:35:12 +1000 Subject: [PATCH] Git Init with small 'isa' demo Change-Id: I39ed4809035ac72e822934b063617bab1de86fa9 --- Cargo.toml | 10 ++++++++ src/main.rs | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 Cargo.toml create mode 100644 src/main.rs diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 000000000..979d9a9c3 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "arcs-ibis" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +crepe = "0.1.5" +lazy_static = "1.4.0" diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 000000000..e86843ba9 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,70 @@ +#[macro_use] +extern crate lazy_static; + +use crepe::crepe; +use std::collections::HashMap; + +type Ent = u32; + +struct Context { + last_ent: Ent, + names: HashMap, +} + +impl Context { + fn new() -> Self { + Self { + last_ent: 0, + names: HashMap::new(), + } + } + + fn get_ent(&mut self) -> Ent { + self.last_ent += 1; + self.last_ent + } + + fn name(&self, id: &Ent) -> &str { + // TODO: Check! + self.names.get(id).unwrap() + } + + fn decl(&mut self, name: &str) -> Ent { + let new_id: Ent = self.get_ent(); + // TODO: Check! + self.names.insert(new_id, name.to_string()); + new_id + } +} + +crepe! { + @input + struct IsAClaim(Ent, Ent); + @input + struct Exists(Ent); + + @output + struct IsA(Ent, Ent); + + IsA(x,x) <- Exists(x); + IsA(x,x) <- IsAClaim(x, _); + IsA(x,x) <- IsAClaim(_, x); + IsA(x, z) <- IsAClaim(x, y), IsA(y, z); +} + +fn main() { + let mut ctx = Context::new(); + let mut runtime = Crepe::new(); + + let socretes: Ent = ctx.decl("socretes"); + let man: Ent = ctx.decl("man"); + let mortal: Ent = ctx.decl("mortal"); + + runtime.extend(&[Exists(socretes), Exists(man), Exists(mortal)]); + runtime.extend(&[IsAClaim(socretes, man), IsAClaim(man, mortal)]); + + let (reachable,) = &runtime.run(); + for IsA(x, y) in reachable { + println!("{} is a {}", ctx.name(x), ctx.name(y)); + } +}