Skip to content

Commit 7a88cdf

Browse files
Gabriella439mergify[bot]
authored andcommitted
Change ImportSemantics to not necessarily be α-normalized (#1162)
The motivation for this change is to avoid α-normalizing all imported expressions. For example, before this change you would get the following behavior beginning with an empty cache ``` $ cat ./example.dhall λ(a : Type) → a $ dhall <<< './example.dhall' λ(_ : Type) → _ ``` The reason why is that the current code α-normalizes all imported expressions, even when returning them fresh. To fix this, I changed the `ImportSemantics` type to not require that expressions are α-normalized. Instead, the α-normalization only happens at the last minute when interacting with the semantic cache, but nowhere else. I figured that this change would also be fine from the perspective of the semi-semantic cache because false-negatives for this cache are fine. In particular, we probably don't mind if we get a cache miss for the semi-semantic cache if the user renames a variable. After this change imports are no longer α-normalized, whether loaded from a hot or cold cache: ``` $ cat ./example.dhall λ(a : Type) → a $ dhall <<< './example.dhall' λ(a : Type) → a $ dhall <<< './example.dhall' λ(a : Type) → a ```
1 parent 1970aa3 commit 7a88cdf

File tree

2 files changed

+5
-7
lines changed

2 files changed

+5
-7
lines changed

dhall/src/Dhall/Import.hs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -517,14 +517,14 @@ loadImportWithSemanticCache
517517
Nothing -> do
518518
ImportSemantics { importSemantics } <- loadImportWithSemisemanticCache import_
519519

520-
let variants = map (\version -> encodeExpression version importSemantics)
520+
let variants = map (\version -> encodeExpression version (Dhall.Core.alphaNormalize importSemantics))
521521
[ minBound .. maxBound ]
522522
case Data.Foldable.find ((== semanticHash). Crypto.Hash.hash) variants of
523523
Just bytes -> liftIO $ writeToSemanticCache semanticHash bytes
524524
Nothing -> do
525525
let expectedHash = semanticHash
526526
Status { _standardVersion, _stack } <- State.get
527-
let actualHash = hashExpression _standardVersion importSemantics
527+
let actualHash = hashExpression _standardVersion (Dhall.Core.alphaNormalize importSemantics)
528528
throwMissingImport (Imported _stack (HashMismatch {..}))
529529

530530
return (ImportSemantics {..})
@@ -608,12 +608,10 @@ loadImportWithSemisemanticCache (Chained (Import (ImportHashed _ importType) Cod
608608
Left err -> throwMissingImport (Imported _stack err)
609609
Right _ -> return (Dhall.Core.normalizeWith _normalizer resolvedExpr)
610610

611-
let alphaBetaNormal = Dhall.Core.alphaNormalize betaNormal
612-
613-
let bytes = encodeExpression _standardVersion alphaBetaNormal
611+
let bytes = encodeExpression _standardVersion betaNormal
614612
lift $ writeToSemisemanticCache semisemanticHash bytes
615613

616-
return alphaBetaNormal
614+
return betaNormal
617615

618616
return (ImportSemantics {..})
619617

dhall/src/Dhall/Import/Types.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ instance Pretty Chained where
4848

4949
data ImportSemantics = ImportSemantics
5050
{ importSemantics :: Expr Src X
51-
-- ^ The fully resolved import, typechecked and alpha-beta-normal.
51+
-- ^ The fully resolved import, typechecked and beta-normal.
5252
}
5353

5454
-- | `parent` imports (i.e. depends on) `child`

0 commit comments

Comments
 (0)