Esempio n. 1
0
        private static Initialization <Guid> CompilePositiveConstructor(Class <Guid, Guid, ITerm <Guid, Guid> > @class, Initialization <Guid, Func <Term <Guid>, Term <Guid> > > initialization)
        {
            switch (@class.Tag)
            {
            case Class <Guid, Guid, ITerm <Guid, Guid> > .Tags.Quantifier:
            {
                var quantifier = (Class <Guid, Guid, ITerm <Guid, Guid> > .Quantifier)@class;
                var pair       = (Initialization <Guid, Func <Term <Guid>, Term <Guid> > > .Exists.Quantifier)initialization;

                return(new Initialization <Guid> .Pair(pair.Left(null), pair.Right(null)));
            }

            case Class <Guid, Guid, ITerm <Guid, Guid> > .Tags.Shift:
            {
                var shift = (Class <Guid, Guid, ITerm <Guid, Guid> > .Shift)@class;
                var delay = (Initialization <Guid, Func <Term <Guid>, Term <Guid> > > .Exists.Shift)initialization;

                var fresh = Fresh();

                return(new Initialization <Guid> .Lambda(fresh, delay.Body(new Term <Guid> .Variable(fresh))));
            }

            default:
                throw new InvalidProgramException("Should never happen.");
            }
        }
Esempio n. 2
0
        private static ContinuationPassing.Continuation <Guid> CompileNegativeConstructor(Class <Guid, Guid, ITerm <Guid, Guid> > @class, Initialization <Guid, Func <Term <Guid>, Term <Guid> > > initialization)
        {
            switch (@class.Tag)
            {
            case Class <Guid, Guid, ITerm <Guid, Guid> > .Tags.Quantifier:
            {
                var quantifier = (Class <Guid, Guid, ITerm <Guid, Guid> > .Quantifier)@class;
                var lambda     = (Initialization <Guid, Func <Term <Guid>, Term <Guid> > > .Forall.Quantifier)initialization;

                Guid fresh = Fresh();

                return(new ContinuationPassing.Continuation <Guid> .Extract(lambda.Parameter, fresh, lambda.Body(new Term <Guid> .Variable(fresh))));
            }

            case Class <Guid, Guid, ITerm <Guid, Guid> > .Tags.Shift:
            {
                var shift   = (Class <Guid, Guid, ITerm <Guid, Guid> > .Shift)@class;
                var @return = (Initialization <Guid, Func <Term <Guid>, Term <Guid> > > .Forall.Shift)initialization;

                return(new ContinuationPassing.Continuation <Guid> .Jump(@return.Body(null)));
            }

            default:
                throw new InvalidProgramException("Should never happen.");
            }
        }
Esempio n. 3
0
        private static Func <Term <Guid>, Term <Guid> > CompileConstructor(IUniverse universe, Class <Guid, Guid, ITerm <Guid, Guid> > @class, Initialization <Guid, Func <Term <Guid>, Term <Guid> > > initialization)
        {
            switch (universe.Polarity)
            {
            case Polarity.Forall:
            {
                var qualifiedSelfType = universe.Qualify <Guid, Guid, ITerm <Guid, Guid> >(new Term <Guid, Guid>(new TermF <Guid, Guid, ITerm <Guid, Guid> > .Type(@class)));

                var compiledSelfType = Compile(qualifiedSelfType);

                var selfUniverse = new Term <Guid> .Universe(universe.Rank);

                return(continuation =>
                    {
                        var qualifiedSelf = new Qualified <Guid, Term <Guid> >(selfUniverse, compiledSelfType, continuation);

                        var arguments = CompileNegativeConstructor(@class, initialization);

                        return new Term <Guid> .Destructor(qualifiedSelf, arguments);
                    });
            }

            case Polarity.Exists:
            {
                return(_ =>
                    {
                        var arguments = CompilePositiveConstructor(@class, initialization);

                        return new Term <Guid> .Constructor(arguments);
                    });
            }

            case null:
            default:
                throw new ArgumentOutOfRangeException("polarity");
            }
        }
Esempio n. 4
0
        private Initialization <Bind, TR> Map(Sequence <IExpression <Bind, Id, Bind> > environment, IUniverse universe, Class <Bind, Id, ITerm <Bind, Id> > @class, Initialization <Bind, T> initialization)
        {
            switch (universe.Polarity)
            {
            case Polarity.Forall:

                switch (@class.Tag)
                {
                case Class <Bind, Id, ITerm <Bind, Id> > .Tags.Quantifier:
                {
                    var quantifier = (Class <Bind, Id, ITerm <Bind, Id> > .Quantifier)@class;
                    var lambda     = (Initialization <Bind, T> .Forall.Quantifier)initialization;

                    var returnType = Substitute(environment, quantifier.Dependency, quantifier.Dependent, UseDeclarationF(lambda.Parameter));

                    var qualifiedBody = new Expression <Bind, Id, T>(universe, returnType, lambda.Body);

                    var dependency = quantifier.Dependency.Fmap(_ => lambda.Parameter);

                    environment = environment.Push(dependency);

                    var body = ConvertF(qualifiedBody.Annotate(environment));

                    return(new Initialization <Bind, TR> .Forall.Quantifier(lambda.Parameter, body));
                }

                case Class <Bind, Id, ITerm <Bind, Id> > .Tags.Shift:
                {
                    var shift   = (Class <Bind, Id, ITerm <Bind, Id> > .Shift)@class;
                    var @return = (Initialization <Bind, T> .Forall.Shift)initialization;

                    var qualifiedBody = new Expression <Bind, Id, T>(universe.Dual(), shift.Content, @return.Body);

                    var body = ConvertF(qualifiedBody.Annotate(environment));

                    return(new Initialization <Bind, TR> .Forall.Shift(body));
                }

                default:
                    throw new InvalidProgramException("Should never happen.");
                }

            case Polarity.Exists:

                switch (@class.Tag)
                {
                case Class <Bind, Id, ITerm <Bind, Id> > .Tags.Quantifier:
                {
                    var quantifier = (Class <Bind, Id, ITerm <Bind, Id> > .Quantifier)@class;
                    var pair       = (Initialization <Bind, T> .Exists.Quantifier)initialization;

                    var qualifiedLeft = quantifier.Dependency.Fmap(_ => pair.Left);

                    var rightType = Substitute(environment, quantifier.Dependency, quantifier.Dependent, pair.Left);

                    var qualifiedRight = new Expression <Bind, Id, T>(universe, @rightType, pair.Right);

                    var left  = ConvertF(qualifiedLeft.Annotate(environment));
                    var right = ConvertF(qualifiedRight.Annotate(environment));

                    return(new Initialization <Bind, TR> .Exists.Quantifier(left, right));
                }

                case Class <Bind, Id, ITerm <Bind, Id> > .Tags.Shift:
                {
                    var shift = (Class <Bind, Id, ITerm <Bind, Id> > .Shift)@class;
                    var delay = (Initialization <Bind, T> .Exists.Shift)initialization;

                    var qualifiedBody = new Expression <Bind, Id, T>(universe.Dual(), shift.Content, delay.Body);

                    var body = ConvertF(qualifiedBody.Annotate(environment));

                    return(new Initialization <Bind, TR> .Exists.Shift(body));
                }

                default:
                    throw new InvalidProgramException("Should never happen.");
                }

            case null:
            default:
                throw new InvalidProgramException("Should never happen.");
            }
        }
Esempio n. 5
0
        private IEnumerable <Bits> SerializeConstructor(Sequence <IExpression <Bind, Id, Bind> > environment, IUniverse universe, Class <Bind, Id, ITerm <Bind, Id> > @class, Initialization <Bind, IEnumerable <Bits> > initialization)
        {
            switch (universe.Polarity)
            {
            case Polarity.Forall:

                switch (@class.Tag)
                {
                case Class <Bind, Id, ITerm <Bind, Id> > .Tags.Quantifier:
                {
                    var quantifier = (Class <Bind, Id, ITerm <Bind, Id> > .Quantifier)@class;
                    var lambda     = (Initialization <Bind, IEnumerable <Bits> > .Forall.Quantifier)initialization;

                    var parameter = SerializeBinding(new Annotated <Bind, Id, Bind>(environment, quantifier.Dependency.Fmap(_ => lambda.Parameter)));

                    foreach (var bit in Encoding.Concatenate(parameter, lambda.Body))
                    {
                        yield return(bit);
                    }

                    yield break;
                }

                case Class <Bind, Id, ITerm <Bind, Id> > .Tags.Shift:
                {
                    var shift   = (Class <Bind, Id, ITerm <Bind, Id> > .Shift)@class;
                    var @return = (Initialization <Bind, IEnumerable <Bits> > .Forall.Shift)initialization;

                    foreach (var bit in @return.Body)
                    {
                        yield return(bit);
                    }

                    yield break;
                }

                default:
                {
                    throw new InvalidProgramException("Should never happen.");
                }
                }

            case Polarity.Exists:

                switch (@class.Tag)
                {
                case Class <Bind, Id, ITerm <Bind, Id> > .Tags.Quantifier:
                {
                    var quantifier = (Class <Bind, Id, ITerm <Bind, Id> > .Quantifier)@class;
                    var pair       = (Initialization <Bind, IEnumerable <Bits> > .Exists.Quantifier)initialization;

                    foreach (var bit in Encoding.Concatenate(pair.Left, pair.Right))
                    {
                        yield return(bit);
                    }

                    yield break;
                }

                case Class <Bind, Id, ITerm <Bind, Id> > .Tags.Shift:
                {
                    var shift = (Class <Bind, Id, ITerm <Bind, Id> > .Shift)@class;
                    var delay = (Initialization <Bind, IEnumerable <Bits> > .Exists.Shift)initialization;

                    foreach (var bit in delay.Body)
                    {
                        yield return(bit);
                    }

                    yield break;
                }

                default:
                {
                    throw new InvalidProgramException("Should never happen.");
                }
                }

            case null:
            default:
                throw new InvalidProgramException("Should never happen.");
            }
        }
Esempio n. 6
0
        private static IEvaluator <R> EvaluateConstructor <R>(Polarity?polarity, Class <Unit, uint, ITerm <Unit, uint> > @class, Initialization <Unit, IEvaluator <R> > initialization)
        {
            switch (polarity)
            {
            case Polarity.Forall:

                switch (@class.Tag)
                {
                case Class <Unit, uint, ITerm <Unit, uint> > .Tags.Quantifier:
                {
                    var quantifier = (Class <Unit, uint, ITerm <Unit, uint> > .Quantifier)@class;
                    var lambda     = (Initialization <Unit, IEvaluator <R> > .Forall.Quantifier)initialization;

                    if (quantifier.Dependency.Universe.Rank != 0)
                    {
                        return(lambda.Body);
                    }

                    return(new Evaluator <R>(
                               evaluateF: (locals, argument) =>
                        {
                            var pair = (Value <R> .Pair)argument;

                            return lambda.Body.Evaluate(locals.Push(pair.Left), pair.Right);
                        }));
                }

                case Class <Unit, uint, ITerm <Unit, uint> > .Tags.Shift:
                {
                    var shift   = (Class <Unit, uint, ITerm <Unit, uint> > .Shift)@class;
                    var @return = (Initialization <Unit, IEvaluator <R> > .Forall.Shift)initialization;

                    return(new Evaluator <R>(
                               evaluateF: (locals, argument) =>
                        {
                            return @return.Body.Evaluate(locals, argument);
                        }));
                }

                default:
                    throw new InvalidProgramException("Should never happen.");
                }

            case Polarity.Exists:

                switch (@class.Tag)
                {
                case Class <Unit, uint, ITerm <Unit, uint> > .Tags.Quantifier:
                {
                    var quantifier = (Class <Unit, uint, ITerm <Unit, uint> > .Quantifier)@class;
                    var pair       = (Initialization <Unit, IEvaluator <R> > .Exists.Quantifier)initialization;

                    if (quantifier.Dependency.Universe.Rank != 0)
                    {
                        return(pair.Right);
                    }

                    return(new Evaluator <R>(
                               evaluateF: (locals, argument) =>
                        {
                            return pair.Left.Evaluate(locals,
                                                      argument: new Value <R> .Continuation(
                                                          content: new Continuation <R>(
                                                              throwF: left =>
                            {
                                return pair.Right.Evaluate(locals,
                                                           argument: new Value <R> .Continuation(
                                                               content: new Continuation <R>(
                                                                   throwF: right =>
                                {
                                    var continuation = (Value <R> .Continuation)argument;

                                    return continuation.Content.Throw(new Value <R> .Pair(left, right));
                                })));
                            })));
                        }));
                }

                case Class <Unit, uint, ITerm <Unit, uint> > .Tags.Shift:
                {
                    var shift = (Class <Unit, uint, ITerm <Unit, uint> > .Shift)@class;
                    var delay = (Initialization <Unit, IEvaluator <R> > .Exists.Shift)initialization;

                    return(new Evaluator <R>(
                               evaluateF: (locals, argument) =>
                        {
                            var continuation = (Value <R> .Continuation)argument;

                            return continuation.Content.Throw(
                                argument: new Value <R> .Continuation(
                                    new Continuation <R>(
                                        throwF: result =>
                            {
                                return delay.Body.Evaluate(locals, result);
                            })));
                        }));
                }

                default:
                    throw new InvalidProgramException("Should never happen.");
                }

            case null:
            default:
                throw new InvalidProgramException("Should never happen.");
            }
        }