예제 #1
0
파일: CallTests.cs 프로젝트: plioi/rook
        public void HasATypeEqualToTheInferredReturnTypeOfGenericCallableObjects()
        {
            var x = new TypeVariable(123456);

            Type("func([1, 2, 3])", func => Function(new[] { NamedType.Vector(x) }, x)).ShouldEqual(Integer);
            Type("func([true, false])", func => Function(new[] { NamedType.Vector(x) }, x)).ShouldEqual(Boolean);
        }
예제 #2
0
 public TypeUnifierTests()
 {
     unifier = new TypeUnifier();
     x = new TypeVariable(0);
     y = new TypeVariable(1);
     z = new TypeVariable(2);
 }
예제 #3
0
        private DataType NormalizeVariable(TypeVariable variable)
        {
            if (substitutions.ContainsKey(variable))
            {
                DataType substitution = substitutions[variable];

                //We cannot simply return substitution, because it may be a complex
                //type containing type variables that have themselves been unified.
                //Instead, we must return the normalized version of that substitution.
                DataType normalizedSubstitution = Normalize(substitution);

                //Optimization: We could just return normalizedSubstitution, but if
                //If normalizing the substitution provided a better answer than the substitution
                //itself, we might as well store that better answer as the substitution going
                //forward.  This helps by shortening long chains so that they don't have to be
                //traversed again.
                if (normalizedSubstitution != substitution)
                    substitutions[variable] = normalizedSubstitution;

                return normalizedSubstitution;
            }
            return variable;
        }
예제 #4
0
파일: NamedType.cs 프로젝트: plioi/rook
 public override bool Contains(TypeVariable typeVariable)
 {
     return(genericArguments.Any(genericArgument => genericArgument.Contains(typeVariable)));
 }
예제 #5
0
파일: UnknownType.cs 프로젝트: plioi/rook
 public override bool Contains(TypeVariable typeVariable)
 {
     return false;
 }
예제 #6
0
 public override bool Contains(TypeVariable typeVariable)
 {
     return genericArguments.Any(genericArgument => genericArgument.Contains(typeVariable));
 }
예제 #7
0
 public TypeVariableTests()
 {
     a = new TypeVariable(0);
     b = new TypeVariable(1);
 }
예제 #8
0
 public override bool Contains(TypeVariable typeVariable)
 {
     return(false);
 }
예제 #9
0
 public void CanBeEitherGenericOrNonGeneric()
 {
     TypeVariable.CreateGeneric().IsGeneric.ShouldBeTrue();
     TypeVariable.CreateNonGeneric().IsGeneric.ShouldBeFalse();
 }
예제 #10
0
        public void CanPerformTypeVariableSubstitutions()
        {
            var a = new TypeVariable(0);
            var b = new TypeVariable(1);

            var replaceAWithInteger = new Dictionary<TypeVariable, DataType> { { a, NamedType.Integer } };
            var replaceBWithA = new Dictionary<TypeVariable, DataType> { { b, a } };
            var replaceBoth = new Dictionary<TypeVariable, DataType> { { a, NamedType.Integer }, { b, a } };

            var concrete = Create("A", Create("B"));
            concrete.ReplaceTypeVariables(replaceAWithInteger).ShouldEqual(concrete);
            concrete.ReplaceTypeVariables(replaceBWithA).ShouldEqual(concrete);
            concrete.ReplaceTypeVariables(replaceBoth).ShouldEqual(concrete);

            Create("A", a, b, a).ReplaceTypeVariables(replaceAWithInteger).ShouldEqual(Create("A", NamedType.Integer, b, NamedType.Integer));
            Create("B", b, a, b).ReplaceTypeVariables(replaceAWithInteger).ShouldEqual(Create("B", b, NamedType.Integer, b));

            Create("A", a, b, a).ReplaceTypeVariables(replaceBWithA).ShouldEqual(Create("A", a, a, a));
            Create("B", b, a, b).ReplaceTypeVariables(replaceBWithA).ShouldEqual(Create("B", a, a, a));

            //Unlike the type unification/normlization substitutions, these substitutions are ignorant of
            //chains like { b -> a, a -> int }.
            Create("A", a, b, a).ReplaceTypeVariables(replaceBoth).ShouldEqual(Create("A", NamedType.Integer, a, NamedType.Integer));
            Create("B", b, a, b).ReplaceTypeVariables(replaceBoth).ShouldEqual(Create("B", a, NamedType.Integer, a));
        }
예제 #11
0
        public void CanFindAllDistinctOccurrencesOfContainedTypeVariables()
        {
            var x = new TypeVariable(0);
            var y = new TypeVariable(1);
            var z = new TypeVariable(2);

            Create("A").FindTypeVariables().ShouldBeEmpty();
            Create("A", Create("B")).FindTypeVariables().ShouldBeEmpty();
            Create("A", x, y, z).FindTypeVariables().ShouldList(x, y, z);
            Create("A", Create("B", x, y), Create("C", y, z)).FindTypeVariables().ShouldList(x, y, z);
        }
예제 #12
0
        public void CanDetermineWhetherTheTypeContainsASpecificTypeVariable()
        {
            var x = new TypeVariable(12345);

            Create("A").Contains(x).ShouldBeFalse();
            Create("A", x).Contains(x).ShouldBeTrue();
            Create("A", Create("B", x)).Contains(x).ShouldBeTrue();
        }
예제 #13
0
        public void PerformsTypeVariableSubstitutionsAgainstNonGenericTypesByPerformingNoChanges()
        {
            var @class = "class Foo { int Square(int x) {x*x} }".ParseClass();

            var foo = new NamedType(@class, new TypeRegistry());

            var a = new TypeVariable(0);
            var replaceAWithInteger = new Dictionary<TypeVariable, DataType> { { a, NamedType.Integer } };

            var fooAfterSubstitutions = (NamedType)foo.ReplaceTypeVariables(replaceAWithInteger);

            fooAfterSubstitutions.ShouldBeSameAs(foo);
        }
예제 #14
0
 public void CanNeverBeAGenericTypeDefinition()
 {
     TypeVariable.CreateGeneric().IsGenericTypeDefinition.ShouldBeFalse();
     TypeVariable.CreateNonGeneric().IsGenericTypeDefinition.ShouldBeFalse();
 }
예제 #15
0
파일: TypeVariable.cs 프로젝트: plioi/rook
 public override bool Contains(TypeVariable typeVariable)
 {
     return typeVariable == this;
 }
예제 #16
0
 public abstract bool Contains(TypeVariable typeVariable);
예제 #17
0
 public override bool Contains(TypeVariable typeVariable)
 {
     return(typeVariable == this);
 }
예제 #18
0
 public TypeVariableTests()
 {
     a = new TypeVariable(0);
     b = new TypeVariable(1);
 }