Skip to content
This repository has been archived by the owner on May 23, 2024. It is now read-only.

ICE - rust-lang/rust#79768 #557

Merged
merged 1 commit into from
Dec 7, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions ices/79768.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#![feature(generic_associated_types)]

trait Monad /* : Applicative (for pure/return, doesn't matter for this example) */ {
// Self is like the "f a" in haskell

/// extract the "a" from "f a"
type Unplug;

/// exchange the "a" in "f a" in the type of Self with B
type Plug<B>: Monad;

fn bind<B, F>(self, f: F) -> Self::Plug<B>
where
F: Fn(Self::Unplug) -> Self::Plug<B>;
}

impl<A> Monad for Option<A> {
type Unplug = A;
type Plug<B> = Option<B>;
fn bind<B, F>(self, f: F) -> Option<B>
where
F: Fn(A) -> Option<B>,
{
self.and_then(f)
}
}

// This function causes the compiler error, specifically, when i added the Plug = P constraint.
fn stringify<T, M1, P>(m: M1) -> M1::Plug<P>
where
T: core::fmt::Display,
M1: Monad<Unplug = T, Plug = P>,
{
m.bind(|x| format!("{}", x))
}

fn main() {
let x = Some(1).bind(|x| Some(x * 2));
println!("{:?}", x);
}