public IType GetLastCommonAncestorOrNull(IType otherType) { var arrayType = otherType as Array; if (arrayType == null) { return(Primitive.Any); } var elementTypeA = Element as IType; if (elementTypeA == null) { return(null); } var elementTypeB = arrayType.Element as IType; if (elementTypeB == null) { return(null); } var ancestor = elementTypeA.GetLastCommonAncestorOrNull(elementTypeB); if (ancestor == null) { return(null); } return(Array.Of(ancestor)); }
public void GetLastCommonAncestorOrNull_ConcreteFunTypeAndConstrainsArray_ReturnsAny() { var fun = Fun.Of(Primitive.I32, Primitive.I64); var array = Array.Of(CreateConstrainsNode()); Assert.AreEqual(Primitive.Any, fun.GetLastCommonAncestorOrNull(array)); Assert.AreEqual(Primitive.Any, array.GetLastCommonAncestorOrNull(fun)); }
static void ConcreteConcatEx() { // 2 0 1 //a:int[]; y = concat(a,b) var graph = new GraphBuilder(); graph.SetVarType("a", Array.Of(Primitive.I32)); graph.SetVar("a", 0); graph.SetVar("b", 1); graph.SetConcatCall(0, 1, 2); graph.SetDef("y", 2); var result = graph.Solve(); }
public void Array_referencesItself() { // 1 0 //x = reverse(x) var graph = new GraphBuilder(); graph.SetVar("x", 0); graph.SetReverse(0, 1); graph.SetDef("x", 1); var result = graph.Solve(); var res = result.AssertAndGetSingleGeneric(null, null); result.AssertNamed(Array.Of(res), "x"); }
public void ConcreteArg() { // 2 0 //x:int[]; y = sum(x) var graph = new GraphBuilder(); graph.SetVarType("x", Array.Of(Primitive.I32)); graph.SetVar("x", 0); graph.SetSumCall(0, 1); graph.SetDef("y", 1); var result = graph.Solve(); result.AssertNoGenerics(); result.AssertNamedEqualToArrayOf(Primitive.I32, "x"); result.AssertNamed(Primitive.I32, "y"); }
public void ConcreteCall() { // 1 0 //y = NoNans(x) var graph = new GraphBuilder(); graph.SetVar("x", 0); graph.SetCall(new IState[] { Array.Of(Primitive.Real), Primitive.Bool }, new [] { 0, 1 }); graph.SetDef("y", 1); var result = graph.Solve(); result.AssertNoGenerics(); result.AssertNamed(Array.Of(Primitive.Real), "x"); result.AssertNamed(Primitive.Bool, "y"); }
public void Anything_WithConcreteFun() { // 2 0 1 //y = Any(a, isNan) var graph = new GraphBuilder(); graph.SetVar("a", 0); graph.SetVarType("isNan", Fun.Of(Primitive.Real, Primitive.Bool)); graph.SetVar("isNan", 1); graph.SetIsAny(0, 1, 2); graph.SetDef("y", 2); var result = graph.Solve(); result.AssertNoGenerics(); result.AssertNamed(Array.Of(Primitive.Real), "a"); }
public void ConcreteFunAndUpcast() { // 2 0 1 //a:int[]; y = First(a, isNan) var graph = new GraphBuilder(); graph.SetVarType("a", Array.Of(Primitive.I32)); graph.SetVar("a", 0); graph.SetVarType("isNan", Fun.Of(Primitive.Real, Primitive.Bool)); graph.SetVar("isNan", 1); graph.SetGetFirst(0, 1, 2); graph.SetDef("y", 2); var result = graph.Solve(); result.AssertNoGenerics(); }
public void Array_referencesItselfManyTimes() { // 4 0 3 1 2 //x = concat(x,concat(x,x) var graph = new GraphBuilder(); graph.SetVar("x", 0); graph.SetVar("x", 1); graph.SetVar("x", 2); graph.SetConcatCall(1, 2, 3); graph.SetConcatCall(0, 3, 4); graph.SetDef("x", 4); var result = graph.Solve(); var res = result.AssertAndGetSingleGeneric(null, null); result.AssertNamed(Array.Of(res), "x"); }
public void ConcreteFun() { // 2 0 1 //y = Map(a, SQRT) var graph = new GraphBuilder(); graph.SetVar("a", 0); graph.SetVarType("SQRT", Fun.Of(Primitive.Real, Primitive.Real)); graph.SetVar("SQRT", 1); graph.SetMap(0, 1, 2); graph.SetDef("y", 2); var result = graph.Solve(); result.AssertNoGenerics(); result.AssertNamed(Array.Of(Primitive.Real), "a", "y"); }
public void ConcreteArg() { // 2 0,1 //x:int[]; y = get(x,0) var graph = new GraphBuilder(); graph.SetVarType("x", Array.Of(Primitive.I32)); graph.SetVar("x", 0); graph.SetConst(1, Primitive.I32); graph.SetArrGetCall(0, 1, 2); graph.SetDef("y", 2); var result = graph.Solve(); result.AssertNoGenerics(); result.AssertNamedEqualToArrayOf(Primitive.I32, "x"); result.AssertNamed(Primitive.I32, "y"); }
public void Array_referencesItselfWithMap_RetTypeIsConcrete() { // 0 5 4 132 //x = x.map(f(a):i64=a+1) var graph = new GraphBuilder(); graph.SetVar("x", 0); graph.SetVar("la", 1); graph.SetIntConst(2, Primitive.U8); graph.SetArith(1, 2, 3); graph.CreateLambda(3, 4, Primitive.I64, "la"); graph.SetMap(0, 4, 5); graph.SetDef("x", 5); var result = graph.Solve(); result.AssertNoGenerics(); result.AssertNamed(Array.Of(Primitive.I64), "x"); }
public void Array_referencesItselfWithMap() { // 0 5 4 132 //x = x.map(f(a)=a+1) var graph = new GraphBuilder(); graph.SetVar("x", 0); graph.SetVar("la", 1); graph.SetIntConst(2, Primitive.U8); graph.SetArith(1, 2, 3); graph.CreateLambda(3, 4, "la"); graph.SetMap(0, 4, 5); graph.SetDef("x", 5); var result = graph.Solve(); var res = result.AssertAndGetSingleArithGeneric(); result.AssertNamed(Array.Of(res), "x"); }
public void Anything_WithBoolArray() { // 3 0 2 1 //y = Any(a, x->x) var graph = new GraphBuilder(); graph.SetVar("a", 0); graph.SetVar("2lx", 1); graph.CreateLambda(1, 2, "2lx"); graph.SetIsAny(0, 2, 3); graph.SetDef("y", 3); var result = graph.Solve(); result.AssertNoGenerics(); result.AssertNamed(Array.Of(Primitive.Bool), "a"); result.AssertNamed(Primitive.Bool, "2lx"); result.AssertNode(Fun.Of(Primitive.Bool, Primitive.Bool), 2); }
public void TwoDimentions_Generic() { // 4 2 0,1 3 //y = get(get(x,0),0) var graph = new GraphBuilder(); graph.SetVar("x", 0); graph.SetConst(1, Primitive.I32); graph.SetArrGetCall(0, 1, 2); graph.SetConst(3, Primitive.I32); graph.SetArrGetCall(2, 3, 4); graph.SetDef("y", 4); var result = graph.Solve(); var generic = result.AssertAndGetSingleGeneric(null, null); result.AssertNamed(Array.Of(new Array(generic)), "x"); result.AssertAreGenerics(generic, "y"); }
public void ArrayCycle_Solved() { // 4 2 0 1 3 //x = [ get(x,0) , 1] var graph = new GraphBuilder(); graph.SetVar("x", 0); graph.SetIntConst(1, Primitive.U8); graph.SetArrGetCall(0, 1, 2); graph.SetIntConst(3, Primitive.U8); graph.SetArrayInit(4, 2, 3); graph.SetDef("x", 4); var res = graph.Solve(); var t = res.AssertAndGetSingleGeneric(Primitive.U8, Primitive.Real); res.AssertNamed(Array.Of(t), "x"); }
public void TwoDimentions_ConcreteArg() { // 4 2 0,1 3 //x:int[][]; y = get(get(x,0),0) var graph = new GraphBuilder(); graph.SetVarType("x", Array.Of(Array.Of(Primitive.I32))); graph.SetVar("x", 0); graph.SetConst(1, Primitive.I32); graph.SetArrGetCall(0, 1, 2); graph.SetConst(3, Primitive.I32); graph.SetArrGetCall(2, 3, 4); graph.SetDef("y", 4); var result = graph.Solve(); result.AssertNoGenerics(); result.AssertNamed(Array.Of(Array.Of(Primitive.I32)), "x"); result.AssertNamed(Primitive.I32, "y"); }
public void ThreeDimentions_ConcreteDefArrayOf() { // 4 2 0,1 3 //y:real[] = get(get(x,0),0) var graph = new GraphBuilder(); graph.SetVar("x", 0); graph.SetConst(1, Primitive.I32); graph.SetArrGetCall(0, 1, 2); graph.SetConst(3, Primitive.I32); graph.SetArrGetCall(2, 3, 4); graph.SetVarType("y", Array.Of(Primitive.Real)); graph.SetDef("y", 4); var result = graph.Solve(); result.AssertNoGenerics(); result.AssertNamed(Array.Of(Array.Of(Array.Of(Primitive.Real))), "x"); result.AssertNamed(Array.Of(Primitive.Real), "y"); }
public void Generic() { // 3 0 2 1 //y = Map(a, x->x) var graph = new GraphBuilder(); graph.SetVar("a", 0); graph.SetVar("2lx", 1); graph.CreateLambda(1, 2, "2lx"); graph.SetMap(0, 2, 3); graph.SetDef("y", 3); var result = graph.Solve(); var t = result.AssertAndGetSingleGeneric(null, null); result.AssertNamed(Array.Of(t), "a", "y"); result.AssertNode(Fun.Of(t, t)); }
//[Ignore("Upcast for complex types")] public void ConcreteCall_WithGenericArray() { // 3 2 0 1 //y = NoNans( [ 1, -1]) var graph = new GraphBuilder(); graph.SetIntConst(0, Primitive.U8); graph.SetIntConst(1, Primitive.I16); graph.SetArrayInit(2, 0, 1); graph.SetCall(new IState[] { Array.Of(Primitive.Real), Primitive.Bool }, new[] { 2, 3 }); graph.SetDef("y", 3); var result = graph.Solve(); result.AssertNoGenerics(); result.AssertNode(Array.Of(Primitive.Real), 2); result.AssertNode(Primitive.Real, 0, 1); result.AssertNamed(Primitive.Bool, "y"); }
public void Impossible_ConcreteArgAndDef_throws() { try { // 1 0 //x:real[]; y:int = sum(x) var graph = new GraphBuilder(); graph.SetVarType("x", Array.Of(Primitive.Real)); graph.SetVar("x", 0); graph.SetSumCall(0, 1); graph.SetVarType("y", Primitive.I32); graph.SetDef("y", 1); var result = graph.Solve(); Assert.Fail("Impossible equation solved"); } catch (Exception e) { Console.WriteLine(e); } }
public void ConcreteArgAndDef_Impossible() { try { // 2 0,1 //x:real[]; y:int = get(x,0) var graph = new GraphBuilder(); graph.SetVarType("x", Array.Of(Primitive.Real)); graph.SetVar("x", 0); graph.SetConst(1, Primitive.I32); graph.SetArrGetCall(0, 1, 2); graph.SetVarType("y", Primitive.I32); graph.SetDef("y", 2); var result = graph.Solve(); Assert.Fail("Impossible equation solved"); } catch (Exception e) { Console.WriteLine(e); } }
public void ConcreteOutput() { // 3 0 2 1 //y:u16[] = Map(a, x->x) var graph = new GraphBuilder(); graph.SetVar("a", 0); graph.SetVar("2lx", 1); graph.CreateLambda(1, 2, "2lx"); graph.SetMap(0, 2, 3); graph.SetVarType("y", Array.Of(Primitive.U16)); graph.SetDef("y", 3); var result = graph.Solve(); result.AssertNoGenerics(); result.AssertNamed(Array.Of(Primitive.U16), "a", "y"); result.AssertNode(Fun.Of(Primitive.U16, Primitive.U16)); }
public void ImpossibleArgType_Throws() { // 1 0 //x:Any[]; y = NoNans(x) var graph = new GraphBuilder(); graph.SetVarType("x", Array.Of(Primitive.Any)); graph.SetVar("x", 0); try { graph.SetCall(new IState[] { Array.Of(Primitive.Real), Primitive.Bool }, new[] { 0, 1 }); graph.SetDef("y", 1); graph.Solve(); Assert.Fail(); } catch (Exception e) { Console.WriteLine(e); } }
public void StrictArrayArg() { // 6 1 0 5 2 4 3 //y = map([ 1i ], x->x == 0) var graph = new GraphBuilder(); graph.SetConst(0, Primitive.I32); graph.SetArrayInit(1, 0); graph.SetVar("lx", 2); graph.SetIntConst(3, Primitive.U8); graph.SetEquality(2, 3, 4); graph.CreateLambda(4, 5, "lx"); graph.SetMap(1, 5, 6); graph.SetDef("y", 6); var result = graph.Solve(); result.AssertNoGenerics(); result.AssertNamed(Array.Of(Primitive.Bool), "y"); result.AssertNamed(Primitive.I32, "lx"); result.AssertNode(Fun.Of(Primitive.I32, Primitive.Bool), 5); }
public void TwoDimentions_ImpossibleConcreteArgAndDef() { // 4 2 0,1 3 //x:int[][]; y:i16 = get(get(x,0),0) var graph = new GraphBuilder(); graph.SetVarType("x", Array.Of(Array.Of(Primitive.I32))); graph.SetVar("x", 0); graph.SetConst(1, Primitive.I32); graph.SetArrGetCall(0, 1, 2); graph.SetConst(3, Primitive.I32); graph.SetArrGetCall(2, 3, 4); try { graph.SetVarType("y", Primitive.I16); graph.SetDef("y", 4); graph.Solve(); Assert.Fail(); } catch (Exception e) { Console.WriteLine(e); } }
public void ConcreteLambdaReturn() { // 6 1 0 5 2 4 3 //y = Map([ 1i ], (x):real->x*2) var graph = new GraphBuilder(); graph.SetConst(0, Primitive.I32); graph.SetArrayInit(1, 0); graph.SetVar("lx", 2); graph.SetIntConst(3, Primitive.U8); graph.SetArith(2, 3, 4); graph.CreateLambda(4, 5, Primitive.Real, "lx"); graph.SetMap(1, 5, 6); graph.SetDef("y", 6); var result = graph.Solve(); result.AssertNoGenerics(); result.AssertNamed(Primitive.I32, "lx"); result.AssertNamed(Array.Of(Primitive.Real), "y"); result.AssertNode(Fun.Of(Primitive.I32, Primitive.Real), 5); }
public void GenericReduce_GetSum() { // 5 0 4 132 //y = reduce(x, f(a,b)=a+b) var graph = new GraphBuilder(); graph.SetVar("x", 0); graph.SetVar("la", 1); graph.SetVar("lb", 2); graph.SetArith(1, 2, 3); graph.CreateLambda(3, 4, "la", "lb"); graph.SetReduceCall(0, 4, 5); graph.SetDef("y", 5); var result = graph.Solve(); var t = result.AssertAndGetSingleArithGeneric(); result.AssertAreGenerics(t, "y", "la", "lb"); result.AssertNamed(Array.Of(t), "x"); result.AssertNode(Fun.Of(new [] { t, t }, t), 4); }
//[Ignore("Input variable generic")] public void GenericFold_AllIsNan() { // 6 0 5 1 4 3 2 //y = fold(x, f(a,b)=a and isNan(b)) var graph = new GraphBuilder(); graph.SetVar("x", 0); graph.SetVar("la", 1); graph.SetVar("lb", 2); graph.SetCall(new [] { Primitive.Real, Primitive.Bool }, new [] { 2, 3 }); graph.SetBoolCall(1, 3, 4); graph.CreateLambda(4, 5, "la", "lb"); graph.SetFoldCall(0, 5, 6); graph.SetDef("y", 6); var result = graph.Solve(); result.AssertNoGenerics(); result.AssertNamed(Array.Of(Primitive.Real), "x"); result.AssertNamed(Primitive.Real, "lb"); result.AssertNamed(Primitive.Bool, "la", "y"); }
public void ReduceConcreteArg_GetSum() { // 5 0 4 132 //x:u32[]; y = reduce(x, f(a,b)=a+b) var graph = new GraphBuilder(); graph.SetVarType("y", Primitive.U32); graph.SetVar("x", 0); graph.SetVar("la", 1); graph.SetVar("lb", 2); graph.SetArith(1, 2, 3); graph.CreateLambda(3, 4, "la", "lb"); graph.SetReduceCall(0, 4, 5); graph.SetDef("y", 5); var result = graph.Solve(); result.AssertNoGenerics(); result.AssertNamed(Primitive.U32, "y", "la", "lb"); result.AssertNamed(Array.Of(Primitive.U32), "x"); result.AssertNode(Fun.Of(new[] { Primitive.U32, Primitive.U32 }, Primitive.U32), 4); }