2个不稳定版本

0.4.0 2023年7月30日
0.2.0 2023年7月3日

编程语言中排名第862

每月下载量46
5个crate(直接使用2个)中使用

MIT/Apache

33KB
856 代码行

Steel

一个用Rust构建的嵌入式且可扩展的scheme方言。

Actions Status Coverage Status Discord Chat

在Playground中尝试 · 阅读Steel书籍(WIP)

入门

此GitHub仓库包含一个命令行解释器。要在在线沙盒中试用,请访问 Steel沙盒。要开始使用crate的repl,请确保您已安装Rust。

然后,克隆仓库并运行以下命令

cargo run

这将启动一个REPL实例,看起来像这样

如果您想安装和使用包,请设置STEEL_HOME环境变量。这将包安装的位置。Steel目前不假设任何默认值。

关于

Steel是一个嵌入式scheme解释器,还包括一个独立的命令行工具。受到Racket的很大启发,该语言旨在成为易于嵌入应用程序或独立使用(具有用Rust实现的性能函数)的scheme方言。语言实现本身包含一个基于syntax-rules风格的相当强大的宏系统和一个字节码虚拟机。目前,它并不明确符合任何特定的scheme规范。

警告 API是不稳定的,没有任何保证,在1.0版本之前可能会随时更改。肯定存在一些错误,任何重大错误报告都将得到迅速解决。但话说回来,我确实用它作为许多脚本任务的日常驱动程序。

特性

  • 支持有限的syntax-rules风格宏
  • 易于与Rust函数和结构体集成
  • 可以从Rust或通过单独的文件轻松调用脚本
  • 高效 - 常用函数和数据结构针对性能进行了优化(如mapfilter等)
  • 高阶合约
  • 内置不可变数据结构包括
    • 列表
    • 向量
    • 哈希表
    • 哈希集

合约

受Racket高级契约的启发,Steel实现了高级契约,以便通过define/contract宏实现契约设计,使操作更符合人体工程学。Racket利用了一种称为责任的概念来识别违反方——Steel尚未完全实现责任机制,但这正在进行中。以下是一些示例:

;; Simple flat contracts
(define/contract (test x y)
    (->/c even? even? odd?)
    (+ x y 1))

(test 2 2) ;; => 5

(define/contract (test-violation x y)
    (->/c even? even? odd?)
    (+ x y 1))

(test-violation 1 2) ;; contract violation

契约被实现为,因此它们绑定到函数上。这使得可以在函数本身上使用契约检查,因为函数可以被传递。

;; Higher order contracts, check on application
(define/contract (higher-order func y)
    (->/c (->/c even? odd?) even? even?)
    (+ 1 (func y)))

(higher-order (lambda (x) (+ x 1)) 2) ;; => 4

(define/contract (higher-order-violation func y)
    (->/c (->/c even? odd?) even? even?)
    (+ 1 (func y)))

(higher-order-violation (lambda (x) (+ x 2)) 2) ;; contract violation

函数上的契约仅在它们被应用时进行检查,因此返回契约函数的函数不会在函数实际使用时导致违规。

;; More higher order contracts, get checked on application
(define/contract (output)
    (->/c (->/c string? int?))
    (lambda (x) 10))

(define/contract (accept func)
    (->/c (->/c string? int?) string?)
    "cool cool cool")

(accept (output)) ;; => "cool cool cool"

;; different contracts on the argument
(define/contract (accept-violation func)
    (->/c (->/c string? string?) string?)
    (func "applesauce")
    "cool cool cool")

(accept-violation (output)) ;; contract violation

;; generates a function
(define/contract (generate-closure)
    (->/c (->/c string? int?))
    (lambda (x) 10))

;; calls generate-closure which should result in a contract violation
(define/contract (accept-violation)
    (->/c (->/c string? string?))
    (generate-closure))

((accept-violation) "test") ;; contract violation

可能是一个更微妙的情况

(define/contract (output)
    (->/c (->/c string? int?))
    (lambda (x) 10.2))

(define/contract (accept)
    (->/c (->/c string? number?))
    (output))


((accept) "test") ;; contract violation 10.2 satisfies number? but _not_ int?

* 正在进行中

转换器

受Clojure转换器的启发,Steel有一个类似的对象,位于转换器和迭代器之间。考虑以下内容:


(mapping (lambda (x) (+ x 1))) ;; => <#iterator>
(filtering even?) ;; => <#iterator>
(taking 15) ;; => <#iterator>

(compose 
    (mapping add1)
    (filtering odd?)
    (taking 15)) ;; => <#iterator>

这些表达式中的每一个都发出一个<#iterator>对象,这意味着它们与transduce兼容。transduce接受一个转换器(即<#iterator>)和一个可迭代的集合(listvectorstreamhashsethashmapstringstruct)并将转换器应用于它。

;; Accepts lists
(transduce (list 1 2 3 4 5) (mapping (lambda (x) (+ x 1))) (into-list)) ;; => '(2 3 4 5 6)

;; Accepts vectors
(transduce (vector 1 2 3 4 5) (mapping (lambda (x) (+ x 1))) (into-vector)) ;; '#(2 3 4 5 6)

;; Even accepts streams!
(define (integers n)
    (stream-cons n (lambda () (integers (+ 1 n)))))

(transduce (integers 0) (taking 5) (into-list)) ;; => '(0 1 2 3 4)

转换器还接受一个归约函数。上面我们使用了into-listinto-vector,但下面我们可以使用任何任意的归约器。

;; (-> transducer reducing-function initial-value iterable)
(transduce (list 0 1 2 3) (mapping (lambda (x) (+ x 1))) (into-reducer + 0)) ;; => 10

组合仅仅是将迭代器函数组合起来,并让我们避免中间分配。组合是从左到右工作的——它通过函数链每个值,然后累积到输出类型中。请看以下内容:

(define xf 
    (compose 
        (mapping add1)
        (filtering odd?)
        (taking 5)))

(transduce (range 0 100) xf (into-list)) ;; => '(1 3 5 7 9)

语法选择

Steel对定义变量和函数的方式有一些观点。除了我从Racket那里借用的缩写函数语法外,这些选择相当随意。我非常鼓励使用defnfn,因为这样可以少输入一些字符。


;; All of the following are equivalent
(define (foo x) (+ x 1))
(define foo (lambda (x) (+ x 1)))
(defn (foo x) (+ x 1))
(defn foo (lambda (x) (+ x 1)))

;; All of the following are equivalent
(lambda (x) (+ x 1))
(λ (x) (+ x 1))
(fn (x) (+ x 1))

模块

为了支持日益增长的代码库,Steel支持跨多个文件的模块。Steel文件可以provide值(附有契约)并从其他文件require模块。

;; main.scm
(require "provide.scm")

(even->odd 10)


;; provide.scm
(provide 
    (contract/out even->odd (->/c even? odd?))
    no-contract
    flat-value)

(define (even->odd x) 
    (+ x 1))

(define (accept-number x) (+ x 10))

(define (no-contract) "cool cool cool")
(define flat-value 15)

(displayln "Calling even->odd with some bad inputs but its okay")
(displayln (even->odd 1))

在这里,如果我们运行main,它将包含provide的内容,并且只有提供的值可以从main访问。契约附加在契约边界处,因此在使用provide模块时,您可以违反契约,但模块外部将应用契约。

关于模块的一些说明

  • 不允许循环依赖
  • 模块只编译一次,并在多个文件中使用。如果A需要BC,而B需要C,则C将只编译一次,并在AB之间共享。
  • 模块在更改时将被重新编译,并且任何相关文件也将在必要时重新编译。

性能

初步基准测试显示以下结果(在我的机器上)

基准测试 Steel Python
(fib 28) 63.383ms 65.10 ms
(ack 3 3) 0.303 ms 0.195 ms

在虚拟机中嵌入Rust值的示例

Rust值、类型和函数可以轻松嵌入到Steel中。使用register_fn调用,您可以轻松地嵌入函数。

use steel_vm::engine::Engine;
use steel_vm::register_fn::RegisterFn;

fn external_function(arg1: usize, arg2: usize) -> usize {
    arg1 + arg2
}

fn option_function(arg1: Option<String>) -> Option<String> {
    arg1
}

fn result_function(arg1: Option<String>) -> Result<String, String> {
    if let Some(inner) = arg1 {
        Ok(inner)
    } else {
        Err("Got a none".to_string())
    }
}

pub fn main() {
    let mut vm = Engine::new();

    // Here we can register functions
    // Any function can accept parameters that implement `FromSteelVal` and
    // return values that implement `IntoSteelVal`
    vm.register_fn("external-function", external_function);

    // See the docs for more information about `FromSteelVal` and `IntoSteelVal`
    // but we can see even functions that accept/return Option<T> or Result<T,E>
    // can be registered
    vm.register_fn("option-function", option_function);

    // Result values will map directly to errors in the VM and bubble back up
    vm.register_fn("result-function", result_function);

    vm.run(
        r#"
        (define foo (external-function 10 25))
        (define bar (option-function "applesauce"))
        (define baz (result-function "bananas"))
    "#,
    )
    .unwrap();

    let foo = vm.extract::<usize>("foo").unwrap();
    println!("foo: {}", foo);
    assert_eq!(35, foo);

    // Can also extract a value by specifying the type on the variable
    let bar: String = vm.extract("bar").unwrap();
    println!("bar: {}", bar);
    assert_eq!("applesauce".to_string(), bar);

    let baz: String = vm.extract("baz").unwrap();
    println!("baz: {}", baz);
    assert_eq!("bananas".to_string(), baz);
}

我们还可以嵌入结构体本身

use steel_vm::engine::Engine;
use steel_vm::register_fn::RegisterFn;

use steel_derive::Steel;

// In order to register a type with Steel,
// it must implement Clone, Debug, and Steel
#[derive(Clone, Debug, Steel, PartialEq)]
pub struct ExternalStruct {
    foo: usize,
    bar: String,
    baz: f64,
}

impl ExternalStruct {
    pub fn new(foo: usize, bar: String, baz: f64) -> Self {
        ExternalStruct { foo, bar, baz }
    }

    // Embedding functions that take self by value
    pub fn method_by_value(self) -> usize {
        self.foo
    }

    pub fn method_by_reference(&self) -> usize {
        self.foo
    }

    // Setters should update the value and return a new instance (functional set)
    pub fn set_foo(mut self, foo: usize) -> Self {
        self.foo = foo;
        self
    }
}

pub fn main() {
    let mut vm = Engine::new();

    // Registering a type gives access to a predicate for the type
    vm.register_type::<ExternalStruct>("ExternalStruct?");

    // Structs in steel typically have a constructor that is the name of the struct
    vm.register_fn("ExternalStruct", ExternalStruct::new);

    // register_fn can be chained
    vm.register_fn("method-by-value", ExternalStruct::method_by_value)
        .register_fn("method-by-reference", ExternalStruct::method_by_reference)
        .register_fn("set-foo", ExternalStruct::set_foo);

    let external_struct = ExternalStruct::new(1, "foo".to_string(), 12.4);

    // Registering an external value is fallible if the conversion fails for some reason
    // For instance, registering an Err(T) is fallible. However, most implementation outside of manual
    // ones should not fail
    vm.register_external_value("external-struct", external_struct)
        .unwrap();

    let output = vm
        .run(
            r#"
            (define new-external-struct (set-foo external-struct 100))
            (define get-output (method-by-value external-struct))
            (define second-new-external-struct (ExternalStruct 50 "bananas" 72.6))
            "last-result"
        "#,
        )
        .unwrap();

    let new_external_struct = vm.extract::<ExternalStruct>("new-external-struct").unwrap();
    println!("new_external_struct: {:?}", new_external_struct);
    assert_eq!(
        ExternalStruct::new(100, "foo".to_string(), 12.4),
        new_external_struct
    );

    // Can also extract a value by specifying the type on the variable
    let get_output: usize = vm.extract("get-output").unwrap();
    println!("get_output: {}", get_output);
    assert_eq!(1, get_output);

    let second_new_external_struct: ExternalStruct =
        vm.extract("second-new-external-struct").unwrap();
    println!(
        "second_new_external_struct: {:?}",
        second_new_external_struct
    );
    assert_eq!(
        ExternalStruct::new(50, "bananas".to_string(), 72.6),
        second_new_external_struct
    );

    // We also get the output of the VM as the value of every expression run
    // we can inspect the results just by printing like so
    println!("{:?}", output);
}

请参阅示例文件夹,了解更多有关嵌入值和与外部世界交互的示例。

许可证

根据以下任一许可证授权:

任选其一。

贡献

除非你明确声明,否则根据Apache-2.0许可证定义的,你有意提交并包含在工作中的任何贡献,将按照上述方式双重许可,不附加任何额外的条款或条件。

请参阅 CONTRIBUTING.md

依赖关系

~3MB
~35K SLoC