Last active
August 29, 2015 14:03
-
-
Save hodzanassredin/eb0aa76f48e37cbcc87f to your computer and use it in GitHub Desktop.
simple forall representation in c#
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
using System; | |
using System.Collections.Generic; | |
namespace HK | |
{ | |
public class ForallSample | |
{ | |
// this is simplified example to show the idea | |
// we dont really need forall here | |
// but in case of two input and output values with different types we need it | |
// public static List<A> Wrap(Func<A, List<A>> f, A val) forall A | |
// where A : ICOnstraint | |
// { | |
// return f(val); | |
// } | |
// public static System.Collections.Generic.List<T> ToList<T> (T val) | |
// { | |
// var lst = new System.Collections.Generic.List<T> (); | |
// lst.Add (val); | |
// return lst; | |
// } | |
// | |
// public static void Test () | |
// { | |
// List<T> lst = Wrap (ToList, 1); | |
// } | |
} | |
public interface IGeneric<T, TCONTAINER> | |
{ | |
} | |
public static class GenericExts | |
{ | |
public static TM UpCast<T, TM, TMB> (this IGeneric<T, TMB> m) | |
where TM : TMB, IGeneric<T, TMB> | |
{ | |
return (TM)m;//safe for single inheritance | |
} | |
} | |
interface IFn<B, TConstraint> | |
{ | |
CB Apply<T,CB> (T a) | |
where T : TConstraint | |
where CB : B, IGeneric<T,B>; | |
} | |
public class List<TConstraint> | |
{ | |
public Sstatic System.Collections.Generic.List<T> Cast<T> (IGeneric<T, List<TConstraint>> lst) | |
{ | |
return lst.UpCast<T, ListGeneric<T>, List<TConstraint>> ()._lst; | |
} | |
public sealed class ListGeneric<T> : List<TConstraint>, IGeneric<T, List<TConstraint>> | |
where T : TConstraint | |
{ | |
public System.Collections.Generic.List<T> _lst; | |
public ListGeneric (System.Collections.Generic.List<T> lst) | |
{ | |
_lst = lst; | |
} | |
public static implicit operator System.Collections.Generic.List<T> (ListGeneric<T> d) | |
{ | |
return d._lst; | |
} | |
public static implicit operator ListGeneric<T> (System.Collections.Generic.List<T> d) | |
{ | |
return new ListGeneric<T> (d); | |
} | |
} | |
} | |
class MainClass | |
{ | |
class ToListFN<TConstraint> : IFn<List<TConstraint>, TConstraint> | |
{ | |
#region IFn implementation | |
public CB Apply<T, CB> (T a) | |
where CB : List<TConstraint>, IGeneric<T, List<TConstraint>> | |
where T : TConstraint | |
{ | |
var res = new System.Collections.Generic.List<T> (); | |
res.Add (a); | |
return new List<TConstraint>.ListGeneric<T> (res).UpCast<T,CB,List<TConstraint>> (); | |
} | |
#endregion | |
} | |
public static TR Wrap<T, TC, TR, TConstraint> (IFn<TC, TConstraint> f, T i) | |
where TR : TC, IGeneric<T, TC> | |
where T : TConstraint | |
{ | |
return f.Apply<T,TR> (i); | |
} | |
public static void Main (string[] args) | |
{ | |
System.Collections.Generic.List<int> lst = Wrap<int, List<object>, List<object>.ListGeneric<int>,object> (new ToListFN<object> (), 1); | |
Console.WriteLine (lst [0]); | |
Console.ReadKey (); | |
} | |
} | |
} |
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
using System; | |
using System.Collections.Generic; | |
namespace HK | |
{ | |
public class ForallSample | |
{ | |
// this is simplified example to show the idea | |
// we dont really need forall here | |
// but in case of two input and output values with different types we need it | |
// public static List<A> Wrap(Func<A, List<A>> f, A val) forall A | |
// where A : ICOnstraint | |
// { | |
// return f(val); | |
// } | |
// public static System.Collections.Generic.List<T> ToList<T> (T val) | |
// { | |
// var lst = new System.Collections.Generic.List<T> (); | |
// lst.Add (val); | |
// return lst; | |
// } | |
// | |
// public static void Test () | |
// { | |
// List<T> lst = Wrap (ToList, 1); | |
// } | |
} | |
interface IFn<TConstraint> | |
{ | |
System.Collections.Generic.List<T> Apply<T> (T a) where T : TConstraint; | |
} | |
class MainClass | |
{ | |
class ToListFN<TConstraint> : IFn<TConstraint> | |
{ | |
public System.Collections.Generic.List<T> Apply<T> (T a) | |
where T : TConstraint | |
{ | |
var res = new System.Collections.Generic.List<T> (); | |
res.Add (a); | |
return res; | |
} | |
} | |
public static System.Collections.Generic.List<T> Wrap<T, T2, TConstraint> (IFn<TConstraint> f, T i, T2 i2) | |
where T : TConstraint | |
where T2 : TConstraint | |
{ | |
f.Apply<T2> (i2); | |
return f.Apply<T> (i); | |
} | |
public static void Main (string[] args) | |
{ | |
System.Collections.Generic.List<int> lst = Wrap<int, string, object> (new ToListFN<object> (), 1, "sss"); | |
Console.WriteLine (lst [0]); | |
Console.ReadKey (); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment