示例#1
0
        private static ContinuationPassing.Continuation <Guid> CompilePositiveDestructor(Term <Guid> arguments, Class <Guid, Guid, ITerm <Guid, Guid> > @class, Continuation <Guid, Func <Term <Guid>, Term <Guid> > > continuation)
        {
            switch (@class.Tag)
            {
            case Class <Guid, Guid, ITerm <Guid, Guid> > .Tags.Quantifier:
            {
                var quantifier = (Class <Guid, Guid, ITerm <Guid, Guid> > .Quantifier)@class;
                var extract    = (Continuation <Guid, Func <Term <Guid>, Term <Guid> > > .Exists.Quantifier)continuation;

                return(new ContinuationPassing.Continuation <Guid> .Extract(extract.Left, extract.Right, extract.Body(arguments)));
            }

            case Class <Guid, Guid, ITerm <Guid, Guid> > .Tags.Shift:
            {
                var shift = (Class <Guid, Guid, ITerm <Guid, Guid> > .Shift)@class;
                var force = (Continuation <Guid, Func <Term <Guid>, Term <Guid> > > .Exists.Shift)continuation;

                return(new ContinuationPassing.Continuation <Guid> .Jump(arguments));
            }

            default:
                throw new InvalidProgramException("Should never happen.");
            }
        }
示例#2
0
        private static Initialization <Guid> CompileNegativeDestructor(Term <Guid> arguments, Class <Guid, Guid, ITerm <Guid, Guid> > @class, Continuation <Guid, Func <Term <Guid>, Term <Guid> > > continuation)
        {
            switch (@class.Tag)
            {
            case Class <Guid, Guid, ITerm <Guid, Guid> > .Tags.Quantifier:
            {
                var quantifier  = (Class <Guid, Guid, ITerm <Guid, Guid> > .Quantifier)@class;
                var application = (Continuation <Guid, Func <Term <Guid>, Term <Guid> > > .Forall.Quantifier)continuation;

                return(new Initialization <Guid> .Pair(application.Argument(null), arguments));
            }

            case Class <Guid, Guid, ITerm <Guid, Guid> > .Tags.Shift:
            {
                var shift   = (Class <Guid, Guid, ITerm <Guid, Guid> > .Shift)@class;
                var extract = (Continuation <Guid, Func <Term <Guid>, Term <Guid> > > .Forall.Shift)continuation;

                return(new Initialization <Guid> .Lambda(extract.Identifier, extract.Body(arguments)));
            }

            default:
                throw new InvalidProgramException("Should never happen.");
            }
        }
示例#3
0
        private IEnumerable <Bits> SerializeContinuation(Sequence <IExpression <Bind, Id, Bind> > environment, IUniverse universe, Class <Bind, Id, ITerm <Bind, Id> > @class, Continuation <Bind, IEnumerable <Bits> > continuation)
        {
            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 application = (Continuation <Bind, IEnumerable <Bits> > .Forall.Quantifier)continuation;

                    foreach (var bit in application.Argument)
                    {
                        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 extract = (Continuation <Bind, IEnumerable <Bits> > .Forall.Shift)continuation;

                    var binding = SerializeBinding(new Annotated <Bind, Id, Bind>(environment, new Expression <Bind, Id, Bind>(universe.Dual(), shift.Content, extract.Identifier)));

                    foreach (var bit in Encoding.Concatenate(binding, extract.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 extract    = (Continuation <Bind, IEnumerable <Bits> > .Exists.Quantifier)continuation;

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

                    var left  = SerializeBinding(new Annotated <Bind, Id, Bind>(environment, quantifier.Dependency.Fmap(_ => extract.Left)));
                    var right = SerializeBinding(new Annotated <Bind, Id, Bind>(environment, new Expression <Bind, Id, Bind>(universe, rightType, extract.Right)));

                    foreach (var bit in Encoding.Concatenate(left, right, extract.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 force = (Continuation <Bind, IEnumerable <Bits> > .Exists.Shift)continuation;

                    yield break;
                }

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

            case null:
            default:
                throw new InvalidProgramException("Should never happen.");
            }
        }
示例#4
0
        private static Func <Term <Guid>, Term <Guid> > CompileDestructor(IUniverse universe, Class <Guid, Guid, ITerm <Guid, Guid> > @class, Func <Term <Guid>, Term <Guid> > focus, Continuation <Guid, Func <Term <Guid>, Term <Guid> > > continuation)
        {
            switch (universe.Polarity)
            {
            case Polarity.Forall:
            {
                return(arguments =>
                    {
                        var initialization = CompileNegativeDestructor(arguments, @class, continuation);

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

            case Polarity.Exists:
            {
                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(arguments =>
                    {
                        var qualifiedSelf = new Qualified <Guid, Term <Guid> >(selfUniverse, compiledSelfType, focus(null));

                        var cont = CompilePositiveDestructor(arguments, @class, continuation);

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

            case null:
            default:
                throw new ArgumentOutOfRangeException("polarity");
            }
        }
示例#5
0
        private static IEvaluator <R> EvaluateDestructor <R>(Polarity?polarity, Class <Unit, uint, ITerm <Unit, uint> > @class, IEvaluator <R> focus, Continuation <Unit, IEvaluator <R> > continuation)
        {
            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 application = (Continuation <Unit, IEvaluator <R> > .Forall.Quantifier)continuation;

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

                    return(new Evaluator <R>(
                               evaluateF: (locals, argument) =>
                        {
                            return application.Argument.Evaluate(locals,
                                                                 argument: new Value <R> .Continuation(
                                                                     content: new Continuation <R>(
                                                                         throwF: result =>
                            {
                                return focus.Evaluate(locals, new Value <R> .Pair(result, argument));
                            })));
                        }));
                }

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

                    return(new Evaluator <R>(
                               evaluateF: (locals, argument) =>
                        {
                            return focus.Evaluate(locals,
                                                  argument: new Value <R> .Continuation(
                                                      content: new Continuation <R>(
                                                          throwF: result =>
                            {
                                locals = locals.Push(result);

                                return extract.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 extract    = (Continuation <Unit, IEvaluator <R> > .Exists.Quantifier)continuation;

                    return(new Evaluator <R>(
                               evaluateF: (locals, argument) =>
                        {
                            return focus.Evaluate(locals,
                                                  argument: new Value <R> .Continuation(
                                                      content: new Continuation <R>(
                                                          throwF: result =>
                            {
                                if (quantifier.Dependency.Universe.Rank != 0)
                                {
                                    locals = locals.Push(result);
                                }
                                else
                                {
                                    var pair = (Value <R> .Pair)result;

                                    locals = locals.Push(pair.Left);
                                    locals = locals.Push(pair.Right);
                                }

                                return extract.Body.Evaluate(locals, argument);
                            })));
                        }));
                }

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

                    return(new Evaluator <R>(
                               evaluateF: (locals, argument) =>
                        {
                            return focus.Evaluate(locals,
                                                  argument: new Value <R> .Continuation(
                                                      content: new Continuation <R>(
                                                          throwF: result =>
                            {
                                var delayed = (Value <R> .Continuation)result;

                                return delayed.Content.Throw(argument);
                            })));
                        }));
                }

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

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