Current Architecture #26
gnumonik
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Architecture
This document contains an overview of the current architecture of our compiler pipeline. It is not meant to be exhaustive and is intended to serve as a guide for onboarding new developers / make the giant PRs more accessible.
Step 1: Type Deduction/Synthesis (Language.PureScript.CoreFn.Desugar)
During the research phase of this project, we determined that PIR (Plutus Intermediate Representation) should be our ultimate compilation target (with final compilation to UPLC handled by the PIR compiler). Because PIR is an explicitly typed language, and because the vanilla
CoreFn
AST is not explicitly typed, it is necessary to convert theLanguage.PureScript.AST
AST into a typed variant.Because conversion to
CoreFn
occurs after the initial (vanilla PS) typechecker pass, we receive most expressions (but not all of them) annotated with the inferred or explicitly declared type. Desugared expressions or declarations involving type classes, however, are not ignored by the PS typechecker, but we require explicit annotations for explicit type class dictionaries and the functions that operate on them.This step consists of two main phases:
Module
that is the input to our conversion function and desugar constraint types to types that require explicit dictionary arguments. E.g.Eq a => a -> (...)
becomesEq$Dict a -> a -> (...)
.Some notes:
unify
.Step 2: Monomorphization & Inlining (Language.PureScript.CoreFn.Convert.Monomorphize)
PureScript's implementation of Records employs Row Types. Moreover, PureScript supports polymorphic records, which are backed by open rows (e.g.
{a :: Foo | r}
).Records in PureScript (unlike in Haskell) are not just syntatic sugar for products - the order of fields in the record is not determined by the order of fields in the declaration or expression that introduces the record. An order can be established for any fully instantiated (i.e. closed - doesn't contain any type variables of kind
Row Type
) record types - we choose a lexicographic ordering to mirror PureScript'sRowToList
sorting but this is not essenital.We must, therefore, perform monomorphization on record types in order to transform Object Literals to literal products, transform record accessors into
case
analysis of the product that corresponds to the original record, and transform record updates into product updates.Because a single polymorphic record type may be instantiated to different concrete types at different places in the AST, we must also inline while we monomorphize.
The general monomorphization procedure is as follows (not a full algorithm):
App
node.(f,args)
wheref
is the "terminal" function expression andargs
is a list of the argument expressions to which it is applied.f
is already monomorphic. If it is, we make no change to the node.f
is not monomorphic, we strip the quantifiers and check whether we can specialize any bound type variables to a concrete type. E.g. iff :: forall x. Tuple x x -> x -> Int
and the arguments are[Tuple String String, String]
, we instantiatex
toString
.until we run out of arguments to process.
f
in the resulting expression. (It may contain free type variables of kindType
, which can be represented in PIR)The procedure for inlining is:
- If the declaration is non-recursive we "walk" its expression-body and monomorphize/inline the sub-expressions as necessary in order to properly assign the expression the type that it is to be monomorphized to.
- If the declaration the member of a recursive binding group, we pause inlining, walk the expression, and "collect" a
Map Name (Name,SourceType,Expr)
where the key is the original name of the expression, and the values are, respectively: The new "fresh" name to give to the monomorphized expression, the monomorphic type that we must assign the expression to, and the body of the declaration. We do this recursively until we have collected every member of the recursive binding group used in the target expression. Finally, we use that map to construct a monomorphic mutually recursive binding group (where the names are all fresh) and create aLet
-binding for the monomorphized mutually recursive group.The implementation of the monomorphizer/inliner consists in a few key functions that call each other recursively. To make reviewing easier, here's a brief explanation of what the key functions do (or are supposed to do at any rate):
monomorphizeA
is the entry point that checks whether the node is anApp
, peels the arguments and function parts from theApp
, and callshandleFunction
on the function expression and its arguments.handleFunction
branches depending on the function expression it is passed:Var
qualified by modulename, and the modulename isBuiltin
, it just returns the variable (since Builtins cannot be meaningfully inlined).Abs
,handleFunction
tries to instantiate the type variables of the function type with corresponding concrete types of the arguments. If it succeeds, it subsitutes the concrete type in for the bound type variable in all sub-expressions and their types, then calls itself recursively on the body of theAbs
until the type has been fully monomorphized.Var
that is not a builtin,handleFunction
attempts to inline the expression theVar
refers to by callinginlineAs
with the monomorphized type. If this succeeds,handleFunction
calls itself recursively with the monomorphized/inlined expression.f
, it checks whether the function type is monomorphic.f
is monomorphic, it applies it to its arguments and returns the resulting expresion.f
is not monomorphic,handleFunction
throws an error.inlineAs
performs inlining and implements the above algorithm. Note thatinlineAs
is passed a PSType
argument, which represents the type that the expression corresponding to theName
being inlined should have after inlining.monomorphizeWithType
rewrites the type of an expression to match the supplied type and (much more importantly) rewrites the types of all sub-expressions to conform with the supplied type of the top-level expression.Step 3: Object desugaring and final IR (Language.PureScript.CoreFn.[IR / DesugarObjects])
By the end of step 2, all polymorphic records have been monommorphized that can be monomorphized, but record-specific expression nodes (object updates/accessors/literals) still remain in the AST. In order to ensure that all "invalid" expressions and types have been desugared/eliminated prior to final PIR conversion, we define a restricted AST and
Type
type such that only expressions which can be converted into PIR can be represented - a kind of "parse-don't-validate" approach. (This AST is implemented with theBound
library.)At this stage, we construct a set of dictionaries for type and data constructors. When constructing these maps, we add an arbitrary number of constructors for anonymous products (i.e. tuples) to accommodate objects. Specifically, we add constructors for tuples of up to 100 (they look like
data $GEN.~Tuple1 a = $GEN.~Tuple1 a
etc). These dictionaries serve two purposes:[(Int,[Type])]
where theInt
represents the corresponding data constructor's index in the data type, where this information (the constructor's index & arguments) is also available in the dictionary for each data constructor. (Due to the implementation of the PS AST, we need both of these dictionaries, even though in principle only the tycon dictionary is necessary)Conversion into this restricted IR AST is, aside from object expressions, very straightforward. Therefore, in the rest of this section I will explain the object desugaring process.
Object Literals
Map PSString SourceType
from the object literal expression by inspecting the expressions in each field.$GEN.~Tuple2 a b
.Record Accessors
Binder
(a pattern), where the arguments are wildcard binders for every argument except the one that corresponds to the field being accessed.- This is kind of hard to explain without an example. Suppose we have
foo = {a: 1, b :: "yup"}
infoo.b
- That gets turned into (something like)
case foo of {$GEN.Tuple2 _ $HERE -> $HERE}
Record Updates
- Using the above definition of
foo
, if we havefoo {b = "hello"}
, this turns into:-
case foo of {$GEN.Tuple2 a _ -> $GEN.Tuple2 a "hello"}
Step 4: Final Conversion to PIR (Language.PureScript.CoreFn.Convert.ToPIR)
The final step of compilation is conceptually simple: We match on the constructors of our final IR and translate expressions (and the types they contain) into PIR
Term
s andType
s, using some machinery to generate fresh names when we need to construct a lambda (which should only happen when desugaring case expressions).NOTE: The implementation of case expressions is incredibly complex. At the moment we support matching on simple constructor patterns. Going forward, we ought to weak the final IR so that case expressions are presented in a form that makes it simpler to handle.
Beta Was this translation helpful? Give feedback.
All reactions