Skip to content

Instantly share code, notes, and snippets.

@internetimagery
Last active August 31, 2021 20:02
Show Gist options
  • Save internetimagery/e213e4060b0fedc350ce0dc4a2e0db8e to your computer and use it in GitHub Desktop.
Save internetimagery/e213e4060b0fedc350ce0dc4a2e0db8e to your computer and use it in GitHub Desktop.
Failed attempt at higher kinded types in python :P
from typing import TypeVar, Generic
F = TypeVar("F")
A = TypeVar("A")
class Kind(Generic[F, A]):
def __init__(self: F):
self.hkt = self
class Box(Kind["Box[A]", A]):
def __init__(self, val: A):
super(Box, self).__init__()
self.val = val
def get(self) -> A:
return self.val
def stringify(val: Kind[F, int]) -> Kind[F, str]: ...
b = Box(123)
reveal_type(b) # Box[int]
reveal_type(b.get()) # int
reveal_type(b.hkt) # Box[int]
reveal_type(b.hkt.get()) # int
c = stringify(b)
reveal_type(c) # Kind[Box, str]
reveal_type(c.hkt.get()) # int.. DAMN
from typing import TypeVar, Generic, overload, List
F = TypeVar("F")
A = TypeVar("A")
class Kind(Generic[F, A]):
...
class Box(Kind["Box", A]):
def __init__(self, val: A):
super(Box, self).__init__()
self.val = val
def get(self) -> A:
return self.val
# Can this be more generic? Can we define these where we also define Kinded classes?
@overload
def dekind(kind: Kind[Box, A]) -> Box[A]:
...
@overload
def dekind(kind: Kind[List, A]) -> List[A]:
...
def dekind(kind):
return kind
def stringify(val: Kind[F, int]) -> Kind[F, str]:
...
b = Box(123)
reveal_type(b) # Box[int]
reveal_type(b.get()) # int
c = dekind(stringify(b))
reveal_type(c) # Box[str]
reveal_type(c.get()) # str
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment