Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

integrate lambda calculus crate #5

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions interpreter.rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ authors = ["Martín Coll <martingonzalezcoll@gmail.com>"]
edition = "2018"

[dependencies]
lambda_calculus = "^3.0"
lambda-calculus-yaml = { path = "./lambda-calculus-yaml" }
reflection = { path = "./serde-yaml-reflection" }
risp = "0.7.0"
risp-yaml = { path = "./risp-yaml" }
Expand All @@ -14,6 +16,7 @@ serde_yaml = "0.8"

[workspace]
members = [
"lambda-calculus-yaml",
"risp-yaml",
"serde-yaml-reflection",
]
1 change: 1 addition & 0 deletions interpreter.rust/examples/e0-playground.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-
2 changes: 2 additions & 0 deletions interpreter.rust/examples/e1-scalar.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# a scalar
42
2 changes: 0 additions & 2 deletions interpreter.rust/examples/e1.yaml

This file was deleted.

5 changes: 5 additions & 0 deletions interpreter.rust/examples/e3-nested-lists.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# 19
- +
- 10
- 5
- [-, 10, 3, 3]
2 changes: 0 additions & 2 deletions interpreter.rust/examples/e3.yaml

This file was deleted.

8 changes: 0 additions & 8 deletions interpreter.rust/examples/e4-abstraction.yaml

This file was deleted.

15 changes: 15 additions & 0 deletions interpreter.rust/examples/e4-mapping.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
- do

- - def
- add-one
- x: [+, 1, x]

- - def
- add-two
- x: [+, 2, x]

# 105
- - add-one
- - add-two
- - add-two
- 100
27 changes: 27 additions & 0 deletions interpreter.rust/examples/e5-nested-mapping.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# examples from https://github.com/shybyte/risp
- do

# ; Define a function
# (defn double [x] (* x 2))
- - def
- double
- x: ['*', x, 2]

# ; Function which returns a function (some call it a closure), which adds x1 to its single argument
# (defn create_adder [x1]
# (fn [x2] (+ x1 x2)))
- - def
- create_adder
- x1:
x2: [+, x1, x2]

# (def add_20 (create_adder 20))
- - def
- add_20
- - create_adder
- 20

# 220
- - add_20
- - double
- 100
12 changes: 12 additions & 0 deletions interpreter.rust/lambda-calculus-yaml/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "lambda-calculus-yaml"
version = "0.1.0"
authors = ["Martín Coll <martingonzalezcoll@gmail.com>"]
edition = "2018"

[dependencies]
lambda_calculus = "^3.0"
reflection = { path = "../serde-yaml-reflection" }
serde = { version = "1.0", features = ["derive"] }
serde_yaml = "0.8"
serde_test = "1.0.117"
34 changes: 34 additions & 0 deletions interpreter.rust/lambda-calculus-yaml/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use reflection::Yaml;
use std::fs::File;
use std::io::BufReader;
use lambda_calculus::*;
use lambda_calculus::data::num::church;
use std::path::Path;

use std::error::Error;

pub fn read_term_from_file<P: AsRef<Path>>(path: P) -> Result<Yaml, Box<dyn Error>> {
let file = File::open(path)?;
let reader = BufReader::new(file);
Ok(serde_yaml::from_reader(reader)?)
}

pub fn to_lambda_calculus(yaml_term: &Yaml) -> Term {
match yaml_term {
Yaml::String(var) => match var.as_ref() {
"+" => church::add(),
"-" => church::sub(),
_ => panic!("variables are numbers because we use de Bruijn notation"),
}
Yaml::Integer(var) => var.into_church(),
// https://www.cs.cornell.edu/courses/cs4110/2018fa/lectures/lecture15.pdf
Yaml::Hash(_) => panic!("variables are numbers because we use de Bruijn notation"),
Yaml::Array(vec) => match vec.as_slice() {
[term] => to_lambda_calculus(term),
[lhs, rhs] => app(to_lambda_calculus(lhs), to_lambda_calculus(rhs)),
[t0, t1, t2] => app!(to_lambda_calculus(t0), to_lambda_calculus(t1), to_lambda_calculus(t2)),
[t0, t1, t2, t3] => app!(to_lambda_calculus(t0), to_lambda_calculus(t1), to_lambda_calculus(t2), to_lambda_calculus(t3)),
_ => panic!("unsupported")
},
}
}
1 change: 1 addition & 0 deletions interpreter.rust/risp-yaml/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
lambda_calculus = "^3.0"
reflection = { path = "../serde-yaml-reflection" }
risp = "0.7"
serde = { version = "1.0", features = ["derive"] }
Expand Down
3 changes: 2 additions & 1 deletion interpreter.rust/risp-yaml/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::convert::TryInto;
use reflection::Yaml;
use risp::types::RispType;
use std::fs::File;
Expand All @@ -15,7 +16,7 @@ pub fn read_term_from_file<P: AsRef<Path>>(path: P) -> Result<Yaml, Box<dyn Erro
pub fn to_risp(yaml_term: &Yaml) -> RispType {
match yaml_term {
Yaml::String(var) => RispType::Symbol(var.to_string()),
Yaml::Integer(var) => RispType::Int(*var),
Yaml::Integer(var) => RispType::Int((*var).try_into().unwrap()),
Yaml::Hash(hm) => {
let (key, val) = hm.iter().next().unwrap();
RispType::List(vec![
Expand Down
3 changes: 2 additions & 1 deletion interpreter.rust/serde-yaml-reflection/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use std::collections::HashMap;
#[derive(Serialize, Deserialize)]
#[serde(untagged)]
pub enum Yaml {
Integer(i64),
Integer(usize),
// Integer(i64),
String(String),
Array(Vec<Yaml>),
Hash(HashMap<String, Yaml>),
Expand Down
12 changes: 12 additions & 0 deletions interpreter.rust/src/bin/eval.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use interpreter::eval_file;
use risp::core::create_core_environment;
use std::env;
use std::path::Path;

fn main() {
let env = &mut create_core_environment();
match env::args().skip(1).next() {
Some(path) => eval_file(Path::new(&path), env),
None => println!("Usage:\n cargo run --example eval-file -- [path_to_yaml]"),
}
}
10 changes: 10 additions & 0 deletions interpreter.rust/src/bin/lambda.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use interpreter::eval_lambda_calculus;
use std::env;
use std::path::Path;

fn main() {
match env::args().skip(1).next() {
Some(path) => eval_lambda_calculus(Path::new(&path)),
None => println!("Usage:\n cargo run --bin lambda -- [path_to_yaml]"),
}
}
20 changes: 20 additions & 0 deletions interpreter.rust/src/bin/repl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use risp::core::create_core_environment;
use risp::eval::eval;
use risp_yaml::to_risp;
use std::io;

fn main() {
let env = &mut create_core_environment();
loop {
println!("risp-yaml >");
let mut expr = String::new();
while io::stdin().read_line(&mut expr).expect("valid code") > 1 {}

let yaml_expr = serde_yaml::from_str(&expr).expect("invalid yaml line");
let risp_expr = to_risp(&yaml_expr);
match eval(risp_expr, env) {
Ok(res) => println!("🔥 {:?}", res),
Err(e) => println!("🙀 {:?}", e),
}
}
}
10 changes: 10 additions & 0 deletions interpreter.rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use lambda_calculus::reduction::*;
use lambda_calculus_yaml::to_lambda_calculus;
use risp::environment::Environment;
use risp::eval::eval;
use risp_yaml::read_term_from_file;
Expand All @@ -13,3 +15,11 @@ pub fn eval_file(path: &Path, env: &mut Environment) {
Err(e) => println!("// 🙀 => {:?}", e),
}
}

pub fn eval_lambda_calculus(path: &Path) {
let file = read_term_from_file(path).unwrap();

let mut expr = to_lambda_calculus(&file);
println!("steps: {}", expr.reduce(NOR, 0));
println!("{}", expr);
}
6 changes: 3 additions & 3 deletions yaml.yaml/data/bool/and.yaml.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# λp.λq.(p q false)
- p
- q
# λp.λq.(p q false) = λλ.(1 0 false)
- 1
- 0
- false
7 changes: 5 additions & 2 deletions yaml.yaml/data/bool/yaml.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# https://users.monash.edu.au/~lloyd/tildeFP/Lambda/Examples/const-bool/
- true: a
- false: b
# and in de Bruijn notation:
# true := λa.λb.a = λλ.1
# false := λa.λb.a = λλ.0
- false
- true
5 changes: 5 additions & 0 deletions yaml.yaml/node/term.yaml.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
# also very terse as a general-purpose
# functional programming language

# https://www.win.tue.nl/automath/archive/pdf/aut029.pdf
# section 14. ALGORITHMS gave me an idea about the meaning.
# of mappings and variable names. those are given as free variable
# lists that carry semantics and are only understood by humans.

scalar: variable
sequence:
- mapping:
Expand Down