Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alternative approach to higher-kinded types #547

Closed
pelotom opened this issue Aug 24, 2018 · 5 comments
Closed

Alternative approach to higher-kinded types #547

pelotom opened this issue Aug 24, 2018 · 5 comments

Comments

@pelotom
Copy link

pelotom commented Aug 24, 2018

Hi @gcanti, I"ve been playing with a simple encoding of HKTs using conditional types to substitute virtual type variables within a saturated type. Reference project here:

https://github.com/pelotom/hkts

It"s attractive to me in that it doesn"t require defining your type constructors in any special way; you can take any given type Foo<A> = ... for example and then abstract over Foo. Would this be a useful approach for fp-ts? Would love to hear your thoughts!

@gcanti
Copy link
Owner

gcanti commented Aug 25, 2018

Hi @pelotom, thanks for sharing.

How do you define a Monad instance for Either?

@pelotom
Copy link
Author

pelotom commented Aug 26, 2018

How do you define a Monad instance for Either?

Good question. The only thing I can think to do is have a parameterized instance creator

const RightMonad = <L>() => Monad<Either<Fixed<L>, _>>({
  pure: right,
  bind: (ma, f) => (ma.tag === "left" ? ma : f(ma.right)),
});

const either: Either<string, number> = right(42);
const result = RightMonad<string>().map(either, n => n + 1);
expect(result).toEqual(right(43));

The new Fixed operator is needed to instruct $ to leave bound type parameters unmolested. What do you think, is there a better approach?

@masaeedu
Copy link

It"s a shame that you can"t just say:

type EitherMonad = <L> Monad<Either<Fixed<L>, _>>

The ability to introduce foralls is restricted to functions.

@pelotom
Copy link
Author

pelotom commented Sep 1, 2018

Actually it looks like the Fixed operator is no longer needed here, and we can just write

const RightMonad = <L>() => Monad<Either<L, _>>({
  pure: right,
  bind: (ma, f) => (ma.tag === "left" ? ma : f(ma.right)),
});

Not really sure why this is 😅

@pelotom
Copy link
Author

pelotom commented Sep 16, 2018

@masaeedu

It"s a shame that you can"t just say:

type EitherMonad = <L> Monad<Either<Fixed<L>, _>>

The ability to introduce foralls is restricted to functions.

Yes, it is... I hope your issue microsoft/TypeScript#17574 gets some attention!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants