Beispiel #1
0
        private TermF <Bind, Id, TR> Map(IClosedTermF <Bind, Id, T> annotated)
        {
            switch (annotated.Expression.Term.Production)
            {
            case Productions.Variable:
            {
                var variable = (TermF <Bind, Id, T> .Variable)annotated.Expression.Term;

                return(new TermF <Bind, Id, TR> .Variable(variable.Identifier));
            }

            case Productions.Universe:
            {
                var universe = (TermF <Bind, Id, T> .Universe)annotated.Expression.Term;

                return(new TermF <Bind, Id, TR> .Universe(universe.Order));
            }

            case Productions.Type:
            {
                var type = (TermF <Bind, Id, T> .Type)annotated.Expression.Term;

                var universe = (TermF <Bind, Id, ITerm <Bind, Id> > .Universe)annotated.Expression.Type.Content;

                var @class = Map(annotated.Environment, universe.Order, type.Class);

                return(new TermF <Bind, Id, TR> .Type(@class));
            }

            case Productions.Constructor:
            {
                var constructor = (TermF <Bind, Id, T> .Constructor)annotated.Expression.Term;

                var type = (TermF <Bind, Id, ITerm <Bind, Id> > .Type)annotated.Expression.Type.Content;

                var initialization = Map(annotated.Environment, annotated.Expression.Universe, type.Class, constructor.Initialization);

                return(new TermF <Bind, Id, TR> .Constructor(initialization));
            }

            case Productions.Destructor:
            {
                var destructor = (TermF <Bind, Id, T> .Destructor)annotated.Expression.Term;

                var focus = ConvertF(destructor.Focus.Annotate(annotated.Environment));

                var qualifiedFocus = destructor.Focus.Fmap(_ => focus);

                var type = (TermF <Bind, Id, ITerm <Bind, Id> > .Type)destructor.Focus.Type.Content;

                var continuation = Map(annotated.Environment, destructor.Focus.Universe, type.Class, annotated.Expression.Fmap(_ => destructor.Continuation));

                return(new TermF <Bind, Id, TR> .Destructor(qualifiedFocus, continuation));
            }

            default:
                throw new InvalidProgramException("Should never happen.");
            }
        }
Beispiel #2
0
 /*
  * [ (a, b) ]
  * ([ a ], [ b ])
  *
  * [ a -> b ]
  * ([ a ], [ b ])
  *
  * [ !a ]
  * ~[ a ]
  *
  * [ ?b ]
  * ~[ b ]
  *
  * ---
  *
  * [ x ]
  * x
  *
  * [ \x. e ]
  * \k. let (x, k') = !k; [ e ] k'
  *
  * [ return e ]
  * \k. jump !k [ e ]
  *
  * [ (l, r) ]
  * ([ l ], [ r ])
  *
  * [ { e } ]
  * \!k. [ e ] k
  *
  * ---
  *
  * [ f e ]
  * \k. [ f ] ([ e ], !k)
  *
  * [ do { x <- e; f } ]
  * \k. [ e ] \!x. [ f ] k
  *
  * [ let (x, y) = p; f ]
  * \k. let (x, y) = [ p ]; [ f ] k
  *
  * [ force e ]
  * \k. jump [ e ] !k
  *
  */
 /*
  * [ x ]
  * \c. c x
  *
  * [ \x. e ]
  * \k. let# (x, k') = !k; [ e ] k'
  *
  * [ return e ]
  * \k. [ e ] \x. jump# !k x
  *
  * [ (l, r) ]
  * \c. [ l ] \x. [ r ] \y. let p = (x, y); c p
  *
  * [ { e } ]
  * \c. let f = (\!k. [ e ] k); c f
  *
  * ---
  *
  * [ f e ]
  * \k. [ e ] \x. let# k' = (x, !k); [ f ] k'
  *
  * [ do { x <- e; f } ]
  * \k. let# k' = (\!x. [ f ] k); [ e ] k'
  *
  * [ let (x, y) = e; f ]
  * \k. [ e ] \p. let (x, y) = p; [ f ] k
  *
  * [ force e ]
  * \k. [ e ] \x. jump x !k
  *
  */
 private static Func <Term <Guid>, Term <Guid> > CompilationStep(IClosedTermF <Guid, Guid, Func <Term <Guid>, Term <Guid> > > annotated)
 {
     return(CompilationStep(annotated.Environment, annotated.Expression));
 }
Beispiel #3
0
 public IClosedTermF <Bind, Id, TR> Fmap(IClosedTermF <Bind, Id, T> annotated)
 {
     return(new ClosedTermF <Bind, Id, TR>(annotated.Environment, annotated.Expression.Fmap(_ => Map(annotated))));
 }
Beispiel #4
0
        private IEnumerable <Bits> SerializationStep(IClosedTermF <Bind, Id, IEnumerable <Bits> > closed)
        {
            foreach (var bit in EncodeTerm(closed.Expression.Universe, closed.Expression.Term.Production))
            {
                yield return(bit);
            }

            switch (closed.Expression.Term.Production)
            {
            case Productions.Variable:
            {
                var variable = (TermF <Bind, Id, IEnumerable <Bits> > .Variable)closed.Expression.Term;

                var annotated = new Annotated <Bind, Id, Id>(closed.Environment, closed.Expression.Fmap(_ => variable.Identifier));

                foreach (var bit in SerializeIdentifier(annotated))
                {
                    yield return(bit);
                }

                yield break;
            }

            case Productions.Universe:
            {
                var universe = (TermF <Bind, Id, IEnumerable <Bits> > .Universe)closed.Expression.Term;

                foreach (var bit in EncodeUniverse(universe.Order))
                {
                    yield return(bit);
                }

                yield break;
            }

            case Productions.Type:
            {
                var type = (TermF <Bind, Id, IEnumerable <Bits> > .Type)closed.Expression.Term;

                var universe = (TermF <Bind, Id, ITerm <Bind, Id> > .Universe)closed.Expression.Type.Content;

                foreach (var bit in SerializeClass(closed.Environment, universe.Order, type.Class))
                {
                    yield return(bit);
                }

                yield break;
            }

            case Productions.Constructor:
            {
                var constructor = (TermF <Bind, Id, IEnumerable <Bits> > .Constructor)closed.Expression.Term;

                var type = (TermF <Bind, Id, ITerm <Bind, Id> > .Type)closed.Expression.Type.Content;

                foreach (var bit in SerializeConstructor(closed.Environment, closed.Expression.Universe, type.Class, constructor.Initialization))
                {
                    yield return(bit);
                }

                yield break;
            }

            case Productions.Destructor:
            {
                var destructor = (TermF <Bind, Id, IEnumerable <Bits> > .Destructor)closed.Expression.Term;

                var focus = destructor.Focus;

                var type = (TermF <Bind, Id, ITerm <Bind, Id> > .Type)focus.Type.Content;

                var focusUniv = EncodeUniverse(focus.Universe);
                var focusType = SerializeTerm(new Annotated <Bind, Id, ITerm <Bind, Id> >(closed.Environment, focus.TypeOf()));

                foreach (var bit in Encoding.Concatenate(focusUniv, focusType, focus.Term))
                {
                    yield return(bit);
                }

                foreach (var bit in SerializeContinuation(closed.Environment, focus.Universe, type.Class, destructor.Continuation))
                {
                    yield return(bit);
                }

                yield break;
            }

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