Пример #1
0
        public void TestTypeclasses()
        {
            var initialState = new InferenceState(new FreshVariableStream(),
                                                  new TypeclassDeclaration("Eq",
                                                                           new TypeScheme(new TypeConstructor("int", new DataKind())),
                                                                           new TypeScheme(new QualifiedType(
                                                                                              new TypeApplication(new TypeConstructor("[]", new ArrowKind(new DataKind(), new DataKind())),
                                                                                                                  new TypeVariable("a", new DataKind())),
                                                                                              new Predicate("Eq", new TypeVariable("a", new DataKind()))), ("a", new DataKind()))),
                                                  new TypeclassDeclaration("Ord"),
                                                  new TypeclassDeclaration("Show"),
                                                  new TypeclassDeclaration("Read"),
                                                  new TypeclassDeclaration("Bounded"),
                                                  new TypeclassDeclaration("Enum"),
                                                  new TypeclassDeclaration("Functor"),
                                                  new TypeclassDeclaration("Monad"),
                                                  new TermVariableBinding("eq", new TypeScheme(new QualifiedType(
                                                                                                   PrimType.Fun(new TypeVariable("a", new DataKind()), new TypeVariable("a", new DataKind()), new TypeConstructor("bool", new DataKind())),
                                                                                                   new Predicate("Eq", new TypeVariable("a", new DataKind()))),
                                                                                               ("a", new DataKind()))),
                                                  new TermVariableBinding("zero", new TypeScheme(new QualifiedType(new TypeConstructor("int", new DataKind())))),
                                                  new TermVariableBinding("true", new TypeScheme(new QualifiedType(new TypeConstructor("bool", new DataKind())))),
                                                  new TermVariableBinding("show", new TypeScheme(new QualifiedType(
                                                                                                     PrimType.Fun(new TypeVariable("a", new DataKind()), new TypeConstructor("string", new DataKind())),
                                                                                                     new Predicate("Show", new TypeVariable("a", new DataKind()))),
                                                                                                 ("a", new DataKind()))),
                                                  new TermVariableBinding("read", new TypeScheme(new QualifiedType(
                                                                                                     PrimType.Fun(new TypeConstructor("string", new DataKind()), new TypeVariable("a", new DataKind())),
                                                                                                     new Predicate("Read", new TypeVariable("a", new DataKind()))),
                                                                                                 ("a", new DataKind()))));

            Assert.AreEqual(
                TypeInference.Infer(initialState, new TermVariable("eq")),
                new QualifiedType(PrimType.Fun(new TypeVariable("t0", new DataKind()), new TypeVariable("t0", new DataKind()), new TypeConstructor("bool", new DataKind())),
                                  new Predicate("Eq", new TypeVariable("t0", new DataKind()))));

            Assert.AreEqual(
                TypeInference.Infer(initialState, new Application(new TermVariable("eq"), new TermVariable("zero"))),
                new QualifiedType(PrimType.Fun(new TypeConstructor("int", new DataKind()), new TypeConstructor("bool", new DataKind()))));

            Assert.ThrowsException <Exception>(() =>
                                               TypeInference.Infer(initialState, new Application(new TermVariable("eq"), new TermVariable("true"))));

            Assert.ThrowsException <Exception>(() =>
                                               TypeInference.Infer(initialState, new LambdaAbstraction("x", new Application(new TermVariable("show"), new Application(new TermVariable("read"), new TermVariable("x"))))));
        }
Пример #2
0
        public void CoreHindleyMilnerTests()
        {
            var InitialState = new InferenceState(new FreshVariableStream());

            Assert.AreEqual(
                TypeInference.Infer(InitialState, new LambdaAbstraction("x", new TermVariable("x"))),
                new QualifiedType(PrimType.Fun(new TypeVariable("t0", new DataKind()), new TypeVariable("t0", new DataKind()))));

            Assert.AreEqual(
                TypeInference.Infer(InitialState, new LambdaAbstraction("x", new LambdaAbstraction("y", new TermVariable("y")))),
                new QualifiedType(PrimType.Fun(new TypeVariable("t0", new DataKind()), new TypeVariable("t1", new DataKind()), new TypeVariable("t1", new DataKind()))));

            Assert.AreEqual(
                TypeInference.Infer(InitialState, new LambdaAbstraction("x", new LambdaAbstraction("y", new TermVariable("x")))),
                new QualifiedType(PrimType.Fun(new TypeVariable("t0", new DataKind()), new TypeVariable("t1", new DataKind()), new TypeVariable("t0", new DataKind()))));

            Assert.AreEqual(
                TypeInference.Infer(InitialState, new LambdaAbstraction("x", new Application(new LambdaAbstraction("y", new TermVariable("y")), new TermVariable("x")))),
                new QualifiedType(PrimType.Fun(new TypeVariable("t0", new DataKind()), new TypeVariable("t0", new DataKind()))));

            Assert.AreEqual(
                TypeInference.Infer(InitialState, new LambdaAbstraction("x", new Application(new LambdaAbstraction("y", new TermVariable("x")), new TermVariable("x")))),
                new QualifiedType(PrimType.Fun(new TypeVariable("t0", new DataKind()), new TypeVariable("t0", new DataKind()))));

            Assert.AreEqual(
                TypeInference.Infer(InitialState, new LetBinding("m", new LambdaAbstraction("a", new TermVariable("a")), new Application(new TermVariable("m"), new TermVariable("m")))),
                new QualifiedType(PrimType.Fun(new TypeVariable("t2", new DataKind()), new TypeVariable("t2", new DataKind()))));

            Assert.AreEqual(
                TypeInference.Infer(InitialState,
                                    new LetBinding("s", new LetBinding("m", new LambdaAbstraction("a", new TermVariable("a")), new Application(new TermVariable("m"), new TermVariable("m"))), new TermVariable("s"))),
                new QualifiedType(PrimType.Fun(new TypeVariable("t4", new DataKind()), new TypeVariable("t4", new DataKind()))));

            Assert.AreEqual(
                TypeInference.Infer(InitialState, new LambdaAbstraction("x", new LambdaAbstraction("y", new Application(new TermVariable("x"), new TermVariable("y"))))),
                new QualifiedType(PrimType.Fun(PrimType.Fun(new TypeVariable("t1", new DataKind()), new TypeVariable("t2", new DataKind())), new TypeVariable("t1", new DataKind()), new TypeVariable("t2", new DataKind()))));
        }