-
Notifications
You must be signed in to change notification settings - Fork 5
Allows Python Function Registration #18
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
9f3b669
Allows Python Function Registration
meso03 a6f4b79
fixed errors
meso03 f33d279
Created new Python domain
meso03 9f9ea98
Addressed some comments
meso03 46c7c8e
Py domain can register constants
meso03 7c1bcdc
Made a create python dsl function and removed it from dsl.rs
f1a8d7e
Revert unnecessary changes
6786256
Moved Python Production Function to py.rs
abfce32
Removed unwanted lines
d40f6dc
Addressed code review comments
bf4a17f
Merge remote-tracking branch 'upstream/main' into py_registration
c28462b
Clippy changes
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| use crate::*; | ||
| use std::collections::HashSet; | ||
| use std::sync::Arc; | ||
|
|
||
|
|
||
| use pyo3::prelude::*; | ||
| use pyo3::types::PyAny; | ||
|
|
||
| #[derive(Clone,Debug, PartialEq, Eq, Hash)] | ||
| pub enum PyVal { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could potentially just be PyAny |
||
| Int(i32), | ||
| List(Vec<Val>), | ||
| } | ||
|
|
||
| #[derive(Clone,Debug, PartialEq, Eq, Hash)] | ||
| pub enum PyType { | ||
| TInt, | ||
| TList | ||
| } | ||
|
|
||
|
|
||
| type Val = crate::eval::Val<PyVal>; | ||
|
|
||
|
|
||
| #[cfg(feature = "python")] | ||
| pub fn create_python_production<D: Domain>( | ||
| name: Symbol, | ||
| tp: SlowType, | ||
| lazy_args: Option<&[usize]>, | ||
| pyfunc: Py<PyAny>, | ||
| ) -> Production<D> { | ||
| let arity = tp.arity(); | ||
| let lazy: HashSet<usize> = lazy_args | ||
| .map(|xs| xs.iter().copied().collect()) | ||
| .unwrap_or_default(); | ||
|
|
||
| use crate::eval::{CurriedFn, Val}; | ||
|
|
||
| Production { | ||
| name: name.clone(), | ||
| val: Val::PrimFun(CurriedFn::<D>::new(name.clone(), arity)), | ||
| tp, | ||
| arity, | ||
| lazy_args: lazy, | ||
| fn_ptr: Some(FnPtr::Python(Arc::new(pyfunc))) | ||
| } | ||
| } | ||
|
|
||
| // From<Val> impls are needed for unwrapping values. We can assume the program | ||
| // has been type checked so it's okay to panic if the type is wrong. Each val variant | ||
| // must map to exactly one unwrapped type (though it doesnt need to be one to one in the | ||
| // other direction) | ||
| impl FromVal<PyVal> for i32 { | ||
| fn from_val(v: Val) -> Result<Self, VError> { | ||
| match v { | ||
| Dom(PyVal::Int(i)) => Ok(i), | ||
| _ => Err("from_val_to_i32: not an int".into()) | ||
| } | ||
| } | ||
| } | ||
| impl<T: FromVal<PyVal>> FromVal<PyVal> for Vec<T> { | ||
| fn from_val(v: Val) -> Result<Self, VError> { | ||
| match v { | ||
| Dom(PyVal::List(v)) => v.into_iter().map(|v| T::from_val(v)).collect(), | ||
| _ => Err("from_val_to_vec: not a list".into()) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl From<i32> for Val { | ||
| fn from(i: i32) -> Val { | ||
| Dom(PyVal::Int(i)) | ||
| } | ||
| } | ||
| impl<T: Into<Val>> From<Vec<T>> for Val { | ||
| fn from(vec: Vec<T>) -> Val { | ||
| Dom(PyVal::List(vec.into_iter().map(|v| v.into()).collect())) | ||
| } | ||
| } | ||
|
|
||
| impl Domain for PyVal { | ||
| type Data = (); | ||
|
|
||
| #[cfg(feature = "python")] | ||
| fn py_val_to_py(py: Python<'_>, v: crate::eval::Val<Self>) -> PyResult<Py<PyAny>> { | ||
| crate::domains::simple_python::val_to_py(py, v) | ||
| } | ||
|
|
||
| #[cfg(feature = "python")] | ||
| fn py_py_to_val(obj: &Bound<'_, PyAny>) -> Result<crate::eval::Val<Self>, String> { | ||
| crate::domains::simple_python::py_to_val(obj) | ||
| } | ||
|
|
||
| fn new_dsl() -> DSL<Self> { | ||
kavigupta marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| let prods = vec![]; | ||
| let dsl = DSL::new(prods); | ||
| dsl | ||
| } | ||
|
|
||
| fn val_of_prim_fallback(p: &Symbol) -> Option<Val> { | ||
| None | ||
| } | ||
|
|
||
| fn type_of_dom_val(&self) -> SlowType { | ||
| match self { | ||
| PyVal::Int(_) => SlowType::base(Symbol::from("int")), | ||
| PyVal::List(xs) => { | ||
| let elem_tp = if xs.is_empty() { | ||
| SlowType::Var(0) // (list t0) | ||
| } else { | ||
| let result = Self::type_of_dom_val(&xs.first().unwrap().clone().dom().unwrap()); | ||
| assert!(xs.iter().all(|v| result == Self::type_of_dom_val(&v.clone().dom().unwrap()))); | ||
| result | ||
| }; | ||
| SlowType::Term("list".into(),vec![elem_tp]) | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| #![cfg(feature = "python")] | ||
|
|
||
| use pyo3::prelude::*; | ||
| use pyo3::types::PyList; | ||
| use pyo3::conversion::IntoPyObjectExt; | ||
|
|
||
| use crate::domains::py::PyVal; | ||
| use crate::eval; | ||
| type Val = eval::Val<PyVal>; | ||
| use PyVal::*; | ||
|
|
||
|
|
||
| pub fn val_to_py(py: Python<'_>, v: Val) -> PyResult<Py<PyAny>> { | ||
| match v.dom().expect("Val should be Dom") { | ||
| Int(i) => { | ||
| Ok(i.into_py_any(py)?) | ||
| } | ||
| List(xs) => { | ||
| let mut elems: Vec<Py<PyAny>> = Vec::with_capacity(xs.len()); | ||
| for x in xs { | ||
| elems.push(val_to_py(py, x.clone())?); | ||
| } | ||
|
|
||
meso03 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| let list_bound: Bound<'_, PyList> = PyList::new(py, &elems)?; | ||
|
|
||
| Ok(list_bound.into_any().unbind()) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub fn py_to_val(obj: &Bound<'_, PyAny>) -> Result<Val, String> { | ||
| if let Ok(i) = obj.extract::<i32>() { | ||
| return Ok(Val::from(Int(i))); | ||
| } | ||
| if let Ok(list) = obj.downcast::<PyList>() { | ||
| let mut out: Vec<Val> = Vec::with_capacity(list.len()); | ||
| for item in list.iter() { | ||
| out.push(py_to_val(&item)?); | ||
| } | ||
| return Ok(Val::from(List(out))); | ||
| } | ||
| Err("unsupported Python type for PyVal".into()) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.