|
| 1 | +module Unison.Runtime.Referenced |
| 2 | + ( Referenced (..), |
| 3 | + dereference, |
| 4 | + RTrav, |
| 5 | + Canonize, |
| 6 | + canonicalizeRefs, |
| 7 | + recanonicalizeRefs, |
| 8 | + ) |
| 9 | +where |
| 10 | + |
| 11 | +import Control.Monad.State.Strict |
| 12 | +import Data.Maybe (mapMaybe) |
| 13 | +import Unison.Reference |
| 14 | +import Unison.Runtime.Canonicalizer |
| 15 | + |
| 16 | +-- A value with optional optimization information for serialization. |
| 17 | +-- The references are required for serialization V5, and are assumed |
| 18 | +-- to be the only references used in the value _up to in-memory |
| 19 | +-- uniqueness_. |
| 20 | +-- |
| 21 | +-- This is parameterized so that it can be used with both Value and |
| 22 | +-- Code. |
| 23 | +-- |
| 24 | +-- Also note, the stored referenced might not be 'tight' in the sense |
| 25 | +-- that they all actually occur in the value. Maintaining this |
| 26 | +-- invariant together with actual canonicalization would be onerous |
| 27 | +-- and isn't done at this time. |
| 28 | +data Referenced a |
| 29 | + = -- types, terms |
| 30 | + WithRefs [Reference] [Reference] a |
| 31 | + | Plain a |
| 32 | + deriving (Show, Eq) |
| 33 | + |
| 34 | +dereference :: Referenced a -> a |
| 35 | +dereference (WithRefs _ _ x) = x |
| 36 | +dereference (Plain x) = x |
| 37 | + |
| 38 | +type RTrav a = |
| 39 | + forall f. |
| 40 | + (Applicative f) => |
| 41 | + (Bool -> Reference -> f Reference) -> |
| 42 | + (a -> f a) |
| 43 | + |
| 44 | +type Canonize = |
| 45 | + StateT (Canonicalizer Reference, [Reference], [Reference]) IO |
| 46 | + |
| 47 | +-- Given a reference traversal, canonicalizes the references in a |
| 48 | +-- value. The operation is presented as a state transformation, so |
| 49 | +-- that it can hook into a larger canonicalization procedure. The |
| 50 | +-- lists of canonical references of each sort are yielded as part of |
| 51 | +-- the state. |
| 52 | +canonicalizeRefs :: RTrav a -> a -> Canonize a |
| 53 | +canonicalizeRefs trav = trav h |
| 54 | + where |
| 55 | + h isTy r = StateT \st@(canon, tys, tms) -> |
| 56 | + categorize canon r >>= \case |
| 57 | + Canonical -> pure (r, st) |
| 58 | + Novel canon |
| 59 | + | isTy -> pure (r, (canon, r : tys, tms)) |
| 60 | + | otherwise -> pure (r, (canon, tys, r : tms)) |
| 61 | + Equivalent s canon -> pure (s, (canon, tys, tms)) |
| 62 | +{-# INLINE canonicalizeRefs #-} |
| 63 | + |
| 64 | +-- Given a `Referenced` value, this recanonicalizes the references in |
| 65 | +-- the wrapped value. The intention is for this to be hooked into a |
| 66 | +-- larger canonicalization procedure, so that already canonicalized |
| 67 | +-- values can be more efficiently brought in line with other values |
| 68 | +-- that are already canonicalized. |
| 69 | +-- |
| 70 | +-- If the `Referenced` value is `Plain`, then all we can do is |
| 71 | +-- traverse it, canonicalizing the references. However, if it is |
| 72 | +-- tagged with canonical refs, we can see if they all match existing |
| 73 | +-- canonical refs. If so, we don't need to traverse the value. Even if |
| 74 | +-- not, we can traverse with marginally more efficient lookups. |
| 75 | +recanonicalizeRefs :: RTrav a -> Referenced a -> Canonize a |
| 76 | +recanonicalizeRefs trav = \case |
| 77 | + Plain v -> canonicalizeRefs trav v |
| 78 | + WithRefs tys tms v -> do |
| 79 | + typs <- mapMaybe id <$> traverse (g True) tys |
| 80 | + tmps <- mapMaybe id <$> traverse (g False) tms |
| 81 | + |
| 82 | + ctys <- lift $ fromList typs |
| 83 | + ctms <- lift $ fromList tmps |
| 84 | + |
| 85 | + let f False r = findWithDefault r r ctms |
| 86 | + f True r = findWithDefault r r ctys |
| 87 | + |
| 88 | + if null typs && null tmps |
| 89 | + then pure v -- already canonical |
| 90 | + else lift $ trav f v |
| 91 | + where |
| 92 | + g isTy r = StateT \st@(canon, tys, tms) -> |
| 93 | + categorize canon r >>= \case |
| 94 | + Canonical -> pure (Nothing, st) |
| 95 | + Novel canon -> |
| 96 | + pure |
| 97 | + ( Nothing, |
| 98 | + ( canon, |
| 99 | + if isTy then r : tys else tys, |
| 100 | + if isTy then tms else r : tms |
| 101 | + ) |
| 102 | + ) |
| 103 | + Equivalent s canon -> pure (Just (r, s), (canon, tys, tms)) |
| 104 | +{-# INLINE recanonicalizeRefs #-} |
0 commit comments