{-# LANGUAGE DeriveAnyClass #-}{-# LANGUAGE DeriveGeneric #-}{-# LANGUAGE OverloadedRecordDot #-}{-# LANGUAGE OverloadedStrings #-}{-# LANGUAGE RecordWildCards #-}-- SPDX-FileCopyrightText: Copyright (c) 2025 Objectionary.com-- SPDX-License-Identifier: MITmodule DataizeSpec (spec) whereimport ASTimport Control.Exception (SomeException)import Control.Monadimport Data.Aeson (FromJSON)import Data.IORef (newIORef)import Data.List (find, isInfixOf, isPrefixOf, nub)import Data.List.NonEmpty (NonEmpty (..))import Data.Map.Strict qualified as Mapimport Data.Maybe (fromMaybe)import Data.Yaml qualified as Decodeimport Dataize (Outcome (..), dataize, dataize', reduction)import Deps (Judgment (..), State, dontSaveEval, dontSaveStep)import Engine (Engine (_normal), building)import Evaluate (evaluation)import Files (allPathsIn)import Fixtures (defaultReduceContext, fixtureLambdas, linked, loopingLambdas, overdue, primitives, recorded, withLambdas)import GHC.Generics (Generic)import Lambdas (Lambdas, emptyLambdas, readLambdas)import Matcher (substEmpty)import Morph (ReduceContext (..), Steps (..), emptyState, execBuildTerm)import Parser (Form (MONOFORM), parseBytes, parseExpressionThrows, parseExpressionThrows')import Rewriter (Rewritten)import Rule (RuleContext (RuleContext), matchExpressionWithRule')import Sides (sides)import System.FilePath (makeRelative)import Tau (seedTaus)import Test.Hspecimport Yaml qualifiedtest :: (Eq a, Show a) => ((Expression, NonEmpty Rewritten) -> Expression -> State -> ReduceContext -> IO ((a, [Rewritten]), State)) -> [(String, Expression, Expression, a)] -> Spectest func useCases = forM_ useCases $ \(desc, input, expr, output) -> it desc $ do ((res, _), _) <- func (input, (expr, Nothing) :| []) expr emptyState =<< defaultReduceContext ExRoot res `shouldBe` outputdata DataizePack = DataizePack { location :: Maybe String , input :: String , model :: Maybe Bool , symbolic :: Maybe Bool , result :: Maybe String , fails :: Maybe String } deriving (Generic, Show, FromJSON)testDataize :: Lambdas -> FilePath -> ExpectationtestDataize known pth = do DataizePack{..} <- Decode.decodeFileThrow pth expr <- parseExpressionThrows' MONOFORM (if model == Just True then primitives input else input) loc <- parseExpressionThrows' MONOFORM (fromMaybe "Q" location) base <- defaultReduceContext loc let ctx = base{_symbolic = if symbolic == Just True then known else emptyLambdas} case (result, fails) of (Just res, Nothing) -> do bts <- either (fail . ("cannot read the expected bytes: " ++)) pure (parseBytes res) (value, _, _) <- dataize expr emptyState ctx value `shouldBe` Dataized bts (Nothing, Just message) -> dataize expr emptyState ctx `shouldThrow` (\err -> message `isInfixOf` show (err :: SomeException)) _ -> expectationFailure "The pack holds neither a single 'result' nor a single 'fails'"partially :: Lambdas -> String -> IO ((Outcome, [Rewritten]), String)partially known src = do expr <- parseExpressionThrows (primitives src) seedTaus expr recorded $ \record -> do base <- defaultReduceContext ExRoot let ctx = (withLambdas known base){_partial = True, _saveEval = record} (outcome, chain, _) <- dataize expr emptyState ctx pure (outcome, chain)looping :: (Lambdas -> IO a) -> IO alooping action = loopingLambdas (readLambdas >=> action)spec :: Specspec = do known <- runIO fixtureLambdas describe "dataize' fails when no dataization rule matches the term" $ it "throws instead of treating the unmatched meta as ⊥" $ (dataize' (ExMeta "unbound", (ExRoot, Nothing) :| []) ExRoot emptyState =<< defaultReduceContext ExRoot) `shouldThrow` (\e -> "no dataization rule matched" `isInfixOf` show (e :: SomeException)) describe "dataization 'norm' is disjoint from the specific clauses" $ do ctx <- runIO (defaultReduceContext ExRoot) let rctx = RuleContext (execBuildTerm ExRoot ctx) Nothing (_normal linked) False dataizeRule :: String -> Yaml.DataizeRule dataizeRule nm = fromMaybe (error ("no dataization rule named " ++ nm)) (find (\r -> r.name == nm) Yaml.dataizationRules) asRule :: Yaml.DataizeRule -> Yaml.Rule asRule r = Yaml.Rule r.name Nothing Nothing r.match ExRoot r.when Nothing Nothing it "does not fire on a formation" $ do substs <- matchExpressionWithRule' [substEmpty] (ExFormation [BiDelta (BtOne "00")]) (asRule (dataizeRule "norm")) rctx substs `shouldBe` [] it "does not fire on the termination ⊥" $ do substs <- matchExpressionWithRule' [substEmpty] ExTermination (asRule (dataizeRule "norm")) rctx substs `shouldBe` [] it "still fires on a non-formation, non-termination normal form" $ do substs <- matchExpressionWithRule' [substEmpty] (ExDispatch ExXi (AtLabel "x")) (asRule (dataizeRule "norm")) rctx null substs `shouldBe` False forM_ ["delta", "fire"] ( \name -> it ("leaves '" ++ name ++ "' off a formation holding both Δ and λ") $ do substs <- matchExpressionWithRule' [substEmpty] (ExFormation [BiDelta (BtOne "01"), BiLambda (Function "Foo")]) (asRule (dataizeRule name)) rctx substs `shouldBe` [] ) describe "dataize" $ do let resources = "test-resources/dataization-packs" packs <- runIO (allPathsIn resources) forM_ packs (\pth -> it (makeRelative resources pth) (testDataize known pth)) describe "dataize'" $ test dataize' [ ("[[ D> 00- ]] => 00-", ExFormation [BiDelta (BtOne "00")], ExRoot, BtOne "00") , ( "[[ @ -> [[ D> 00-]] ]] => 00-" , ExFormation [BiTau AtPhi (ExFormation [BiDelta (BtOne "00"), BiVoid AtRho]), BiVoid AtRho] , ExRoot , BtOne "00" ) , ( "[[ @ -> [[ x -> [[ D> 01-, y -> ? ]](y -> [[ ]]) ]].x ]] => 01-" , ExFormation [ BiTau AtPhi ( ExDispatch ( ExFormation [ BiTau (AtLabel "x") ( ExApplication ( ExFormation [ BiDelta (BtOne "01") , BiVoid (AtLabel "y") , BiVoid AtRho ] ) (ArTau (AtLabel "y") (ExFormation [])) ) ] ) (AtLabel "x") ) ] , ExRoot , BtOne "01" ) ] describe "fails to dataize the terminator" $ do let failsOn desc input = it desc $ (dataize' (input, (ExRoot, Nothing) :| []) ExRoot emptyState =<< defaultReduceContext ExRoot) `shouldThrow` (\e -> "terminator" `isInfixOf` show (e :: SomeException)) failsOn "throws on ⊥ instead of mapping it to empty bytes" ExTermination failsOn "throws on a data-less formation, which dataizes ⊥" (ExFormation []) failsOn "throws on a void slot fed a non-absolute argument instead of looping forever" (ExApplication (ExFormation [BiVoid (AtLabel "x")]) (ArTau (AtLabel "x") (ExDispatch ExXi (AtLabel "foo")))) describe "stops a dataization that never reaches bytes" $ do it "fails on the step limit instead of morphing forever" $ looping $ \endless -> do expr <- parseExpressionThrows "⟦ @ ↦ ⟦ λ ⤍ L_loop ⟧ ⟧" minted <- newIORef 0 named <- sides dataize expr emptyState (ReduceContext ExRoot ExRoot Nothing 25 25 Nothing Nothing (Steps 40 0) Nothing minted named Nothing Nothing 1 Nothing False True False Nothing 0 Dataization [] Map.empty Nothing endless [] False Nothing (building linked) reduction evaluation dontSaveStep dontSaveEval linked) `shouldThrow` (\e -> "--max-steps=40" `isInfixOf` show (e :: SomeException)) it "parks the step limit as a residual with --partial" $ looping $ \endless -> do expr <- parseExpressionThrows "⟦ @ ↦ ⟦ λ ⤍ L_loop ⟧ ⟧" minted <- newIORef 0 named <- sides (outcome, _, _) <- dataize expr emptyState (ReduceContext ExRoot ExRoot Nothing 25 25 Nothing Nothing (Steps 40 0) Nothing minted named Nothing Nothing 1 Nothing False True True Nothing 0 Dataization [] Map.empty Nothing endless [] False Nothing (building linked) reduction evaluation dontSaveStep dontSaveEval linked) case outcome of Residual _ -> pure () Dataized bts -> expectationFailure ("expected a residual, dataized to " ++ show bts) describe "stops a dataization by the clock of --max-seconds" $ it "leaves a partial dataization as a residual once the deadline has passed" $ do expr <- parseExpressionThrows "[[ @ -> [[ D> 7E- ]] ]]" deadline <- overdue 29 ctx <- defaultReduceContext ExRoot (outcome, _, _) <- dataize expr emptyState ctx{_deadline = Just deadline, _partial = True} outcome `shouldBe` Residual expr describe "partially evaluates around a λ function that cannot fire (--partial)" $ do let placeholder = ExFormation [BiLambda (Function "Sym_arg_0")] it "fails on it without the flag, naming the λ function" $ do expr <- parseExpressionThrows (primitives "2.times(3).nope") (dataize expr emptyState . withLambdas known =<< defaultReduceContext ExRoot) `shouldThrow` (\e -> "No entry of --symbolic answers the λ function 'L_number_nope'" `isInfixOf` show (e :: SomeException)) it "leaves the application of the unanswered λ function in place" $ do ((outcome, _), _) <- partially known "2.times(3).nope" case outcome of Residual (ExFormation bds) -> bds `shouldContain` [BiLambda (Function "L_number_nope")] other -> expectationFailure ("expected a residual formation, got " ++ show other) it "keeps what was evaluated before the stuck site in the residue" $ do ((outcome, _), _) <- partially known "2.times(3).nope" case outcome of Residual (ExFormation bds) -> do let rho = [value | BiTau AtRho value <- bds] length rho `shouldBe` 1 [() | ExApplication (ExDispatch ExRoot (AtLabel "number")) (ArTau AtPhi _) <- rho] `shouldBe` [()] other -> expectationFailure ("expected a residual formation, got " ++ show other) it "writes the firing that answered into the protocol and stops at the stuck one" $ do (_, protocol) <- partially known "2.times(3).nope" protocol `shouldBe` unlines [ " 𝔻(Φ):" , " boxed 𝑛·0·1 := Φ.φ" , " normalized 𝑛·0·2 := 2.0.times( 3.0 ).nope( ρ ↦ Φ ) # Φ.φ" , " 𝕄(𝑛·0·1):" , " normalized 𝑛·0·3 := ⟦ φ ↦ ∅, as-bytes ↦ φ, plus(ρ, x) ↦ L_number_plus:λ, times(ρ, x) ↦ L_number_times:λ, div(ρ, x) ↦ L_number_div:λ, gt(ρ, x) ↦ L_number_gt:λ, eq(ρ, x) ↦ ρ.as-bytes.eq( x.as-bytes ):φ, nope(ρ) ↦ L_number_nope:λ ⟧ # Φ.number" , " 𝑛·0·4 := 2.0" , " 𝑛·0·5 := ⟦ φ ↦ Φ.bytes( φ ↦ 40-00-00-00-00-00-00-00:Δ ), as-bytes ↦ φ, plus(ρ, x) ↦ L_number_plus:λ, times(ρ, x) ↦ L_number_times:λ, div(ρ, x) ↦ L_number_div:λ, gt(ρ, x) ↦ L_number_gt:λ, eq(ρ, x) ↦ ρ.as-bytes.eq( x.as-bytes ):φ, nope(ρ) ↦ L_number_nope:λ ⟧" , " normalized 𝑛·0·6 := ⟦ ρ ↦ 𝑛·0·4, x ↦ ∅, λ ⤍ L_number_times ⟧ # 𝑛·0·5.times" , " 𝑛·0·7 := 𝑛·0·4.times( x ↦ 3.0 )" , " 𝑛·0·8 := ⟦ ρ ↦ 𝑛·0·4, x ↦ 3.0, λ ⤍ L_number_times ⟧" , " 𝔼(L_number_times):" , " 𝔻(ξ.ρ):" , " 𝕄(ξ.ρ):" , " 𝑛·1·1 := 2.0" , " 𝑛·1·2 := ⟦ φ ↦ Φ.bytes( φ ↦ 40-00-00-00-00-00-00-00:Δ ), as-bytes ↦ φ, plus(ρ, x) ↦ L_number_plus:λ, times(ρ, x) ↦ L_number_times:λ, div(ρ, x) ↦ L_number_div:λ, gt(ρ, x) ↦ L_number_gt:λ, eq(ρ, x) ↦ ρ.as-bytes.eq( x.as-bytes ):φ, nope(ρ) ↦ L_number_nope:λ ⟧" , " morphed 𝑛·1·3 := 𝑛·1·2" , " boxed 𝑛·1·4 := 𝑛·1·3.φ" , " normalized 𝑛·1·5 := Φ.bytes( φ ↦ 40-00-00-00-00-00-00-00:Δ, ρ ↦ 𝑛·1·1 ) # 𝑛·1·4" , " 𝕄(𝑛·1·4):" , " normalized 𝑛·1·6 := ⟦ φ ↦ ∅, not(ρ) ↦ L_bytes_not:λ, eq(ρ, b) ↦ L_bytes_eq:λ ⟧ # Φ.bytes" , " 𝑛·1·7 := Φ.bytes( φ ↦ 40-00-00-00-00-00-00-00:Δ )" , " 𝑛·1·8 := ⟦ φ ↦ 40-00-00-00-00-00-00-00:Δ, not(ρ) ↦ L_bytes_not:λ, eq(ρ, b) ↦ L_bytes_eq:λ ⟧" , " morphed 𝑛·1·9 := 𝑛·1·8" , " boxed 𝑛·1·10 := 𝑛·1·9.φ" , " normalized 𝑛·1·11 := 40-00-00-00-00-00-00-00:Δ # 𝑛·1·10" , " 𝛿·1·1 := 40-00-00-00-00-00-00-00 # 2.0" , " 𝛿1·1 := 𝛿·1·1 # 𝔻(ξ.ρ)" , " 𝔻(ξ.x):" , " 𝕄(ξ.x):" , " 𝑛·1·12 := 3.0" , " 𝑛·1·13 := ⟦ φ ↦ Φ.bytes( φ ↦ 40-08-00-00-00-00-00-00:Δ ), as-bytes ↦ φ, plus(ρ, x) ↦ L_number_plus:λ, times(ρ, x) ↦ L_number_times:λ, div(ρ, x) ↦ L_number_div:λ, gt(ρ, x) ↦ L_number_gt:λ, eq(ρ, x) ↦ ρ.as-bytes.eq( x.as-bytes ):φ, nope(ρ) ↦ L_number_nope:λ ⟧" , " morphed 𝑛·1·14 := 𝑛·1·13" , " boxed 𝑛·1·15 := 𝑛·1·14.φ" , " normalized 𝑛·1·16 := Φ.bytes( φ ↦ 40-08-00-00-00-00-00-00:Δ, ρ ↦ 𝑛·1·12 ) # 𝑛·1·15" , " 𝕄(𝑛·1·15):" , " 𝑛·1·17 := Φ.bytes( φ ↦ 40-08-00-00-00-00-00-00:Δ )" , " 𝑛·1·18 := ⟦ φ ↦ 40-08-00-00-00-00-00-00:Δ, not(ρ) ↦ L_bytes_not:λ, eq(ρ, b) ↦ L_bytes_eq:λ ⟧" , " morphed 𝑛·1·19 := 𝑛·1·18" , " boxed 𝑛·1·20 := 𝑛·1·19.φ" , " normalized 𝑛·1·21 := 40-08-00-00-00-00-00-00:Δ # 𝑛·1·20" , " 𝛿·1·2 := 40-08-00-00-00-00-00-00 # 3.0" , " 𝛿2·1 := 𝛿·1·2 # 𝔻(ξ.x)" , " 𝑛·1·22 := Φ.number( φ ↦ 𝜎1:λ ) # 𝑛 of L_number_times" , " 𝕄(𝑛·1·22):" , " 𝑛·1·23 := Φ.number( φ ↦ 𝜎1:λ )" , " 𝑛·1·24 := ⟦ φ ↦ 𝜎1:λ, as-bytes ↦ φ, plus(ρ, x) ↦ L_number_plus:λ, times(ρ, x) ↦ L_number_times:λ, div(ρ, x) ↦ L_number_div:λ, gt(ρ, x) ↦ L_number_gt:λ, eq(ρ, x) ↦ ρ.as-bytes.eq( x.as-bytes ):φ, nope(ρ) ↦ L_number_nope:λ ⟧" , " morphed 𝑛·1·25 := 𝑛·1·24" , " 𝑛·1·26 := 𝑛·1·25 # 𝕄(𝑛·1·22)" , " evaluated 𝑛·1·27 := 𝑛·1·26 # 𝑛·0·8" , " normalized 𝑛·0·9 := ⟦ ρ ↦ 𝑛·1·23, λ ⤍ L_number_nope ⟧ # 𝑛·1·27.nope" , " morphed 𝑛·0·10 := ⟦ ρ ↦ 𝑛·1·23, λ ⤍ L_number_nope ⟧" , " unanswered L_number_nope # 𝔻(⟦ ρ ↦ 𝑛·1·23, λ ⤍ L_number_nope ⟧)" ] it "leaves an unanswered λ function dataized directly as the whole residue" $ do ((outcome, chain), protocol) <- partially known "[[ L> Sym_arg_0 ]]" outcome `shouldBe` Residual placeholder protocol `shouldBe` " 𝔻(Φ):\n boxed 𝑛·0·1 := Φ.φ\n normalized 𝑛·0·2 := Sym_arg_0:λ # Φ.φ\n unanswered Sym_arg_0 # 𝔻(Sym_arg_0:λ)\n" map fst chain `shouldEndWith` [placeholder] it "closes a dataization with the datum the delta rule found" $ do (_, protocol) <- partially known "[[ @ -> [[ D> 01-02 ]] ]]" protocol `shouldBe` " 𝔻(Φ):\n boxed 𝑛·0·1 := Φ.φ\n normalized 𝑛·0·2 := 01-02:Δ:φ # Φ.φ\n boxed 𝑛·0·3 := 𝑛·0·2.φ\n normalized 𝑛·0·4 := 01-02:Δ # 𝑛·0·3\n 𝛿·0·1 := 01-02\n" it "continues the block of a dataization the box rule enters again at the same site" $ do (_, protocol) <- partially known "[[ @ -> [[ @ -> [[ D> 0A- ]] ]] ]]" protocol `shouldBe` " 𝔻(Φ):\n boxed 𝑛·0·1 := Φ.φ\n normalized 𝑛·0·2 := 0A-:Δ:φ:φ # Φ.φ\n boxed 𝑛·0·3 := 𝑛·0·2.φ\n normalized 𝑛·0·4 := 0A-:Δ:φ # 𝑛·0·3\n boxed 𝑛·0·5 := 𝑛·0·4.φ\n normalized 𝑛·0·6 := 0A-:Δ # 𝑛·0·5\n 𝛿·0·1 := 0A-\n" it "writes the two morphings of one dataization as two blocks" $ do (_, protocol) <- partially known "73.25" length (filter (isPrefixOf " 𝕄(") (lines protocol)) `shouldBe` 2 it "writes every box step of a dataization into the protocol" $ do (_, protocol) <- partially known "73.25" filter (isInfixOf "boxed") (lines protocol) `shouldBe` [" boxed 𝑛·0·1 := Φ.φ", " boxed 𝑛·0·7 := 𝑛·0·6.φ", " boxed 𝑛·0·13 := 𝑛·0·12.φ"] it "names the morphing that follows a box step by the meta the box step bound" $ do (_, protocol) <- partially known "73.25" lines protocol `shouldContain` [" boxed 𝑛·0·1 := Φ.φ", " normalized 𝑛·0·2 := 73.25 # Φ.φ", " 𝕄(𝑛·0·1):"] it "still reaches the manufactured datum when nothing is stuck" $ do ((outcome, _), _) <- partially known "2.times(3)" outcome `shouldBe` Dataized (BtMany ["40", "45", "00", "00", "00", "00", "00", "00"]) it "parks a firing whose operand dataizes the terminator ⊥" $ do ((outcome, _), protocol) <- partially known "5.plus( ⟦ ⟧ )" case outcome of Residual (ExFormation bds) -> bds `shouldContain` [BiLambda (Function "L_number_plus")] other -> expectationFailure ("expected a residual formation, got " ++ show other) protocol `shouldSatisfy` isInfixOf "unanswered ⊥ # 𝔻(⊥)" it "leaves the terminator ⊥ the object reduces to as the whole residue" $ do ((outcome, _), _) <- partially known "⊥" outcome `shouldBe` Residual ExTermination it "writes the terminator ⊥ the object reduces to into the protocol" $ do (_, protocol) <- partially known "⊥" protocol `shouldBe` " 𝔻(Φ):\n boxed 𝑛·0·1 := Φ.φ\n normalized 𝑛·0·2 := ⊥ # Φ.φ\n unanswered ⊥ # 𝔻(⊥)\n" describe "ReduceContext's --max-depth/--max-cycles reach into the normalization it splices in" $ do let wrapped = "[[ @ -> [[ D> 00- ]] ]]" forM_ [ ( "--max-cycles" , \minted named -> ReduceContext ExRoot ExRoot Nothing 25 0 Nothing Nothing (Steps 250 0) Nothing minted named Nothing Nothing 1 Nothing True True False Nothing 0 Dataization [] Map.empty Nothing emptyLambdas [] False Nothing (building linked) reduction evaluation dontSaveStep dontSaveEval linked , "--max-cycles=0" ) , ( "--max-depth" , \minted named -> ReduceContext ExRoot ExRoot Nothing 0 25 Nothing Nothing (Steps 250 0) Nothing minted named Nothing Nothing 1 Nothing True True False Nothing 0 Dataization [] Map.empty Nothing emptyLambdas [] False Nothing (building linked) reduction evaluation dontSaveStep dontSaveEval linked , "--max-depth=0" ) ] ( \(flag, reducing, message) -> it ("throws once " ++ flag ++ " is exhausted with --depth-sensitive") $ do expr <- parseExpressionThrows "[[ @ -> $.f(y -> [[ ]]), f -> [[ D> 00-, y -> ? ]] ]]" ctx <- reducing <$> newIORef 0 <*> sides dataize expr emptyState ctx `shouldThrow` (\e -> message `isInfixOf` show (e :: SomeException)) ) it "does not throw without --depth-sensitive even once --max-depth is exhausted" $ do expr <- parseExpressionThrows wrapped minted <- newIORef 0 named <- sides (value, _, _) <- dataize expr emptyState (ReduceContext ExRoot ExRoot Nothing 1 25 Nothing Nothing (Steps 250 0) Nothing minted named Nothing Nothing 1 Nothing False True False Nothing 0 Dataization [] Map.empty Nothing emptyLambdas [] False Nothing (building linked) reduction evaluation dontSaveStep dontSaveEval linked) value `shouldBe` Dataized (BtOne "00") it "throws once --max-cycles is exhausted even without --depth-sensitive" $ do expr <- parseExpressionThrows wrapped minted <- newIORef 0 named <- sides dataize expr emptyState (ReduceContext ExRoot ExRoot Nothing 25 0 Nothing Nothing (Steps 250 0) Nothing minted named Nothing Nothing 1 Nothing False True False Nothing 0 Dataization [] Map.empty Nothing emptyLambdas [] False Nothing (building linked) reduction evaluation dontSaveStep dontSaveEval linked) `shouldThrow` (\e -> "--max-cycles=0" `isInfixOf` show (e :: SomeException)) describe "labels every step with a defined rule or operation" $ do let verb op = case op of Yaml.OpMorph _ _ -> "morph" Yaml.OpNormalize _ -> "normalize" Yaml.OpEvaluate _ _ -> "evaluate" Yaml.OpContextualize _ _ -> "contextualize" Yaml.OpDataize _ _ -> "dataize" allowed = map (.name) Yaml.morphingRules ++ map (.name) Yaml.dataizationRules ++ map (.name) Yaml.normalizationRules ++ concatMap (map (verb . (.operation)) . (.premises)) Yaml.morphingRules ++ concatMap (map (verb . (.operation)) . (.premises)) Yaml.dataizationRules it "uses no step label without a defining rule or operation" $ do expr <- parseExpressionThrows (primitives "5.plus(6)") loc <- parseExpressionThrows "Q" (_, chain, _) <- dataize expr emptyState . withLambdas known =<< defaultReduceContext loc let orphans = nub [label | (_, Just (_, label)) <- chain, label `notElem` allowed, label /= "symbol"] unless (null orphans) (expectationFailure ("Dataization emitted step labels with no defining rule or operation: " ++ show orphans)) it "takes the step of a firing by evaluation" $ do expr <- parseExpressionThrows (primitives "5.plus(6)") loc <- parseExpressionThrows "Q" (_, chain, _) <- dataize expr emptyState . withLambdas known =<< defaultReduceContext loc map snd chain `shouldContain` [Just (Evaluation, "evaluate")] it "takes the step of a box by dataization" $ do expr <- parseExpressionThrows "[[ @ -> [[ D> 0A- ]] ]]" (_, chain, _) <- dataize expr emptyState =<< defaultReduceContext ExRoot map snd chain `shouldContain` [Just (Dataization, "box")] it "takes the step of a delta by dataization" $ do expr <- parseExpressionThrows "[[ D> 3C- ]]" (_, chain, _) <- dataize expr emptyState =<< defaultReduceContext ExRoot map snd chain `shouldBe` [Just (Dataization, "delta"), Nothing] describe "names every rule uniquely across rule sets" $ it "shares no rule name between morphing, dataization, normalization and contextualization" $ do let names = map (.name) Yaml.morphingRules ++ map (.name) Yaml.dataizationRules ++ map (.name) Yaml.normalizationRules ++ map (.name) Yaml.contextualizationRules clashes = nub (filter (\n -> length (filter (== n) names) > 1) names) clashes `shouldBe` [] describe "preserves the reduction label sequence" $ do let labelsOf loc src = do expr <- parseExpressionThrows src loc' <- parseExpressionThrows loc (_, chain, _) <- dataize expr emptyState . withLambdas known =<< defaultReduceContext loc' pure [label | (_, Just (_, label)) <- chain] it "dataizes 5.plus(6) through the expected rules" $ do labels <- labelsOf "Q" "[[ bytes ↦ ⟦ φ ↦ ∅ ⟧, number(φ) -> [[ plus(^, x) -> [[ L> L_number_plus ]] ]], @ -> 5.plus(6) ]]" labels `shouldBe` [ "box" , "dot" , "ma" , "stay" , "mf" , "evaluate" , "box" , "dot" , "skip" , "symbol" ] it "dataizes a located reference through the expected rules" $ do labels <- labelsOf "Q.foo.bar" "[[ foo -> [[ bar -> [[ @ -> Q.x ]] ]], x -> [[ D> 42- ]] ]]" labels `shouldBe` ["box", "dot", "ma", "skip", "mf", "delta"] it "takes every step of 5.plus(6) by the judgment of its rule" $ do expr <- parseExpressionThrows "[[ bytes ↦ ⟦ φ ↦ ∅ ⟧, number(φ) -> [[ plus(^, x) -> [[ L> L_number_plus ]] ]], @ -> 5.plus(6) ]]" (_, chain, _) <- dataize expr emptyState . withLambdas known =<< defaultReduceContext ExRoot [judgment | (_, Just (judgment, _)) <- chain] `shouldBe` [ Dataization , Normalization , Morphing , Normalization , Morphing , Evaluation , Dataization , Normalization , Normalization , Dataization ]