A paper I hear discussed a lot in FP spaces is Recovering Purity with Comonads and Capabilities. I've read the first two sections, but things start to go over my head after that. My question is: are there any examples (even toy ones) of using comonads to recover purity in impure languages that I can run, and play around with?
Not runnable, but you could implement such system e.g. in Rust:
// opaque "newtype"structPure<T>(T);// We could create some macro which enforces use of pure building// blocks in expressions/declarations/whatever - we could make it so// that it implicitly unwraps "Pure" inside and wraps returned values// again.macropure{$x}impl<T>Pure<T>{// We provide way to "unbox" that pure value using methodfnextract(self)-> T{self.0}}pure!{fnadd(a: i32,b: i32)-> i32{a+b}}// gets turned into e.g.:// // const functions must be executable at compile-time (and pure)// const fn _add(a: i32, b: i32) -> i32 {// a + b// }// val add: Pure<fn(i32, i32) -> i32> = Pure(_add);fnmain(){println!("Sum is: {}",pure!{add(1,2)}.extract());}
A paper I hear discussed a lot in FP spaces is Recovering Purity with Comonads and Capabilities. I've read the first two sections, but things start to go over my head after that. My question is: are there any examples (even toy ones) of using comonads to recover purity in impure languages that I can run, and play around with?
Not runnable, but you could implement such system e.g. in Rust:
Or language developers could do something as simple as adding
const
marker to types that allows you to require pure arguments