Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![feature(specialization)]
- use std::ops::{Add, Sub};
- #[derive(Clone, Debug)]
- struct One;
- #[derive(Clone, Debug)]
- struct Int(i32);
- #[derive(Clone, Debug)]
- struct Addition<T: Term, U: Term>(T, U);
- #[derive(Clone, Debug)]
- struct Substraction<T: Term, U: Term>(T, U);
- impl Add<One> for Int {
- type Output = Addition<Int, One>;
- fn add(self, one: One) -> Self::Output { Addition(self, one) }
- }
- impl Sub<One> for Int {
- type Output = Substraction<Int, One>;
- fn sub(self, one: One) -> Self::Output { Substraction(self, one) }
- }
- impl<T: Term, U: Term> Sub<One> for Addition<T, U> {
- type Output = Substraction<Addition<T, U>, One>;
- fn sub(self, one: One) -> Self::Output { Substraction(self, one) }
- }
- trait Term {
- type Reduced: Term;
- fn reduce(self) -> Self::Reduced;
- }
- impl Term for One {
- type Reduced = Self;
- fn reduce(self) -> Self { self }
- }
- impl Term for Int {
- type Reduced = Self;
- fn reduce(self) -> Self { self }
- }
- impl<T: Term, U: Term> Term for Addition<T, U> {
- type Reduced = Self;
- fn reduce(self) -> Self { self }
- }
- impl<T: Term, U: Term> Term for Substraction<T, U> {
- default type Reduced = Self;
- default fn reduce(self) -> Self::Reduced { unimplemented!() }
- }
- impl<T: Term> Term for Substraction<Addition<T, One>, One> {
- type Reduced = T;
- fn reduce(self) -> Self::Reduced { (self.0).0 }
- }
- fn main() {
- let x = Int(1) + One - One;
- println!("{:?}", x);
- let x = x.reduce();
- println!("{:?}", x);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement