How can we store a chain of heterogeneous functions in Rust? - rust

I'm working on some code where I'm interested in a lazy-evaluated function chain. In other words, it stores all the operations you want, and only evaluates them all together.
This is very easy when all the functions in the chain take the same type and return the same type. However, I'm stuck on how to make this work when the chain of functions returns a different type each time. This easy case can be seen in the following code:
struct FuncChain<T> {
funcs: Vec<fn(T) -> T>
}
impl<T> FuncChain<T> {
fn call(&self, input: T) -> T {
self.funcs.iter().fold(input, |prev, func| func(prev))
}
}
fn main(){
let fc = FuncChain {
funcs: vec![
|x| x + 1,
|x| x + 2,
|x| x * 2,
|x| x - 2,
]
};
println!("{}", fc.call(1));
}
(Playground)
So in this case we go i32 -> i32 -> i32 -> i32 -> i32.
What I want to do is a more general case where we go A -> B -> C -> D -> E, meaning that the funcs vector contains: fn(A) -> B, fn(B) -> C, fn(C) -> D, and fn(D) -> E. But how can this type definition be assigned to a struct? I can't create a vector with heterogeneous types, and even if I could, what would the type signature of the struct be?
I could make a recursive type definition perhaps, where the FuncChain holds a pointer to the first function object, and also the next object in the chain :
struct FuncChain<S, T, U> {
func: fn(S) -> T,
next: FuncChain<T, U, ?>
}
impl<S, T, U> FuncChain<S, T, U> {
fn call(&self, input: T) -> T {
self.funcs.iter().fold(input, |prev, func| func(prev))
}
}
fn main(){
let fc = FuncChain {
funcs: vec![
|x| x.toString(),
|x| u8::fromStr(x),
|x| x.toString(),
|x| i32::fromStr(x),
]
};
println!("{}", fc.call(1));
}
However of course this won't work, because I can't know the output type of next.
How can this be done?

You question is similar to Iterator, and so can be solved the same solution: a trait indicating a "callable".
The trait lets you "break" the infinite recursion of your current struct-based system, by having the struct just denote it as "whatever that does".
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=f0d6bcc9eb8e070c1d9b6469f6a5e148
struct Chain<U, V, F> {
prev: F,
f: fn(U) -> V,
}
trait FuncChain<T, U> {
fn call(&self, _: T) -> U;
fn chain<V>(self, next: fn(U) -> V) -> Chain<U, V, Self>
where
Self: Sized,
{
Chain {
prev: self,
f: next,
}
}
}
impl<T, U> FuncChain<T, U> for fn(T) -> U {
fn call(&self, t: T) -> U {
self(t)
}
}
impl<T, U, V, F> FuncChain<T, V> for Chain<U, V, F>
where
F: FuncChain<T, U>,
{
fn call(&self, t: T) -> V {
(self.f)(self.prev.call(t))
}
}
fn main() {
let c = ((|x| x + 1) as fn(i32) -> i32)
.chain(|x| x * 2)
.chain(|x| x - 2);
println!("{}", c.call(5));
}
A better Rustacean can probably design a simpler way to achieve this.
If you're fine with using nightly, there's probably a way to use Fn instead of needing a custom trait.
Hell, fundamentally it's just . so you can probably manage with just a generic function and a closure, I'll have to check.

Related

Enum with one of the values being function with &mut argument

I had the following data type that worked for my code
#[derive(PartialEq, Debug, Clone)]
enum Foo {
A,
B,
Fun(fn(X, Y) -> Z)
}
but I noticed that argument Y needs to be mutable, so I changed the signature to
#[derive(PartialEq, Debug, Clone)]
enum Foo {
A,
B,
Fun(fn(X, &mut Y) -> Z)
}
and the hell went loose: it cannot deduce either of the derived traits. I was trying to add lifetimes, implementing the traits by myself, etc, but I had no luck. What should I do for it to work again?
The problem is in how std defines the Debug and PartialEq traits. They use something like:
impl<R, A, B> Debug for fn(A, B) -> R {
//...
}
Which doesn't play nice with HRTBs.
The Clone instance can still be derived without a problem, the rest you'll have to implement yourself:
struct X;
struct Y;
struct Z;
#[derive(Clone)]
enum Foo {
A,
B,
Fun(fn(X, &mut Y) -> Z)
}
impl std::fmt::Debug for Foo {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
use Foo::*;
match self {
A => write!(f, "A"),
B => write!(f, "B"),
// taking inspiration from std's `Debug` impl
Fun(fun) => write!(f, "Fun(0x{:x})", *fun as usize),
}
}
}
impl std::cmp::PartialEq for Foo {
fn eq(&self, other: &Self) -> bool {
use Foo::*;
match (self, other) {
(A, A) => true,
(B, B) => true,
// again taking inspiration from std
(Fun(s), Fun(o)) => *s as usize == *o as usize,
_ => false,
}
}
}
Though you can just declare the lifetime and make Debug and PartialEq derivable as well.
#[derive(PartialEq, Debug, Clone)]
enum Foo<'a> {
A,
B,
Fun(fn(X, &'a mut Y) -> Z)
}
Playground

What does this line of code in Rust mean?

I was going through rust-by-practice and came across a problem. I have no idea what a particular line of code does. This is the exact piece of code for context:
fn example1() {
// `T: Trait` is the commonly used way.
// `T: Fn(u32) -> u32` specifies that we can only pass a closure to `T`.
struct Cacher<T: Fn(u32) -> u32> {
calculation: T,
value: Option<u32>,
}
impl<T: Fn(u32) -> u32> Cacher<T> {
fn new(calculation: T) -> Cacher<T> {
Cacher {
calculation,
value: None,
}
}
fn value(&mut self, arg: u32) -> u32 {
match self.value {
Some(v) => v,
None => {
let v = (self.calculation)(arg); // This exact line of code
self.value = Some(v);
v
},
}
}
}
let mut cacher = Cacher::new(|x| x+1);
assert_eq!(cacher.value(10), __);
assert_eq!(cacher.value(15), __);
}
The calculation field of Cacher is a closure (type T where T: Fn(u32) -> u32), and that line is calling it with arg as an argument, then assigning the resulting u32 to v. Without the parentheses, let v = self.calculation(arg) will look for a method named calculation, which doesn't exist. The parenthesis force a different order of operations which produces a different result.

Reached the type-length limit while instantiating std::vec::IntoIter<i32>

I am trying to implement a Tree structure, but I keep getting an error whenever I try to run the following code:
fn main() {
let tree = Tree::create(1, |_| Vec::new());
println!("{:?}", tree);
}
#[derive(Debug)]
struct Tree<T> {
value: T,
children: Vec<Tree<T>>,
}
impl<T> Tree<T> {
fn create<F>(value: T, get_children: F) -> Tree<T>
where
F: Fn(&T) -> Vec<T>,
{
let children = get_children(&value);
Tree {
value,
children: children
.into_iter()
.map(|x| Tree::create(x, |y| get_children(y)))
.collect(),
}
}
}
The error:
error: reached the type-length limit while instantiating `<std::vec::IntoIter<i32> as std::iter::Iterator>::map::<Tree<i32...`
|
= note: consider adding a `#![type_length_limit="2097152"]` attribute to your crate
You are making a recursive call when creating Tree<T>:
impl<T> Tree<T> {
fn create<F>(value: T, get_children: F) -> Tree<T>
//...
//...
.map(|x| Tree::create(x, |y| get_children(y))) //endless recursive call
I'm confused why it infinitely recursed since I returned an empty
vector in the closure.
This error occurs during compilation and the error says that reached the type-length limit while instantiating.... This means you are generating an enormously long type.
How does it happen?
When you call Tree::create(x, |y| get_children(y)) you are creating an argument closure which calls an existing closure. This is okay, but when you call it recursively the compiler will not able to detect the type of F at the most inner call.
Remember get_children has a type F where F: Fn(&T) -> Vec<T>.
When you call Tree::create for the first time, F in create<F> will be inferred like this:
let tree = Tree::create(1, |_| Vec::new());
//inference of F: Fn(&T) -> Vec<T>
After the second call in map(...) :
Tree::create(x, |y| get_children(y))
//inference of F: Fn(&T) -> Fn(&T) -> Vec<T>
Then it will eventually turn into this:
//inference of F: Fn(&T)-> Fn(&T) -> Fn(&T) -> Vec<T>
//inference of F: Fn(&T)-> ... -> Fn(&T) -> Fn(&T) -> Vec<T>
At the end, the compiler reaches the type-length limit.
Solution with recursion
As an addition to Shepmaster's answer, you can use function pointers:
impl<T> Tree<T> {
fn create(value: T, get_children: fn(&T) -> Vec<T>) -> Tree<T> {
let children = get_children(&value);
Tree {
value,
children: children
.into_iter()
.map(|x| Tree::create(x, get_children))
.collect(),
}
}
}
Solution without recursion
You can fix the issue by sending the function to Vec<Tree<T>> as get_children instead of generating in create, like this:
fn main() {
let inner_tree = Tree::create(1, |_| Vec::new());
let tree = Tree::create(1, move |_| vec![inner_tree]);
println!("{:?}", tree);
}
#[derive(Debug)]
struct Tree<T> {
value: T,
children: Vec<Tree<T>>,
}
impl<T> Tree<T> {
fn create<F>(value: T, get_children: F) -> Tree<T>
where
F: FnOnce(&T) -> Vec<Tree<T>>,
{
let children = get_children(&value);
Tree { value, children }
}
}
Please notice that I changed the function parameter's type from Fn to FnOnce. It is needed to move ownership of inner trees into a closure. It is going to be called once so it can consume the variable.
This is the same underlying problem as What does "Overflow evaluating the requirement" mean and how can I fix it? and can be solved the same way. This means you avoid the type-level recursion by using a reference trait object:
impl<T> Tree<T> {
fn create(value: T, mut get_children: impl FnMut(&T) -> Vec<T>) -> Tree<T> {
fn create_inner<T>(value: T, get_children: &mut FnMut(&T) -> Vec<T>) -> Tree<T> {
let children = get_children(&value)
.into_iter()
.map(|x| create_inner(x, get_children))
.collect();
Tree { value, children }
}
create_inner(value, &mut get_children)
}
}
I've also switched from Fn to FnMut, as its better to be more flexible with closure types when possible.

How do I remove excessive `clone` calls from a struct that caches arbitrary results?

I am reading the section on closures in the second edition of the Rust book. At the end of this section, there is an exercise to extend the Cacher implementation given before. I gave it a try:
use std::clone::Clone;
use std::cmp::Eq;
use std::collections::HashMap;
use std::hash::Hash;
struct Cacher<T, K, V>
where
T: Fn(K) -> V,
K: Eq + Hash + Clone,
V: Clone,
{
calculation: T,
values: HashMap<K, V>,
}
impl<T, K, V> Cacher<T, K, V>
where
T: Fn(K) -> V,
K: Eq + Hash + Clone,
V: Clone,
{
fn new(calculation: T) -> Cacher<T, K, V> {
Cacher {
calculation,
values: HashMap::new(),
}
}
fn value(&mut self, arg: K) -> V {
match self.values.clone().get(&arg) {
Some(v) => v.clone(),
None => {
self.values
.insert(arg.clone(), (self.calculation)(arg.clone()));
self.values.get(&arg).unwrap().clone()
}
}
}
}
After creating a version that finally works, I am really unhappy with it. What really bugs me is that cacher.value(...) has 5(!) calls to clone() in it. Is there a way to avoid this?
Your suspicion is correct, the code contains too many calls to clone(), defeating the very optimizations Cacher is designed to achieve.
Cloning the entire cache
The one to start with is the call to self.values.clone() - it creates a copy of the entire cache on every single access.
After non-lexical lifetimes
Remove this clone.
Before non-lexical lifetimes
As you likely discovered yourself, simply removing .clone() doesn't compile. This is because the borrow checker considers the map referenced for the entire duration of match. The shared reference returned by HashMap::get points to the item inside the map, which means that while it exists, it is forbidden to create another mutable reference to the same map, which is required by HashMap::insert. For the code to compile, you need to split up the match in order to force the shared reference to go out of scope before insert is invoked:
// avoids unnecessary clone of the whole map
fn value(&mut self, arg: K) -> V {
if let Some(v) = self.values.get(&arg).map(V::clone) {
return v;
} else {
let v = (self.calculation)(arg.clone());
self.values.insert(arg, v.clone());
v
}
}
This is much better and probably "good enough" for most practical purposes. The hot path, where the value is already cached, now consists of only a single clone, and that one is actually necessary because the original value must remain in the hash map. (Also, note that cloning doesn't need to be expensive or imply deep copying - the stored value can be an Rc<RealValue>, which buys object sharing for free. In that case, clone() will simply increment the reference count on the object.)
Clone on cache miss
In case of cache miss, the key must be cloned, because calculation is declared to consume it. A single cloning will be sufficient, though, so we can pass the original arg to insert without cloning it again. The key clone still feels unnecessary, though - a calculation function shouldn't require ownership of the key it is transforming. Removing this clone boils down to modifying the signature of the calculation function to take the key by reference. Changing the trait bounds of T to T: Fn(&K) -> V allows the following formulation of value():
// avoids unnecessary clone of the key
fn value(&mut self, arg: K) -> V {
if let Some(v) = self.values.get(&arg).map(V::clone) {
return v;
} else {
let v = (self.calculation)(&arg);
self.values.insert(arg, v.clone());
v
}
}
Avoiding double lookups
Now are left with exactly two calls to clone(), one in each code path. This is optimal, as far as value cloning is concerned, but the careful reader will still be nagged by one detail: in case of cache miss, the hash table lookup will effectively happen twice for the same key: once in the call to HashMap::get, and then once more in HashMap::insert. It would be nice if we could instead reuse the work done the first time and perform only one hash map lookup. This can be achieved by replacing get() and insert() with entry():
// avoids the second lookup on cache miss
fn value(&mut self, arg: K) -> V {
match self.values.entry(arg) {
Entry::Occupied(entry) => entry.into_mut(),
Entry::Vacant(entry) => {
let v = (self.calculation)(entry.key());
entry.insert(v)
}
}.clone()
}
We've also taken the opportunity to move the .clone() call after the match.
Runnable example in the playground.
I was solving the same exercise and ended with the following code:
use std::thread;
use std::time::Duration;
use std::collections::HashMap;
use std::hash::Hash;
use std::fmt::Display;
struct Cacher<P, R, T>
where
T: Fn(&P) -> R,
P: Eq + Hash + Clone,
{
calculation: T,
values: HashMap<P, R>,
}
impl<P, R, T> Cacher<P, R, T>
where
T: Fn(&P) -> R,
P: Eq + Hash + Clone,
{
fn new(calculation: T) -> Cacher<P, R, T> {
Cacher {
calculation,
values: HashMap::new(),
}
}
fn value<'a>(&'a mut self, key: P) -> &'a R {
let calculation = &self.calculation;
let key_copy = key.clone();
self.values
.entry(key_copy)
.or_insert_with(|| (calculation)(&key))
}
}
It only makes a single copy of the key in the value() method. It does not copy the resulting value, but instead returns a reference with a lifetime specifier, which is equal to the lifetime of the enclosing Cacher instance (which is logical, I think, because values in the map will continue to exist until the Cacher itself is dropped).
Here's a test program:
fn main() {
let mut cacher1 = Cacher::new(|num: &u32| -> u32 {
println!("calculating slowly...");
thread::sleep(Duration::from_secs(2));
*num
});
calculate_and_print(10, &mut cacher1);
calculate_and_print(20, &mut cacher1);
calculate_and_print(10, &mut cacher1);
let mut cacher2 = Cacher::new(|str: &&str| -> usize {
println!("calculating slowly...");
thread::sleep(Duration::from_secs(2));
str.len()
});
calculate_and_print("abc", &mut cacher2);
calculate_and_print("defghi", &mut cacher2);
calculate_and_print("abc", &mut cacher2);
}
fn calculate_and_print<P, R, T>(intensity: P, cacher: &mut Cacher<P, R, T>)
where
T: Fn(&P) -> R,
P: Eq + Hash + Clone,
R: Display,
{
println!("{}", cacher.value(intensity));
}
And its output:
calculating slowly...
10
calculating slowly...
20
10
calculating slowly...
3
calculating slowly...
6
3
If you remove the requirement of returning values, you don't need to perform any clones by making use of the Entry:
use std::{
collections::{hash_map::Entry, HashMap},
fmt::Display,
hash::Hash,
thread,
time::Duration,
};
struct Cacher<P, R, T>
where
T: Fn(&P) -> R,
P: Eq + Hash,
{
calculation: T,
values: HashMap<P, R>,
}
impl<P, R, T> Cacher<P, R, T>
where
T: Fn(&P) -> R,
P: Eq + Hash,
{
fn new(calculation: T) -> Cacher<P, R, T> {
Cacher {
calculation,
values: HashMap::new(),
}
}
fn value<'a>(&'a mut self, key: P) -> &'a R {
let calculation = &self.calculation;
match self.values.entry(key) {
Entry::Occupied(e) => e.into_mut(),
Entry::Vacant(e) => {
let result = (calculation)(e.key());
e.insert(result)
}
}
}
}
fn main() {
let mut cacher1 = Cacher::new(|num: &u32| -> u32 {
println!("calculating slowly...");
thread::sleep(Duration::from_secs(1));
*num
});
calculate_and_print(10, &mut cacher1);
calculate_and_print(20, &mut cacher1);
calculate_and_print(10, &mut cacher1);
let mut cacher2 = Cacher::new(|str: &&str| -> usize {
println!("calculating slowly...");
thread::sleep(Duration::from_secs(2));
str.len()
});
calculate_and_print("abc", &mut cacher2);
calculate_and_print("defghi", &mut cacher2);
calculate_and_print("abc", &mut cacher2);
}
fn calculate_and_print<P, R, T>(intensity: P, cacher: &mut Cacher<P, R, T>)
where
T: Fn(&P) -> R,
P: Eq + Hash,
R: Display,
{
println!("{}", cacher.value(intensity));
}
You could then choose to wrap this in another struct that performed a clone:
struct ValueCacher<P, R, T>
where
T: Fn(&P) -> R,
P: Eq + Hash,
R: Clone,
{
cacher: Cacher<P, R, T>,
}
impl<P, R, T> ValueCacher<P, R, T>
where
T: Fn(&P) -> R,
P: Eq + Hash,
R: Clone,
{
fn new(calculation: T) -> Self {
Self {
cacher: Cacher::new(calculation),
}
}
fn value(&mut self, key: P) -> R {
self.cacher.value(key).clone()
}
}

How to implement the Y combinator in Rust? [duplicate]

I've just started Rust tutorial and ended with such code using recursion
extern crate rand;
use std::io;
use rand::Rng;
use std::cmp::Ordering;
use std::str::FromStr;
use std::fmt::{Display, Debug};
fn try_guess<T: Ord>(guess: T, actual: T) -> bool {
match guess.cmp(&actual) {
Ordering::Less => {
println!("Too small");
false
}
Ordering::Greater => {
println!("Too big");
false
}
Ordering::Equal => {
println!("You win!");
true
}
}
}
fn guess_loop<T: Ord + FromStr + Display + Copy>(actual: T)
where <T as FromStr>::Err: Debug
{
println!("PLease input your guess.");
let mut guess = String::new();
io::stdin()
.read_line(&mut guess)
.expect("Failed to read line");
let guess_int: T = guess.trim()
.parse()
.expect("Should enter integer number");
println!("You guessed {} !", guess_int);
if !try_guess(guess_int, actual) {
guess_loop(actual)
}
}
fn main() {
println!("Guess the number!!!");
let secret_number = rand::thread_rng().gen_range(1, 51);
guess_loop(secret_number);
}
I was hoping to factor-out the recursion from the guess_loop function and introduced a fix point operator:
fn guess_loop<T: Ord + FromStr + Display + Copy>(actual: T, recur: fn(T) -> ()) -> ()
where <T as FromStr>::Err: Debug
{
println!("PLease input your guess.");
let mut guess = String::new();
io::stdin()
.read_line(&mut guess)
.expect("Failed to read line");
let guess_int: T = guess.trim()
.parse()
.expect("Should enter integer number");
println!("You guessed {} !", guess_int);
if !try_guess(guess_int, actual) {
recur(actual)
}
}
fn fix<T, R>(func: fn(T, fn(T) -> R) -> R) -> fn(T) -> R {
fn fixed(val: T) -> R {
func(val, fixed)
}
fixed
}
fn main() {
println!("Guess the number!!!");
let secret_number = rand::thread_rng().gen_range(1, 51);
fix(guess_loop)(secret_number);
}
but this led to numerous errors, such as
error[E0401]: can't use type parameters from outer function; try using a local type parameter instead
--> src/main.rs:49:19
|
49 | fn fixed(val: T) -> R {
| ^ use of type variable from outer function
error[E0401]: can't use type parameters from outer function; try using a local type parameter instead
--> src/main.rs:49:25
|
49 | fn fixed(val: T) -> R {
| ^ use of type variable from outer function
error[E0434]: can't capture dynamic environment in a fn item; use the || { ... } closure form instead
--> src/main.rs:50:9
|
50 | func(val, fixed)
| ^^^^
My next attempt was changing guess_loop's definition to
fn guess_loop<T: Ord + FromStr + Display + Copy, F>(actual: T, recur: F) -> ()
where <T as FromStr>::Err: Debug,
F: Fn(T) -> ()
{ ... }
and redefine fix as
fn fix<T, R, F>(func: fn(T, F) -> R) -> F
where F: Fn(T) -> R
{
let fixed = |val: T| func(val, fix(func));
fixed
}
this led to
error[E0308]: mismatched types
--> src/main.rs:53:5
|
53 | fixed
| ^^^^^ expected type parameter, found closure
|
= note: expected type `F`
= note: found type `[closure#src/main.rs:52:17: 52:46 func:_]`
error: the type of this value must be known in this context
--> src/main.rs:61:5
|
61 | fix(guess_loop)(secret_number);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
How can I write a similar fix function?
Firstly, variable names don't exist until after they're initialised. You can't have fixed refer to itself like that.
Secondly, you can't return closures by-value from a function, period. Generic parameters are chosen by the caller, and the caller has no idea what the type of a closure inside the function is going to be.
I'm not claiming that what follows is the best way of doing this, but it was the simplest I was able to come up with that type-checks.
fn guess_loop<T>(actual: T, recur: &Fn(T)) -> ()
where T: Ord + FromStr + Display + Copy,
<T as FromStr>::Err: Debug
{
// ...
}
fn fix<T, R, F>(func: F) -> Box<Fn(T) -> R>
where T: 'static,
R: 'static,
F: Fn(T, &Fn(T) -> R) -> R + 'static
{
use std::cell::RefCell;
use std::rc::Rc;
let fixed = Rc::new(RefCell::new(None));
let fixed_fn = {
let fixed = fixed.clone();
move |val: T| -> R {
let fixed_ref = fixed.borrow();
let fixed_ref: &Box<_> = fixed_ref.as_ref().unwrap();
func(val, &**fixed_ref)
}
};
*fixed.borrow_mut() = Some(Box::new(fixed_fn));
Box::new(move |val: T| -> R {
let fixed_ref = fixed.borrow();
let fixed_ref: &Box<_> = fixed_ref.as_ref().unwrap();
fixed_ref(val)
})
}
In order for fixed_fn to refer to itself, we have to create something for it to read from before it exists. Unfortunately, this means having a cycle, and Rust hates cycles. So, we do this by constructing a reference-counted RefCell<Option<_>> that starts with None, and which will be mutated later to contain the fixed-point closure.
Secondly, we can't use this handle as a callable, so we have to explicitly pull a pointer to the closure out so that we can pass it to func.
Third, the compiler doesn't seem to be able to infer the type of fixed correctly. I was hoping it would be able to work out that it is Rc<RefCell<Option<{closure}>>>, but it refused to do so. As a result, we have to resort to storing a Box<Fn(T) -> R>, since we can't name the type of the closure explicitly.
Finally, we have to construct a new closure that takes a second handle to fixed, unpacks it, and calls it. Again, we can't use fixed as a callable directly. We also can't re-use the closure inside fixed, because to do that we'd have to put that inside its own Rc and at that point, things are starting to get crazy.
... more crazy.
Finally, we have to return this second closure in a Box because, as I said before, we can't return closures by value because we can't name their types in the signature.
*deep breath*
If someone has a simpler solution, I'd love to see it. :P
This is an answer to my own question about implementing the Y combinator which is a subset of this question. In pure lambda expression, a version of the Y combinator looks like
λf.(λw.w w)(λw.f (w w))
The solution in Rosetta Code is too complicated and used Box to allocate memory in the heap. I want to simplify this.
First, let's implement the type Mu<T> as a trait instead.
trait Mu<T> {
fn unroll(&self, &Mu<T>) -> T;
}
Note that we need this trait to be object safe, which means we cannot ask for Self in any of its definition so the second parameter is typed &Mu<T> and it is a trait object.
Now we can write a generic trait implementation:
impl<T, F: Fn(&Mu<T>) -> T> Mu<T> for F {
fn unroll(&self, o: &Mu<T>) -> T {
self(o)
}
}
With this, we can now write the y combinator as the following:
fn y<T, F: Fn(T) -> T>(f: &F) -> T {
(&|w: &Mu<T>| w.unroll(w))(&|w: &Mu<T>| f(w.unroll(w)))
}
The above compiles in the Rust playground without enabling any features and using only the stable channel so this is a pretty good answer to my question.
However, the above would not work in practice because Rust is call-by-value but the code above is the call-by-name Y combinator.
The call-by-value solution
To work with the stable channel without requiring any features, we cannot return closures (which requires impl Trait). Instead, I came up with making another Mu2 type that takes two type parameters:
trait Mu2<T, R> {
fn unroll(&self, &Mu2<T, R>, t: T) -> R;
}
As above, let's implement this new trait.
impl<T, R, F> Mu2<T, R> for F
where
F: Fn(&Mu2<T, R>, T) -> R,
{
fn unroll(&self, o: &Mu2<T, R>, t: T) -> R {
self(o, t)
}
}
The new Y combinator:
fn y<T, R, F>(f: &F, t: T) -> R
where
F: Fn(&Fn(T) -> R, T) -> R,
{
(&|w: &Mu2<T, R>, t| w.unroll(w, t))((&|w: &Mu2<T, R>, t| f(&|t| w.unroll(w, t), t)), t)
}
Now it is time to test our new facility.
fn main() {
let fac = &|f: &Fn(i32) -> i32, i| if i > 0 { i * f(i - 1) } else { 1 };
println!("{}", y(fac, 10))
}
Results in:
3628800
All done!
You can see that the y function has a slightly different signature than the questioner's fix, but it shouldn't matter.
The direct recurring version
The same technology to avoid returning a closure can be used for the normal direct recurring version as well:
fn fix<T, R, F>(f: &F, t: T) -> R
where
F: Fn(&Fn(T) -> R, T) -> R,
{
f(&|t| fix(f, t), t)
}
fn fib(i: i32) -> i32 {
let fn_ = &|f:&Fn(i32) -> i32, x| if x < 2 { x } else { f(x-1) + f(x-2) };
fix(fn_, i)
}
Basically, whenever you need to return a closure from a function, you can add the closure's parameter to the function, and change the return type to the closure's return type. Later on when you need a real closure, just create the closure by partial evaluating that function.
Further discussions
Compare to other languages, in Rust there is a big difference: the function given to find fix point must not have any internal states. In Rust this is a requirement that the F type parameter of y must be Fn, not FnMut or FnOnce.
For example, we cannot implement a fix_mut that would be used like
fn fib1(i: u32) -> u32 {
let mut i0 = 1;
let mut i1 = 1;
let fn_ = &mut |f:&Fn(u32) -> u32, x|
match x {
0 => i0,
1 => i1,
_ => {
let i2 = i0;
i0 = i1;
i1 = i1 + i2;
f(x)
}
};
fix_mut(fn_, i)
}
without unsafe code whilst this version, if it works, performs much better (O(N)) than the version given above (O(2^N)).
This is because you can only have one &mut of one object at a single time. But the idea of Y combinator, or even the fix point function, requires capturing/passing the function at the same time when calling it, that's two references and you can't just mark any of them immutable without marking another so.
On the other hand, I was wonder if we could do something that other languages usually not able to but Rust seems to be able. I was thinking restricting the first argument type of F from Fn to FnOnce (as y function will provide the implementation, change to FnMut does not make sense, we know it will not have states, but change to FnOnce means we want it to be used only once), Rust would not allow at the moment as we cannot pass unsized object by value.
So basically, this implementation is the most flexible solution we could think of.
By the way, the work around of the immutable restriction is to use pseudo-mutation:
fn fib(i: u32) -> u32 {
let fn_ = &|f:&Fn((u32,u32,u32)) -> u32, (x,i,j)|
match x {
0 => i,
1 => j,
_ => {
f((x-1,j,i+j))
}
};
fix(&fn_, (i,1,1))
}
Starting at where you left off:
fn fix<T, R, F>(func: fn(T, F) -> R) -> F
where F: Fn(T) -> R
{
|val: T| func(val, fix(func))
}
The returned object has an unnameable closure type. Using a generic type won’t help here, since the type of the closure is decided by the callee, not the caller. Here’s where impl traits come in handy:
fn fix<T, R, F>(func: fn(T, F) -> R) -> impl Fn(T) -> R
where F: Fn(T) -> R
{
|val: T| func(val, fix(func))
}
We can’t pass fix(func) to func because it expects a nameable type for F. We’ll have to settle for a trait object instead:
fn fix<T, R>(func: fn(T, &Fn(T) -> R) -> R) -> impl Fn(T) -> R {
|val: T| func(val, &fix(func))
}
Now it’s time to fight the lifetime checker. The compiler complains:
only named lifetimes are allowed in `impl Trait`, but `` was found in the type `…`
This is a somewhat cryptic message. Since impl traits are always 'static by default, this is a roundabout way of saying: “the closure does not live long enough for 'static”. To get the real error message, we append + 'static to the impl Fn(T) -> R and recompile:
closure may outlive the current function, but it borrows `func`, which is owned by the current function
So that was the real problem. It is borrowing func. We don’t need to borrow func because fn is Copy, so we can duplicate it as much as we want. Let’s prepend the closure with move and get rid of the + 'static from earlier:
fn fix<T, R>(func: fn(T, &Fn(T) -> R) -> R) -> impl Fn(T) -> R {
move |val: T| func(val, &fix(func))
}
And voila, it works! Well, almost … you’ll have to edit guess_loop and change fn(T) -> () to &Fn(T) -> (). I’m actually quite amazed that this solution doesn’t require any allocations.
If you can’t use impl traits, you can instead write:
fn fix<T, R>(func: fn(T, &Fn(T) -> R) -> R) -> Box<Fn(T) -> R>
where T: 'static,
R: 'static
{
Box::new(move |val: T| func(val, fix(func).as_ref()))
}
which is unfortunately not allocation-free.
Also, we can generalize the result a bit to allow arbitrary closures and lifetimes:
fn fix<'a, T, R, F>(func: F) -> impl 'a + Fn(T) -> R
where F: 'a + Fn(T, &Fn(T) -> R) -> R + Copy
{
move |val: T| func(val, &fix(func))
}
In the process of figuring out a solution for your problem, I ended up writing a simpler version of fix, which actually ended up guide me towards a solution to your fix function:
type Lazy<'a, T> = Box<FnBox() -> T + 'a>;
// fix: (Lazy<T> -> T) -> T
fn fix<'a, T, F>(f: F) -> T
where F: Fn(Lazy<'a, T>) -> T + Copy + 'a
{
f(Box::new(move || fix(f)))
}
Here’s a demonstration of how this fix function could be used to calculate the factorial:
fn factorial(n: u64) -> u64 {
// f: Lazy<u64 -> u64> -> u64 -> u64
fn f(fac: Lazy<'static, Box<FnBox(u64) -> u64>>) -> Box<FnBox(u64) -> u64> {
Box::new(move |n| {
if n == 0 {
1
} else {
n * fac()(n - 1)
}
})
}
fix(f)(n)
}
This can be done at zero runtime cost if you're willing to use unstable features (i.e. a nightly compiler) and willing to... obfuscate your code slightly.
First, we need to turn the result of fix into a named struct. This struct needs to implement Fn, so we'll implement it manually (this is an unstable feature).
#![feature(fn_traits)]
#![feature(unboxed_closures)]
extern crate rand;
use rand::Rng;
use std::cmp::Ordering;
fn try_guess<T: Ord>(guess: T, actual: T) -> bool {
match guess.cmp(&actual) {
Ordering::Less => {
println!("Too small");
false
}
Ordering::Greater => {
println!("Too big");
false
}
Ordering::Equal => {
println!("You win!");
true
}
}
}
struct Fix<F>
where F: Fn(i32, &Fix<F>)
{
func: F,
}
impl<F> FnOnce<(i32,)> for Fix<F>
where F: Fn(i32, &Fix<F>)
{
type Output = ();
extern "rust-call" fn call_once(self, args: (i32,)) -> Self::Output {
self.call(args)
}
}
impl<F> FnMut<(i32,)> for Fix<F>
where F: Fn(i32, &Fix<F>)
{
extern "rust-call" fn call_mut(&mut self, args: (i32,)) -> Self::Output {
self.call(args)
}
}
impl<F> Fn<(i32,)> for Fix<F>
where F: Fn(i32, &Fix<F>)
{
extern "rust-call" fn call(&self, (val,): (i32,)) -> Self::Output {
(self.func)(val, self);
}
}
fn fix<F>(func: F) -> Fix<F>
where F: Fn(i32, &Fix<F>)
{
Fix { func: func }
}
fn guess_loop<F>(actual: i32, recur: &F)
where F: Fn(i32)
{
let guess_int = rand::thread_rng().gen_range(1, 51);
if guess_int != actual {
recur(actual)
}
}
fn main() {
let secret_number = rand::thread_rng().gen_range(1, 51);
fix(guess_loop)(secret_number);
}
However, we're not done yet. This fails to compile with the following error:
error[E0281]: type mismatch: the type `fn(i32, &_) {guess_loop::<_>}` implements the trait `for<'r> std::ops::Fn<(i32, &'r _)>`, but the trait `for<'r> std::ops::Fn<(i32, &'r Fix<fn(i32, &_) {guess_loop::<_>}>)>` is required (cyclic type of infinite size)
--> src/main.rs:77:5
|
77 | fix(guess_loop)(secret_number);
| ^^^
|
= note: required by `fix`
Note: In case you're not aware, in Rust, each function has its own, zero-sized type. If a function is generic, then each instantiation of that function will have its own type as well. For example, the type of guess_loop::<X> will be reported by the compiler as fn(i32, &X) {guess_loop::<X>} (as you can see in the error message above, except with underscores where the concrete type hasn't been resolved yet). That type can be coerced to a function pointer type implicitly in some contexts or explicitly with a cast (as).
The problem is that, in the expression fix(guess_loop), the compiler needs to instantiate guess_loop, which is a generic function, and it looks like the compiler isn't able to figure out the proper type to instantiate it with. In fact, the type we would like to set for type parameter F references the type of guess_loop. If we were to write it out in the style reported by the compiler, the type would look like fn(i32, &Fix<X>) {guess_loop::<Fix<&X>>}, where X is replaced by the type itself (you can see now where the "cyclic type of infinite size" comes from).
We can solve this by replacing the guess_loop function by a non-generic struct (we'll call it GuessLoop) that implements Fn by referring to itself. (You can't do this with a normal function because you can't name a function's type.)
struct GuessLoop;
impl<'a> FnOnce<(i32, &'a Fix<GuessLoop>)> for GuessLoop {
type Output = ();
extern "rust-call" fn call_once(self, args: (i32, &Fix<GuessLoop>)) -> Self::Output {
self.call(args)
}
}
impl<'a> FnMut<(i32, &'a Fix<GuessLoop>)> for GuessLoop {
extern "rust-call" fn call_mut(&mut self, args: (i32, &Fix<GuessLoop>)) -> Self::Output {
self.call(args)
}
}
impl<'a> Fn<(i32, &'a Fix<GuessLoop>)> for GuessLoop {
extern "rust-call" fn call(&self, (actual, recur): (i32, &Fix<GuessLoop>)) -> Self::Output {
let guess_int = rand::thread_rng().gen_range(1, 51);
if !try_guess(guess_int, actual) {
recur(actual)
}
}
}
fn main() {
let secret_number = rand::thread_rng().gen_range(1, 51);
fix(GuessLoop)(secret_number);
}
Notice that GuessLoop's implementation of Fn is no longer generic on the type of the recur parameter. What if we tried to make the implementation of Fn generic (while still leaving the struct itself non-generic, to avoid cyclic types)?
struct GuessLoop;
impl<'a, F> FnOnce<(i32, &'a F)> for GuessLoop
where F: Fn(i32),
{
type Output = ();
extern "rust-call" fn call_once(self, args: (i32, &'a F)) -> Self::Output {
self.call(args)
}
}
impl<'a, F> FnMut<(i32, &'a F)> for GuessLoop
where F: Fn(i32),
{
extern "rust-call" fn call_mut(&mut self, args: (i32, &'a F)) -> Self::Output {
self.call(args)
}
}
impl<'a, F> Fn<(i32, &'a F)> for GuessLoop
where F: Fn(i32),
{
extern "rust-call" fn call(&self, (actual, recur): (i32, &'a F)) -> Self::Output {
let guess_int = rand::thread_rng().gen_range(1, 51);
if !try_guess(guess_int, actual) {
recur(actual)
}
}
}
Unfortunately, this fails to compile with the following error:
error[E0275]: overflow evaluating the requirement `<Fix<GuessLoop> as std::ops::FnOnce<(i32,)>>::Output == ()`
--> src/main.rs:99:5
|
99 | fix(GuessLoop)(secret_number);
| ^^^
|
= note: required because of the requirements on the impl of `for<'r> std::ops::Fn<(i32, &'r Fix<GuessLoop>)>` for `GuessLoop`
= note: required by `fix`
Essentially, the compiler is unable to verify that Fix<GuessLoop> implements Fn(i32), because in order to do that, it needs to verify that GuessLoop implements Fn(i32, &Fix<GuessLoop>), but that is only true if Fix<GuessLoop> implements Fn(i32) (because that impl is conditional), which is only true if GuessLoop implements Fn(i32, &Fix<GuessLoop>) (because that impl is conditional too), which... you get the idea. In order words, the two implementations of Fn here are dependent on each other, and the compiler is unable to resolve that.

Resources