| Safe Haskell | None |
|---|---|
| Language | GHC2024 |
Pykinos.Schema.HiddenClass
Description
Eliminates dynamic Map Text Expr trees by interning field names into
rank-ordered FieldIds and representing record shapes as interned SchemaIds
with precomputed 3-way MergeMorph3 gather plans.
Architectural Rationale
Traditional AST record representations in interpreters (such as Dhall and 1ML)
use unbalanced search trees or associative maps. When performing record operations
(such as record merge //, deep meet /\, or signature projection :>), every
operation traverses pointer-heavy tree structures, resulting in significant GC
pressure and branch mispredictions.
By decoupling the field names into a frozen, rank-ordered Lexicon and mapping
field sets into contiguous sorted PrimArrays of Word32s (SchemaId), record
merge operations become pure array-to-array linear scans synthesized in $O(N_1 + N_2)$
time into unboxed 64-bit gather codes (MergeMorph3).
3-Way Gather Code Representation
Each gather code in MergeMorph3 is an unboxed Int64:
- Positive values (
code >= 0): The field is copied directly from slotcodeof the left record. - Negative values with bit 62 clear (
code < 0,bit 62 == 0): The field is copied directly from slotcomplement codeof the right record. - Negative values with bit 62 set (
code < 0,bit 62 == 1): Both records contain the field. A collision has occurred that must be resolved via recursive normalization (forOpCombineDeep). The two source slot indices(idx1, idx2)are packed into bits 0..59.
Synopsis
- newtype FieldId = FieldId {
- unFieldId :: Word32
- newtype SchemaId = SchemaId {
- unSchemaId :: Word32
- data RecordOp
- newtype MergeMorph3 = MergeMorph3 {
- unMergeMorph3 :: PrimArray Int64
- data Lexicon = Lexicon {
- lexToId :: !(Map Text FieldId)
- lexToText :: !(SmallArray Text)
- buildLexicon :: [Text] -> Lexicon
- lookupFieldId :: Lexicon -> Text -> Maybe FieldId
- lookupFieldText :: Lexicon -> FieldId -> Text
- findFieldSlot :: PrimArray Word32 -> FieldId -> Maybe Int
- collisionBit3 :: Int64
- encodeRightSlot3 :: Int -> Int64
- encodeCollision3 :: Int -> Int -> Int64
- decodeMergeCode3 :: Int64 -> Either (Either Int Int) (Int, Int)
- synthesizeMergeMorph3 :: RecordOp -> PrimArray Word32 -> PrimArray Word32 -> (PrimArray Word32, MergeMorph3)
Field and Schema Identifiers
Rank-ordered 32-bit field identifier.
Invariant: For all field identifiers fid1 and fid2,
fid1 < fid2 holds if and only if text(fid1) < text(fid2) in standard
lexicographical UTF-8 byte order. This invariant guarantees that sorting by
FieldId is completely isomorphic to sorting by record label Text.
Interned 32-bit identifier for a sorted PrimArray of FieldIds.
A SchemaId represents the immutable structural type of a record. Two records
share the same SchemaId if and only if they possess identical field sets in
identical order.
Constructors
| SchemaId | |
Fields
| |
Dynamic Record Operations
Dynamic record algebra operations supported by MergeMorph3.
These correspond to the fundamental record combinators in languages with first-class records and modules:
Constructors
| OpPreferR | Right-biased override union. When a label appears in both records,
the value from the right-hand record overwrites the left-hand record.
Corresponds to Dhall's |
| OpCombineDeep | Recursive deep meet. When a label appears in both records,
the two values collide and must be recursively merged.
Corresponds to Dhall's |
| OpRestrict | Domain restriction / projection. Restricts the fields of the left
record to the schema defined by the right record.
Corresponds to 1ML existential sealing ( |
newtype MergeMorph3 Source #
3-way gather plan encoded in an unboxed PrimArray of Int64 codes.
The array length matches the number of fields in the resulting merged record. Each slot contains an instruction specifying how the target value is obtained:
code >= 0: copy slotcodefrom left recordcode < 0, bit 62 == 0: copy slotcomplement codefrom right recordcode < 0, bit 62 == 1: lazy deep-meet collision on(idx1, idx2)
Constructors
| MergeMorph3 | |
Fields
| |
Instances
| Eq MergeMorph3 Source # | |
Defined in Pykinos.Schema.HiddenClass | |
| Show MergeMorph3 Source # | |
Defined in Pykinos.Schema.HiddenClass Methods showsPrec :: Int -> MergeMorph3 -> ShowS show :: MergeMorph3 -> String showList :: [MergeMorph3] -> ShowS | |
Symbol Lexicon
Frozen rank-ordered lexicon mapping Text labels to FieldIds.
Provides bidirectional O(log L) lookup from Text to FieldId and
O(1) indexed recovery from FieldId back to Text.
Constructors
| Lexicon | |
Fields
| |
buildLexicon :: [Text] -> Lexicon Source #
Construct a rank-ordered Lexicon from all field labels appearing in an AST.
Collects all unique labels, sorts them into strictly ascending order, and assigns
zero-based sequential Word32 identifiers.
lookupFieldId :: Lexicon -> Text -> Maybe FieldId Source #
Resolve a Text label to its rank-ordered FieldId in O(log L) time.
lookupFieldText :: Lexicon -> FieldId -> Text Source #
Recover the original Text label from a FieldId in strictly O(1) time.
Slot Search and Gather Code Encoding
findFieldSlot :: PrimArray Word32 -> FieldId -> Maybe Int Source #
collisionBit3 :: Int64 Source #
Bit 62 distinguishes a right-record slot from a two-record deep collision. Bit 63 is the sign bit (1 for negative numbers). Bit 62 serves as the discriminator flag.
encodeRightSlot3 :: Int -> Int64 Source #
Encode a right-record slot index idx2 as a negative Int64 with bit 62 clear.
Uses bitwise complement (complement) so that idx2 = 0 maps to -1,
idx2 = 1 maps to -2, etc., ensuring strict negativity while keeping bit 62 zero.
encodeCollision3 :: Int -> Int -> Int64 Source #
Encode a colliding pair of slot indices (idx1, idx2) for OpCombineDeep.
Packs idx1 into bits 30..59 and idx2 into bits 0..29, sets bit 62, and negates.
decodeMergeCode3 :: Int64 -> Either (Either Int Int) (Int, Int) Source #
Decode a MergeMorph3 gather code into its constituent source slots:
Left (Left idx1): Unambiguous value from slotidx1of the left record.Left (Right idx2): Unambiguous value from slotidx2of the right record.Right (idx1, idx2): Recursive collision betweenidx1andidx2.
Linear Merge Synthesis
synthesizeMergeMorph3 :: RecordOp -> PrimArray Word32 -> PrimArray Word32 -> (PrimArray Word32, MergeMorph3) Source #
Two-finger linear synthesis of a 3-way MergeMorph3 over two sorted schemas.
Traverses the two sorted PrimArrays in parallel in $O(N_1 + N_2)$ time, emitting:
- The merged schema's sorted
PrimArrayof field IDs. - The compiled
MergeMorph3gather codes for instantiating the merged record values.