Deriving Semantics for Compositional Copatterns
The LaTeX source files for the article can be found in paper/.
Executable Racket versions of the examples (given in sections 1 and 2) can be
found in ex/, along with the copattern-matching library composable.rkt. The
same code can be run on another R6RS-compliant Scheme implementation (e.g., Chez
Scheme) using [https://github.com/pdownen/CoScheme].
The step-by-step derivation of semantic artifacts in terms of Haskell program
transformations can be found in the src/ directory. To follow along in the
order of the paper, consider visiting the modules in this order:
-
(Section 3)
Copattern.Block.Subst: Starting from a substitution-based reduction function and search algorithm forCopattern.Block.Syntax, derive a small-step operational semantics, abstract machine, and continuation-passing style (CPS) embedding.-
Copattern.Block.Subst.Reduce: The definitional reduction function for turning a (potential)Redexinto a (potential)ReductandFollowupquestion. The reduction function is transformed as follows:-
Copattern.Block.Subst.Reduce.Direct: The direct-style reduction function. -
Copattern.Block.Subst.Reduce.CPS: CPS transformation of the direct reduction function. -
Copattern.Block.Subst.Reduce.Defunc: Defunctionalization of the CPS. -
Copattern.Block.Subst.Reduce.EnvCxt: Isomorphic change of representation, replacing the evaluation contexts generated by defunctionalization with an equivalent environment. -
Copattern.Block.Subst.Reduce.Reverse: Allowing a reversal of binding order for order-irrelevant environments (guaranteed for well-formed copatterns that bind distinct variable names). -
Copattern.Block.Subst.Reduce.Corridor: Compression of corridor transitions to short-cut known reduction paths.
-
-
Copattern.Block.Subst.Search: The search function that identifies the nextRedexand surroundingQuestionin aTerm.-
Copattern.Block.Subst.Search.Direct: The direct style search function. -
Copattern.Block.Subst.Search.CPS: CPS transformation of the direct reduction function. -
Copattern.Block.Subst.Search.Defunc: Defunctionalization of the CPS. -
Copattern.Block.Subst.Search.EvalQuestion: Isomorphic change of representation, replacing the evaluation contexts generated by defunctionalization with an equivalentQuestion. -
Copattern.Block.Subst.Search.QuestionMonoid: Optimize the search function using the known monoid laws onQuestions. -
Copattern.Block.Subst.Search.Corridor: Compression of corridor transitions to short-cut known reduction paths.
-
-
Copattern.Block.Subst.Decomp: The decomposition, recomposition, and refocusing functions derived fromsearch. -
Copattern.Block.Subst.SmallStep: The small-step reduction-based interpreter.-
Copattern.Block.Subst.Step.Small: The direct-style small-step interpreter loop. -
Copattern.Block.Subst.Step.Refocus: The refocusing optimization of the small-step interpreter. -
Copattern.Block.Subst.Step.Fuse: Loop fusion of the main interpreter loop with refocusing and reduction. -
Copattern.Block.Subst.Step.Corridor: Compression of corridor transitions to short-cut known reduction paths in the fused interpreter. -
Copattern.Block.Subst.Step.Deforest: Eliminate intermediate data structures that are no longer necessary in the compressed interpreter. -
Copattern.Block.Subst.Step.Subst: Immediately substitute when a binding becomes available in the deforested interpreter.
-
-
Copattern.Block.Subst.Machine: The tail-recursive, abstract machine interpreter.-
Copattern.Block.Subst.Machine.Shallow: Break nested pattern-matching into flat patterns. -
Copattern.Block.Subst.Machine.Case: Desugar simultaneous pattern-matching on multiple values to sequential case analysis on a single value. -
Copattern.Block.Subst.Machine.Eta: Eta reduction. -
Copattern.Block.Subst.Machine.Denote: Convert to a "denotational" form, where transition functions are applied to sub-expressions as soon as they become available to the recursive interpreter.
-
-
Copattern.Block.Subst.CPS: The CPS translation embedding theCopattern.Block.Sytnaxinto native Haskell functions.
-
-
(Section 5)
Copattern.Nest.Subst:Copattern.Block.Subst: Starting from a continuation-passing style embedding forCopattern.Block.Syntax, derive an abstract machine and small-step operational semantics.-
Copattern.Nest.Subst.CPS: The CPS translation embedding theCopattern.Nest.Syntaxinto native Haskell functions.-
Copattern.Nest.Subst.CPS.Direct: The CPS embedding with some direct-style function composition of terms applied to responses. -
Copattern.Nest.Subst.CPS.Double: A second layer of CPS transformation of the above, generating a fully CPS embedding with another "meta-continuation" guiding application of terms to responses. -
Copattern.Nest.Subst.CPS.Defunc: Defunctionalization of the meta-continuations in the fully CPS embedding. -
Copattern.Nest.Subst.CPS.Syntactic: Delay application of the translation functions as late as possible, preserving the original syntax until right before the denotations would be applied. -
Copattern.Nest.Subst.CPS.Inline: Inline the relationship between variables as names versus variables as holding the substituted CPS denotations of expressions. -
Copattern.Nest.Subst.CPS.Fuse: Fuse the inlining of denotation substitution with the translation function. -
Copattern.Nest.Subst.CPS.Eta: Eta expansion. -
Copattern.Nest.Subst.CPS.Match: Compress multiple levels of case analysis into nested pattern matching.
-
-
Copattern.Nest.Subst.Machine: The tail-recursive abstract machine interpreter derived from the CPS embedding.-
Copattern.Nest.Subst.Machine.CoCase: A reorganization of the machine, labeling each step as either "refocusing" (perfectly reversible, no information is lost), "reduction" (information loss due to reduction, loop continues), or "terminal" (loop ends,Answeris returned). -
Copattern.Nest.Subst.Machine.Reduced: An extraction of the non-recursive reduction function by defusing reduction steps from the main loop. -
Copattern.Nest.Subst.Machine.Refocused: An extraction of the independently self-recursive refocusing function by defusing refocusing steps from the main loop. -
Copattern.Nest.Subst.Machine.Recomp: A reversal of the refocusing optimization by recomposing after each step. -
Copattern.Nest.Subst.Machine.Small: The small-step interpreter loop.
-
-
Copattern.Nest.Subst.Reduce: The non-recursive reduction function extracted from the abstract machine. -
Copattern.Nest.Subst.Decomp: The decomposition / refocusing functions extracted from the machine. -
Copattern.Nest.Subst.SmallStep: The small-step interpreter loop derived from the abstract machine.
-
-
(Appendix A.1)
Copattern.Block.Env: Starting from an environment-based reduction function and search algorithm forCopattern.Nest.Syntax, derive a small-step operational semantics, abstract machine, and continuation-passing style (CPS) embedding.Follow a similar path as
Copattern.Block.Subst.-
Copattern.Block.Subst.Reduce: The definitional reduction function for turning a (potential)Redexinto a (potential)ReductandFollowupquestion. The reduction function is transformed as follows:-
Copattern.Block.Subst.Reduce.Direct: The direct-style reduction function. -
Copattern.Block.Subst.Reduce.CPS: CPS transformation of the direct reduction function. -
Copattern.Block.Subst.Reduce.Defunc: Defunctionalization of the CPS. -
Copattern.Block.Subst.Reduce.EnvCxt: Isomorphic change of representation, replacing the evaluation contexts generated by defunctionalization with an equivalent environment. -
Copattern.Block.Subst.Reduce.Reverse: Allowing a reversal of binding order for order-irrelevant environments (guaranteed for well-formed copatterns that bind distinct variable names). -
Copattern.Block.Subst.Reduce.Corridor: Compression of corridor transitions to short-cut known reduction paths.
-
-
Copattern.Block.Subst.Decomp: The decomposition, recomposition, and refocusing functions. Note that sincesearchis not impacted by environments, we skip the identical the derivation. -
Copattern.Block.Subst.SmallStep: The small-step reduction-based interpreter, already in refocusing form.-
Copattern.Block.Subst.Step.Small: The direct-style small-step interpreter loop. -
Copattern.Block.Subst.Step.Fuse: Loop fusion of the main interpreter loop with refocusing and reduction. -
Copattern.Block.Subst.Step.Corridor: Compression of corridor transitions to short-cut known reduction paths in the fused interpreter. -
Copattern.Block.Subst.Step.Deforest: Eliminate intermediate data structures that are no longer necessary in the compressed interpreter.
-
-
Copattern.Block.Subst.Machine: The tail-recursive, abstract machine interpreter.-
Copattern.Block.Subst.Machine.Shallow: Break nested pattern-matching into flat patterns. -
Copattern.Block.Subst.Machine.Case: Desugar simultaneous pattern-matching on multiple values to sequential case analysis on a single value. -
Copattern.Block.Subst.Machine.Eta: Eta reduction. -
Copattern.Block.Subst.Machine.Denote: Convert to a "denotational" form, where transition functions are applied to sub-expressions as soon as they become available to the recursive interpreter. -
Copattern.Block.Subst.Machine.Bind: Merge separate environments (the "old" one inherited from surrounding scope and the "new" one created by copattern matching) by binding directly on top of the surrounding environment.
-
-
Copattern.Block.Subst.CPS: The CPS translation embeddingCopattern.Block.Syntaxinto native Haskell functions.
-
-
(Appendix A.2)
Copattern.Nest.Env: Starting from a continuation-passing style embedding forCopattern.Nest.Syntax, derive an abstract machine.Follow a similar path as
Copattern.Nest.Substup to the tail-recursive, abstract machine interpreter.-
Copattern.Nest.Subst.CPS: The CPS translation embedding theCopattern.Nest.Syntaxinto native Haskell functions.-
Copattern.Nest.Subst.CPS.Direct: The CPS embedding with some direct-style function composition of terms applied to responses. -
Copattern.Nest.Subst.CPS.Double: A second layer of CPS transformation of the above, generating a fully CPS embedding with another "meta-continuation" guiding application of terms to responses. -
Copattern.Nest.Subst.CPS.Defunc: Defunctionalization of the meta-continuations in the fully CPS embedding. -
Copattern.Nest.Subst.CPS.Syntactic: Delay application of the translation functions as late as possible, preserving the original syntax until right before the denotations would be applied. -
Copattern.Nest.Subst.CPS.Eta: Eta expansion. -
Copattern.Nest.Subst.CPS.Match: Compress multiple levels of case analysis into nested pattern matching.
-
-
Copattern.Nest.Subst.Machine: The tail-recursive abstract machine interpreter derived from the CPS embedding.
-