├── ExtensibilityForMasses ├── Op.cs ├── Env.cs ├── Forest.cs ├── NormType.cs ├── SqlQuery.cs ├── SqlTable.cs ├── Expression.cs ├── ISchema.cs ├── IIntAlg.cs ├── IValue.cs ├── IExp.cs ├── IStmt.cs ├── IPrint.cs ├── IIntVal.cs ├── IExpIntBool.cs ├── IIntValue.cs ├── IIntExp.cs ├── ExtensibilityForMasses.csproj ├── IBoolAlg.cs ├── IIntBoolAlg.cs ├── BoolFactory.cs ├── IntBoolFactory2.cs ├── IStmtAlg.cs ├── VInt.cs ├── VBool.cs ├── EvalIntAlg.cs ├── IPartitionFactory.cs ├── Lit.cs ├── IntFactory.cs ├── Print2.cs ├── Bool.cs ├── IntBoolFactory.cs ├── Add.cs ├── Iff.cs ├── Eval.cs ├── Debug.cs ├── ISqlTranslation.cs ├── Pair.cs ├── Combine.cs ├── IntAlg.cs ├── GCombine.cs ├── Union.cs ├── GUnion.cs ├── IBatchFactory.cs ├── IntPrint.cs ├── IntBoolPrint.cs └── StmtFactory.cs ├── ExtensibilityForMasses.Test ├── ExtensibilityForMasses.Test.csproj └── ExpTests.cs ├── ExtensibilityForMasses.sln ├── README.md ├── .gitattributes └── .gitignore /ExtensibilityForMasses/Op.cs: -------------------------------------------------------------------------------- 1 | namespace Ploeh.Study.ExtensibilityForMasses 2 | { 3 | public class Op 4 | { 5 | } 6 | } -------------------------------------------------------------------------------- /ExtensibilityForMasses/Env.cs: -------------------------------------------------------------------------------- 1 | namespace Ploeh.Study.ExtensibilityForMasses 2 | { 3 | public class Env 4 | { 5 | } 6 | } -------------------------------------------------------------------------------- /ExtensibilityForMasses/Forest.cs: -------------------------------------------------------------------------------- 1 | namespace Ploeh.Study.ExtensibilityForMasses 2 | { 3 | public class Forest 4 | { 5 | } 6 | } -------------------------------------------------------------------------------- /ExtensibilityForMasses/NormType.cs: -------------------------------------------------------------------------------- 1 | namespace Ploeh.Study.ExtensibilityForMasses 2 | { 3 | public class NormType 4 | { 5 | } 6 | } -------------------------------------------------------------------------------- /ExtensibilityForMasses/SqlQuery.cs: -------------------------------------------------------------------------------- 1 | namespace Ploeh.Study.ExtensibilityForMasses 2 | { 3 | public class SqlQuery 4 | { 5 | } 6 | } -------------------------------------------------------------------------------- /ExtensibilityForMasses/SqlTable.cs: -------------------------------------------------------------------------------- 1 | namespace Ploeh.Study.ExtensibilityForMasses 2 | { 3 | public class SqlTable 4 | { 5 | } 6 | } -------------------------------------------------------------------------------- /ExtensibilityForMasses/Expression.cs: -------------------------------------------------------------------------------- 1 | namespace Ploeh.Study.ExtensibilityForMasses 2 | { 3 | public class Expression 4 | { 5 | } 6 | } -------------------------------------------------------------------------------- /ExtensibilityForMasses/ISchema.cs: -------------------------------------------------------------------------------- 1 | namespace Ploeh.Study.ExtensibilityForMasses 2 | { 3 | public interface ISchema 4 | { 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /ExtensibilityForMasses/IIntAlg.cs: -------------------------------------------------------------------------------- 1 | namespace Ploeh.Study.ExtensibilityForMasses 2 | { 3 | public interface IIntAlg 4 | { 5 | A Lit(int x); 6 | A Add(A e1, A e2); 7 | } 8 | } -------------------------------------------------------------------------------- /ExtensibilityForMasses/IValue.cs: -------------------------------------------------------------------------------- 1 | namespace Ploeh.Study.ExtensibilityForMasses 2 | { 3 | public interface IValue 4 | { 5 | int Int { get; } 6 | bool Bool { get; } 7 | } 8 | } -------------------------------------------------------------------------------- /ExtensibilityForMasses/IExp.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Ploeh.Study.ExtensibilityForMasses 8 | { 9 | public interface IExp 10 | { 11 | IValue Eval(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /ExtensibilityForMasses/IStmt.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Ploeh.Study.ExtensibilityForMasses 8 | { 9 | public interface IStmt 10 | { 11 | void Eval(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /ExtensibilityForMasses/IPrint.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Ploeh.Study.ExtensibilityForMasses 8 | { 9 | public interface IPrint 10 | { 11 | string Print(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /ExtensibilityForMasses/IIntVal.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Ploeh.Study.ExtensibilityForMasses 8 | { 9 | public interface IIntVal 10 | { 11 | A Lit(int x); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /ExtensibilityForMasses/IExpIntBool.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Ploeh.Study.ExtensibilityForMasses 8 | { 9 | public interface IExpIntBool : IBoolAlg, IIntAlg 10 | { 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /ExtensibilityForMasses/IIntValue.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Ploeh.Study.ExtensibilityForMasses 8 | { 9 | public interface IIntValue 10 | { 11 | int Int { get; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /ExtensibilityForMasses/IIntExp.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Ploeh.Study.ExtensibilityForMasses 8 | { 9 | public interface IIntExp : IIntVal 10 | { 11 | A Add(A x, A y); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /ExtensibilityForMasses/ExtensibilityForMasses.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net5.0 5 | Ploeh.Study.ExtensibilityForMasses 6 | Ploeh.Study.ExtensibilityForMasses 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /ExtensibilityForMasses/IBoolAlg.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Ploeh.Study.ExtensibilityForMasses 8 | { 9 | public interface IBoolAlg 10 | { 11 | A Bool(bool b); 12 | A Iff(A b, A e1, A e2); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /ExtensibilityForMasses/IIntBoolAlg.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Ploeh.Study.ExtensibilityForMasses 8 | { 9 | public interface IIntBoolAlg : IIntAlg 10 | { 11 | A Bool(bool b); 12 | A Iff(A e1, A e2, A e3); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /ExtensibilityForMasses/BoolFactory.cs: -------------------------------------------------------------------------------- 1 | namespace Ploeh.Study.ExtensibilityForMasses 2 | { 3 | public class BoolFactory : IBoolAlg 4 | { 5 | public IExp Bool(bool b) 6 | { 7 | return new Bool(b); 8 | } 9 | 10 | public IExp Iff(IExp b, IExp e1, IExp e2) 11 | { 12 | return new Iff(b, e1, e2); 13 | } 14 | } 15 | } -------------------------------------------------------------------------------- /ExtensibilityForMasses/IntBoolFactory2.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Ploeh.Study.ExtensibilityForMasses 8 | { 9 | public class IntBoolFactory2 : Union 10 | { 11 | public IntBoolFactory2() : base(new BoolFactory(), new IntFactory()) 12 | { 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /ExtensibilityForMasses/IStmtAlg.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Ploeh.Study.ExtensibilityForMasses 8 | { 9 | public interface IStmtAlg : IIntBoolAlg 10 | { 11 | E Var(string x); 12 | E Assign(string x, E e); 13 | S Expr(E e); 14 | S Comp(S e1, S e2); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /ExtensibilityForMasses/VInt.cs: -------------------------------------------------------------------------------- 1 | namespace Ploeh.Study.ExtensibilityForMasses 2 | { 3 | public class VInt : IValue 4 | { 5 | public VInt(int x) 6 | { 7 | Int = x; 8 | } 9 | 10 | public int Int { get; } 11 | 12 | public bool Bool => throw new System.NotImplementedException(); 13 | 14 | public override string ToString() 15 | { 16 | return Int.ToString(); 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /ExtensibilityForMasses/VBool.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Ploeh.Study.ExtensibilityForMasses 8 | { 9 | public class VBool : IValue 10 | { 11 | public VBool(bool b) 12 | { 13 | Bool = b; 14 | } 15 | 16 | public int Int => throw new NotImplementedException(); 17 | 18 | public bool Bool { get; } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /ExtensibilityForMasses/EvalIntAlg.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Ploeh.Study.ExtensibilityForMasses 8 | { 9 | public class EvalIntAlg : IIntAlg 10 | { 11 | public int Add(int e1, int e2) 12 | { 13 | return e1 + e2; 14 | } 15 | 16 | public int Lit(int x) 17 | { 18 | return x; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /ExtensibilityForMasses/IPartitionFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Ploeh.Study.ExtensibilityForMasses 8 | { 9 | public interface IPartitionFactory : IBatchFactory 10 | { 11 | E Other(object external, params E[] subs); 12 | E DynamicCall(E target, string method, IEnumerable args); 13 | E Mobile(string type, object obj, E exp); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /ExtensibilityForMasses/Lit.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Ploeh.Study.ExtensibilityForMasses 8 | { 9 | public class Lit : IExp 10 | { 11 | private readonly int x; 12 | 13 | public Lit(int x) 14 | { 15 | this.x = x; 16 | } 17 | 18 | public IValue Eval() 19 | { 20 | return new VInt(x); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /ExtensibilityForMasses/IntFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Ploeh.Study.ExtensibilityForMasses 8 | { 9 | public class IntFactory : IIntAlg 10 | { 11 | public IExp Add(IExp e1, IExp e2) 12 | { 13 | return new Add(e1, e2); 14 | } 15 | 16 | public IExp Lit(int x) 17 | { 18 | return new Lit(x); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /ExtensibilityForMasses/Print2.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Ploeh.Study.ExtensibilityForMasses 8 | { 9 | public class Print2 : IIntAlg 10 | { 11 | public string Add(string e1, string e2) 12 | { 13 | return $"{e1} + {e2}"; 14 | } 15 | 16 | public string Lit(int x) 17 | { 18 | return x.ToString(); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /ExtensibilityForMasses/Bool.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Ploeh.Study.ExtensibilityForMasses 8 | { 9 | public sealed class Bool : IExp 10 | { 11 | private readonly bool b; 12 | 13 | public Bool(bool b) 14 | { 15 | this.b = b; 16 | } 17 | 18 | public IValue Eval() 19 | { 20 | return new VBool(b); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /ExtensibilityForMasses/IntBoolFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Ploeh.Study.ExtensibilityForMasses 8 | { 9 | public class IntBoolFactory : IntFactory, IIntBoolAlg 10 | { 11 | public IExp Bool(bool b) 12 | { 13 | return new Bool(b); 14 | } 15 | 16 | public IExp Iff(IExp e1, IExp e2, IExp e3) 17 | { 18 | return new Iff(e1, e2, e3); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /ExtensibilityForMasses/Add.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Ploeh.Study.ExtensibilityForMasses 8 | { 9 | public class Add : IExp 10 | { 11 | private readonly IExp l; 12 | private readonly IExp r; 13 | 14 | public Add(IExp l, IExp r) 15 | { 16 | this.l = l; 17 | this.r = r; 18 | } 19 | 20 | public IValue Eval() 21 | { 22 | return new VInt(l.Eval().Int + r.Eval().Int); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /ExtensibilityForMasses/Iff.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Ploeh.Study.ExtensibilityForMasses 8 | { 9 | public sealed class Iff : IExp 10 | { 11 | private readonly IExp e1; 12 | private readonly IExp e2; 13 | private readonly IExp e3; 14 | 15 | public Iff(IExp e1, IExp e2, IExp e3) 16 | { 17 | this.e1 = e1; 18 | this.e2 = e2; 19 | this.e3 = e3; 20 | } 21 | 22 | public IValue Eval() 23 | { 24 | return e1.Eval().Bool ? e2.Eval() : e3.Eval(); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /ExtensibilityForMasses/Eval.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Ploeh.Study.ExtensibilityForMasses 8 | { 9 | public class Eval : IIntExp 10 | where A : IIntValue 11 | where V : IIntVal 12 | { 13 | protected readonly V valFact; 14 | 15 | public Eval(V valFact) 16 | { 17 | this.valFact = valFact; 18 | } 19 | 20 | public A Lit(int x) 21 | { 22 | return valFact.Lit(x); 23 | } 24 | 25 | public A Add(A x, A y) 26 | { 27 | return valFact.Lit(x.Int + y.Int); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /ExtensibilityForMasses/Debug.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Ploeh.Study.ExtensibilityForMasses 8 | { 9 | public class Debug : Combine 10 | { 11 | public Debug() : base(new IntFactory(), new IntPrint()) 12 | { 13 | } 14 | 15 | public override Pair Add(Pair e1, Pair e2) 16 | { 17 | Console.WriteLine($"The first expression {e1.TheB.Print()} evaluates to {e1.TheA.Eval()}"); 18 | Console.WriteLine($"The second expression {e2.TheB.Print()} evaluates to {e2.TheA.Eval()}"); 19 | return base.Add(e1, e2); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /ExtensibilityForMasses/ISqlTranslation.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Ploeh.Study.ExtensibilityForMasses 8 | { 9 | public interface ISqlTranslation 10 | { 11 | void ToSql(StringBuilder sb, IEnumerable @params, Forest data); 12 | Expression Normalize(ISchema schema, SqlQuery query, 13 | Expression outerCond, Env env, NormType normType); 14 | SqlTable GetTable(); 15 | Expression InvertPath(Expression e, Env env, bool fromChild); 16 | SqlTable GetTableNoJoins(Env env); 17 | SqlTable GetBase(Env env); 18 | Expression WithoutTransformations(); 19 | Expression GetTransformations(Expression @base); 20 | Expression TrimLast(Env env); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /ExtensibilityForMasses/Pair.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Ploeh.Study.ExtensibilityForMasses 8 | { 9 | public class Pair 10 | { 11 | public Pair(A a, B b) 12 | { 13 | TheA = a; 14 | TheB = b; 15 | } 16 | 17 | public A TheA { get; } 18 | public B TheB { get; } 19 | 20 | public override bool Equals(object obj) 21 | { 22 | return obj is Pair pair && 23 | EqualityComparer.Default.Equals(TheA, pair.TheA) && 24 | EqualityComparer.Default.Equals(TheB, pair.TheB); 25 | } 26 | 27 | public override int GetHashCode() 28 | { 29 | return HashCode.Combine(TheA, TheB); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /ExtensibilityForMasses/Combine.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Ploeh.Study.ExtensibilityForMasses 8 | { 9 | public class Combine : IIntAlg> 10 | { 11 | private readonly IIntAlg v1; 12 | private readonly IIntAlg v2; 13 | 14 | public Combine(IIntAlg v1, IIntAlg v2) 15 | { 16 | this.v1 = v1; 17 | this.v2 = v2; 18 | } 19 | 20 | public Pair Lit(int x) 21 | { 22 | return new Pair(v1.Lit(x), v2.Lit(x)); 23 | } 24 | 25 | public virtual Pair Add(Pair e1, Pair e2) 26 | { 27 | return new Pair( 28 | v1.Add(e1.TheA, e2.TheA), 29 | v2.Add(e1.TheB, e2.TheB)); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /ExtensibilityForMasses/IntAlg.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Ploeh.Study.ExtensibilityForMasses 8 | { 9 | public static class IntAlg 10 | { 11 | public static A Make3Plus5(this IIntAlg f) 12 | { 13 | return f.Add(f.Lit(3), f.Lit(5)); 14 | } 15 | 16 | public static A Make3Plus4(this IIntAlg v) 17 | { 18 | return v.Add(v.Lit(3), v.Lit(4)); 19 | } 20 | 21 | public static A ParseExp(this IIntAlg f, string s) 22 | { 23 | if (int.TryParse(s, out var x)) 24 | return f.Lit(x); 25 | else 26 | { 27 | return s.Split('+') 28 | .Select(x => f.Lit(int.Parse(x))) 29 | .Aggregate(f.Add); 30 | } 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /ExtensibilityForMasses/GCombine.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Ploeh.Study.ExtensibilityForMasses 8 | { 9 | public class GCombine : IIntAlg> 10 | where V1 : IIntAlg 11 | where V2 : IIntAlg 12 | { 13 | private readonly V1 v1; 14 | private readonly V2 v2; 15 | 16 | public GCombine(V1 v1, V2 v2) 17 | { 18 | this.v1 = v1; 19 | this.v2 = v2; 20 | } 21 | 22 | public Pair Lit(int x) 23 | { 24 | return new Pair(v1.Lit(x), v2.Lit(x)); 25 | } 26 | 27 | public Pair Add(Pair e1, Pair e2) 28 | { 29 | return new Pair( 30 | v1.Add(e1.TheA, e2.TheA), 31 | v2.Add(e1.TheB, e2.TheB)); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /ExtensibilityForMasses/Union.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Ploeh.Study.ExtensibilityForMasses 8 | { 9 | public class Union : IExpIntBool 10 | { 11 | private readonly IBoolAlg v1; 12 | private readonly IIntAlg v2; 13 | 14 | public Union(IBoolAlg v1, IIntAlg v2) 15 | { 16 | this.v1 = v1; 17 | this.v2 = v2; 18 | } 19 | 20 | public A Lit(int x) 21 | { 22 | return v2.Lit(x); 23 | } 24 | 25 | public A Add(A e1, A e2) 26 | { 27 | return v2.Add(e1, e2); 28 | } 29 | 30 | public A Bool(bool b) 31 | { 32 | return v1.Bool(b); 33 | } 34 | 35 | public A Iff(A b, A e1, A e2) 36 | { 37 | return v1.Iff(b, e1, e2); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /ExtensibilityForMasses/GUnion.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Ploeh.Study.ExtensibilityForMasses 8 | { 9 | public class GUnion : IExpIntBool 10 | where V1 : IBoolAlg 11 | where V2 : IIntAlg 12 | { 13 | private readonly V1 v1; 14 | private readonly V2 v2; 15 | 16 | public GUnion(V1 v1, V2 v2) 17 | { 18 | this.v1 = v1; 19 | this.v2 = v2; 20 | } 21 | 22 | public A Bool(bool b) 23 | { 24 | return v1.Bool(b); 25 | } 26 | 27 | public A Iff(A b, A e1, A e2) 28 | { 29 | return v1.Iff(b, e1, e2); 30 | } 31 | 32 | public A Lit(int x) 33 | { 34 | return v2.Lit(x); 35 | } 36 | 37 | public A Add(A e1, A e2) 38 | { 39 | return v2.Add(e1, e2); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /ExtensibilityForMasses/IBatchFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Ploeh.Study.ExtensibilityForMasses 8 | { 9 | public interface IBatchFactory 10 | { 11 | E Var(string name); // Variable reference 12 | E Data(object value); // Simple constant (number, string, or date) 13 | E Fun(string var, E body); 14 | E Prim(Op op, IEnumerable args); // Unary and binary operators 15 | E Prop(E @base, string field); // Field access 16 | E Assign(Op op, E target, E source); // Assignment 17 | E Let(string var, E expression, E body); // Control flow 18 | E If(E condition, E thenExp, E elseExp); 19 | E Loop(Op op, string var, E collection, E body); 20 | E Call(E target, string method, IEnumerable args); // Method invocation 21 | E In(string location); // Reading and writing forest 22 | E Out(string location, E expression); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /ExtensibilityForMasses.Test/ExtensibilityForMasses.Test.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net5.0 5 | 6 | false 7 | 8 | Ploeh.Study.ExtensibilityForMasses.Test 9 | 10 | 11 | 12 | 13 | 14 | 15 | runtime; build; native; contentfiles; analyzers; buildtransitive 16 | all 17 | 18 | 19 | runtime; build; native; contentfiles; analyzers; buildtransitive 20 | all 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /ExtensibilityForMasses/IntPrint.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Ploeh.Study.ExtensibilityForMasses 8 | { 9 | public class IntPrint : IIntAlg 10 | { 11 | public IPrint Lit(int x) 12 | { 13 | return new LitPrint(x); 14 | } 15 | 16 | private sealed class LitPrint : IPrint 17 | { 18 | private readonly int x; 19 | 20 | public LitPrint(int x) 21 | { 22 | this.x = x; 23 | } 24 | 25 | public string Print() 26 | { 27 | return x.ToString(); 28 | } 29 | } 30 | 31 | public IPrint Add(IPrint e1, IPrint e2) 32 | { 33 | return new AddPrint(e1, e2); 34 | } 35 | 36 | private sealed class AddPrint : IPrint 37 | { 38 | private readonly IPrint e1; 39 | private readonly IPrint e2; 40 | 41 | public AddPrint(IPrint e1, IPrint e2) 42 | { 43 | this.e1 = e1; 44 | this.e2 = e2; 45 | } 46 | 47 | public string Print() 48 | { 49 | return $"{e1.Print()} + {e2.Print()}"; 50 | } 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /ExtensibilityForMasses/IntBoolPrint.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Ploeh.Study.ExtensibilityForMasses 8 | { 9 | public class IntBoolPrint : IntPrint, IIntBoolAlg 10 | { 11 | public IPrint Bool(bool b) 12 | { 13 | return new BoolPrint(b); 14 | } 15 | 16 | private sealed class BoolPrint : IPrint 17 | { 18 | private readonly bool b; 19 | 20 | public BoolPrint(bool b) 21 | { 22 | this.b = b; 23 | } 24 | 25 | public string Print() 26 | { 27 | return b.ToString(); 28 | } 29 | } 30 | 31 | public IPrint Iff(IPrint e1, IPrint e2, IPrint e3) 32 | { 33 | return new IffPrint(e1, e2, e3); 34 | } 35 | 36 | private sealed class IffPrint : IPrint 37 | { 38 | private readonly IPrint e1; 39 | private readonly IPrint e2; 40 | private readonly IPrint e3; 41 | 42 | public IffPrint(IPrint e1, IPrint e2, IPrint e3) 43 | { 44 | this.e1 = e1; 45 | this.e2 = e2; 46 | this.e3 = e3; 47 | } 48 | 49 | public string Print() 50 | { 51 | return $"if ({e1.Print()}) then {e2.Print()} else {e3.Print()}"; 52 | } 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /ExtensibilityForMasses.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.31313.79 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExtensibilityForMasses", "ExtensibilityForMasses\ExtensibilityForMasses.csproj", "{330262A5-1593-4493-B805-25765CAFE061}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExtensibilityForMasses.Test", "ExtensibilityForMasses.Test\ExtensibilityForMasses.Test.csproj", "{C638B22B-A493-45A9-BAAC-8511C824283E}" 9 | EndProject 10 | Global 11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 12 | Debug|Any CPU = Debug|Any CPU 13 | Release|Any CPU = Release|Any CPU 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {330262A5-1593-4493-B805-25765CAFE061}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 17 | {330262A5-1593-4493-B805-25765CAFE061}.Debug|Any CPU.Build.0 = Debug|Any CPU 18 | {330262A5-1593-4493-B805-25765CAFE061}.Release|Any CPU.ActiveCfg = Release|Any CPU 19 | {330262A5-1593-4493-B805-25765CAFE061}.Release|Any CPU.Build.0 = Release|Any CPU 20 | {C638B22B-A493-45A9-BAAC-8511C824283E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 21 | {C638B22B-A493-45A9-BAAC-8511C824283E}.Debug|Any CPU.Build.0 = Debug|Any CPU 22 | {C638B22B-A493-45A9-BAAC-8511C824283E}.Release|Any CPU.ActiveCfg = Release|Any CPU 23 | {C638B22B-A493-45A9-BAAC-8511C824283E}.Release|Any CPU.Build.0 = Release|Any CPU 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | GlobalSection(ExtensibilityGlobals) = postSolution 29 | SolutionGuid = {E5E4F878-B3C0-4AE3-A9F2-6F51137B1215} 30 | EndGlobalSection 31 | EndGlobal 32 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Extensibility for the Masses 2 | 3 | This repository contains the code I wrote as I was reading the paper *Extensibility for the Masses. Practical Extensibility with Object Algebras* by Bruno C. d. S. Oliveira and William R. Cook (available many places on the internet; I used this one: https://www.cs.utexas.edu/~wcook/Drafts/2012/ecoop2012.pdf, which I suppose is William R. Cook's). 4 | 5 | When reading papers and books, I've found that I usually learn better if I type in the code as I read along. 6 | 7 | The paper's example code is written in Java, and while I can read and understand Java, I don't really write it. For that reason, and because the authors claim that the concepts translate to other, similar languages like C#, I decided to port the article's code to C#. 8 | 9 | ## A reading log 10 | 11 | I've written the code as I was reading the article. After each little step, I checked in my changes. Whenever relevant, I've left a commit message discussing the change. If you're interested, it might be worthwhile to peruse the Git log if you're wondering about some of the choices I made as I ported the Java code to C#. 12 | 13 | ## Prescience 14 | 15 | If you look at the Git log, you'll see that this README file appears right at the start, complete with its ponderings on the process. I'm not *that* prescient; I rebased it in halfway through the reading process. 16 | 17 | ## Unit tests 18 | 19 | I obviously didn't write this code base with test-driven development (TDD). Rather, it was written by the process known as PDD (paper-driven development; i.e. typing in code from the paper). I early on decided to add unit tests to be able to interact with the example code. 20 | 21 | Most of it is rather straightforward, but I still feel that I better understand an API when I get the chance to interact with it. -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /ExtensibilityForMasses/StmtFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Ploeh.Study.ExtensibilityForMasses 8 | { 9 | public class StmtFactory : IntBoolFactory, IStmtAlg 10 | { 11 | private readonly Dictionary map; 12 | 13 | public StmtFactory() 14 | { 15 | map = new Dictionary(); 16 | } 17 | 18 | public IExp Var(string x) 19 | { 20 | return new VarExp(map, x); 21 | } 22 | 23 | private class VarExp : IExp 24 | { 25 | private readonly IDictionary map; 26 | private readonly string x; 27 | 28 | public VarExp(IDictionary map, string x) 29 | { 30 | this.map = map; 31 | this.x = x; 32 | } 33 | 34 | public IValue Eval() 35 | { 36 | return map[x]; 37 | } 38 | } 39 | 40 | public IExp Assign(string x, IExp e) 41 | { 42 | return new AssignExp(map, x, e); 43 | } 44 | 45 | private class AssignExp : IExp 46 | { 47 | private readonly IDictionary map; 48 | private readonly string x; 49 | private readonly IExp e; 50 | 51 | public AssignExp(IDictionary map, string x, IExp e) 52 | { 53 | this.map = map; 54 | this.x = x; 55 | this.e = e; 56 | } 57 | 58 | public IValue Eval() 59 | { 60 | var value = e.Eval(); 61 | map[x] = value; 62 | return value; 63 | } 64 | } 65 | 66 | public IStmt Comp(IStmt e1, IStmt e2) 67 | { 68 | return new CompStmt(e1, e2); 69 | } 70 | 71 | private class CompStmt : IStmt 72 | { 73 | private readonly IStmt e1; 74 | private readonly IStmt e2; 75 | 76 | public CompStmt(IStmt e1, IStmt e2) 77 | { 78 | this.e1 = e1; 79 | this.e2 = e2; 80 | } 81 | 82 | public void Eval() 83 | { 84 | e1.Eval(); 85 | e2.Eval(); 86 | } 87 | } 88 | 89 | public IStmt Expr(IExp e) 90 | { 91 | return new ExprStmt(e); 92 | } 93 | 94 | private class ExprStmt : IStmt 95 | { 96 | private readonly IExp e; 97 | 98 | public ExprStmt(IExp e) 99 | { 100 | this.e = e; 101 | } 102 | 103 | public void Eval() 104 | { 105 | e.Eval(); 106 | } 107 | } 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Ww][Ii][Nn]32/ 27 | [Aa][Rr][Mm]/ 28 | [Aa][Rr][Mm]64/ 29 | bld/ 30 | [Bb]in/ 31 | [Oo]bj/ 32 | [Oo]ut/ 33 | [Ll]og/ 34 | [Ll]ogs/ 35 | 36 | # Visual Studio 2015/2017 cache/options directory 37 | .vs/ 38 | # Uncomment if you have tasks that create the project's static files in wwwroot 39 | #wwwroot/ 40 | 41 | # Visual Studio 2017 auto generated files 42 | Generated\ Files/ 43 | 44 | # MSTest test Results 45 | [Tt]est[Rr]esult*/ 46 | [Bb]uild[Ll]og.* 47 | 48 | # NUnit 49 | *.VisualState.xml 50 | TestResult.xml 51 | nunit-*.xml 52 | 53 | # Build Results of an ATL Project 54 | [Dd]ebugPS/ 55 | [Rr]eleasePS/ 56 | dlldata.c 57 | 58 | # Benchmark Results 59 | BenchmarkDotNet.Artifacts/ 60 | 61 | # .NET Core 62 | project.lock.json 63 | project.fragment.lock.json 64 | artifacts/ 65 | 66 | # ASP.NET Scaffolding 67 | ScaffoldingReadMe.txt 68 | 69 | # StyleCop 70 | StyleCopReport.xml 71 | 72 | # Files built by Visual Studio 73 | *_i.c 74 | *_p.c 75 | *_h.h 76 | *.ilk 77 | *.meta 78 | *.obj 79 | *.iobj 80 | *.pch 81 | *.pdb 82 | *.ipdb 83 | *.pgc 84 | *.pgd 85 | *.rsp 86 | *.sbr 87 | *.tlb 88 | *.tli 89 | *.tlh 90 | *.tmp 91 | *.tmp_proj 92 | *_wpftmp.csproj 93 | *.log 94 | *.vspscc 95 | *.vssscc 96 | .builds 97 | *.pidb 98 | *.svclog 99 | *.scc 100 | 101 | # Chutzpah Test files 102 | _Chutzpah* 103 | 104 | # Visual C++ cache files 105 | ipch/ 106 | *.aps 107 | *.ncb 108 | *.opendb 109 | *.opensdf 110 | *.sdf 111 | *.cachefile 112 | *.VC.db 113 | *.VC.VC.opendb 114 | 115 | # Visual Studio profiler 116 | *.psess 117 | *.vsp 118 | *.vspx 119 | *.sap 120 | 121 | # Visual Studio Trace Files 122 | *.e2e 123 | 124 | # TFS 2012 Local Workspace 125 | $tf/ 126 | 127 | # Guidance Automation Toolkit 128 | *.gpState 129 | 130 | # ReSharper is a .NET coding add-in 131 | _ReSharper*/ 132 | *.[Rr]e[Ss]harper 133 | *.DotSettings.user 134 | 135 | # TeamCity is a build add-in 136 | _TeamCity* 137 | 138 | # DotCover is a Code Coverage Tool 139 | *.dotCover 140 | 141 | # AxoCover is a Code Coverage Tool 142 | .axoCover/* 143 | !.axoCover/settings.json 144 | 145 | # Coverlet is a free, cross platform Code Coverage Tool 146 | coverage*.json 147 | coverage*.xml 148 | coverage*.info 149 | 150 | # Visual Studio code coverage results 151 | *.coverage 152 | *.coveragexml 153 | 154 | # NCrunch 155 | _NCrunch_* 156 | .*crunch*.local.xml 157 | nCrunchTemp_* 158 | 159 | # MightyMoose 160 | *.mm.* 161 | AutoTest.Net/ 162 | 163 | # Web workbench (sass) 164 | .sass-cache/ 165 | 166 | # Installshield output folder 167 | [Ee]xpress/ 168 | 169 | # DocProject is a documentation generator add-in 170 | DocProject/buildhelp/ 171 | DocProject/Help/*.HxT 172 | DocProject/Help/*.HxC 173 | DocProject/Help/*.hhc 174 | DocProject/Help/*.hhk 175 | DocProject/Help/*.hhp 176 | DocProject/Help/Html2 177 | DocProject/Help/html 178 | 179 | # Click-Once directory 180 | publish/ 181 | 182 | # Publish Web Output 183 | *.[Pp]ublish.xml 184 | *.azurePubxml 185 | # Note: Comment the next line if you want to checkin your web deploy settings, 186 | # but database connection strings (with potential passwords) will be unencrypted 187 | *.pubxml 188 | *.publishproj 189 | 190 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 191 | # checkin your Azure Web App publish settings, but sensitive information contained 192 | # in these scripts will be unencrypted 193 | PublishScripts/ 194 | 195 | # NuGet Packages 196 | *.nupkg 197 | # NuGet Symbol Packages 198 | *.snupkg 199 | # The packages folder can be ignored because of Package Restore 200 | **/[Pp]ackages/* 201 | # except build/, which is used as an MSBuild target. 202 | !**/[Pp]ackages/build/ 203 | # Uncomment if necessary however generally it will be regenerated when needed 204 | #!**/[Pp]ackages/repositories.config 205 | # NuGet v3's project.json files produces more ignorable files 206 | *.nuget.props 207 | *.nuget.targets 208 | 209 | # Microsoft Azure Build Output 210 | csx/ 211 | *.build.csdef 212 | 213 | # Microsoft Azure Emulator 214 | ecf/ 215 | rcf/ 216 | 217 | # Windows Store app package directories and files 218 | AppPackages/ 219 | BundleArtifacts/ 220 | Package.StoreAssociation.xml 221 | _pkginfo.txt 222 | *.appx 223 | *.appxbundle 224 | *.appxupload 225 | 226 | # Visual Studio cache files 227 | # files ending in .cache can be ignored 228 | *.[Cc]ache 229 | # but keep track of directories ending in .cache 230 | !?*.[Cc]ache/ 231 | 232 | # Others 233 | ClientBin/ 234 | ~$* 235 | *~ 236 | *.dbmdl 237 | *.dbproj.schemaview 238 | *.jfm 239 | *.pfx 240 | *.publishsettings 241 | orleans.codegen.cs 242 | 243 | # Including strong name files can present a security risk 244 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 245 | #*.snk 246 | 247 | # Since there are multiple workflows, uncomment next line to ignore bower_components 248 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 249 | #bower_components/ 250 | 251 | # RIA/Silverlight projects 252 | Generated_Code/ 253 | 254 | # Backup & report files from converting an old project file 255 | # to a newer Visual Studio version. Backup files are not needed, 256 | # because we have git ;-) 257 | _UpgradeReport_Files/ 258 | Backup*/ 259 | UpgradeLog*.XML 260 | UpgradeLog*.htm 261 | ServiceFabricBackup/ 262 | *.rptproj.bak 263 | 264 | # SQL Server files 265 | *.mdf 266 | *.ldf 267 | *.ndf 268 | 269 | # Business Intelligence projects 270 | *.rdl.data 271 | *.bim.layout 272 | *.bim_*.settings 273 | *.rptproj.rsuser 274 | *- [Bb]ackup.rdl 275 | *- [Bb]ackup ([0-9]).rdl 276 | *- [Bb]ackup ([0-9][0-9]).rdl 277 | 278 | # Microsoft Fakes 279 | FakesAssemblies/ 280 | 281 | # GhostDoc plugin setting file 282 | *.GhostDoc.xml 283 | 284 | # Node.js Tools for Visual Studio 285 | .ntvs_analysis.dat 286 | node_modules/ 287 | 288 | # Visual Studio 6 build log 289 | *.plg 290 | 291 | # Visual Studio 6 workspace options file 292 | *.opt 293 | 294 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 295 | *.vbw 296 | 297 | # Visual Studio LightSwitch build output 298 | **/*.HTMLClient/GeneratedArtifacts 299 | **/*.DesktopClient/GeneratedArtifacts 300 | **/*.DesktopClient/ModelManifest.xml 301 | **/*.Server/GeneratedArtifacts 302 | **/*.Server/ModelManifest.xml 303 | _Pvt_Extensions 304 | 305 | # Paket dependency manager 306 | .paket/paket.exe 307 | paket-files/ 308 | 309 | # FAKE - F# Make 310 | .fake/ 311 | 312 | # CodeRush personal settings 313 | .cr/personal 314 | 315 | # Python Tools for Visual Studio (PTVS) 316 | __pycache__/ 317 | *.pyc 318 | 319 | # Cake - Uncomment if you are using it 320 | # tools/** 321 | # !tools/packages.config 322 | 323 | # Tabs Studio 324 | *.tss 325 | 326 | # Telerik's JustMock configuration file 327 | *.jmconfig 328 | 329 | # BizTalk build output 330 | *.btp.cs 331 | *.btm.cs 332 | *.odx.cs 333 | *.xsd.cs 334 | 335 | # OpenCover UI analysis results 336 | OpenCover/ 337 | 338 | # Azure Stream Analytics local run output 339 | ASALocalRun/ 340 | 341 | # MSBuild Binary and Structured Log 342 | *.binlog 343 | 344 | # NVidia Nsight GPU debugger configuration file 345 | *.nvuser 346 | 347 | # MFractors (Xamarin productivity tool) working folder 348 | .mfractor/ 349 | 350 | # Local History for Visual Studio 351 | .localhistory/ 352 | 353 | # BeatPulse healthcheck temp database 354 | healthchecksdb 355 | 356 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 357 | MigrationBackup/ 358 | 359 | # Ionide (cross platform F# VS Code tools) working folder 360 | .ionide/ 361 | 362 | # Fody - auto-generated XML schema 363 | FodyWeavers.xsd -------------------------------------------------------------------------------- /ExtensibilityForMasses.Test/ExpTests.cs: -------------------------------------------------------------------------------- 1 | using Ploeh.Study.ExtensibilityForMasses; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.IO; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | using Xunit; 9 | 10 | namespace ExtensibilityForMasses.Test 11 | { 12 | public sealed class ExpTests 13 | { 14 | [Theory] 15 | [InlineData(-1)] 16 | [InlineData(00)] 17 | [InlineData(01)] 18 | [InlineData(11)] 19 | public void EvalLit(int x) 20 | { 21 | var sut = new Lit(x); 22 | var actual = sut.Eval().Int; 23 | Assert.Equal(x, actual); 24 | } 25 | 26 | [Theory] 27 | [InlineData(-1, 00, -1)] 28 | [InlineData(-1, 01, 00)] 29 | [InlineData(00, 00, 00)] 30 | [InlineData(00, 01, 01)] 31 | [InlineData(01, 01, 02)] 32 | [InlineData(07, 03, 10)] 33 | public void EvalSimpleAddition(int l, int r, int expected) 34 | { 35 | var sut = new Add(new Lit(l), new Lit(r)); 36 | var actual = sut.Eval().Int; 37 | Assert.Equal(expected, actual); 38 | } 39 | 40 | [Theory] 41 | [InlineData(-1, 00, 00, -1)] 42 | [InlineData(00, 00, 00, 00)] 43 | [InlineData(01, 00, 00, 01)] 44 | [InlineData(01, 02, 00, 03)] 45 | [InlineData(01, 02, 04, 07)] 46 | public void EvalNestedAddition(int x, int y, int z, int expected) 47 | { 48 | var sut = new Add(new Add(new Lit(x), new Lit(y)), new Lit(z)); 49 | var actual = sut.Eval().Int; 50 | Assert.Equal(expected, actual); 51 | } 52 | 53 | [Theory] 54 | [InlineData(-1, "-1")] 55 | [InlineData(00, "0")] 56 | [InlineData(10, "10")] 57 | public void PrintLit(int x, string expected) 58 | { 59 | var sut = new Print2(); 60 | var actual = sut.Lit(x); 61 | Assert.Equal(expected, actual); 62 | } 63 | 64 | [Theory] 65 | [InlineData(-1, 00, 00, "-1 + 0 + 0")] 66 | [InlineData(00, 00, 00, "0 + 0 + 0")] 67 | [InlineData(01, 00, 00, "1 + 0 + 0")] 68 | [InlineData(01, 02, 00, "1 + 2 + 0")] 69 | [InlineData(01, 02, 04, "1 + 2 + 4")] 70 | [InlineData(11, 03, 04, "11 + 3 + 4")] 71 | public void PrintAddition(int x, int y, int z, string expected) 72 | { 73 | var sut = new Print2(); 74 | var actual = sut.Add(sut.Lit(x), sut.Add(sut.Lit(y), sut.Lit(z))); 75 | Assert.Equal(expected, actual); 76 | } 77 | 78 | [Fact] 79 | public void Test3Plus5() 80 | { 81 | IExp e = IntAlg.Make3Plus5(new IntFactory()); 82 | var actual = e.Eval().Int; 83 | Assert.Equal(3 + 5, actual); 84 | } 85 | 86 | [Fact] 87 | public void UseMake3Plus5AsExtensionMethod() 88 | { 89 | var actual = new Print2().Make3Plus5(); 90 | Assert.Equal("3 + 5", actual); 91 | } 92 | 93 | [Theory] 94 | [InlineData("-98", -98)] 95 | [InlineData("-04", -04)] 96 | [InlineData("000", 000)] 97 | [InlineData("002", 002)] 98 | [InlineData("042", 042)] 99 | [InlineData("119", 119)] 100 | public void ParseIntegers(string s, int expected) 101 | { 102 | var actual = new EvalIntAlg().ParseExp(s); 103 | Assert.Equal(expected, actual); 104 | } 105 | 106 | [Theory] 107 | [InlineData( "-1 + -2", -3)] 108 | [InlineData( "-1 + 2", 01)] 109 | [InlineData( " 0 + 0", 00)] 110 | [InlineData( " 0 + 2", 02)] 111 | [InlineData( "10 + 2", 12)] 112 | [InlineData( "19 + 23", 42)] 113 | [InlineData("1 + 2 + 3", 06)] 114 | public void ParseAndEvaluateSums(string s, int expected) 115 | { 116 | var actual = new EvalIntAlg().ParseExp(s); 117 | Assert.Equal(expected, actual); 118 | } 119 | 120 | [Theory] 121 | [InlineData(-4, "-4")] 122 | [InlineData(00, "0")] 123 | [InlineData(07, "7")] 124 | [InlineData(24, "24")] 125 | public void PrintLitViaInterface(int x, string expected) 126 | { 127 | var sut = new IntPrint(); 128 | var actual = sut.Lit(x).Print(); 129 | Assert.Equal(expected, actual); 130 | } 131 | 132 | [Theory] 133 | [InlineData(-2, -4, "-2 + -4")] 134 | [InlineData(-3, 17, "-3 + 17")] 135 | [InlineData(88, 91, "88 + 91")] 136 | public void PrintSumViaInterface(int x, int y, string expected) 137 | { 138 | var sut = new IntPrint(); 139 | var actual = sut.Add(sut.Lit(x), sut.Lit(y)).Print(); 140 | Assert.Equal(expected, actual); 141 | } 142 | 143 | [Fact] 144 | public void Use3Plus4WithPrintInterface() 145 | { 146 | var @base = new IntFactory(); 147 | var print = new IntPrint(); 148 | 149 | var x = @base.Make3Plus4().Eval().Int; 150 | var s = print.Make3Plus4().Print(); 151 | 152 | Assert.Equal(7, x); 153 | Assert.Equal("3 + 4", s); 154 | } 155 | 156 | [Fact] 157 | public void Print2Example() 158 | { 159 | var p = new Print2(); 160 | var s = p.Make3Plus4(); 161 | Assert.Equal("3 + 4", s); 162 | } 163 | 164 | [Theory] 165 | [InlineData( true, "True")] 166 | [InlineData(false, "False")] 167 | public void PrintBool(bool b, string expected) 168 | { 169 | var sut = new IntBoolPrint(); 170 | var actual = sut.Bool(b).Print(); 171 | Assert.Equal(expected, actual); 172 | } 173 | 174 | [Theory] 175 | [InlineData( true, true, true, "if (True) then True else True")] 176 | [InlineData(false, true, true, "if (False) then True else True")] 177 | [InlineData(false, false, true, "if (False) then False else True")] 178 | [InlineData(false, false, false, "if (False) then False else False")] 179 | public void PrintIff(bool b1, bool b2, bool b3, string expected) 180 | { 181 | var sut = new IntBoolPrint(); 182 | var actual = 183 | sut.Iff(sut.Bool(b1), sut.Bool(b2), sut.Bool(b3)).Print(); 184 | Assert.Equal(expected, actual); 185 | } 186 | 187 | [Fact] 188 | public void PrintNestedNonsense() 189 | { 190 | var p = new IntBoolPrint(); 191 | var five = p.Lit(5); 192 | var seven = p.Lit(7); 193 | var add = p.Add(five, seven); 194 | var innerIff = p.Iff(add, p.Bool(false), p.Lit(2)); 195 | var outerIff = p.Iff(innerIff, p.Lit(6), p.Bool(true)); 196 | 197 | var actual = outerIff.Print(); 198 | 199 | var expected = 200 | "if (if (5 + 7) then False else 2) then 6 else True"; 201 | Assert.Equal(expected, actual); 202 | } 203 | 204 | [Fact] 205 | public void FirstExampleInSection5Dot3() 206 | { 207 | static A exp(IIntAlg v) => v.Add(v.Lit(3), v.Lit(4)); 208 | var p2 = new IntBoolPrint(); 209 | 210 | var actual = exp(p2).Print(); 211 | 212 | Assert.Equal("3 + 4", actual); 213 | } 214 | 215 | [Fact] 216 | public void SecondExampleInSection5Dot3() 217 | { 218 | static A exp(IIntAlg v) => v.Add(v.Lit(3), v.Lit(4)); 219 | static A exp2(IIntBoolAlg v) => 220 | v.Iff(v.Bool(false), exp(v), v.Lit(0)); 221 | var p = new IntPrint(); 222 | 223 | // var _ = exp2(p).Print(); // Type error 224 | var actual = exp2(new IntBoolPrint()).Print(); 225 | 226 | Assert.Equal("if (False) then 3 + 4 else 0", actual); 227 | } 228 | 229 | [Theory] 230 | [InlineData(false)] 231 | [InlineData( true)] 232 | public void EvalBool(bool expected) 233 | { 234 | var sut = new Bool(expected); 235 | var actual = sut.Eval(); 236 | Assert.Equal(expected, actual.Bool); 237 | } 238 | 239 | [Theory] 240 | [InlineData(false, false, false, false)] 241 | [InlineData( true, false, false, false)] 242 | [InlineData(false, true, false, false)] 243 | [InlineData(false, false, true, true)] 244 | [InlineData( true, true, false, true)] 245 | [InlineData( true, true, true, true)] 246 | public void EvalIff(bool x, bool y, bool z, bool expected) 247 | { 248 | var sut = new Iff(new Bool(x), new Bool(y), new Bool(z)); 249 | var actual = sut.Eval(); 250 | Assert.Equal(expected, actual.Bool); 251 | } 252 | 253 | [Theory] 254 | [InlineData("foo", -1)] 255 | [InlineData("foo", 00)] 256 | [InlineData("bar", 42)] 257 | public void AssignLitVar(string name, int expected) 258 | { 259 | var sut = new StmtFactory(); 260 | 261 | var evaluatedExp = sut.Assign(name, sut.Lit(expected)); 262 | var actual = sut.Var(name); 263 | 264 | Assert.Equal(expected, evaluatedExp.Eval().Int); 265 | Assert.Equal(expected, actual.Eval().Int); 266 | } 267 | 268 | [Fact] 269 | public void FirstExampleInSection6Dot2() 270 | { 271 | static E exp(IStmtAlg v) => 272 | v.Assign("x", v.Add(v.Lit(3), v.Lit(4))); 273 | static S stmt(IStmtAlg v) => 274 | v.Comp(v.Expr(exp(v)), v.Expr(v.Var("x"))); 275 | //S badStmt(IStmtAlg v) => 276 | // v.Comp(exp(v), v.Var("x")); // Type error 277 | 278 | var factory = new StmtFactory(); 279 | exp(factory).Eval(); 280 | stmt(factory).Eval(); 281 | 282 | Assert.Equal(7, factory.Var("x").Eval().Int); 283 | } 284 | 285 | [Fact] 286 | public void UseIntBoolFactory2() 287 | { 288 | var sut = new IntBoolFactory2(); 289 | var actual = 290 | sut.Iff( 291 | sut.Bool(false), 292 | sut.Lit(5), 293 | sut.Add(sut.Lit(3), sut.Lit(8))); 294 | Assert.Equal(11, actual.Eval().Int); 295 | } 296 | 297 | [Fact] 298 | public void CombineExample() 299 | { 300 | var sut = 301 | new Combine(new EvalIntAlg(), new Print2()); 302 | 303 | var actual = 304 | sut.Add( 305 | new Pair( 42, "foo"), 306 | new Pair(1337, "bar")); 307 | 308 | Assert.Equal(1379, actual.TheA); 309 | Assert.Equal("foo + bar", actual.TheB); 310 | } 311 | 312 | [Fact] 313 | public void DebugExample() 314 | { 315 | var sut = new Debug(); 316 | var e1 = sut.Lit( 2112); 317 | var e2 = sut.Add(sut.Lit(90125), sut.Lit(5150)); 318 | var originalOut = Console.Out; 319 | using var sw = new StringWriter(); 320 | Console.SetOut(sw); 321 | try 322 | { 323 | var actual = sut.Add(e1, e2); 324 | 325 | Assert.Equal( 326 | "The first expression 2112 evaluates to 2112" + Environment.NewLine + 327 | "The second expression 90125 + 5150 evaluates to 95275" + Environment.NewLine, 328 | sw.ToString()); 329 | } 330 | finally 331 | { 332 | Console.SetOut(originalOut); 333 | } 334 | } 335 | 336 | /* This test attempts to demonstrate that GUnion not only works in its 337 | * own right, but that it can be used to extend the interfaces it 338 | * composes. 339 | * In order to demonstrate that, I've 'extended' the IBoolAlg 340 | * interface, and then created a new union of interfaces called 341 | * IIntBoolExtra. The derived class GUnionExtra only has to implement 342 | * the extra method, while it inherits all the base implementations 343 | * from GUnion. 344 | * I think that's the point of that class... */ 345 | [Fact] 346 | public void GUnionBoolExample() 347 | { 348 | var sut = new GUnionExtra( 349 | new BoolExtraFactory(), 350 | new IntFactory()); 351 | 352 | Assert.Equal("foo", sut.ExtraMember()); 353 | Assert.False(sut.Bool(false).Eval().Bool); 354 | Assert.True(sut.Bool(true).Eval().Bool); 355 | Assert.True(sut.Iff( 356 | sut.Bool(false), 357 | sut.Bool(false), 358 | sut.Bool(true)).Eval().Bool); 359 | Assert.Equal(42, sut.Lit(42).Eval().Int); 360 | Assert.Equal( 361 | 9, 362 | sut.Add(sut.Lit(3), sut.Lit(6)).Eval().Int); 363 | } 364 | 365 | private interface IBoolAlgExtra : IBoolAlg 366 | { 367 | string ExtraMember(); 368 | } 369 | 370 | private class BoolExtraFactory : BoolFactory, IBoolAlgExtra 371 | { 372 | public string ExtraMember() 373 | { 374 | return "foo"; 375 | } 376 | } 377 | 378 | private interface IIntBoolExtra : IIntAlg, IBoolAlgExtra 379 | { 380 | } 381 | 382 | private class GUnionExtra : 383 | GUnion, IIntAlg>, IIntBoolExtra 384 | { 385 | private readonly IBoolAlgExtra v1; 386 | 387 | public GUnionExtra(IBoolAlgExtra v1, IIntAlg v2) : 388 | base(v1, v2) 389 | { 390 | this.v1 = v1; 391 | } 392 | 393 | public string ExtraMember() 394 | { 395 | return v1.ExtraMember(); 396 | } 397 | } 398 | 399 | [Fact] 400 | public void GCombineExample() 401 | { 402 | var sut = new CombineMonoid(); 403 | 404 | Assert.Equal(new Pair(0, ""), sut.Identity); 405 | Assert.Equal(new Pair(48, "48"), sut.Lit(48)); 406 | Assert.Equal( 407 | new Pair(42, "40 + 2"), 408 | sut.Add(sut.Lit(40), sut.Lit(2))); 409 | 410 | } 411 | 412 | private interface IIntMonoid : IIntAlg 413 | { 414 | A Identity { get; } 415 | } 416 | 417 | private class EvalIntMonoid : EvalIntAlg, IIntMonoid 418 | { 419 | public int Identity => 0; 420 | } 421 | 422 | private class Print2Monoid : Print2, IIntMonoid 423 | { 424 | public string Identity => ""; 425 | } 426 | 427 | private class CombineMonoid : 428 | GCombine, IIntMonoid>, 429 | IIntMonoid> 430 | { 431 | private readonly IIntMonoid v1; 432 | private readonly IIntMonoid v2; 433 | 434 | public CombineMonoid() : 435 | this(new EvalIntMonoid(), new Print2Monoid()) 436 | { 437 | } 438 | 439 | private CombineMonoid(IIntMonoid v1, IIntMonoid v2) : 440 | base(v1, v2) 441 | { 442 | this.v1 = v1; 443 | this.v2 = v2; 444 | } 445 | 446 | public Pair Identity => new(v1.Identity, v2.Identity); 447 | } 448 | 449 | [Fact] 450 | public void EvalExample() 451 | { 452 | var sut = new InvertibleEval(new InvertibleIntVal()); 453 | 454 | var actual = sut.Add(sut.Lit(4), sut.Lit(7)); 455 | var inverted = sut.Invert(actual); 456 | 457 | Assert.Equal(11, actual.Int); 458 | Assert.Equal(-11, inverted.Int); 459 | Assert.Equal("xxxxxxxxxxx", actual.Repeat('x')); 460 | } 461 | 462 | private class InvertibleEval : 463 | Eval>, 464 | IInvertibleVal 465 | { 466 | public InvertibleEval(IInvertibleVal valFact) : 467 | base(valFact) 468 | { 469 | } 470 | 471 | public ICharRepeater Invert(ICharRepeater v) 472 | { 473 | return valFact.Invert(v); 474 | } 475 | } 476 | 477 | private class IntValue : IIntValue 478 | { 479 | protected readonly int x; 480 | 481 | public IntValue(int x) 482 | { 483 | this.x = x; 484 | } 485 | 486 | public int Int => x; 487 | } 488 | 489 | private interface ICharRepeater : IIntValue 490 | { 491 | string Repeat(char c); 492 | } 493 | 494 | private class CharRepeater : IntValue, ICharRepeater 495 | { 496 | public CharRepeater(int x) : base(x) 497 | { 498 | } 499 | 500 | public string Repeat(char c) 501 | { 502 | return new string(c, x); 503 | } 504 | } 505 | 506 | private class IntValueIntVal : IIntVal 507 | { 508 | public ICharRepeater Lit(int x) 509 | { 510 | return new CharRepeater(x); 511 | } 512 | } 513 | 514 | private interface IInvertibleVal : IIntVal 515 | { 516 | A Invert(A v); 517 | } 518 | 519 | private class InvertibleIntVal : 520 | IntValueIntVal, IInvertibleVal 521 | { 522 | public ICharRepeater Invert(ICharRepeater v) 523 | { 524 | return Lit(-v.Int); 525 | } 526 | } 527 | } 528 | } 529 | --------------------------------------------------------------------------------