diff --git a/CHANGELOG.md b/CHANGELOG.md
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -15,6 +15,144 @@
 
 ---
 
+## [0.18.1] - 2026-11-20
+
+Textual-sources release: the whole `hydra-kernel` is now authored as textual `.hydra` files, including the
+primitive library `hydra.core.lib.*`, and the textual syntax gains the sugar, parsers, and tooling needed to carry it.
+Also lands translingual tail-call optimization for the Scala and TypeScript coders, named provisions and
+primitive parameter names, the property-graph encoding of term graphs, and a large body of host-specific bootstrap
+and CI fixes.
+
+### Breaking changes
+
+- **Type renames** ([#837](https://github.com/CategoricalData/hydra/issues/837)): four types whose variant names clashed with other kernel types are renamed.
+  Any code, JSON, or `.hydra` text that names them must be updated.
+  - `hydra.core.regex.QuantifierRange` is now `hydra.core.regex.Range`.
+  - `hydra.ext.azure.dtld.SchemaPrimitive` is now `PrimitiveSchema`, and `SchemaComplex` is now `ComplexSchema`.
+  - `hydra.rust.syntax.TypePath` is now `PathType`.
+- **New primitive signature syntax in `.hydra`** ([#837](https://github.com/CategoricalData/hydra/issues/837)): a `primitive` definition names its parameters,
+  `primitive ifElse ∀x.(p: boolean, t: lazy x, f: lazy x) : x`, instead of the old arrow chain
+  (`primitive ifElse : ∀t. boolean → lazy t → lazy t → t`).
+  Parameter and result descriptions move into `@param` and `@return` doc lines.
+  The arrow-chain form is for `term` signatures only.
+- **Raw `case` keyword is now `cases`** ([#810](https://github.com/CategoricalData/hydra/issues/810)): the textual-syntax case statement keyword matches `Term.cases`.
+- **`hydra.core.json.bootstrap` is now `hydra.core.bootstrap`** ([#791](https://github.com/CategoricalData/hydra/issues/791)).
+- **`and` and `or` are lazy in their second operand** ([#802](https://github.com/CategoricalData/hydra/issues/802)): both are marked lazy, mirroring `ifElse`, and the
+  Java and Python hosts add thunk-accepting overloads.
+
+### Highlights
+
+- **The whole kernel is `.hydra`-sourced** ([#837](https://github.com/CategoricalData/hydra/issues/837), [#711](https://github.com/CategoricalData/hydra/issues/711), [#710](https://github.com/CategoricalData/hydra/issues/710), [#497](https://github.com/CategoricalData/hydra/issues/497)):
+  every `hydra-kernel` module, including all `hydra.core.lib.*` primitive modules and the test modules, is sourced from
+  `packages/hydra-kernel/src/main/hydra/**/*.hydra`; Phase 1 reads that text instead of the Haskell DSL.
+  The Haskell DSL modules remain as a twin that must stay byte-identical (the dual-edit rule).
+  `bin/verify-hydra-sources.sh` is the identity guard (it is not run by CI),
+  and `heads/haskell/bin/print-hydra-sources.sh` regenerates the text from the DSL.
+  A package may textually source only modules it owns.
+- **Named primitive parameters** ([#837](https://github.com/CategoricalData/hydra/issues/837)): all 292 primitives print with named parameters and `@param`/`@return`
+  descriptions, carried through the kernel metadata.
+- **`parseModules`** ([#842](https://github.com/CategoricalData/hydra/issues/842)): `hydra.core.parse.packaging.parseModules` reads `.hydra` text into modules and is the
+  supported entry point for downstream code generation from textual sources
+  (see `docs/recipes/downstream-codegen.md`).
+- **Kernel JSON on the classpath** ([#786](https://github.com/CategoricalData/hydra/issues/786)): the published `hydra-kernel` jar bundles the kernel's JSON modules, so
+  Java downstream projects no longer need a source checkout to load the kernel.
+- **Tail-call optimization on more hosts** ([#709](https://github.com/CategoricalData/hydra/issues/709)): shared `TcoLoopPlan`/`TcoStep` kernel types and a
+  `lowerSelfTailRecursion` pass, consumed by the new Scala (`@tailrec`) and TypeScript lowerings.
+- **Named provisions** ([#725](https://github.com/CategoricalData/hydra/issues/725)): primitives and tests carry named requirements and recommendations,
+  spec prose is ported into module and primitive metadata, and the generated spec pages render comments and lifecycle.
+- **Term graphs as property graphs** ([#738](https://github.com/CategoricalData/hydra/issues/738)): `hydra.pg.subtermGraph` encodes a term graph as a property graph,
+  with specification pages (`docs/specification/pg/subterm-graph.md`).
+
+### New features
+
+- **Textual syntax sugar** ([#809](https://github.com/CategoricalData/hydra/issues/809), [#811](https://github.com/CategoricalData/hydra/issues/811)): nominal construction and elimination sugar
+  (`record`/`inject`/`wrap {...}`, `project`/`unwrap [...]`), applied `case` sugar, and monadic `do` blocks
+  (`do either`, `do list`, `do optional`, `do effect`).
+- **Source spans** ([#815](https://github.com/CategoricalData/hydra/issues/815)): the parsers track source offsets and record term spans through the new
+  `hydra.core.positions` module; spans are round-trip-exempt surface trivia.
+- **Parsers and printers** ([#710](https://github.com/CategoricalData/hydra/issues/710), [#497](https://github.com/CategoricalData/hydra/issues/497)): `hydra.core.parse.core` (term, type, and type scheme parser),
+  parse/print for packaging, doc-comment parsing into `EntityMetadata`, and `line:col` parse errors.
+  A signature-level `lazy` keyword round-trips `Parameter.isLazy` ([#800](https://github.com/CategoricalData/hydra/issues/800)),
+  and raw `cases(...)` accepts the `_` default field ([#822](https://github.com/CategoricalData/hydra/issues/822)).
+- **JSON** ([#776](https://github.com/CategoricalData/hydra/issues/776), [#777](https://github.com/CategoricalData/hydra/issues/777)): `JsonWriterOptions` and `printJsonWith` for configurable JSON layout,
+  and `\uXXXX` escapes with surrogate pairs in the JSON parser.
+- **Error printers** ([#778](https://github.com/CategoricalData/hydra/issues/778)): real printers for inference and extraction errors replace placeholder constants.
+- **Spec generator** ([#723](https://github.com/CategoricalData/hydra/issues/723)): the `hydra.RegenerateSpec` driver and `bin/regenerate-spec.sh` now exist,
+  and the module-reference pages under `docs/specification/` are generated by them.
+  The module reference is still partial (only the primitives and four kernel type modules are covered).
+  Primitive pages show named signatures (`isAlpha(char: int32) → boolean`) with Parameters and Returns entries,
+  type pages list fields and variants, and headings use local names so in-page links work.
+- **Build system in Hydra** ([#788](https://github.com/CategoricalData/hydra/issues/788)): the lib-pass partition (`isLibModule`, `partitionLibModules`) moves into the
+  translingual `hydra.build.modules`, consumed by the Java, Scala, and Haskell drivers.
+- **Bootstrap matrix** ([#769](https://github.com/CategoricalData/hydra/issues/769)): `docs/bootstrap-matrix.md` persists the N×N cross-host generation matrix.
+- **Build slot tooling** ([#730](https://github.com/CategoricalData/hydra/issues/730), [#814](https://github.com/CategoricalData/hydra/issues/814)): `with-stack-slot.sh` gains a second lane, priorities, pinning, and a memory guard,
+  and scripts that run bare `stack` re-exec under it.
+- **CI** ([#821](https://github.com/CategoricalData/hydra/issues/821), [#700](https://github.com/CategoricalData/hydra/issues/700)): an inference-cost guard (time and peak memory) and a dedicated haskell-tests job.
+
+### Bug fixes
+
+- [#799](https://github.com/CategoricalData/hydra/issues/799): shadow-renaming of variables was decoupled across three layers (`unshadowVariables`,
+  `analyzeFunctionTermWithGather`, the Java coder's `dedupBindings`).
+- [#803](https://github.com/CategoricalData/hydra/issues/803), [#804](https://github.com/CategoricalData/hydra/issues/804), [#805](https://github.com/CategoricalData/hydra/issues/805): printed import aliases and bare names now round-trip.
+- [#798](https://github.com/CategoricalData/hydra/issues/798): a `Term.variable` encodes and decodes as `{"@var": "name"}` at any JSON position.
+- [#817](https://github.com/CategoricalData/hydra/issues/817): `printFloat32`/`printFloat64` for subnormals in the Common Lisp, Emacs Lisp, and Clojure hosts.
+- [#779](https://github.com/CategoricalData/hydra/issues/779): the Python coder no longer breaks on nested module names.
+- [#752](https://github.com/CategoricalData/hydra/issues/752): `PropertyType.required` was hardcoded to true.
+- [#751](https://github.com/CategoricalData/hydra/issues/751), [#832](https://github.com/CategoricalData/hydra/issues/832), [#838](https://github.com/CategoricalData/hydra/issues/838): the SHACL serializer emits blank nodes for anonymous shape references, RDF lists for
+  `sh:in`/`sh:and`/`sh:or`/`sh:xone`, definition references under `sh:node`/`sh:not`/`sh:and`/`sh:or`/`sh:xone`,
+  and qualified value shapes with their counts.
+- [#754](https://github.com/CategoricalData/hydra/issues/754): IRI strings are escaped in the rdf4j serializer.
+- [#748](https://github.com/CategoricalData/hydra/issues/748), [#781](https://github.com/CategoricalData/hydra/issues/781): `inferInGraphContext` routes through the discharge-and-finalize tail, and
+  `inferModules` seeds its graph from both universe and target modules.
+- [#764](https://github.com/CategoricalData/hydra/issues/764): `refreshModule` fails loudly on dropped bindings; binding dedup and primitive shadowing are fixed.
+- [#649](https://github.com/CategoricalData/hydra/issues/649): the Python coder merges a type and a term that share a module path.
+- [#780](https://github.com/CategoricalData/hydra/issues/780): generated Javadoc escapes backslashes and `*/`.
+- [#782](https://github.com/CategoricalData/hydra/issues/782): simple Unicode case mapping on the Common Lisp and Emacs Lisp hosts.
+- [#775](https://github.com/CategoricalData/hydra/issues/775): `mapSet` is unregistered on hosts that never implemented it.
+- [#289](https://github.com/CategoricalData/hydra/issues/289), [#762](https://github.com/CategoricalData/hydra/issues/762), [#801](https://github.com/CategoricalData/hydra/issues/801): stale post-rename paths in the Go overlay imports, the TypeScript bootstrap driver,
+  and the Common Lisp and Emacs Lisp test loaders.
+- [#741](https://github.com/CategoricalData/hydra/issues/741), [#747](https://github.com/CategoricalData/hydra/issues/747), [#757](https://github.com/CategoricalData/hydra/issues/757), [#771](https://github.com/CategoricalData/hydra/issues/771), [#787](https://github.com/CategoricalData/hydra/issues/787), [#823](https://github.com/CategoricalData/hydra/issues/823), [#831](https://github.com/CategoricalData/hydra/issues/831): sync and cold-seed fixes
+  (target assembly order, `hostOverrides` immunity, overlay hashing in the freshness gate, downstream assembly after
+  overlay re-apply, the generator stamp's coder-copy component).
+- [#736](https://github.com/CategoricalData/hydra/issues/736): CI pins JDK 11 for the Haskell job after `ubuntu-latest` moved to JDK 25.
+- [#755](https://github.com/CategoricalData/hydra/issues/755): effects fixes (Type.effect encoder arms, Python `map_list`, Emacs Lisp loading).
+- [#812](https://github.com/CategoricalData/hydra/issues/812): the Clojure/Scheme `testEnv` redirect applies to generated test trees only.
+- [#768](https://github.com/CategoricalData/hydra/issues/768): the cold-seed driver step fails fast when its local fallback cannot succeed.
+- [#825](https://github.com/CategoricalData/hydra/issues/825): `dist/haskell/hydra-kernel` is reseeded when stale relative to `dist/json`.
+- [#839](https://github.com/CategoricalData/hydra/issues/839): the assemble freshness gate refreshes a stale input digest after a rebase instead of failing, and never rewrites a current one.
+- [#826](https://github.com/CategoricalData/hydra/issues/826): Lisp coders reference nullary effect primitives by call.
+- [#827](https://github.com/CategoricalData/hydra/issues/827): overlay variant-order tables are checked against `model.json`.
+- [#772](https://github.com/CategoricalData/hydra/issues/772), [#756](https://github.com/CategoricalData/hydra/issues/756): several silent failures in the sync scripts and vitest/Scala drivers now fail loudly.
+
+### Improvements
+
+- Python DSL phantom types are threaded through combinator signatures ([#731](https://github.com/CategoricalData/hydra/issues/731), [#732](https://github.com/CategoricalData/hydra/issues/732), [#818](https://github.com/CategoricalData/hydra/issues/818)).
+- Hackage and PyPI batch publish sets are derived from the package registry ([#573](https://github.com/CategoricalData/hydra/issues/573), [#577](https://github.com/CategoricalData/hydra/issues/577)).
+- Pre-existing namespace leftovers from the 0.18.0 rename are cleaned up in Lisp, Scala, TypeScript, and Java bootstrap
+  drivers ([#767](https://github.com/CategoricalData/hydra/issues/767), [#729](https://github.com/CategoricalData/hydra/issues/729)).
+- `/bootstrap`, `/sync-default`, `/test`, and the pre-push gate default to Java and Python, with Haskell opt-in
+  ([#773](https://github.com/CategoricalData/hydra/issues/773)); the bootstrap demo holds the build slot only for its own `stack` calls ([#797](https://github.com/CategoricalData/hydra/issues/797)).
+- `bin/sync.sh` prints per-phase timing ([#824](https://github.com/CategoricalData/hydra/issues/824)), and the CI Haskell job is split to stay within the runner time limit.
+- `with-stack-slot.sh` fails fast on low disk before a build ([#789](https://github.com/CategoricalData/hydra/issues/789)).
+- `bin/verify-hydra-sources.sh --dsl` runs the dual-edit guard in reverse, regenerating from the Haskell DSL twins and
+  comparing with the committed JSON ([#837](https://github.com/CategoricalData/hydra/issues/837)).
+- `docs/hydra-lexicon.txt` prints primitives with named parameters, matching the generated spec pages
+  ([#723](https://github.com/CategoricalData/hydra/issues/723)).
+
+### Documentation
+
+- `docs/specification/syntax.md` gains the module-level grammar, doc comments, import aliasing, file layout,
+  `@param`/`@return`, and the named-parameter primitive signature.
+- The kernel README, `docs/implementation.md`, `docs/build-system.md`, and the test and primitive recipes describe the
+  `.hydra` sources and the dual-edit rule.
+- The hand-written primitive spec pages use the `hydra.core.lib.*` namespace.
+- Recipes and READMEs document the decoder/encoder fixpoint guard ([#760](https://github.com/CategoricalData/hydra/issues/760)), keeping derived modules out of
+  re-inference ([#758](https://github.com/CategoricalData/hydra/issues/758)), the hydra-ext coder testing pattern ([#699](https://github.com/CategoricalData/hydra/issues/699)), scripts with no automated caller
+  ([#714](https://github.com/CategoricalData/hydra/issues/714)), overlay-versus-prune ordering ([#645](https://github.com/CategoricalData/hydra/issues/645)), the Phase 1 memory envelope ([#592](https://github.com/CategoricalData/hydra/issues/592)),
+  and the Rust coder's home in `hydra-rust` ([#382](https://github.com/CategoricalData/hydra/issues/382)).
+
+---
+
 ## [0.18.0] - 2026-09-29
 
 Module-naming systematization release: every kernel and package module moves onto a uniform
@@ -48,9 +186,11 @@
   JSON-Schema/Avro/GraphQL/GraphSON/Protobuf coders (extended integer widths, map-key handling), the
   kernel's encode/decode generators (reversed type-parameter order on 2+-param polymorphic types), and
   the Python coder (silently dropping a module's types when types and terms share a file path).
-- **Self-hosted specification docs** ([#723](https://github.com/CategoricalData/hydra/issues/723)):
-  the module-reference pages under `docs/specification/` are now generated directly from the kernel
-  (`bin/regenerate-spec.sh`) via a new metadata-only doc coder, rather than hand-maintained.
+- **Specification-docs generator, kernel side** ([#723](https://github.com/CategoricalData/hydra/issues/723)):
+  a new metadata-only doc coder (`generateModuleDoc`) and `hydra.markdown` model, the translingual core
+  for generating the module-reference pages under `docs/specification/`. *Erratum:* the Java driver that
+  `bin/regenerate-spec.sh` invokes (`hydra.RegenerateSpec`) did not ship in 0.18.0, so the pages are still
+  hand-maintained; the driver is pending under #723.
 
 ### New features
 
@@ -58,7 +198,7 @@
   suite covering the minimal-core grammar (literals, classes, quantifiers, alternation, anchors,
   grouping) across all 9 hosts.
 - **[#723](https://github.com/CategoricalData/hydra/issues/723)** `hydra.markdown` type module and
-  `generateModuleDoc`, the metadata-only doc coder backing the self-generated spec pages.
+  `generateModuleDoc`, the metadata-only doc coder intended to back self-generated spec pages.
 - **[#382](https://github.com/CategoricalData/hydra/issues/382)** the Rust coder is split out of
   `hydra-ext` into its own first-class `hydra-rust` package.
 
diff --git a/hydra-pg.cabal b/hydra-pg.cabal
--- a/hydra-pg.cabal
+++ b/hydra-pg.cabal
@@ -5,7 +5,7 @@
 -- see: https://github.com/sol/hpack
 
 name:           hydra-pg
-version:        0.18.0
+version:        0.18.1
 synopsis:       Hydra's property-graph (TinkerPop/Gremlin) model and coder support
 description:    Hydra is an implementation of the LambdaGraph data model, which takes advantage of an isomorphism between labeled hypergraphs and typed lambda calculus: in Hydra, "graphs are programs, and programs are graphs". Property graph support for Hydra
 category:       Data
@@ -61,6 +61,7 @@
       Hydra.Pg.Query
       Hydra.Pg.Rdf.Environment
       Hydra.Pg.Rdf.Mappings
+      Hydra.Pg.SubtermGraph
       Hydra.Pg.TermsToElements
       Hydra.Pg.Tinkerpop.Examples.Modern
       Hydra.Pg.Tinkerpop.Features
@@ -77,7 +78,7 @@
       base >=4.19.0 && <4.22
     , bytestring >=0.11.5 && <0.13
     , containers >=0.6.7 && <0.8
-    , hydra-kernel ==0.18.0
-    , hydra-rdf ==0.18.0
+    , hydra-kernel ==0.18.1
+    , hydra-rdf ==0.18.1
     , scientific >=0.3.7 && <0.4
   default-language: Haskell2010
diff --git a/src/main/haskell/Hydra/Pg/Coder.hs b/src/main/haskell/Hydra/Pg/Coder.hs
--- a/src/main/haskell/Hydra/Pg/Coder.hs
+++ b/src/main/haskell/Hydra/Pg/Coder.hs
@@ -325,13 +325,18 @@
           values = Pairs.first (Pairs.second spec)
           alias = Pairs.second (Pairs.second spec)
           key = PgModel.PropertyKey (Optionals.withDefault (CoreModel.unName (CoreModel.fieldTypeName tfield)) alias)
+          ftyp = Strip.deannotateType (CoreModel.fieldTypeType tfield)
+          isMaybe =
+                  case ftyp of
+                    CoreModel.TypeOptional _ -> True
+                    _ -> False
       in (Eithers.bind (Coders.coderEncode (Mapping.schemaPropertyTypes schema) (CoreModel.fieldTypeType tfield)) (\pt -> Eithers.bind (TermsToElements.parseValueSpec cx g values) (\traversal -> Right (Coders.Adapter {
         Coders.adapterIsLossy = True,
         Coders.adapterSource = tfield,
         Coders.adapterTarget = PgModel.PropertyType {
           PgModel.propertyTypeKey = key,
           PgModel.propertyTypeValue = pt,
-          PgModel.propertyTypeRequired = True},
+          PgModel.propertyTypeRequired = (Logic.not isMaybe)},
         Coders.adapterCoder = Coders.Coder {
           Coders.coderEncode = (\dfield -> Eithers.bind (traverseToSingleTerm cx "property traversal" (traversal cx) (CoreModel.fieldTerm dfield)) (\result -> Eithers.bind (Coders.coderEncode (Mapping.schemaPropertyValues schema) result) (\value -> Right (PgModel.Property {
             PgModel.propertyKey = key,
@@ -344,7 +349,7 @@
     Lists.map (\a -> PgModel.PropertyType {
       PgModel.propertyTypeKey = (PgModel.propertyTypeKey (Coders.adapterTarget a)),
       PgModel.propertyTypeValue = (PgModel.propertyTypeValue (Coders.adapterTarget a)),
-      PgModel.propertyTypeRequired = True}) propAdapters
+      PgModel.propertyTypeRequired = (PgModel.propertyTypeRequired (Coders.adapterTarget a))}) propAdapters
 
 -- | Select an edge id from record fields using an id adapter
 selectEdgeId :: t0 -> M.Map CoreModel.Name t1 -> (CoreModel.Name, (Coders.Adapter t2 t3 t1 t4 Errors.Error)) -> Either Errors.Error t4
diff --git a/src/main/haskell/Hydra/Pg/Decode/Model.hs b/src/main/haskell/Hydra/Pg/Decode/Model.hs
--- a/src/main/haskell/Hydra/Pg/Decode/Model.hs
+++ b/src/main/haskell/Hydra/Pg/Decode/Model.hs
@@ -22,7 +22,7 @@
 import Data.Void
 
 -- | Decoder for hydra.pg.model.AdjacentEdge
-adjacentEdge :: (Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError t0) -> Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError (PgModel.AdjacentEdge t0)
+adjacentEdge :: (Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError v) -> Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError (PgModel.AdjacentEdge v)
 adjacentEdge v cx raw =
     Eithers.either (\err -> Left err) (\stripped -> case stripped of
       CoreModel.TermRecord v0 ->
@@ -57,7 +57,7 @@
       _ -> Left (Errors.DecodingError "expected union")) (ExtractModel.stripWithDecodingError cx raw)
 
 -- | Decoder for hydra.pg.model.Edge
-edge :: (Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError t0) -> Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError (PgModel.Edge t0)
+edge :: (Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError v) -> Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError (PgModel.Edge v)
 edge v cx raw =
     Eithers.either (\err -> Left err) (\stripped -> case stripped of
       CoreModel.TermRecord v0 ->
@@ -82,7 +82,7 @@
       _ -> Left (Errors.DecodingError "expected wrapped type")) (ExtractModel.stripWithDecodingError cx raw)
 
 -- | Decoder for hydra.pg.model.EdgeType
-edgeType :: (Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError t0) -> Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError (PgModel.EdgeType t0)
+edgeType :: (Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError t) -> Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError (PgModel.EdgeType t)
 edgeType t cx raw =
     Eithers.either (\err -> Left err) (\stripped -> case stripped of
       CoreModel.TermRecord v0 ->
@@ -96,7 +96,7 @@
       _ -> Left (Errors.DecodingError "expected a record of type hydra.pg.model.EdgeType")) (ExtractModel.stripWithDecodingError cx raw)
 
 -- | Decoder for hydra.pg.model.Element
-element :: (Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError t0) -> Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError (PgModel.Element t0)
+element :: (Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError v) -> Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError (PgModel.Element v)
 element v cx raw =
     Eithers.either (\err -> Left err) (\stripped -> case stripped of
       CoreModel.TermInject v0 ->
@@ -132,7 +132,7 @@
       _ -> Left (Errors.DecodingError "expected union")) (ExtractModel.stripWithDecodingError cx raw)
 
 -- | Decoder for hydra.pg.model.ElementTree
-elementTree :: (Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError t0) -> Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError (PgModel.ElementTree t0)
+elementTree :: (Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError v) -> Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError (PgModel.ElementTree v)
 elementTree v cx raw =
     Eithers.either (\err -> Left err) (\stripped -> case stripped of
       CoreModel.TermRecord v0 ->
@@ -143,7 +143,7 @@
       _ -> Left (Errors.DecodingError "expected a record of type hydra.pg.model.ElementTree")) (ExtractModel.stripWithDecodingError cx raw)
 
 -- | Decoder for hydra.pg.model.ElementType
-elementType :: (Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError t0) -> Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError (PgModel.ElementType t0)
+elementType :: (Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError t) -> Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError (PgModel.ElementType t)
 elementType t cx raw =
     Eithers.either (\err -> Left err) (\stripped -> case stripped of
       CoreModel.TermInject v0 ->
@@ -161,7 +161,7 @@
       _ -> Left (Errors.DecodingError "expected union")) (ExtractModel.stripWithDecodingError cx raw)
 
 -- | Decoder for hydra.pg.model.ElementTypeTree
-elementTypeTree :: (Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError t0) -> Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError (PgModel.ElementTypeTree t0)
+elementTypeTree :: (Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError t) -> Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError (PgModel.ElementTypeTree t)
 elementTypeTree t cx raw =
     Eithers.either (\err -> Left err) (\stripped -> case stripped of
       CoreModel.TermRecord v0 ->
@@ -172,7 +172,7 @@
       _ -> Left (Errors.DecodingError "expected a record of type hydra.pg.model.ElementTypeTree")) (ExtractModel.stripWithDecodingError cx raw)
 
 -- | Decoder for hydra.pg.model.Graph
-graph :: Ord t0 => ((Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError t0) -> Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError (PgModel.Graph t0))
+graph :: Ord v => ((Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError v) -> Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError (PgModel.Graph v))
 graph v cx raw =
     Eithers.either (\err -> Left err) (\stripped -> case stripped of
       CoreModel.TermRecord v0 ->
@@ -183,7 +183,7 @@
       _ -> Left (Errors.DecodingError "expected a record of type hydra.pg.model.Graph")) (ExtractModel.stripWithDecodingError cx raw)
 
 -- | Decoder for hydra.pg.model.GraphSchema
-graphSchema :: (Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError t0) -> Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError (PgModel.GraphSchema t0)
+graphSchema :: (Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError t) -> Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError (PgModel.GraphSchema t)
 graphSchema t cx raw =
     Eithers.either (\err -> Left err) (\stripped -> case stripped of
       CoreModel.TermRecord v0 ->
@@ -212,7 +212,7 @@
       _ -> Left (Errors.DecodingError "expected union")) (ExtractModel.stripWithDecodingError cx raw)
 
 -- | Decoder for hydra.pg.model.LazyGraph
-lazyGraph :: (Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError t0) -> Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError (PgModel.LazyGraph t0)
+lazyGraph :: (Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError v) -> Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError (PgModel.LazyGraph v)
 lazyGraph v cx raw =
     Eithers.either (\err -> Left err) (\stripped -> case stripped of
       CoreModel.TermRecord v0 ->
@@ -223,7 +223,7 @@
       _ -> Left (Errors.DecodingError "expected a record of type hydra.pg.model.LazyGraph")) (ExtractModel.stripWithDecodingError cx raw)
 
 -- | Decoder for hydra.pg.model.Property
-property :: (Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError t0) -> Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError (PgModel.Property t0)
+property :: (Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError v) -> Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError (PgModel.Property v)
 property v cx raw =
     Eithers.either (\err -> Left err) (\stripped -> case stripped of
       CoreModel.TermRecord v0 ->
@@ -245,7 +245,7 @@
       _ -> Left (Errors.DecodingError "expected wrapped type")) (ExtractModel.stripWithDecodingError cx raw)
 
 -- | Decoder for hydra.pg.model.PropertyType
-propertyType :: (Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError t0) -> Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError (PgModel.PropertyType t0)
+propertyType :: (Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError t) -> Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError (PgModel.PropertyType t)
 propertyType t cx raw =
     Eithers.either (\err -> Left err) (\stripped -> case stripped of
       CoreModel.TermRecord v0 ->
@@ -261,7 +261,7 @@
       _ -> Left (Errors.DecodingError "expected a record of type hydra.pg.model.PropertyType")) (ExtractModel.stripWithDecodingError cx raw)
 
 -- | Decoder for hydra.pg.model.Vertex
-vertex :: (Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError t0) -> Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError (PgModel.Vertex t0)
+vertex :: (Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError v) -> Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError (PgModel.Vertex v)
 vertex v cx raw =
     Eithers.either (\err -> Left err) (\stripped -> case stripped of
       CoreModel.TermRecord v0 ->
@@ -284,7 +284,7 @@
       _ -> Left (Errors.DecodingError "expected wrapped type")) (ExtractModel.stripWithDecodingError cx raw)
 
 -- | Decoder for hydra.pg.model.VertexType
-vertexType :: (Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError t0) -> Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError (PgModel.VertexType t0)
+vertexType :: (Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError t) -> Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError (PgModel.VertexType t)
 vertexType t cx raw =
     Eithers.either (\err -> Left err) (\stripped -> case stripped of
       CoreModel.TermRecord v0 ->
@@ -296,7 +296,7 @@
       _ -> Left (Errors.DecodingError "expected a record of type hydra.pg.model.VertexType")) (ExtractModel.stripWithDecodingError cx raw)
 
 -- | Decoder for hydra.pg.model.VertexWithAdjacentEdges
-vertexWithAdjacentEdges :: (Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError t0) -> Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError (PgModel.VertexWithAdjacentEdges t0)
+vertexWithAdjacentEdges :: (Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError v) -> Graph.Graph -> CoreModel.Term -> Either Errors.DecodingError (PgModel.VertexWithAdjacentEdges v)
 vertexWithAdjacentEdges v cx raw =
     Eithers.either (\err -> Left err) (\stripped -> case stripped of
       CoreModel.TermRecord v0 ->
diff --git a/src/main/haskell/Hydra/Pg/Dsl/Model.hs b/src/main/haskell/Hydra/Pg/Dsl/Model.hs
--- a/src/main/haskell/Hydra/Pg/Dsl/Model.hs
+++ b/src/main/haskell/Hydra/Pg/Dsl/Model.hs
@@ -4,6 +4,7 @@
 
 module Hydra.Pg.Dsl.Model where
 
+import qualified Hydra.Core.Dsl.Model as DslModel
 import qualified Hydra.Core.Errors as Errors
 import qualified Hydra.Core.Graph as Graph
 import qualified Hydra.Core.Model as CoreModel
diff --git a/src/main/haskell/Hydra/Pg/Encode/Model.hs b/src/main/haskell/Hydra/Pg/Encode/Model.hs
--- a/src/main/haskell/Hydra/Pg/Encode/Model.hs
+++ b/src/main/haskell/Hydra/Pg/Encode/Model.hs
@@ -14,7 +14,7 @@
 import Data.Void
 
 -- | Encoder for hydra.pg.model.AdjacentEdge
-adjacentEdge :: (t0 -> CoreModel.Term) -> PgModel.AdjacentEdge t0 -> CoreModel.Term
+adjacentEdge :: (v -> CoreModel.Term) -> PgModel.AdjacentEdge v -> CoreModel.Term
 adjacentEdge v x =
     CoreModel.TermRecord (CoreModel.Record {
       CoreModel.recordTypeName = (CoreModel.Name "hydra.pg.model.AdjacentEdge"),
@@ -58,7 +58,7 @@
           CoreModel.fieldTerm = CoreModel.TermUnit}})
 
 -- | Encoder for hydra.pg.model.Edge
-edge :: (t0 -> CoreModel.Term) -> PgModel.Edge t0 -> CoreModel.Term
+edge :: (v -> CoreModel.Term) -> PgModel.Edge v -> CoreModel.Term
 edge v x =
     CoreModel.TermRecord (CoreModel.Record {
       CoreModel.recordTypeName = (CoreModel.Name "hydra.pg.model.Edge"),
@@ -87,7 +87,7 @@
       CoreModel.wrappedTermBody = ((\x2 -> CoreModel.TermLiteral (CoreModel.LiteralString x2)) (PgModel.unEdgeLabel x))})
 
 -- | Encoder for hydra.pg.model.EdgeType
-edgeType :: (t0 -> CoreModel.Term) -> PgModel.EdgeType t0 -> CoreModel.Term
+edgeType :: (t -> CoreModel.Term) -> PgModel.EdgeType t -> CoreModel.Term
 edgeType t x =
     CoreModel.TermRecord (CoreModel.Record {
       CoreModel.recordTypeName = (CoreModel.Name "hydra.pg.model.EdgeType"),
@@ -109,7 +109,7 @@
           CoreModel.fieldTerm = ((\xs -> CoreModel.TermList (Lists.map (propertyType t) xs)) (PgModel.edgeTypeProperties x))}]})
 
 -- | Encoder for hydra.pg.model.Element
-element :: (t0 -> CoreModel.Term) -> PgModel.Element t0 -> CoreModel.Term
+element :: (v -> CoreModel.Term) -> PgModel.Element v -> CoreModel.Term
 element v x =
     case x of
       PgModel.ElementVertex v0 -> CoreModel.TermInject (CoreModel.Injection {
@@ -139,7 +139,7 @@
           CoreModel.fieldTerm = CoreModel.TermUnit}})
 
 -- | Encoder for hydra.pg.model.ElementTree
-elementTree :: (t0 -> CoreModel.Term) -> PgModel.ElementTree t0 -> CoreModel.Term
+elementTree :: (v -> CoreModel.Term) -> PgModel.ElementTree v -> CoreModel.Term
 elementTree v x =
     CoreModel.TermRecord (CoreModel.Record {
       CoreModel.recordTypeName = (CoreModel.Name "hydra.pg.model.ElementTree"),
@@ -152,7 +152,7 @@
           CoreModel.fieldTerm = ((\xs -> CoreModel.TermList (Lists.map (elementTree v) xs)) (PgModel.elementTreeDependencies x))}]})
 
 -- | Encoder for hydra.pg.model.ElementType
-elementType :: (t0 -> CoreModel.Term) -> PgModel.ElementType t0 -> CoreModel.Term
+elementType :: (t -> CoreModel.Term) -> PgModel.ElementType t -> CoreModel.Term
 elementType t x =
     case x of
       PgModel.ElementTypeVertex v0 -> CoreModel.TermInject (CoreModel.Injection {
@@ -167,7 +167,7 @@
           CoreModel.fieldTerm = (edgeType t v0)}})
 
 -- | Encoder for hydra.pg.model.ElementTypeTree
-elementTypeTree :: (t0 -> CoreModel.Term) -> PgModel.ElementTypeTree t0 -> CoreModel.Term
+elementTypeTree :: (t -> CoreModel.Term) -> PgModel.ElementTypeTree t -> CoreModel.Term
 elementTypeTree t x =
     CoreModel.TermRecord (CoreModel.Record {
       CoreModel.recordTypeName = (CoreModel.Name "hydra.pg.model.ElementTypeTree"),
@@ -180,7 +180,7 @@
           CoreModel.fieldTerm = ((\xs -> CoreModel.TermList (Lists.map (elementTypeTree t) xs)) (PgModel.elementTypeTreeDependencies x))}]})
 
 -- | Encoder for hydra.pg.model.Graph
-graph :: Ord t0 => ((t0 -> CoreModel.Term) -> PgModel.Graph t0 -> CoreModel.Term)
+graph :: Ord v => ((v -> CoreModel.Term) -> PgModel.Graph v -> CoreModel.Term)
 graph v x =
     CoreModel.TermRecord (CoreModel.Record {
       CoreModel.recordTypeName = (CoreModel.Name "hydra.pg.model.Graph"),
@@ -193,7 +193,7 @@
           CoreModel.fieldTerm = ((\m -> CoreModel.TermMap (Maps.bimap v (edge v) m)) (PgModel.graphEdges x))}]})
 
 -- | Encoder for hydra.pg.model.GraphSchema
-graphSchema :: (t0 -> CoreModel.Term) -> PgModel.GraphSchema t0 -> CoreModel.Term
+graphSchema :: (t -> CoreModel.Term) -> PgModel.GraphSchema t -> CoreModel.Term
 graphSchema t x =
     CoreModel.TermRecord (CoreModel.Record {
       CoreModel.recordTypeName = (CoreModel.Name "hydra.pg.model.GraphSchema"),
@@ -221,7 +221,7 @@
           CoreModel.fieldTerm = (edgeLabel v0)}})
 
 -- | Encoder for hydra.pg.model.LazyGraph
-lazyGraph :: (t0 -> CoreModel.Term) -> PgModel.LazyGraph t0 -> CoreModel.Term
+lazyGraph :: (v -> CoreModel.Term) -> PgModel.LazyGraph v -> CoreModel.Term
 lazyGraph v x =
     CoreModel.TermRecord (CoreModel.Record {
       CoreModel.recordTypeName = (CoreModel.Name "hydra.pg.model.LazyGraph"),
@@ -234,7 +234,7 @@
           CoreModel.fieldTerm = ((\xs -> CoreModel.TermList (Lists.map (edge v) xs)) (PgModel.lazyGraphEdges x))}]})
 
 -- | Encoder for hydra.pg.model.Property
-property :: (t0 -> CoreModel.Term) -> PgModel.Property t0 -> CoreModel.Term
+property :: (v -> CoreModel.Term) -> PgModel.Property v -> CoreModel.Term
 property v x =
     CoreModel.TermRecord (CoreModel.Record {
       CoreModel.recordTypeName = (CoreModel.Name "hydra.pg.model.Property"),
@@ -254,7 +254,7 @@
       CoreModel.wrappedTermBody = ((\x2 -> CoreModel.TermLiteral (CoreModel.LiteralString x2)) (PgModel.unPropertyKey x))})
 
 -- | Encoder for hydra.pg.model.PropertyType
-propertyType :: (t0 -> CoreModel.Term) -> PgModel.PropertyType t0 -> CoreModel.Term
+propertyType :: (t -> CoreModel.Term) -> PgModel.PropertyType t -> CoreModel.Term
 propertyType t x =
     CoreModel.TermRecord (CoreModel.Record {
       CoreModel.recordTypeName = (CoreModel.Name "hydra.pg.model.PropertyType"),
@@ -270,7 +270,7 @@
           CoreModel.fieldTerm = ((\x2 -> CoreModel.TermLiteral (CoreModel.LiteralBoolean x2)) (PgModel.propertyTypeRequired x))}]})
 
 -- | Encoder for hydra.pg.model.Vertex
-vertex :: (t0 -> CoreModel.Term) -> PgModel.Vertex t0 -> CoreModel.Term
+vertex :: (v -> CoreModel.Term) -> PgModel.Vertex v -> CoreModel.Term
 vertex v x =
     CoreModel.TermRecord (CoreModel.Record {
       CoreModel.recordTypeName = (CoreModel.Name "hydra.pg.model.Vertex"),
@@ -293,7 +293,7 @@
       CoreModel.wrappedTermBody = ((\x2 -> CoreModel.TermLiteral (CoreModel.LiteralString x2)) (PgModel.unVertexLabel x))})
 
 -- | Encoder for hydra.pg.model.VertexType
-vertexType :: (t0 -> CoreModel.Term) -> PgModel.VertexType t0 -> CoreModel.Term
+vertexType :: (t -> CoreModel.Term) -> PgModel.VertexType t -> CoreModel.Term
 vertexType t x =
     CoreModel.TermRecord (CoreModel.Record {
       CoreModel.recordTypeName = (CoreModel.Name "hydra.pg.model.VertexType"),
@@ -309,7 +309,7 @@
           CoreModel.fieldTerm = ((\xs -> CoreModel.TermList (Lists.map (propertyType t) xs)) (PgModel.vertexTypeProperties x))}]})
 
 -- | Encoder for hydra.pg.model.VertexWithAdjacentEdges
-vertexWithAdjacentEdges :: (t0 -> CoreModel.Term) -> PgModel.VertexWithAdjacentEdges t0 -> CoreModel.Term
+vertexWithAdjacentEdges :: (v -> CoreModel.Term) -> PgModel.VertexWithAdjacentEdges v -> CoreModel.Term
 vertexWithAdjacentEdges v x =
     CoreModel.TermRecord (CoreModel.Record {
       CoreModel.recordTypeName = (CoreModel.Name "hydra.pg.model.VertexWithAdjacentEdges"),
diff --git a/src/main/haskell/Hydra/Pg/SubtermGraph.hs b/src/main/haskell/Hydra/Pg/SubtermGraph.hs
new file mode 100644
--- /dev/null
+++ b/src/main/haskell/Hydra/Pg/SubtermGraph.hs
@@ -0,0 +1,211 @@
+-- Note: this is an automatically generated file. Do not edit.
+
+-- | The property-graph encoding of a term graph (hydra.core.subterms.TermGraph): a total, lossless map from the already-built, already-classified term-graph view of a typed graph onto hydra.pg's element model. No bespoke term traversal; this module consumes only the TermGraph interface.
+
+module Hydra.Pg.SubtermGraph where
+
+import qualified Hydra.Core.Ast as Ast
+import qualified Hydra.Core.Coders as Coders
+import qualified Hydra.Core.Diff as Diff
+import qualified Hydra.Core.Docs as Docs
+import qualified Hydra.Core.Error.Checking as Checking
+import qualified Hydra.Core.Error.File as ErrorFile
+import qualified Hydra.Core.Error.Model as ErrorModel
+import qualified Hydra.Core.Error.Packaging as ErrorPackaging
+import qualified Hydra.Core.Error.System as ErrorSystem
+import qualified Hydra.Core.Errors as Errors
+import qualified Hydra.Core.File as File
+import qualified Hydra.Core.Graph as Graph
+import qualified Hydra.Core.Json.Model as JsonModel
+import qualified Hydra.Core.Overlay.Haskell.Lib.Lists as Lists
+import qualified Hydra.Core.Overlay.Haskell.Lib.Logic as Logic
+import qualified Hydra.Core.Overlay.Haskell.Lib.Maps as Maps
+import qualified Hydra.Core.Overlay.Haskell.Lib.Optionals as Optionals
+import qualified Hydra.Core.Overlay.Haskell.Lib.Pairs as Pairs
+import qualified Hydra.Core.Overlay.Haskell.Lib.Strings as Strings
+import qualified Hydra.Core.Markdown as Markdown
+import qualified Hydra.Core.Model as CoreModel
+import qualified Hydra.Core.Packaging as Packaging
+import qualified Hydra.Core.Parsing as Parsing
+import qualified Hydra.Core.Paths as Paths
+import qualified Hydra.Core.Print.Model as PrintModel
+import qualified Hydra.Core.Print.Paths as PrintPaths
+import qualified Hydra.Core.Query as Query
+import qualified Hydra.Core.Regex as Regex
+import qualified Hydra.Core.Relational as Relational
+import qualified Hydra.Core.System as System
+import qualified Hydra.Core.Tabular as Tabular
+import qualified Hydra.Core.Testing as Testing
+import qualified Hydra.Core.Time as Time
+import qualified Hydra.Core.Topology as Topology
+import qualified Hydra.Core.Typed as Typed
+import qualified Hydra.Core.Typing as Typing
+import qualified Hydra.Core.Util as Util
+import qualified Hydra.Core.Validation as Validation
+import qualified Hydra.Core.Variants as Variants
+import qualified Hydra.Pg.Model as PgModel
+import Prelude hiding  (Enum, Ordering, decodeFloat, encodeFloat, fail, lines, map, pure, sum, unlines)
+import qualified Data.Scientific as Sci
+import Data.Void
+import qualified Data.Map as M
+
+-- | Encode a term graph as a property graph: a vertex per non-leaf node, an edge per non-primitive variable reference, printed subterm paths as edge labels/ids and property keys
+encodeTermGraph :: Paths.TermGraph -> PgModel.Graph CoreModel.Term
+encodeTermGraph tg =
+
+      let roots = Paths.termGraphRoots tg
+          rootResults =
+                  Lists.map (\nt ->
+                    let name = Pairs.first nt
+                        node = Pairs.second nt
+                        emptyPath = Paths.SubtermPath []
+                        sub = encodeTermNode name emptyPath node
+                        rootVertex =
+                                PgModel.Vertex {
+                                  PgModel.vertexLabel = (PgModel.VertexLabel (PrintModel.type_ (Paths.termNodeType node))),
+                                  PgModel.vertexId = (termNodeIdTerm (Paths.TermNodeId {
+                                    Paths.termNodeIdRoot = name,
+                                    Paths.termNodeIdPath = emptyPath})),
+                                  PgModel.vertexProperties = (Pairs.first sub)}
+                    in (Lists.concat2 [
+                      rootVertex] (Pairs.first (Pairs.second sub)), (Pairs.second (Pairs.second sub)))) (Maps.toList roots)
+          allVertices = Lists.concat (Lists.map Pairs.first rootResults)
+          allEdges = Lists.concat (Lists.map Pairs.second rootResults)
+      in PgModel.Graph {
+        PgModel.graphVertices = (Maps.fromList (Lists.map (\v -> (PgModel.vertexId v, v)) allVertices)),
+        PgModel.graphEdges = (Maps.fromList (Lists.map (\e -> (PgModel.edgeId e, e)) allEdges))}
+
+-- | Encode one term-graph node into (this node's own properties, descendant vertices, descendant edges)
+encodeTermNode :: CoreModel.Name -> Paths.SubtermPath -> Paths.TermNode -> (M.Map PgModel.PropertyKey CoreModel.Term, ([PgModel.Vertex CoreModel.Term], [PgModel.Edge CoreModel.Term]))
+encodeTermNode root path node =
+
+      let nid =
+              Paths.TermNodeId {
+                Paths.termNodeIdRoot = root,
+                Paths.termNodeIdPath = path}
+          steps = Paths.unSubtermPath path
+          links = Paths.termNodeLinks node
+          childPathFor = \step -> Paths.SubtermPath (Lists.concat2 steps [
+                step])
+          keyFor = \p -> PgModel.PropertyKey (PrintPaths.subtermPath p)
+          attrKeyFor =
+                  \a -> PgModel.PropertyKey (Strings.concat [
+                    PrintPaths.subtermPath path,
+                    "@",
+                    (termAttributeLinkTag a)])
+          perLink =
+                  Lists.map (\link -> case link of
+                    Paths.TermLinkAttribute v0 -> (Just (attrKeyFor v0, (termAttributeLinkValue v0)), ([], []))
+                    Paths.TermLinkSubterm v0 ->
+                      let step = Paths.subtermLinkStep v0
+                          child = Paths.subtermLinkChild v0
+                          childPath = childPathFor step
+                      in (Logic.ifElse (isLeafNode child) (Just (keyFor childPath, (Paths.termNodeTerm child)), ([], [])) (
+                        let sub = encodeTermNode root childPath child
+                            childVertex =
+                                    PgModel.Vertex {
+                                      PgModel.vertexLabel = (PgModel.VertexLabel (PrintModel.type_ (Paths.termNodeType child))),
+                                      PgModel.vertexId = (termNodeIdTerm (Paths.TermNodeId {
+                                        Paths.termNodeIdRoot = root,
+                                        Paths.termNodeIdPath = childPath})),
+                                      PgModel.vertexProperties = (Pairs.first sub)}
+                        in (Nothing, (Lists.concat2 [
+                          childVertex] (Pairs.first (Pairs.second sub)), (Pairs.second (Pairs.second sub))))))
+                    Paths.TermLinkReference v0 ->
+                      let step = Paths.termReferenceLinkStep v0
+                          target = Paths.termReferenceLinkTarget v0
+                          edgePath = childPathFor step
+                          edgeKey = PrintPaths.subtermPath edgePath
+                          noVertices = []
+                          noEdges = []
+                          noProp = Nothing
+                      in (Optionals.match (termReferenceNodeId target) (Optionals.match (termReferencePrimitiveName target) (noProp, (noVertices, noEdges)) (\pname -> (Just (keyFor edgePath, (CoreModel.TermLiteral (CoreModel.LiteralString (CoreModel.unName pname)))), (noVertices, noEdges)))) (\targetId ->
+                        let edge =
+                                PgModel.Edge {
+                                  PgModel.edgeLabel = (PgModel.EdgeLabel edgeKey),
+                                  PgModel.edgeId = (CoreModel.TermLiteral (CoreModel.LiteralString (Strings.concat [
+                                    CoreModel.unName root,
+                                    "#",
+                                    edgeKey]))),
+                                  PgModel.edgeOut = (termNodeIdTerm nid),
+                                  PgModel.edgeIn = (termNodeIdTerm targetId),
+                                  PgModel.edgeProperties = Maps.empty}
+                        in (noProp, (noVertices, [
+                          edge]))))) links
+          ownProps = Maps.fromList (Optionals.mapOptional Pairs.first perLink)
+          descendants = Lists.map Pairs.second perLink
+      in (ownProps, (Lists.concat (Lists.map Pairs.first descendants), (Lists.concat (Lists.map Pairs.second descendants))))
+
+-- | Whether a term-graph node has only attribute links (no subterm or reference links), and so folds into a parent property rather than its own vertex
+isLeafNode :: Paths.TermNode -> Bool
+isLeafNode node =
+    Lists.isEmpty (Lists.filter (\link -> case link of
+      Paths.TermLinkAttribute _ -> False
+      _ -> True) (Paths.termNodeLinks node))
+
+-- | The variant tag of a term attribute link, used to disambiguate same-path attribute keys
+termAttributeLinkTag :: Paths.TermAttributeLink -> String
+termAttributeLinkTag attr =
+    case attr of
+      Paths.TermAttributeLinkCasesTypeName _ -> "casesTypeName"
+      Paths.TermAttributeLinkInjectTypeName _ -> "injectTypeName"
+      Paths.TermAttributeLinkLambdaDomainGiven _ -> "lambdaDomainGiven"
+      Paths.TermAttributeLinkLambdaParameter _ -> "lambdaParameter"
+      Paths.TermAttributeLinkLetBindingTypeSchemeGiven _ -> "letBindingTypeSchemeGiven"
+      Paths.TermAttributeLinkLiteral _ -> "literal"
+      Paths.TermAttributeLinkProjectFieldName _ -> "projectFieldName"
+      Paths.TermAttributeLinkProjectTypeName _ -> "projectTypeName"
+      Paths.TermAttributeLinkRecordTypeName _ -> "recordTypeName"
+      Paths.TermAttributeLinkTypeApplicationType _ -> "typeApplicationType"
+      Paths.TermAttributeLinkTypeLambdaParameter _ -> "typeLambdaParameter"
+      Paths.TermAttributeLinkUnwrapTypeName _ -> "unwrapTypeName"
+      Paths.TermAttributeLinkWrapTypeName _ -> "wrapTypeName"
+
+-- | Encode a term attribute link's payload as a term
+termAttributeLinkValue :: Paths.TermAttributeLink -> CoreModel.Term
+termAttributeLinkValue attr =
+    case attr of
+      Paths.TermAttributeLinkCasesTypeName v0 -> CoreModel.TermLiteral (CoreModel.LiteralString (CoreModel.unName v0))
+      Paths.TermAttributeLinkInjectTypeName v0 -> CoreModel.TermLiteral (CoreModel.LiteralString (CoreModel.unName v0))
+      Paths.TermAttributeLinkLambdaDomainGiven v0 -> CoreModel.TermLiteral (CoreModel.LiteralString (PrintModel.type_ v0))
+      Paths.TermAttributeLinkLambdaParameter v0 -> CoreModel.TermLiteral (CoreModel.LiteralString (CoreModel.unName v0))
+      Paths.TermAttributeLinkLetBindingTypeSchemeGiven v0 -> CoreModel.TermLiteral (CoreModel.LiteralString (Strings.concat [
+        CoreModel.unName (Pairs.first v0),
+        ": ",
+        (PrintModel.typeScheme (Pairs.second v0))]))
+      Paths.TermAttributeLinkLiteral v0 -> CoreModel.TermLiteral v0
+      Paths.TermAttributeLinkProjectFieldName v0 -> CoreModel.TermLiteral (CoreModel.LiteralString (CoreModel.unName v0))
+      Paths.TermAttributeLinkProjectTypeName v0 -> CoreModel.TermLiteral (CoreModel.LiteralString (CoreModel.unName v0))
+      Paths.TermAttributeLinkRecordTypeName v0 -> CoreModel.TermLiteral (CoreModel.LiteralString (CoreModel.unName v0))
+      Paths.TermAttributeLinkTypeApplicationType v0 -> CoreModel.TermLiteral (CoreModel.LiteralString (PrintModel.type_ v0))
+      Paths.TermAttributeLinkTypeLambdaParameter v0 -> CoreModel.TermLiteral (CoreModel.LiteralString (CoreModel.unName v0))
+      Paths.TermAttributeLinkUnwrapTypeName v0 -> CoreModel.TermLiteral (CoreModel.LiteralString (CoreModel.unName v0))
+      Paths.TermAttributeLinkWrapTypeName v0 -> CoreModel.TermLiteral (CoreModel.LiteralString (CoreModel.unName v0))
+
+-- | Encode a TermNodeId as a term: the root binding name, plus '#' and the printed path if non-empty
+termNodeIdTerm :: Paths.TermNodeId -> CoreModel.Term
+termNodeIdTerm nid =
+
+      let root = Paths.termNodeIdRoot nid
+          path = Paths.termNodeIdPath nid
+          steps = Paths.unSubtermPath path
+      in (CoreModel.TermLiteral (CoreModel.LiteralString (Logic.ifElse (Lists.isEmpty steps) (CoreModel.unName root) (Strings.concat [
+        CoreModel.unName root,
+        "#",
+        (PrintPaths.subtermPath path)]))))
+
+-- | The binder node id of a lambda- or let-bound variable reference; Nothing for a primitive reference
+termReferenceNodeId :: Paths.TermReference -> Maybe Paths.TermNodeId
+termReferenceNodeId ref =
+    case ref of
+      Paths.TermReferenceLambda v0 -> Just (Paths.lambdaVariableReferenceBoundByNode v0)
+      Paths.TermReferenceLet v0 -> Just (Paths.letVariableReferenceBindingNode v0)
+      Paths.TermReferencePrimitive _ -> Nothing
+
+-- | The primitive's name, for a primitive reference; Nothing otherwise
+termReferencePrimitiveName :: Paths.TermReference -> Maybe CoreModel.Name
+termReferencePrimitiveName ref =
+    case ref of
+      Paths.TermReferenceLambda _ -> Nothing
+      Paths.TermReferenceLet _ -> Nothing
+      Paths.TermReferencePrimitive v0 -> Just (Paths.primitiveReferenceName v0)
