public void fold_for() { // fun swapIfNotSorted(T_0[],Int32):T_0[] where T_0: <> // 5 21 0 3 4 //sorted = fold([0..5], input, swapIfNotSorted)"; var graph = new GraphBuilder(); graph.SetIntConst(0, StatePrimitive.U8); graph.SetIntConst(1, StatePrimitive.U8); graph.SetCall(new ITicNodeState[] { StatePrimitive.I32, StatePrimitive.I32, StateArray.Of(StatePrimitive.I32) }, new[] { 1, 0, 2 }); graph.SetVar("input", 3); var tOfSwap = graph.InitializeVarNode(isComparable: true); graph.SetVarType("swapIfNotSorted", StateFun.Of(new ITicNodeState[] { StateArray.Of(tOfSwap), StatePrimitive.I32 }, StateArray.Of(tOfSwap))); graph.SetVar("swapIfNotSorted", 4); graph.SetfoldCall(2, 3, 4, 5); graph.SetDef("sorted", 5); var result = graph.Solve(); var generic = result.AssertAndGetSingleGeneric(null, null, true); result.AssertNamed(StateArray.Of(generic), "sorted", "input"); }
public void ConcreteTypes_DifferentReturns_EqualsReturnsFalse() { var funA = StateFun.Of(StatePrimitive.Any, StatePrimitive.I32); var funB = StateFun.Of(StatePrimitive.Real, StatePrimitive.I32); Assert.IsFalse(funA.Equals(funB)); }
public void fold_foreachi_rangeIsFixed() { // fun swapIfNotSorted(T_0[],Int32):T_0[] where T_0: <> // 6 32 1 0 4 5 //sorted = fold([0..count(input)], input, swapIfNotSorted)"; var graph = new GraphBuilder(); graph.SetVar("input", 0); var tOfCount = graph.InitializeVarNode(); //count graph.SetCall(new ITicNodeState[] { StateArray.Of(tOfCount), StatePrimitive.I32 }, new[] { 0, 1 }); graph.SetIntConst(2, StatePrimitive.U8); //range graph.SetCall(new ITicNodeState[] { StatePrimitive.I32, StatePrimitive.I32, StateArray.Of(StatePrimitive.I32) }, new [] { 2, 1, 3 }); graph.SetVar("input", 4); var tOfSwap = graph.InitializeVarNode(isComparable: true); graph.SetVarType("swapIfNotSorted", StateFun.Of(new ITicNodeState[] { StateArray.Of(tOfSwap), StatePrimitive.I32 }, StateArray.Of(tOfSwap))); graph.SetVar("swapIfNotSorted", 5); graph.SetfoldCall(3, 4, 5, 6); graph.SetDef("sorted", 6); var result = graph.Solve(); var generic = result.AssertAndGetSingleGeneric(null, null, true); result.AssertNamed(StateArray.Of(generic), "sorted", "input"); }
public void ConcreteTypes_SameTypes_EqualsReturnsTrue() { var funA = StateFun.Of(StatePrimitive.Any, StatePrimitive.I32); var funB = StateFun.Of(StatePrimitive.Any, StatePrimitive.I32); Assert.IsTrue(funA.Equals(funB)); }
public void GetLastCommonAncestorOrNull_ConcreteFunTypesAndPrimitive_ReturnsAny() { var fun = StateFun.Of(StatePrimitive.I32, StatePrimitive.I64); Assert.AreEqual(StatePrimitive.Any, fun.GetLastCommonAncestorOrNull(StatePrimitive.I32)); Assert.AreEqual(StatePrimitive.Any, StatePrimitive.I32.GetLastCommonAncestorOrNull(fun)); }
public void GetLastCommonAncestorOrNull_ConcreteFunTypeAndConstrainsArray_ReturnsAny() { var fun = StateFun.Of(StatePrimitive.I32, StatePrimitive.I64); var array = StateArray.Of(CreateConstrainsNode()); Assert.AreEqual(StatePrimitive.Any, fun.GetLastCommonAncestorOrNull(array)); Assert.AreEqual(StatePrimitive.Any, array.GetLastCommonAncestorOrNull(fun)); }
public void GetLastCommonAncestorOrNull_ConcreteAndNotConcreteType_ReturnsNull() { var funA = StateFun.Of(CreateConstrainsNode(), TicNode.CreateTypeVariableNode(StatePrimitive.I32)); var funB = StateFun.Of(StatePrimitive.U16, StatePrimitive.U64); Assert.IsNull(funA.GetLastCommonAncestorOrNull(funB)); Assert.IsNull(funB.GetLastCommonAncestorOrNull(funA)); }
public void GetLastCommonAncestorOrNull_ConcreteType_ReturnsAncestor() { var funA = StateFun.Of(StatePrimitive.I32, StatePrimitive.I64); var funB = StateFun.Of(StatePrimitive.U16, StatePrimitive.U64); var expected = StateFun.Of(StatePrimitive.U16, StatePrimitive.I96); Assert.AreEqual(expected, funA.GetLastCommonAncestorOrNull(funB)); Assert.AreEqual(expected, funB.GetLastCommonAncestorOrNull(funA)); }
public void NonConcreteTypes_SameNodes_EqualsReturnsTrue() { var retNode = CreateConstrainsNode(); var argNode = CreateConstrainsNode(); var funA = StateFun.Of(retNode, argNode); var funB = StateFun.Of(retNode, argNode); Assert.IsTrue(funA.Equals(funB)); }
public static void SetMap(this GraphBuilder builder, int arrId, int funId, int resultId) { var inNode = builder.InitializeVarNode(); var outNode = builder.InitializeVarNode(); builder.SetCall(new ITicNodeState[] { StateArray.Of(inNode), StateFun.Of( returnType: outNode, argType: inNode), StateArray.Of(outNode) }, new [] { arrId, funId, resultId }); }
public static void SetfoldCall(this GraphBuilder graph, int arrId, int funId, int returnId) { var generic = graph.InitializeVarNode(); graph.SetCall(new ITicNodeState[] { StateArray.Of(generic), StateFun.Of(new[] { generic, generic }, generic), generic }, new[] { arrId, funId, returnId }); }
public void GetLastCommonAncestorOrNull_SameConcreteTypes_ReturnsEqualType() { var funA = StateFun.Of(StatePrimitive.Any, StatePrimitive.I32); var funB = StateFun.Of(StatePrimitive.Any, StatePrimitive.I32); var ancestor = funA.GetLastCommonAncestorOrNull(funB); Assert.AreEqual(funA, ancestor); var ancestor2 = funB.GetLastCommonAncestorOrNull(funA); Assert.AreEqual(ancestor2, ancestor); }
public void NonConcreteTypes_DifferentNodes_EqualsReturnsFalse() { var retNodeA = CreateConstrainsNode(); var retNodeB = CreateConstrainsNode(); var argNode = CreateConstrainsNode(); var funA = StateFun.Of(retNodeA, argNode); var funB = StateFun.Of(retNodeB, argNode); Assert.IsFalse(funA.Equals(funB)); }
public static void SetFoldCall(this GraphBuilder graph, int arrId, int funId, int returnId) { var inT = graph.InitializeVarNode(); var outT = graph.InitializeVarNode(); graph.SetCall(new ITicNodeState[] { StateArray.Of(inT), StateFun.Of(new[] { outT, inT }, outT), outT }, new[] { arrId, funId, returnId }); }
public static void SetGetFirst(this GraphBuilder builder, int arrId, int funId, int resultId) { var inNode = builder.InitializeVarNode(); if (inNode != null) { builder.SetCall(new ITicNodeState[] { StateArray.Of(inNode), StateFun.Of(returnType: StatePrimitive.Bool, argType: inNode), inNode }, new[] { arrId, funId, resultId }); } }
public static void SetfoldCall(this GraphBuilder graph, int arrId, int defId, int funId, int resId) { var tRes = graph.InitializeVarNode(); var tArg = graph.InitializeVarNode(); //fold call fold( T[], G, (G,T)->G )->G graph.SetCall(new ITicNodeState[] { StateArray.Of(tArg), tRes, StateFun.Of(new ITicNodeState[] { tRes, tArg }, tRes), tRes }, new[] { arrId, defId, funId, resId }); }
public void SortOneTimeUserFunction() { // fun swapIfNotSorted(T_0[],Int32):T_0[] where T_0: <> // 6 32 1 0 4 5 //sortOneTime(input) = fold([0..count(input)], input, swapIfNotSorted)"; //Exit: 16.Tvar input: Empty //Exit:19.IntConst 0:int //Exit:21.VAR input //Exit:20.Call count(21, 20) //Exit: 18.Call range(19, 20, 18) //Exit: 22.VAR input //Exit:23.VAR swapIfNotSorted //Exit:17.Call fold(18, 22, 23, 17) var graph = new GraphBuilder(); graph.SetVar("input", 0); var fundef = graph.SetFunDef("sortOneTime", 6, null, "input"); graph.SetSizeOfArrayCall(0, 1); //count graph.SetIntConst(2, StatePrimitive.U8); graph.SetRangeCall(2, 1, 3); //range graph.SetVar("input", 4); var tOfSwap = graph.InitializeVarNode(isComparable: true); graph.SetVarType("swapIfNotSorted", StateFun.Of(new ITicNodeState[] { SolvingStates.StateArray.Of(tOfSwap), StatePrimitive.I32 }, SolvingStates.StateArray.Of(tOfSwap))); graph.SetVar("swapIfNotSorted", 5); graph.SetfoldCall(3, 4, 5, 6); var result = graph.Solve(); var generic = result.AssertAndGetSingleGeneric(null, null, true); result.AssertNamed(SolvingStates.StateArray.Of(generic), "input"); var expectedType = StateFun.Of(StateArray.Of(generic.GetNonReference()), StateArray.Of(generic)); //todo //Assert.AreEqual(expectedType, result.GetVariableNode("sortOneTime").State.ToString()); result.AssertNamed( StateFun.Of(StateArray.Of(generic.GetNonReference()), StateArray.Of(generic)), "sortOneTime"); }
public void Anything_WithConcreteFunAndUpcast() { // 2 0 1 //a:int[]; y = Any(a, isNan) var graph = new GraphBuilder(); graph.SetVarType("a", StateArray.Of(StatePrimitive.I32)); graph.SetVar("a", 0); graph.SetVarType("isNan", StateFun.Of(StatePrimitive.Real, StatePrimitive.Bool)); graph.SetVar("isNan", 1); graph.SetIsAny(0, 1, 2); graph.SetDef("y", 2); var result = graph.Solve(); result.AssertNoGenerics(); }
public void ConcreteFun() { // 2 0 1 //y = First(a, isNan) var graph = new GraphBuilder(); graph.SetVar("a", 0); graph.SetVarType("isNan", StateFun.Of(StatePrimitive.Real, StatePrimitive.Bool)); graph.SetVar("isNan", 1); graph.SetGetFirst(0, 1, 2); graph.SetDef("y", 2); var result = graph.Solve(); result.AssertNoGenerics(); result.AssertNamed(StateArray.Of(StatePrimitive.Real), "a"); }
public void SortOneTimeUserFunction_WithSameOrderAsInNfun() { // fun swapIfNotSorted(T_0[],Int32):T_0[] where T_0: <> // 17 18 19 20 21 22 23 //sortOneTime(input) = fold( range(0, count(input)), input, swapIfNotSorted)"; //Nfun Trace: //Exit: 16.Tvar input: Empty //Exit:19.IntConst 0:int //Exit:21.VAR input //Exit:20.Call count(21, 20) //Exit: 18.Call range(19, 20, 18) //Exit: 22.VAR input //Exit:23.VAR swapIfNotSorted //Exit:17.Call fold(18, 22, 23, 17) var graph = new GraphBuilder(); var fundef = graph.SetFunDef("sortOneTime", 17, null, "input"); //Exit:19.IntConst 0:int graph.SetIntConst(19, StatePrimitive.U8); //Exit:21.VAR input graph.SetVar("input", 21); //Exit:20.Call count(21, 20) graph.SetSizeOfArrayCall(21, 20); //count //Exit: 18.Call range(19, 20, 18) graph.SetRangeCall(19, 20, 18); //range //Exit: 22.VAR input graph.SetVar("input", 22); //Exit:23.VAR swapIfNotSorted var tOfSwap = graph.InitializeVarNode(isComparable: true); graph.SetVarType("swapIfNotSorted", StateFun.Of(new ITicNodeState[] { SolvingStates.StateArray.Of(tOfSwap), StatePrimitive.I32 }, SolvingStates.StateArray.Of(tOfSwap))); graph.SetVar("swapIfNotSorted", 23); //Exit:17.Call fold(18, 22, 23, 17) graph.SetfoldCall(18, 22, 23, 17); var result = graph.Solve(); var generic = result.AssertAndGetSingleGeneric(null, null, true); result.AssertNamed(SolvingStates.StateArray.Of(generic), "input"); result.AssertNamed(StateFun.Of(SolvingStates.StateArray.Of(generic), SolvingStates.StateArray.Of(generic)), "sortOneTime"); }
public void DowncastCallOfFunVar() { // 1 0 //g: f(any):int; x = g(1.0) var graph = new GraphBuilder(); graph.SetVarType("g", StateFun.Of(StatePrimitive.Any, StatePrimitive.I32)); graph.SetConst(0, StatePrimitive.Real); graph.SetCall("g", 0, 1); graph.SetDef("x", 1); var result = graph.Solve(); result.AssertNoGenerics(); result.AssertNamed(StatePrimitive.I32, "x"); result.AssertNamed(StateFun.Of(StatePrimitive.Any, StatePrimitive.I32), "g"); }
public void ConcreteFun() { // 2 0 1 //y = Map(a, SQRT) var graph = new GraphBuilder(); graph.SetVar("a", 0); graph.SetVarType("SQRT", StateFun.Of(StatePrimitive.Real, StatePrimitive.Real)); graph.SetVar("SQRT", 1); graph.SetMap(0, 1, 2); graph.SetDef("y", 2); var result = graph.Solve(); result.AssertNoGenerics(); result.AssertNamed(StateArray.Of(StatePrimitive.Real), "a", "y"); }
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(StateArray.Of(StatePrimitive.Bool), "a"); result.AssertNamed(StatePrimitive.Bool, "2lx"); result.AssertNode(StateFun.Of(StatePrimitive.Bool, StatePrimitive.Bool), 2); }
public void GenericCallWithFunVar() { //fun = i->i // 1 0 //x = fun(2) var graph = new GraphBuilder(); var generic = graph.InitializeVarNode(); graph.SetIntConst(0, StatePrimitive.U8); graph.SetCall(StateFun.Of(generic, generic), new[] { 0, 1 }); graph.SetDef("x", 1); var result = graph.Solve(); var t = result.AssertAndGetSingleGeneric(StatePrimitive.U8, StatePrimitive.Real); result.AssertAreGenerics(t, "x"); }
public void FunDefCallTest_returnIsStrict() { // 1 0 //call(f,x):int = f(x) var graph = new GraphBuilder(); graph.SetVar("x", 0); graph.SetCall("f", 0, 1); graph.SetVarType("return", StatePrimitive.I32); graph.SetDef("return", 1); var result = graph.Solve(); var t = result.AssertAndGetSingleGeneric(null, null); result.AssertAreGenerics(t, "x"); result.AssertNamed(StateFun.Of(t, TicNode.CreateTypeVariableNode(StatePrimitive.I32)), "f"); result.AssertNamed(StatePrimitive.I32, "return"); }
public void FunDefCallTest_argIsStrict() { // 1 0 //call(f,x:int) = f(x) var graph = new GraphBuilder(); graph.SetVarType("x", StatePrimitive.I32); graph.SetVar("x", 0); graph.SetCall("f", 0, 1); graph.SetDef("return", 1); var result = graph.Solve(); var t = result.AssertAndGetSingleGeneric(null, null); result.AssertAreGenerics(t, "return"); result.AssertNamed(StateFun.Of(StatePrimitive.I32, new StateRefTo(t)), "f"); }
public void FunDefCallTest_strict() { // 1 0 //call(f,x:int):int = f(x) var graph = new GraphBuilder(); graph.SetVarType("x", StatePrimitive.I32); graph.SetVar("x", 0); graph.SetCall("f", 0, 1); graph.SetVarType("return", StatePrimitive.I32); graph.SetDef("return", 1); var result = graph.Solve(); result.AssertNoGenerics(); result.AssertNamed(StatePrimitive.I32, "x", "return"); result.AssertNamed(StateFun.Of(StatePrimitive.I32, StatePrimitive.I32), "f"); }
/// <summary> /// Transform constrains to fun state /// </summary> public static StateFun TransformToFunOrNull(object descNodeName, ConstrainsState descendant, StateFun ancestor) { if (descendant.NoConstrains) { var argNodes = new TicNode[ancestor.ArgsCount]; for (int i = 0; i < ancestor.ArgsCount; i++) { var argNode = TicNode.CreateTypeVariableNode("a'" + descNodeName + "'" + i, new ConstrainsState()); argNode.AddAncestor(ancestor.ArgNodes[i]); argNodes[i] = argNode; } var retNode = TicNode.CreateTypeVariableNode("r'" + descNodeName, new ConstrainsState()); retNode.AddAncestor(ancestor.RetNode); return(StateFun.Of(argNodes, retNode)); } if (descendant.Descedant is StateFun funDesc && funDesc.ArgsCount == ancestor.ArgsCount) { if (funDesc.IsSolved) { return(funDesc); } // For perfomance bool allArgsAreSolved = true; var nrArgNodes = new TicNode[funDesc.ArgNodes.Length]; for (int i = 0; i < funDesc.ArgNodes.Length; i++) { nrArgNodes[i] = funDesc.ArgNodes[i].GetNonReference(); allArgsAreSolved = allArgsAreSolved && nrArgNodes[i].IsSolved; } var nrRetNode = funDesc.RetNode.GetNonReference(); if (allArgsAreSolved && nrRetNode.IsSolved) { return(StateFun.Of(nrArgNodes, nrRetNode)); } } return(null); }
public void SequenceCallWithFunVar() { //myFun() = i->i // 2 0 1 //x = (myFun())(2) var graph = new GraphBuilder(); var generic = graph.InitializeVarNode(); graph.SetCall(StateFun.Of(new ITicNodeState[0], StateFun.Of(generic, generic)), 0); graph.SetIntConst(1, StatePrimitive.U8); graph.SetCall(0, new[] { 1, 2 }); graph.SetDef("x", 2); var result = graph.Solve(); var t = result.AssertAndGetSingleGeneric(StatePrimitive.U8, StatePrimitive.Real); result.AssertAreGenerics(t, "x"); }
public void Anything_WithStrictArrayArg() { // 6 1 0 5 2 4 3 //y = Any([ 1i ], x->x == 0) var graph = new GraphBuilder(); graph.SetConst(0, StatePrimitive.I32); graph.SetStrictArrayInit(1, 0); graph.SetVar("lx", 2); graph.SetIntConst(3, StatePrimitive.U8); graph.SetEquality(2, 3, 4); graph.CreateLambda(4, 5, "lx"); graph.SetIsAny(1, 5, 6); graph.SetDef("y", 6); var result = graph.Solve(); result.AssertNoGenerics(); result.AssertNamed(StatePrimitive.I32, "lx"); result.AssertNode(StateFun.Of(argType: StatePrimitive.I32, StatePrimitive.Bool), 5); }