Last active
August 7, 2017 12:21
-
-
Save Porges/e041a9b071f2711f2c357817025620ed 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
struct Six : Nat { public int Value => 6; } | |
void Main() | |
{ | |
var four = new Mod<Six>(4); | |
var two = new Mod<Six>(2); | |
Console.WriteLine(four + two == 0); | |
} | |
struct Mod<N> | |
: IEquatable<Mod<N>> | |
, IComparable<Mod<N>> | |
, IEquatable<int> | |
, IComparable<int> | |
where N : struct, Nat | |
{ | |
public Mod(int value) { Value = value; } | |
public int Value { get; } | |
private static int Modulus => default(N).Value; | |
public static int operator+(Mod<N> l, Mod<N> r) | |
=> (l.Value + r.Value) % Modulus; | |
public static int operator*(Mod<N> l, Mod<N> r) | |
=> (l.Value * r.Value) % Modulus; | |
public override string ToString() | |
=> $"{Value} (mod {Modulus})"; | |
public override int GetHashCode() | |
=> Value.GetHashCode(); | |
public int CompareTo(int other) | |
=> Value.CompareTo(other); | |
public bool Equals(int other) | |
=> Value == other; | |
public int CompareTo(Mod<N> other) | |
=> CompareTo(other); | |
public bool Equals(Mod<N> other) | |
=> Equals(other.Value); | |
public override bool Equals(object obj) | |
=> obj is Mod<N> other && Equals(other); | |
} | |
interface Nat | |
{ | |
int Value { get; } | |
} | |
struct Z : Nat | |
{ | |
public int Value => 0; | |
} | |
struct S<X> : Nat where X : struct, Nat | |
{ | |
public int Value => default(X).Value + 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment