Last active
January 27, 2021 18:14
-
-
Save Superstar64/30c5793ba4315f4794d20a837d80ef9b to your computer and use it in GitHub Desktop.
Type Erasure In D
This file contains 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
/* | |
Copyright (C) Freddy A Cubas "Superstar64" 2020 | |
Boost Software License - Version 1.0 - August 17th, 2003 | |
Permission is hereby granted, free of charge, to any person or organization | |
obtaining a copy of the software and accompanying documentation covered by | |
this license (the "Software") to use, reproduce, display, distribute, | |
execute, and transmit the Software, and to prepare derivative works of the | |
Software, and to permit third-parties to whom the Software is furnished to | |
do so, all subject to the following: | |
The copyright notices in the Software and this entire statement, including | |
the above license grant, this restriction and the following disclaimer, | |
must be included in all copies of the Software, in whole or in part, and | |
all derivative works of the Software, unless such copies or derivative | |
works are solely in the form of machine-executable object code generated by | |
a source language processor. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT | |
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE | |
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, | |
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | |
DEALINGS IN THE SOFTWARE. | |
*/ | |
import std.meta; | |
import std.traits : isPointer, PointerTarget; | |
struct SingletonKind(T) | |
{ | |
alias Representation = T; | |
enum allows(K) = is(K == T); | |
} | |
struct PointerKind | |
{ | |
alias Representation = void*; | |
enum allows(T : T*) = true; | |
enum allows(T) = false; | |
} | |
struct TypeVariable(string name, Kind) | |
{ | |
Kind.Representation internal; | |
} | |
alias Primatives = AliasSeq!(void, bool, byte, ubyte, short, ushort, int, uint, | |
long, ulong, float, double, real, ifloat, idouble, ireal, cfloat, | |
cdouble, creal, char, wchar, dchar); | |
template SubstituteCurry(Context) | |
{ | |
alias SubstituteCurry(T) = Substitute!(T, Context); | |
} | |
template Substitute(T : TypeVariable!(name, Kind), Context, string name, Kind) | |
if (Context.contains!name) | |
{ | |
static assert(Kind.allows!(Context.lookup!name)); | |
alias Substitute = Context.lookup!name; | |
} | |
template Substitute(T : TypeVariable!(name, Kind), Context, string name, Kind) | |
if (!Context.contains!name) | |
{ | |
alias Substitute = T; | |
} | |
template Substitute(T, Context) if (staticIndexOf!(T, Primatives) != -1) | |
{ | |
alias Substitute = T; | |
} | |
template Substitute(T, Context) if (isPointer!T) | |
{ | |
alias Substitute = Substitute!(PointerTarget!T, Context)*; | |
} | |
alias Substitute(T : U[], Context, U) = Substitute!(U, Context)[]; | |
alias Substitute(T : K[V], Context, K, V) = Substitute!(K, Context)[Substitute!(V, Context)]; | |
alias Substitute(T : R function(A), Context, R, A...) = Substitute!(R, Context) function( | |
staticMap!(SubstituteCurry!Context, A)); | |
alias Substitute(T : R delegate(A), Context, R, A...) = Substitute!(R, Context) delegate( | |
staticMap!(SubstituteCurry!Context, A)); | |
auto specialize(string name, T, A)(A type) | |
{ | |
struct ThisContext | |
{ | |
enum contains(string candidate) = name == candidate; | |
alias lookup(string candidate : name) = T; | |
} | |
return *cast(Substitute!(A, ThisContext)*)(&type); | |
} | |
/* | |
translation of some haskell | |
id :: a -> a | |
id x = x | |
*/ | |
TypeVariable!("a", PointerKind) identity(TypeVariable!("a", PointerKind) x) | |
{ | |
return x; | |
} | |
/* | |
curry :: ((a, b) -> c) -> a -> b -> c | |
curry f x y = f(x,y) | |
*/ | |
TypeVariable!("c", PointerKind) delegate(TypeVariable!("b", PointerKind)) delegate( | |
TypeVariable!("a", PointerKind)) curry(TypeVariable!("c", PointerKind) delegate( | |
TypeVariable!("a", PointerKind), TypeVariable!("b", PointerKind)) f) | |
{ | |
return x => y => f(x, y); | |
} | |
/* | |
flip :: (a -> b -> c) -> b -> a -> c | |
flip f x y = f y x | |
*/ | |
TypeVariable!("c", PointerKind) delegate(TypeVariable!("a", PointerKind)) delegate( | |
TypeVariable!("b", PointerKind)) flip(TypeVariable!("c", PointerKind) delegate( | |
TypeVariable!("b", PointerKind)) delegate(TypeVariable!("a", PointerKind)) f) | |
{ | |
return x => y => f(y)(x); | |
} | |
// levity polymorphism for free!! | |
TypeVariable!("a", Kind) templateIdentity(Kind)(TypeVariable!("a", Kind) x) | |
{ | |
return x; | |
} | |
void main() | |
{ | |
import std.stdio; | |
auto boxed = new int(5); | |
auto same = (&identity).specialize!("a", int*)()(boxed); | |
writeln(*same); | |
int* subtract(int* a, int* b) | |
{ | |
return new int(*a - *b); | |
} | |
auto curriedSubtract = (&curry).specialize!("a", int*) | |
.specialize!("b", int*) | |
.specialize!("c", int*)()(&subtract); | |
auto six = curriedSubtract(new int(8))(new int(2)); | |
writeln(*six); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This has been superseded: code.dlang.org/packages/erasure