Created
March 21, 2020 20:08
-
-
Save strager/72bcdd0824b7c56eae7f06ae3684e53a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from abc import ABC, ABCMeta | |
import abc | |
import typing | |
from typing import TypeVar, Generic, Any, Type | |
T = TypeVar("T") | |
C = TypeVar("C", bound="Semigroup") | |
# THIS DOESN'T WORK as expected | |
# class Semigroup(ABC, Generic[T]): | |
# We need an append, | |
# Because this is always binary associative operators | |
# BAO = a two argument function which is associative | |
# BAO = a two argument function returns 1 thing | |
# (M,*) ===== | |
# let M = T | |
# let * = append | |
# mathematical closure - when combining two elements of of the same type you get | |
# the same type | |
class Semigroup(Generic[T], ABC): | |
def __init__(self, value: T): | |
self.value = value | |
@classmethod | |
@abc.abstractmethod | |
def append(cls: Type[C], a: C, b: C) -> C: | |
raise NotImplemented | |
def __eq__(self, other: Any) -> bool: | |
return type(self) is type(other) and self.value == other.value | |
# (M,*,i) | |
class Monoid(Generic[T], Semigroup[T]): | |
@abc.abstractmethod | |
@classmethod | |
def empty(self: Type[C]) -> C: | |
raise NotImplemented | |
# Types are Sets are similiar concetps | |
# Type int | |
# Set 0..infinity | |
# Type str: a..z | |
# (M,*,i) | |
# - M is a set | |
# - * is a binary associative operator | |
# - i is identity | |
# | |
# Example | |
# let M = str | |
# let * = + | |
# let i = '' | |
# let a, b, c E M (a,b,c are strings) | |
# such that | |
# (a + b) + c = a + (b + c) | |
# if the set and binary associative operator have an "identity" | |
# then it is Monoid | |
# | |
# do ABC type annotations have any effect on the extender | |
class Sum(Monoid[int]): | |
@classmethod | |
def append(cls: Type[C], a: C, b: C) -> C: | |
return cls(a.value + b.value) | |
@classmethod | |
def empty(cls: Type[C]) -> C: | |
return cls(0) | |
class Product(): | |
pass | |
# strager: Isn't it something like this: @classmethod @abc.abstractmethod def append(cls: Class[C], a: C, b: C) -> C: ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment