dartz icon indicating copy to clipboard operation
dartz copied to clipboard

Type defunctionalization to simulate higher kinded types

Open modulovalue opened this issue 6 years ago • 3 comments

Hey @spebbe,

Have you heard of type defunctionalization?

type defunctionalization

1.3 Type defunctionalization Defunctionalization transforms a program with higher-order values into a program where all values are first-order. Similarly, we can change a program with higher-kinded type expressions into a program where all type expressions are of kind ∗, the kind of types.

Various implementations that use type defunctionalization:

TypeScript: fp-ts Swift: bow and here's a simple explanation of how it works Elm: brands Java: HKT & highj & a Medium article and (not sure if they are using the same technique) Rust: Gist writeup.md & this post

Edit:

That wouldn't be possible with dartz as it stands now without having to do a bunch of casting, right? or am I missing something?

void main() {
  final Either<Err, String> _right = toStrAppend("?", right<Err, int>(123)).D;
  final Option<String> _some = toStrAppend("?", some(456)).D;

  print(_right.fold((err) => "$err", (a) => "value: $a"));
  print(_some.fold(() => "empty", (a) => "value: $a"));
}

Kind<F, String> toStrAppend<F, A>(String str, Functor<F, A> f) {
  return f.D.map((a) => a.toString() + "$str");
}

enum Err {
  someErr,
}

Output:

value: 123?
value: 456?

modulovalue avatar Nov 30 '19 01:11 modulovalue

Hi @modulovalue!

Thanks for the reference material! I've seen Bow before and I've also looked at how they encode HKTs in Arrow (Kotlin), which I think is essentially the same pattern. Since it was possible to cheat your way out of the whole problem in Dart 1, I never implemented it then, but maybe it would be a way to get back some of the abstraction possibilities that have been removed for 0.9? I'll experiment a bit with applying this to MonadLens from the other day and see how it turns out! Might be a little while though -- December is brutal... Thanks again!

spebbe avatar Dec 01 '19 16:12 spebbe

I have some naive implementation of HKT in https://github.com/vysarafanov/javelin (look at the Laws in tests) You're welcome to comment. Anyway - it's absolutely annoying without typealias

vysarafanov avatar May 12 '20 11:05 vysarafanov

@vysarafanov thank you – this will help a lot in comparing approaches! love your project, btw! much tighter structure than i currently have and a great inspiration. i'll look into it more closely!

Also, with a bit of luck, more useful type aliases could be coming our way soon! see https://github.com/dart-lang/sdk/blob/master/tools/experimental_features.yaml section nonfunction-type-aliases. Also, declaration-site variance control (variance) is in there – fingers crossed!

spebbe avatar May 12 '20 12:05 spebbe

For anyone looking into using this approach to implement more abstract constructs in Dart, I've shared an implementation of a catamorphism here. It's a little tricky to get there from scratch, but it's a lot easier once there's some example code.

modulovalue avatar Mar 18 '23 02:03 modulovalue

I've just noticed that this issue is still open.

Since @spebbe appears to have moved on (i.e., dartz appears to no longer be actively maintained), and there are other libraries that implement this issue (e.g. fpdart by @SandroMaglione) I'm going to close this issue.

Thank you @spebbe for getting the ball rolling and introducing a bunch of dart developers to functional programming!

modulovalue avatar Jan 09 '24 11:01 modulovalue