Skip to content

Instantly share code, notes, and snippets.

@knz
Created February 23, 2015 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save knz/45a2987d1369dbdb0cc8 to your computer and use it in GitHub Desktop.
Save knz/45a2987d1369dbdb0cc8 to your computer and use it in GitHub Desktop.
use std::marker::PhantomFn;
trait Tr<T> : PhantomFn<(Self,T)> { type O; }
trait Hello : PhantomFn<Self> {
fn pr<A>(x : A) -> <Self as Tr<A>>::O where Self : Tr<A>;
fn ap<'a, A, B, F : Fn(A)->B> (f : <Self as Tr<&'a F>>::O, x : <Self as Tr<A>>::O) -> <Self as Tr<B>>::O
where Self : Tr<A> + Tr<B> + Tr<&'a F>;
fn foo<A, B>(x : <Self as Tr<A>>::O, y : <Self as Tr<B>>::O) -> <Self as Tr<B>>::O
where Self : Tr<A> + Tr<B>
{
let id : &Fn(B)->B = &|x|{x}; // conceptually: id :: Fn(B) -> B
let di = &|discard:A|{id}; // conceptually: di :: Fn(A) -> Fn(B) -> B
let fdi = <Self as Hello>::pr(di); // conceptually: fdi :: Tr<Fn(A)->Fn(B)->B>::O
let fa = <Self as Hello>::ap(fdi, x); // conceptually: fa :: Tr<Fn(B)->B>::O
let res = <Self as Hello>::ap(fa, y); // conceptually: res :: Tr<B>::O
}
}
// ERRORS:
smallf.rs:17:12: 17:32 error: the trait `Tr<&[closure smallf.rs:16:19: 16:34]>` is not implemented for the type `Self` [E0277]
smallf.rs:17 let fdi = <Self as Hello>::pr(di); // conceptually: fdi :: Tr<Fn(A)->Fn(B)->B>::O
^~~~~~~~~~~~~~~~~~~~
smallf.rs:17:12: 17:32 error: the trait `Tr<&[closure smallf.rs:16:19: 16:34]>` is not implemented for the type `Self` [E0277]
smallf.rs:17 let fdi = <Self as Hello>::pr(di); // conceptually: fdi :: Tr<Fn(A)->Fn(B)->B>::O
^~~~~~~~~~~~~~~~~~~~
smallf.rs:19:11: 19:31 error: the trait `Tr<&_>` is not implemented for the type `Self` [E0277]
smallf.rs:19 let fa = <Self as Hello>::ap(fdi, x); // conceptually: fa :: Tr<Fn(B)->B>::O
^~~~~~~~~~~~~~~~~~~~
smallf.rs:20:12: 20:32 error: the trait `Tr<&_>` is not implemented for the type `Self` [E0277]
smallf.rs:20 let res = <Self as Hello>::ap(fa, y); // conceptually: res :: Tr<B>::O
^~~~~~~~~~~~~~~~~~~~
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment