Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Rust HKT playground powered with derive macro generated type families

NotificationsYou must be signed in to change notification settings

leslie255/families

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rust HKT implementations for playing around, with derive macro for automatically declaring/implementing type families.

What is HKT?

SeeGeneralizing over Generics in Rust (Part 1) - AKA Higher Kinded Types in Rust for an explanation of HKT in Rust syntax

Although in the honest opinion of the author, dipping your toes into Haskell for a bit really helps understanding these concepts, as the traditional generic syntax of Rust (and many other langauges) often hides important patterns regarding understanding of FP concepts.

Overview

The repo provides aFamily trait for generalizing over type functions (i.e. a generic type with one parameter).

A derive macro#[derive(Family)] exists for automatically deriving implementation of a type families for a generic ADT (i.e. struct, enum, union).

The repo also provides type family implementation of some common ADT's in the Rust STD.

An typical type family implementation for a generic type looks like this:

structOneParam<T>(T);structOneParamFamily;implFamilyforOneParamFamily{typeThis<T> =OneParam<T>;}

The above is (almost) the exact same code generated by:

#[derive(Family)]structOneParam<T>(T);

A type function could then be invoked by:

<OneParamFamilyasFamily>::This<T>

Although under most context the shorthand form produces the same result:

OneParamFamily::This<T>

A multi-parameter generic ADT can also be implemented as type families via curried type functions:

#[derive(Family)]structTwoParam<T,U>(T,U);

This generates:

structTwoParam<T,U>(T,U);structTwoParamFamily<T>(PhantomData<T>);implFamilyforTwoParamFamily{typeThis<U> =TwoParam<T,U>,}structTwoParamFamilyFamily;implFamilyforTwoParamFamilyFamily{typeThis<T> =TwoParamFamily<T>,}

Unresolved questions

  • Fitting lifetimes and const parameters into the system.
  • Generic constraints in type families (large sections of the code intypefamilies-derive/src/lib.rs are commented out due to a failed attempt at implementing generic constraints).
  • Somehow, relating monads withTry trait?

TODO

  • Allow speicifying order of generic parameters in#[derive(Family)], this is useful for cases likeResult, where havingT (the success type) afterE in its family types is preferable for implementing Monads.

About

Rust HKT playground powered with derive macro generated type families

Topics

Resources

Stars

Watchers

Forks

Languages


[8]ページ先頭

©2009-2025 Movatter.jp