feat: basic builtin execution
Co-authored-by: Kasey White <kwhitemsg@gmail.com>
This commit is contained in:
parent
9ea57aa461
commit
d55409d8c5
|
@ -0,0 +1,4 @@
|
|||
(program
|
||||
1.0.0
|
||||
[ (builtin addInteger) (con integer 2) (con integer 3) ]
|
||||
)
|
|
@ -10,6 +10,8 @@ mod runtime;
|
|||
use cost_model::{ExBudget, MachineCosts, StepKind};
|
||||
pub use error::Error;
|
||||
|
||||
use self::runtime::BuiltinRuntime;
|
||||
|
||||
pub struct Machine {
|
||||
costs: MachineCosts,
|
||||
ex_budget: ExBudget,
|
||||
|
@ -89,9 +91,16 @@ impl Machine {
|
|||
self.compute(body)
|
||||
}
|
||||
Term::Error => Err(Error::EvaluationFailure),
|
||||
Term::Builtin(_bn) => {
|
||||
Term::Builtin(fun) => {
|
||||
self.step_and_maybe_spend(StepKind::Builtin)?;
|
||||
todo!()
|
||||
|
||||
let runtime: BuiltinRuntime = (*fun).into();
|
||||
|
||||
self.return_compute(Value::Builtin {
|
||||
fun: *fun,
|
||||
term: term.clone(),
|
||||
runtime,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -135,7 +144,7 @@ impl Machine {
|
|||
fn discharge_value(&mut self, value: Value) -> Term<NamedDeBruijn> {
|
||||
match value {
|
||||
Value::Con(x) => Term::Constant(x),
|
||||
Value::Builtin(_, t) => t,
|
||||
Value::Builtin { term, .. } => term,
|
||||
Value::Delay(body) => self.discharge_value_env(Term::Delay(Box::new(body))),
|
||||
Value::Lambda {
|
||||
parameter_name,
|
||||
|
@ -187,7 +196,7 @@ impl Machine {
|
|||
fn force_evaluate(&mut self, value: Value) -> Result<Term<NamedDeBruijn>, Error> {
|
||||
match value {
|
||||
Value::Delay(body) => self.compute(&body),
|
||||
Value::Builtin(_, _) => todo!(),
|
||||
Value::Builtin { .. } => todo!(),
|
||||
rest => Err(Error::NonPolymorphicInstantiation(rest)),
|
||||
}
|
||||
}
|
||||
|
@ -204,11 +213,45 @@ impl Machine {
|
|||
self.env.pop();
|
||||
Ok(term)
|
||||
}
|
||||
Value::Builtin(_, _) => todo!(),
|
||||
Value::Builtin {
|
||||
fun,
|
||||
term,
|
||||
mut runtime,
|
||||
} => {
|
||||
let arg_term = self.discharge_value(argument.clone());
|
||||
|
||||
let t = Term::<NamedDeBruijn>::Apply {
|
||||
function: Box::new(term),
|
||||
argument: Box::new(arg_term),
|
||||
};
|
||||
|
||||
if runtime.is_arrow() {
|
||||
runtime.push(argument)?;
|
||||
}
|
||||
|
||||
let res = self.eval_builtin_app(fun, t, runtime)?;
|
||||
|
||||
self.return_compute(res)
|
||||
}
|
||||
rest => Err(Error::NonFunctionalApplication(rest)),
|
||||
}
|
||||
}
|
||||
|
||||
fn eval_builtin_app(
|
||||
&mut self,
|
||||
fun: DefaultFunction,
|
||||
term: Term<NamedDeBruijn>,
|
||||
runtime: BuiltinRuntime,
|
||||
) -> Result<Value, Error> {
|
||||
if runtime.is_ready() {
|
||||
self.spend_budget(ExBudget::default())?;
|
||||
|
||||
runtime.call()
|
||||
} else {
|
||||
Ok(Value::Builtin { fun, term, runtime })
|
||||
}
|
||||
}
|
||||
|
||||
fn push_frame(&mut self, frame: Context) {
|
||||
self.frames.push(frame);
|
||||
}
|
||||
|
@ -280,10 +323,15 @@ pub enum Value {
|
|||
parameter_name: NamedDeBruijn,
|
||||
body: Term<NamedDeBruijn>,
|
||||
},
|
||||
Builtin(
|
||||
DefaultFunction,
|
||||
Term<NamedDeBruijn>,
|
||||
// Need to figure out run time stuff
|
||||
// BuiltinRuntime (CekValue uni fun)
|
||||
),
|
||||
Builtin {
|
||||
fun: DefaultFunction,
|
||||
term: Term<NamedDeBruijn>,
|
||||
runtime: BuiltinRuntime,
|
||||
},
|
||||
}
|
||||
|
||||
impl Value {
|
||||
pub fn is_integer(&self) -> bool {
|
||||
matches!(self, Value::Con(Constant::Integer(_)))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,44 +1,279 @@
|
|||
use super::cost_model::{CostingFun, ExBudget};
|
||||
use crate::{ast::Constant, builtins::DefaultFunction};
|
||||
|
||||
enum RuntimeScheme {
|
||||
RuntimeSchemeResult,
|
||||
RuntimeSchemeArrow(usize),
|
||||
RuntimeSchemeAll(usize),
|
||||
use super::{
|
||||
// cost_model::{CostingFun, ExBudget},
|
||||
Error,
|
||||
Value,
|
||||
};
|
||||
|
||||
//#[derive(std::cmp::PartialEq)]
|
||||
//pub enum EvalMode {
|
||||
// Immediate,
|
||||
// Deferred,
|
||||
//}
|
||||
|
||||
// pub struct BuiltinRuntimeOptions<T, G> {
|
||||
// immediate_eval: T,
|
||||
// deferred_eval: T,
|
||||
// budget: fn(CostingFun<G>) -> fn(Vec<u32>) -> ExBudget,
|
||||
// }
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct BuiltinRuntime {
|
||||
meaning: BuiltinMeaning,
|
||||
// budget: fn(Vec<u32>) -> ExBudget,
|
||||
}
|
||||
|
||||
#[derive(std::cmp::PartialEq)]
|
||||
enum EvalMode {
|
||||
Immediate,
|
||||
Deferred,
|
||||
#[derive(Clone, Debug)]
|
||||
struct BuiltinMeaning {
|
||||
args: Vec<Value>,
|
||||
fun: DefaultFunction,
|
||||
}
|
||||
|
||||
struct BuiltinRuntimeOptions<T, G> {
|
||||
runtime_scheme: Vec<RuntimeScheme>,
|
||||
immediate_eval: T,
|
||||
deferred_eval: T,
|
||||
budget: fn(CostingFun<G>) -> fn(Vec<u32>) -> ExBudget,
|
||||
impl BuiltinMeaning {
|
||||
pub fn new(fun: DefaultFunction) -> BuiltinMeaning {
|
||||
Self { args: vec![], fun }
|
||||
}
|
||||
}
|
||||
|
||||
struct BuiltinRuntime<T> {
|
||||
runtime_scheme: Vec<RuntimeScheme>,
|
||||
runtime_denotation: T,
|
||||
budget: fn(Vec<u32>) -> ExBudget,
|
||||
impl BuiltinRuntime {
|
||||
// fn from_builtin_runtime_options<G>(
|
||||
// eval_mode: EvalMode,
|
||||
// cost: CostingFun<G>,
|
||||
// runtime_options: BuiltinRuntimeOptions<T, G>,
|
||||
// ) -> BuiltinRuntime {
|
||||
// Self {
|
||||
// budget: (runtime_options.budget)(cost),
|
||||
// }
|
||||
// }
|
||||
|
||||
pub fn is_arrow(&self) -> bool {
|
||||
self.meaning.args.len() != self.meaning.fun.arity()
|
||||
}
|
||||
|
||||
pub fn is_ready(&self) -> bool {
|
||||
self.meaning.args.len() == self.meaning.fun.arity()
|
||||
}
|
||||
|
||||
pub fn call(&self) -> Result<Value, Error> {
|
||||
self.meaning.fun.call(&self.meaning.args)
|
||||
}
|
||||
|
||||
pub fn push(&mut self, arg: Value) -> Result<(), Error> {
|
||||
self.meaning.fun.check_type(&arg)?;
|
||||
|
||||
self.meaning.args.push(arg);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> BuiltinRuntime<T> {
|
||||
fn from_builtin_runtime_options<G>(
|
||||
eval_mode: EvalMode,
|
||||
cost: CostingFun<G>,
|
||||
runtime_options: BuiltinRuntimeOptions<T, G>,
|
||||
) -> BuiltinRuntime<T> {
|
||||
impl From<DefaultFunction> for BuiltinMeaning {
|
||||
fn from(fun: DefaultFunction) -> Self {
|
||||
BuiltinMeaning::new(fun)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<DefaultFunction> for BuiltinRuntime {
|
||||
fn from(fun: DefaultFunction) -> Self {
|
||||
BuiltinRuntime {
|
||||
runtime_scheme: runtime_options.runtime_scheme,
|
||||
runtime_denotation: if eval_mode == EvalMode::Immediate {
|
||||
runtime_options.immediate_eval
|
||||
} else {
|
||||
runtime_options.deferred_eval
|
||||
},
|
||||
budget: (runtime_options.budget)(cost),
|
||||
meaning: fun.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl DefaultFunction {
|
||||
pub fn arity(&self) -> usize {
|
||||
match self {
|
||||
DefaultFunction::AddInteger => 2,
|
||||
DefaultFunction::SubtractInteger => todo!(),
|
||||
DefaultFunction::MultiplyInteger => todo!(),
|
||||
DefaultFunction::DivideInteger => todo!(),
|
||||
DefaultFunction::QuotientInteger => todo!(),
|
||||
DefaultFunction::RemainderInteger => todo!(),
|
||||
DefaultFunction::ModInteger => todo!(),
|
||||
DefaultFunction::EqualsInteger => todo!(),
|
||||
DefaultFunction::LessThanInteger => todo!(),
|
||||
DefaultFunction::LessThanEqualsInteger => todo!(),
|
||||
DefaultFunction::AppendByteString => todo!(),
|
||||
DefaultFunction::ConsByteString => todo!(),
|
||||
DefaultFunction::SliceByteString => todo!(),
|
||||
DefaultFunction::LengthOfByteString => todo!(),
|
||||
DefaultFunction::IndexByteString => todo!(),
|
||||
DefaultFunction::EqualsByteString => todo!(),
|
||||
DefaultFunction::LessThanByteString => todo!(),
|
||||
DefaultFunction::LessThanEqualsByteString => todo!(),
|
||||
DefaultFunction::Sha2_256 => todo!(),
|
||||
DefaultFunction::Sha3_256 => todo!(),
|
||||
DefaultFunction::Blake2b_256 => todo!(),
|
||||
DefaultFunction::VerifySignature => todo!(),
|
||||
DefaultFunction::VerifyEcdsaSecp256k1Signature => todo!(),
|
||||
DefaultFunction::VerifySchnorrSecp256k1Signature => todo!(),
|
||||
DefaultFunction::AppendString => todo!(),
|
||||
DefaultFunction::EqualsString => todo!(),
|
||||
DefaultFunction::EncodeUtf8 => todo!(),
|
||||
DefaultFunction::DecodeUtf8 => todo!(),
|
||||
DefaultFunction::IfThenElse => todo!(),
|
||||
DefaultFunction::ChooseUnit => todo!(),
|
||||
DefaultFunction::Trace => todo!(),
|
||||
DefaultFunction::FstPair => todo!(),
|
||||
DefaultFunction::SndPair => todo!(),
|
||||
DefaultFunction::ChooseList => todo!(),
|
||||
DefaultFunction::MkCons => todo!(),
|
||||
DefaultFunction::HeadList => todo!(),
|
||||
DefaultFunction::TailList => todo!(),
|
||||
DefaultFunction::NullList => todo!(),
|
||||
DefaultFunction::ChooseData => todo!(),
|
||||
DefaultFunction::ConstrData => todo!(),
|
||||
DefaultFunction::MapData => todo!(),
|
||||
DefaultFunction::ListData => todo!(),
|
||||
DefaultFunction::IData => todo!(),
|
||||
DefaultFunction::BData => todo!(),
|
||||
DefaultFunction::UnConstrData => todo!(),
|
||||
DefaultFunction::UnMapData => todo!(),
|
||||
DefaultFunction::UnListData => todo!(),
|
||||
DefaultFunction::UnIData => todo!(),
|
||||
DefaultFunction::UnBData => todo!(),
|
||||
DefaultFunction::EqualsData => todo!(),
|
||||
DefaultFunction::SerialiseData => todo!(),
|
||||
DefaultFunction::MkPairData => todo!(),
|
||||
DefaultFunction::MkNilData => todo!(),
|
||||
DefaultFunction::MkNilPairData => todo!(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn check_type(&self, arg: &Value) -> Result<(), Error> {
|
||||
match self {
|
||||
DefaultFunction::AddInteger => {
|
||||
if arg.is_integer() {
|
||||
Ok(())
|
||||
} else {
|
||||
todo!("type error")
|
||||
}
|
||||
}
|
||||
DefaultFunction::SubtractInteger => todo!(),
|
||||
DefaultFunction::MultiplyInteger => todo!(),
|
||||
DefaultFunction::DivideInteger => todo!(),
|
||||
DefaultFunction::QuotientInteger => todo!(),
|
||||
DefaultFunction::RemainderInteger => todo!(),
|
||||
DefaultFunction::ModInteger => todo!(),
|
||||
DefaultFunction::EqualsInteger => todo!(),
|
||||
DefaultFunction::LessThanInteger => todo!(),
|
||||
DefaultFunction::LessThanEqualsInteger => todo!(),
|
||||
DefaultFunction::AppendByteString => todo!(),
|
||||
DefaultFunction::ConsByteString => todo!(),
|
||||
DefaultFunction::SliceByteString => todo!(),
|
||||
DefaultFunction::LengthOfByteString => todo!(),
|
||||
DefaultFunction::IndexByteString => todo!(),
|
||||
DefaultFunction::EqualsByteString => todo!(),
|
||||
DefaultFunction::LessThanByteString => todo!(),
|
||||
DefaultFunction::LessThanEqualsByteString => todo!(),
|
||||
DefaultFunction::Sha2_256 => todo!(),
|
||||
DefaultFunction::Sha3_256 => todo!(),
|
||||
DefaultFunction::Blake2b_256 => todo!(),
|
||||
DefaultFunction::VerifySignature => todo!(),
|
||||
DefaultFunction::VerifyEcdsaSecp256k1Signature => todo!(),
|
||||
DefaultFunction::VerifySchnorrSecp256k1Signature => todo!(),
|
||||
DefaultFunction::AppendString => todo!(),
|
||||
DefaultFunction::EqualsString => todo!(),
|
||||
DefaultFunction::EncodeUtf8 => todo!(),
|
||||
DefaultFunction::DecodeUtf8 => todo!(),
|
||||
DefaultFunction::IfThenElse => todo!(),
|
||||
DefaultFunction::ChooseUnit => todo!(),
|
||||
DefaultFunction::Trace => todo!(),
|
||||
DefaultFunction::FstPair => todo!(),
|
||||
DefaultFunction::SndPair => todo!(),
|
||||
DefaultFunction::ChooseList => todo!(),
|
||||
DefaultFunction::MkCons => todo!(),
|
||||
DefaultFunction::HeadList => todo!(),
|
||||
DefaultFunction::TailList => todo!(),
|
||||
DefaultFunction::NullList => todo!(),
|
||||
DefaultFunction::ChooseData => todo!(),
|
||||
DefaultFunction::ConstrData => todo!(),
|
||||
DefaultFunction::MapData => todo!(),
|
||||
DefaultFunction::ListData => todo!(),
|
||||
DefaultFunction::IData => todo!(),
|
||||
DefaultFunction::BData => todo!(),
|
||||
DefaultFunction::UnConstrData => todo!(),
|
||||
DefaultFunction::UnMapData => todo!(),
|
||||
DefaultFunction::UnListData => todo!(),
|
||||
DefaultFunction::UnIData => todo!(),
|
||||
DefaultFunction::UnBData => todo!(),
|
||||
DefaultFunction::EqualsData => todo!(),
|
||||
DefaultFunction::SerialiseData => todo!(),
|
||||
DefaultFunction::MkPairData => todo!(),
|
||||
DefaultFunction::MkNilData => todo!(),
|
||||
DefaultFunction::MkNilPairData => todo!(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn call(&self, args: &[Value]) -> Result<Value, Error> {
|
||||
match self {
|
||||
DefaultFunction::AddInteger => {
|
||||
assert_eq!(args.len(), self.arity());
|
||||
|
||||
let args = (&args[0], &args[1]);
|
||||
|
||||
match args {
|
||||
(Value::Con(Constant::Integer(arg1)), Value::Con(Constant::Integer(arg2))) => {
|
||||
Ok(Value::Con(Constant::Integer(arg1 + arg2)))
|
||||
}
|
||||
_ => todo!("handle error"),
|
||||
}
|
||||
}
|
||||
DefaultFunction::SubtractInteger => todo!(),
|
||||
DefaultFunction::MultiplyInteger => todo!(),
|
||||
DefaultFunction::DivideInteger => todo!(),
|
||||
DefaultFunction::QuotientInteger => todo!(),
|
||||
DefaultFunction::RemainderInteger => todo!(),
|
||||
DefaultFunction::ModInteger => todo!(),
|
||||
DefaultFunction::EqualsInteger => todo!(),
|
||||
DefaultFunction::LessThanInteger => todo!(),
|
||||
DefaultFunction::LessThanEqualsInteger => todo!(),
|
||||
DefaultFunction::AppendByteString => todo!(),
|
||||
DefaultFunction::ConsByteString => todo!(),
|
||||
DefaultFunction::SliceByteString => todo!(),
|
||||
DefaultFunction::LengthOfByteString => todo!(),
|
||||
DefaultFunction::IndexByteString => todo!(),
|
||||
DefaultFunction::EqualsByteString => todo!(),
|
||||
DefaultFunction::LessThanByteString => todo!(),
|
||||
DefaultFunction::LessThanEqualsByteString => todo!(),
|
||||
DefaultFunction::Sha2_256 => todo!(),
|
||||
DefaultFunction::Sha3_256 => todo!(),
|
||||
DefaultFunction::Blake2b_256 => todo!(),
|
||||
DefaultFunction::VerifySignature => todo!(),
|
||||
DefaultFunction::VerifyEcdsaSecp256k1Signature => todo!(),
|
||||
DefaultFunction::VerifySchnorrSecp256k1Signature => todo!(),
|
||||
DefaultFunction::AppendString => todo!(),
|
||||
DefaultFunction::EqualsString => todo!(),
|
||||
DefaultFunction::EncodeUtf8 => todo!(),
|
||||
DefaultFunction::DecodeUtf8 => todo!(),
|
||||
DefaultFunction::IfThenElse => todo!(),
|
||||
DefaultFunction::ChooseUnit => todo!(),
|
||||
DefaultFunction::Trace => todo!(),
|
||||
DefaultFunction::FstPair => todo!(),
|
||||
DefaultFunction::SndPair => todo!(),
|
||||
DefaultFunction::ChooseList => todo!(),
|
||||
DefaultFunction::MkCons => todo!(),
|
||||
DefaultFunction::HeadList => todo!(),
|
||||
DefaultFunction::TailList => todo!(),
|
||||
DefaultFunction::NullList => todo!(),
|
||||
DefaultFunction::ChooseData => todo!(),
|
||||
DefaultFunction::ConstrData => todo!(),
|
||||
DefaultFunction::MapData => todo!(),
|
||||
DefaultFunction::ListData => todo!(),
|
||||
DefaultFunction::IData => todo!(),
|
||||
DefaultFunction::BData => todo!(),
|
||||
DefaultFunction::UnConstrData => todo!(),
|
||||
DefaultFunction::UnMapData => todo!(),
|
||||
DefaultFunction::UnListData => todo!(),
|
||||
DefaultFunction::UnIData => todo!(),
|
||||
DefaultFunction::UnBData => todo!(),
|
||||
DefaultFunction::EqualsData => todo!(),
|
||||
DefaultFunction::SerialiseData => todo!(),
|
||||
DefaultFunction::MkPairData => todo!(),
|
||||
DefaultFunction::MkNilData => todo!(),
|
||||
DefaultFunction::MkNilPairData => todo!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue