Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upHigher Kinded Polymorphism / Generics on Generics #2212
Comments
VSadov
added
Feature Request
Area-Language Design
labels
Apr 23, 2015
VSadov
assigned
MadsTorgersen
Apr 23, 2015
This comment has been minimized.
This comment has been minimized.
|
There is an obvious issue with CLR not supporting higher-kinded types. @MadsTorgersen if not already there, higher kinded generics may need to be added to the list of possible future directions. Perhaps next to the Meta-programming :-). |
This comment has been minimized.
This comment has been minimized.
diab0l
commented
Apr 24, 2015
|
From what I've seen on UserVoice and SO, there's encodings to do this stuff in F# using their inlining system. But these encodings tend to be ugly, complex and compromising. It's also worth noting that the feature request has been rejected for F#, since it would require CLR support, it's costs outweigh it's benefits, etc., however that was before Core was open sourced and all this crazy open development started |
gafter
referenced this issue
Apr 24, 2015
Closed
What language proposals would benefit from CLR changes? #420
This comment has been minimized.
This comment has been minimized.
GSPP
commented
Apr 29, 2015
|
@diab0l can you give more examples for what this would be good for? I always wondered what practical things you can do with higher kinded types. For C# 6 they are not going to touch the CLR but there are other potential changes queued up (e.g. structural types and default implementations for interface methods). Maybe C# 7 can batch all of them into a new incompatible runtime. |
This comment has been minimized.
This comment has been minimized.
isaacabraham
commented
May 2, 2015
|
@diab0l I don't think it's been rejected from F# because of lack of CLR support - I believe it could be done in a type erased style - it's simply not high up enough the feature list on uservoice. I've also been hearing people in the F# community asking for GADTs first. |
This comment has been minimized.
This comment has been minimized.
diab0l
commented
May 3, 2015
|
@isaacabraham Of course it could be implemented type-erased style, but that's an unpleasant can of worms. If it were implemented like C++ templates, it would be a source level feature, meaning you can't have higher kinded polymorphic methods in assemblies. I think we can reach consensus that such a feature would only pollute the language. If it were implemented like Java generics, it would lead to all sorts of nonsense. For example you couldn't do So to implement this feature in a reusable way, there would at least need to be a way for the C# compiler to
Now since it has to happen at assembly level, that would essentially be a language-agnostic extension to the CLR's common type system. If we are going to extend the CLR's common type system with some cool feature anyway, then I would suggest we do it right: by adding it as a first-level feature to the standard and implementing it right instead of via (clever or not) ugly hacks. |
This comment has been minimized.
This comment has been minimized.
diab0l
commented
May 3, 2015
|
@GSPP To be frank: I can't give you a whole lot of examples. However, what I can tell you is that, as an abstraction feature which abstracts over abstract data structures it would primarily allow us to write some neat libraries. The best example I can currently come up with is abstracting over the Linq Zoo. The only difference in the shared methods of these APIs ( We cannot currently abstract over a) or b) :( Currently, as a library author who intends to write a method which works on either of these APIs, you would have to write your function each time for each API (without casting to With the introduction of HKPMs, and a retrofitted static interface |
This comment has been minimized.
This comment has been minimized.
diab0l
commented
May 3, 2015
|
Please do not understand me wrong. Also, it's not clear whether higher kinded polymorphism would be a good fit for C#. What I think is clear is that C# and the CLR have incorporated features from functional languages with great success and the trend for using C# as a functional language is strong. Having all this in mind, I think it's worthwhile to explore how C# would benefit from first-level support for Higher Kinded Polymorphism and what such a syntax would be like. Also I think that, along with related issues it raises the question: Should the common type system as a one-size-fits-all solution be all that common? |
This comment has been minimized.
This comment has been minimized.
dzmitry-lahoda
commented
May 4, 2015
|
There is the example for HKT in C# http://www.sparxeng.com/blog/software/an-example-of-what-higher-kinded-types-could-make-possible-in-c. |
This comment has been minimized.
This comment has been minimized.
MI3Guy
commented
May 11, 2015
|
Another advantage would be the ability to use value type collections (e.g. ImmutableArray) without boxing them or writing a method that uses that specific collection. |
This comment has been minimized.
This comment has been minimized.
DrPizza
commented
May 15, 2015
•
Template template parameters are a good fit for C++, so it's natural enough that generic generic parameters would be a good fit for C#. Being able to make Linq methods independent of concrete classes seems nice enough. |
This comment has been minimized.
This comment has been minimized.
|
@TonyValenti Please suggest that in a new issue. |
TonyValenti
referenced this issue
Sep 4, 2015
Closed
REQUEST: Pattern matching/better type inferencing with generics. #5023
This comment has been minimized.
This comment has been minimized.
aluanhaddad
commented
Sep 17, 2015
|
I would love to see this addition if some future version of the framework enables it. |
gafter
unassigned
MadsTorgersen
Nov 20, 2015
gafter
added
the
1 - Planning
label
Nov 20, 2015
weswigham
referenced this issue
Dec 4, 2015
Closed
Need something more robust than polymorphic `this` typing for RxJS #5845
veikkoeeva
referenced this issue
Dec 7, 2015
Closed
IStorageProvider methods parameter grainType from string to Type #1075
This comment has been minimized.
This comment has been minimized.
oscarvarto
commented
Jun 20, 2016
|
+1 I am also craving for some bacon! |
This comment has been minimized.
This comment has been minimized.
mooman219
commented
Jun 23, 2016
|
+1 Ran into an issue where I needed this today |
This comment has been minimized.
This comment has been minimized.
aluanhaddad
commented
Jun 23, 2016
|
It's too bad this is one of those things that definitely is going to require CLR support. But then again maybe it's a good opportunity for the CLR to evolve. |
OzieGamma
referenced this issue
Jul 29, 2016
Open
[Feature request] Higher Kinded Polymorphism #6524
This comment has been minimized.
This comment has been minimized.
Pauan
commented
Sep 23, 2016
•
|
Please excuse the F#, but here is an example of where higher-kinded polymorphism would help out a lot. There is a function called
Its behavior is fairly simple. It allows you to take a "container" (like a list, array, dictionary, etc.) and transform every element inside of the container:
The end result of the above code is As you can see, Because F# has interfaces (just like C#), you might try this:
And then you could implement the
You can then use the
The But with interfaces, we can write it once and reuse it for many types! And of course static typing is fully preserved: you get a compile-time error if you make any mistakes, such as calling The Unfortunately, this does not work, because .NET lacks higher-kinded polymorphism:
In other words, you cannot specify the type This also applies to other functions as well, like Quite a lot of the Unfortunately I do not know C# well enough to give any examples of where higher-ordered polymorphism would be useful in C#, but I hope that So, in short: higher-kinded polymorphism is simply a more powerful form of generics. It is useful for precisely the same reason why generics and interfaces are useful: it allows us to write code which can be reused, rather than reimplemented over and over again. P.S. If somebody with more C# experience than me could translate the above F# code into equivalent C# code, that may help others with understanding what higher-kinded polymorphism is, how to use it, and what it's good for. |
This comment has been minimized.
This comment has been minimized.
|
This repo demonstrates a quite interesting approach to typeclasses in c#: https://github.com/CaptainHayashi/roslyn |
This comment has been minimized.
This comment has been minimized.
Alxandr
commented
Sep 27, 2016
|
Actually, both C# and F# has higher kinded polymorphism to an extent. It's what allows computational expressions in F#, and LINQ in C# to work. For instance, when you in C# do for item in list
select item + 2this gets converted into something like list.Select(item => item + 2)or in F#
This is done through some compile-time constraints that we are unfortunately unable to extend within the language. Basically what we're asking for is the ability to create interfaces like this: interface ILinqable<TSelf<..>> {
TSelf<T1> Select(Func<T1, T2> func);
}
class List<T> : ILinqable<List<..>> {
// need to implement select
} |
This comment has been minimized.
This comment has been minimized.
|
@Alxandr not really, LINQ query expressions are a purely syntactic convention. |
This comment has been minimized.
This comment has been minimized.
aluanhaddad
commented
Sep 27, 2016
•
|
@orthoxerox yes they are but the result is higher kinded typing for a very limited subset of operations. static class EnumerableExtensions
{
public static List<R> Select<T, R>(this List<T> list, Func<T, R> selector) =>
list.asEnumerable().Select(f).ToList();
public static List<T> Where<T>(this List<T> list, Func<T, bool> predicate) =>
list.asEnumerable().Where(predicate).ToList();
public static HashSet<R> Select<T, R>(this HashSet<T> set, Func<T, R> selector) =>
new HashSet<R>(set.asEnumerable().Select(f));
public static HashSet<T> Where<T>(this HashSet<T> set, Func<T, bool> predicate) =>
new HashSet<T>(set.asEnumerable().Select(f));
}
var numbers = new List<int> { 0, 1, 2, 3, 4 };
List<string> values =
from n in numbers
where n % 2 == 0
select $"{n} squared is {n * n}";
var distinctNumbers = new HashSet<int> { 0, 1, 2, 3, 4 };
HashSet<int> distinctSquares =
from n in distinctNumbers
select n * n; |
This comment has been minimized.
This comment has been minimized.
aluanhaddad
commented
Sep 27, 2016
|
The problem is that it has to implemented for every collection type in order to be transparent. In Scala operations like map and filter take an implicit parameter which is used as a factory to create new collections choosing the correct collection type based on the source. |
This comment has been minimized.
This comment has been minimized.
OzieGamma
commented
Sep 27, 2016
|
Indeed overloading lets you use functions as if it was higher-kinded polymorphism. But you still have to write all those overloads. That's what we'd like to avoid. |
This comment has been minimized.
This comment has been minimized.
isaacabraham
commented
Sep 27, 2016
|
This isn't even overloading. There is the ability to "hijack" the LINQ keywords if your types have method that have certain names and signatures - same as foreach really. So in that way I suppose there's some similarity but in my limited understanding of HKTs, it's not the same - you don't have the reuse that they give you. |
This comment has been minimized.
This comment has been minimized.
aluanhaddad
commented
Sep 27, 2016
|
I am not suggesting equivalence. I am suggesting that it is possible to slightly abstract over the type that is itself parameterized by using LINQ method patterns. I was not proposing this as an alternative to higher kinded types as it clearly is not. If Rx is not hijacking LINQ keywords, I hardly think this is, but it is certainly surprising and I would avoid this pattern. |
This comment has been minimized.
This comment has been minimized.
Pauan
commented
Sep 28, 2016
•
|
@Alxandr It's true that LINQ/computation expressions allow you to use the same syntax on multiple different types, but it is a hardcoded syntax trick. Here is an example in C# where LINQ will not help you: This is the reason why higher kinded types are useful. I know you're already aware of this, I'm mentioning this for the sake of other people who think that LINQ is "good enough". It's also possible to hackily add in support for overloaded functions in F# by abusing inline functions and statically resolved type parameters: https://github.com/gmpl/FsControl This is essentially using the same trick that LINQ is using: any type which happens to implement a static method with the right name and right type will work. But unlike interfaces:
So we still need higher-kinded types. |
This comment has been minimized.
This comment has been minimized.
Alxandr
commented
Sep 28, 2016
|
@Pauan I am very well aware of this. I just tried to make a really simple explanation explaining to people who do not know what we want. LINQ is as you said a hardcoded compiler trick. Inline in F# is a bit more of a flexible compiler trick. We would like the CLR to support higher kinded generics. Although, you could make higher-kinded a compiler-only feature it would be better if the CLR supports it. |
This comment has been minimized.
This comment has been minimized.
diab0l
commented
Sep 28, 2016
•
|
I would like to add some more clarification, especially since what I originally wrote may not be clear enough for people unfamiliar with higher kinded types. Proper Types Kinds Such type functions are also called type constructors, since you give them some parameters and they 'construct' a type for you. Generic types To slightly complicate the picture, we also have generic methods which are in a sense function constructors. They are like type constructors, except they do not return a proper type, but instead a proper function. Higher kinded types T<U> Foo<T<>, U>();Another useful kind would be There are other things we cannot express. For example you can't have a variable of type Tl;dr; |
This comment has been minimized.
This comment has been minimized.
Alxandr
commented
Sep 28, 2016
•
|
While I (think) I perfectly understood all of that (I've done haskell), I think the To translate the same into something akin to TypeScript (C# doesn't really have a List: (T: *) => List<T>
Dictionary: (Key: *, Value: *) => Dictionary<Key, Value>whereas what we would like to express is Mappable: (T: (* => *)) => Mappable<T<>>Not sure about the syntax, but I do believe we might want to try to write things in a format thats more similar to what most C# devs are used to (at least while we're in the roslyn repo). Or if an interface would help: interface IMappable<T<>, A> {
T<B> Map<B>(Func<A, B> func);
}
class List<T> : IMappable<List<>, T> {
List<B> IMappable.Map<B>(Func<A, B> func) {
List<B> result = new List<B>(Count);
int index = 0;
foreach (T item in this) {
result[index++] = func(item);
}
}
}Syntax is obviously something that should be discussed, and I don't know what works best, but personally I think figuring out how things should work on the CLR level first is probably the best idea. Also, I haven't written C# in a while, so apologies if there are any glaring errors in my snippets :) |
This comment has been minimized.
This comment has been minimized.
diab0l
commented
Sep 29, 2016
|
@Alxandr The interface is probably the best analogy. |
This comment has been minimized.
This comment has been minimized.
toburger
commented
Oct 12, 2016
|
it seems that there's a light at the end of the tunnel: https://www.youtube.com/watch?v=hK5GJoH4PlI |
This comment has been minimized.
This comment has been minimized.
gusty
commented
Oct 12, 2016
•
|
@toburger That technique doesn't cover Higher Order Kinds. It allows you to define 'first-order-kind typeclasses' like |
This comment has been minimized.
This comment has been minimized.
Opiumtm
commented
Oct 12, 2016
|
Your example is perfectly possible using current C# syntax public static T ConvertTo<T, TItem>(this IEnumerable<TItem> src)
where T : ICollection<TItem>, new()
{
var result = new T();
foreach (var item in src)
{
result.Add(item);
}
return result;
}
public static void Test()
{
var data = Enumerable.Range(1, 10);
var list = data.ConvertTo<List<int>, int>();
var set = data.ConvertTo<HashSet<int>, int>();
} |
This comment has been minimized.
This comment has been minimized.
sideeffffect
commented
Oct 12, 2016
|
if you haven't heard, there's this for HKT in F# https://github.com/palladin/Higher now I'm wondering, that this should also work in C# |
This comment has been minimized.
This comment has been minimized.
Opiumtm
commented
Oct 12, 2016
|
And from mentioned above article. Example: static T<string> GetPurchaseLogs(
T<Person> people,
T<Purchase> purchases)
where T<?> : LINQable<?>
{
return from person in people
from purchase in purchases
where person.Id == purchase.PurchaserId
select person.Name + " purchased " + purchase.ItemName;
}Exact generic logic to example above: static IEnumerable<TItem3> CombineCheck<TArg1, TArg2, TItem1, TItem2, TItem3>(TArg1 a, TArg2 b, Func<TItem1, TItem2, TItem3> combine, Func<TItem1, TItem2, bool> check)
where TArg1 : IEnumerable<TItem1>
where TArg2 : IEnumerable<TItem2>
{
return from item1 in a
from item2 in b
where check(item1, item2)
select combine(item1, item2);
}So, T<?> seems to be just short-hand for existing type constraints that require to explicitly declare type arguments of TItem1, TItem2 item types and TArg1, TArg2 enumerables. And LINQable<?> here isn't possible and not a case of higher-order polymorphism because LINQ queries use structural typing (LINQable is every type which provide standard Where(), Select() and so on methods - it isn't bound to exact interfaces, it use structural typing instead) So, C# should officially support some forms of structural typing as it already support structural typing for awaitables and LINQ-ables and don't support general-purpose structural typing |
This comment has been minimized.
This comment has been minimized.
gabomgp
commented
Nov 10, 2016
•
|
@Opiumtm I think structural typing is a very big change for C#, maybe something similar to traits/type classes/protocols would be better. Traits in Rust are very similiar to extension methods, but think in interfaces than extends the behavior instead of methods. |
This comment has been minimized.
This comment has been minimized.
dzmitry-lahoda
commented
Nov 17, 2016
•
|
I am not sure, but seems case like next could also get use of HKT. Given next usage of CsvHelper: /// <summary>
/// Gets the csv string for the given models.
/// </summary>
/// <param name="models">The data models.</param>
/// <returns>The csv string.</returns>
[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "OK")]
public static string ToCsvString<TEntity, TMap>(IEnumerable<TEntity> models) where TMap : CsvClassMap<TEntity>
{
using (var content = new MemoryStream())
{
var textWriter = new StreamWriter(content, Encoding.UTF8);
var config = new CsvConfiguration();
config.RegisterClassMap<TMap>();
var writer = new CsvWriter(textWriter, config);
writer.WriteHeader<TEntity>();
models.ForEach(writer.WriteRecord);
textWriter.Flush();
content.Position = 0;
return new StreamReader(content).ReadToEnd();
}
}And call: ToCsvString<MyEnitty,MyMap>(...)With HKT is seems would be next: public static string ToCsvString<TMap<TEntity>>(IEnumerable<TEntity> models) where TMap : CsvClassMap<TEntity>With HKT we will get less code to type when method is called - only 1 name of class, instead of 2: ToCsvString<MyMap>(...)And I do not understand Microsoft CodeAnalysis(CA) errors I see - may be will have no reason for these given HKT in place:
Does CA suggest to make HKT into C# for proper design? |
orthoxerox
referenced this issue
Dec 10, 2016
Open
First-class generic method / First-class polymorphism / Rank-N types #15810
This comment has been minimized.
This comment has been minimized.
bondsbw
commented
Dec 14, 2016
|
The following public class DateTimeCollection<U> : ICollection<U>
where U : DateTimeBut var data = Enumerable.Range(0, 20);
var dtCollection = data.To<DateTimeCollection<>, int>(); // int does not inherit DateTimeThe primary location of the problem is in the return type: public static T<A> ... // Should be an error here, cannot construct 'T' with parameter 'A'Because there is no guarantee that public static T<A> To<T, A>(this IEnumerable<A> xs)
where T : <A>, new(), ICollection<> |
This comment has been minimized.
This comment has been minimized.
bondsbw
commented
Dec 14, 2016
|
Perhaps the type constraint for public static T<A> To<T, A>(this IEnumerable<A> xs)
where T : <A>, new(), ICollection<A> Otherwise wouldn't this line fail? ta.Add(x); |
This comment has been minimized.
This comment has been minimized.
alexandru
commented
Feb 2, 2017
•
|
@aluanhaddad OOP subtyping / overloading doesn't help with the most useful operation of all, which is In practice this means you can't describe such operations by means of inheritance (and please excuse the Scala syntax, I'm just a newbie in F#): trait Monad[F[_]] {
// ...
def flatMap[A,B](source: F[A])(f: A => F[B]): F[B]
}
object FutureMonad extends Monad[Future] {
def flatMap[A,B](source: Future[A])(f: A => Future[B]): Future[B] = ???
}Well, in Scala you can describe trait MonadLike[+A, Self[+T] <: MonadLike[T, Self]] { self: Self[A] =>
def flatMap[B](f: A => Self[B]): Self[B]
}
class Future[+A] extends MonadLike[A,Future] {
def flatMap[B](f: A => Future[B]): Future[B] = ???
}Looks ugly but it is very useful for sharing implementation while not downgrading to the super-type in all those operations. |
gafter
referenced this issue
Mar 22, 2017
Open
What language proposals would benefit from CLR changes? #317
This comment has been minimized.
This comment has been minimized.
|
Issue moved to dotnet/csharplang #339 via ZenHub |
diab0l commentedApr 23, 2015
tl;dr
Haskell has monads
Monads are like bacon
C# needs bacon
Introduction
I have a mad craving for higher kinded polymorphism in C#.
And I have no blog to call my own, so here's an essay on this stuff.
That's a fancy name with a lot of crazy type theory and papers behind it, but if you have an understanding of how generic types work in C#, the concept is not terribly hard to understand.
What C# with a taste of bacon would look like
What is going on here?
* -> *ICollection<>(meaningfor each type X, T<X> implements ICollection<X>)Using this you could convert an
IEnumerable<T>to any other type that is a collection.Even all the ones you have never thought about, including the weird ones from libraries which do not get a whole lot of support because nobody uses them (except of course you).
Like
HashSet(such a weird collection) orLinkedList(you would need to be crazy).They are fine citizens of
System.Collections.Genericand very useful, yet they don't get the love they deserve, because they are not as famous asList<>and implementingTo...()methods in Linq for all of them would be a painful task.However, with higher kinded polymorphism, they could all get a general concise implementation.
Where's the rest of that bacon?
That general conversion function is just the tip of the iceberg.
You can do all kinds of crazy transformations with higher kinded polymorphism.
And since it allows for such lovely abstractions you only have to implement them once.
Or better yet: Somebody else implements them in a NuGet package and you don't need to care.
Composition over coding.
Haskell? Is that a dog's name?
That NuGet package could be called something neat and concise like, say, Prelude. Yes, what a fine name!
There are some other party poopers, but the lack of Higher Kinded Polymorphism is the biggest road block in stealing all the juicy bacon from the Haskell community.
You know how Linq is awesome?
Do you also know how Linq is an implementation of the List Monad? (kind of)
Well, there are lots of more Monads in Prelude and most of them are awesome.
And currently a bitch to implement in C#.
Plus, HKP would allow to abstract on the concept of Monads!
And on concepts like
Tuples(never heard of them),Functors(not what you're thinking),Arrows,Categoriesand all the crazy math that comes with them.I've put together a gist of how wonderful this would be in combination with some other features for implementing Maybe.
I don't know what you're talking about, but it sounds expensive
Let's first look at a summary of the benefits that HKP would bring to C#
Now let's talk about work:
typeof()as intypeof(IDictionary<,>)where T : <,>clause seems neat, but is probably not powerful enough (no way to constrain variance on T's parameters or 'swap' parameters for implementations)where T<,> : <U, X>, IDictionary<X, U>void Foo<A<B, C>, D>() where A<B, C> : IDictionary<C, B> where B : classBut I'm a vegetarian
Think of
Baconas an abstract higher kinded type class with non-carnivorous implementations