{-# LANGUAGE OverloadedStrings #-}{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-}-- SPDX-FileCopyrightText: Copyright (c) 2025 Objectionary.com-- SPDX-License-Identifier: MITmodule ASTSpec whereimport ASTimport Control.Exception (evaluate)import Control.Monad (forM_)import Data.List (nub, sort)import Data.List qualified as Listimport Data.Map.Strict qualified as Mapimport Data.Text qualified as Timport GHC.Clock (getMonotonicTime)import Grown (Grown (..))import System.Timeout (timeout)import Test.Hspec (Spec, describe, it, shouldBe, shouldNotBe, shouldReturn, shouldSatisfy)import Test.Hspec.QuickCheck (prop)spec :: Specspec = do describe "Attribute Show instance renders AtLabel" $ forM_ [ ("simple label", AtLabel "x", "x") , ("unicode label", AtLabel "日本語", "日本語") , ("long label", AtLabel "myAttribute", "myAttribute") ] ( \(desc, attr, expected) -> it desc $ show attr `shouldBe` expected ) describe "Attribute Show instance renders special attributes" $ forM_ [ ("rho", AtRho, "ρ") , ("phi", AtPhi, "φ") , ("delta", AtDelta, "Δ") , ("lambda", AtLambda, "λ") ] ( \(desc, attr, expected) -> it desc $ show attr `shouldBe` expected ) describe "Attribute Show instance renders AtMeta" $ forM_ [ ("simple meta", AtMeta "t", "!t") , ("long meta", AtMeta "tttribute", "!tttribute") , ("unicode meta", AtMeta "メタ", "!メタ") ] ( \(desc, attr, expected) -> it desc $ show attr `shouldBe` expected ) describe "Attribute Eq instance compares same constructors" $ forM_ [ ("labels equal", AtLabel "x", AtLabel "x", True) , ("labels differ", AtLabel "x", AtLabel "y", False) , ("metas equal", AtMeta "t", AtMeta "t", True) , ("metas differ", AtMeta "t", AtMeta "b", False) , ("rho equals rho", AtRho, AtRho, True) , ("phi equals phi", AtPhi, AtPhi, True) , ("delta equals delta", AtDelta, AtDelta, True) , ("lambda equals lambda", AtLambda, AtLambda, True) ] ( \(desc, lhs, rhs, expected) -> it desc $ (lhs == rhs) `shouldBe` expected ) describe "Attribute Eq instance compares different constructors" $ forM_ [ ("rho vs phi", AtRho, AtPhi, False) , ("delta vs lambda", AtDelta, AtLambda, False) , ("meta vs label", AtMeta "x", AtLabel "x", False) ] ( \(desc, lhs, rhs, expected) -> it desc $ (lhs == rhs) `shouldBe` expected ) describe "Attribute Ord instance orders correctly" $ it "sorts attributes by constructor order" $ let attrs = [AtMeta "z", AtDelta, AtLambda, AtRho, AtPhi, AtLabel "a"] first : _ = sort attrs isLabel (AtLabel _) = True isLabel _ = False in first `shouldSatisfy` isLabel describe "Bytes Eq instance compares same constructors" $ forM_ [ ("empty equals empty", BtEmpty, BtEmpty, True) , ("one equals one", BtOne "FF", BtOne "FF", True) , ("one differs", BtOne "FF", BtOne "00", False) , ("many equals many", BtMany ["00", "01"], BtMany ["00", "01"], True) , ("many differs", BtMany ["00"], BtMany ["01"], False) , ("meta equals meta", BtMeta "b", BtMeta "b", True) , ("meta differs", BtMeta "b", BtMeta "c", False) ] ( \(desc, lhs, rhs, expected) -> it desc $ (lhs == rhs) `shouldBe` expected ) describe "Bytes Eq instance compares different constructors" $ forM_ [ ("empty vs one", BtEmpty, BtOne "00", False) , ("one vs many", BtOne "00", BtMany ["00"], False) , ("many vs meta", BtMany ["00"], BtMeta "b", False) ] ( \(desc, lhs, rhs, expected) -> it desc $ (lhs == rhs) `shouldBe` expected ) describe "Bytes Ord instance orders correctly" $ it "sorts bytes by constructor order" $ let bytes = [BtMeta "z", BtMany ["00"], BtOne "FF", BtEmpty] first : _ = sort bytes in first `shouldBe` BtEmpty describe "Binding Eq instance compares same constructors" $ forM_ [ ("tau equals tau", BiTau AtRho ExRoot, BiTau AtRho ExRoot, True) , ("tau differs by attr", BiTau AtRho ExRoot, BiTau AtPhi ExRoot, False) , ("tau differs by expr", BiTau AtRho ExRoot, BiTau AtRho ExXi, False) , ("meta equals meta", BiMeta "B", BiMeta "B", True) , ("meta differs", BiMeta "B", BiMeta "C", False) , ("delta equals delta", BiDelta BtEmpty, BiDelta BtEmpty, True) , ("delta differs", BiDelta BtEmpty, BiDelta (BtOne "00"), False) , ("void equals void", BiVoid AtRho, BiVoid AtRho, True) , ("void differs", BiVoid AtRho, BiVoid AtPhi, False) , ("lambda equals lambda", BiLambda (Function "Func"), BiLambda (Function "Func"), True) , ("lambda differs", BiLambda (Function "Func"), BiLambda (Function "Other"), False) , ("metalambda equals", BiLambda (FnMeta "F"), BiLambda (FnMeta "F"), True) , ("metalambda differs", BiLambda (FnMeta "F"), BiLambda (FnMeta "G"), False) ] ( \(desc, lhs, rhs, expected) -> it desc $ (lhs == rhs) `shouldBe` expected ) describe "Binding Eq instance compares different constructors" $ forM_ [ ("tau vs meta", BiTau AtRho ExRoot, BiMeta "B", False) , ("delta vs void", BiDelta BtEmpty, BiVoid AtDelta, False) , ("lambda vs metalambda", BiLambda (Function "F"), BiLambda (FnMeta "F"), False) ] ( \(desc, lhs, rhs, expected) -> it desc $ (lhs == rhs) `shouldBe` expected ) describe "Binding Ord instance orders correctly" $ it "sorts bindings by constructor order" $ let bindings = [BiLambda (FnMeta "Z"), BiLambda (Function "A"), BiVoid AtRho, BiDelta BtEmpty, BiMeta "B", BiTau AtRho ExRoot] first : _ = sort bindings isTau (BiTau _ _) = True isTau _ = False in first `shouldSatisfy` isTau describe "Expression Eq instance compares same constructors" $ forM_ [ ("formation equals", ExFormation [], ExFormation [], True) , ("formation differs", ExFormation [], ExFormation [BiVoid AtRho], False) , ("this equals this", ExXi, ExXi, True) , ("global equals global", ExRoot, ExRoot, True) , ("termination equals", ExTermination, ExTermination, True) , ("meta equals meta", ExMeta "e", ExMeta "e", True) , ("meta differs", ExMeta "e", ExMeta "f", False) , ("application equals", ExApplication ExRoot (ArTau AtRho ExXi), ExApplication ExRoot (ArTau AtRho ExXi), True) , ("dispatch equals", ExDispatch ExRoot AtRho, ExDispatch ExRoot AtRho, True) , ("dispatch differs", ExDispatch ExRoot AtRho, ExDispatch ExRoot AtPhi, False) ] ( \(desc, lhs, rhs, expected) -> it desc $ (lhs == rhs) `shouldBe` expected ) describe "Expression Eq instance compares different constructors" $ forM_ [ ("formation vs this", ExFormation [], ExXi, False) , ("global vs termination", ExRoot, ExTermination, False) , ("meta vs dispatch", ExMeta "e", ExDispatch ExRoot AtRho, False) ] ( \(desc, lhs, rhs, expected) -> it desc $ (lhs == rhs) `shouldBe` expected ) describe "Expression Ord instance orders correctly" $ it "sorts expressions by constructor order" $ let exprs = [ExDispatch ExRoot AtRho, ExApplication ExRoot (ArTau AtRho ExRoot), ExMeta "e", ExTermination, ExRoot, ExXi, ExFormation []] first : _ = sort exprs in first `shouldBe` ExFormation [] describe "Expression Ord instance over a term holding the level below it twice" $ it "compares the term with itself without walking it as a tree" $ do let doubled :: Expression -> Expression doubled inner = ExFormation [BiTau AtRho inner, BiTau (AtLabel "qa") inner] deep :: Expression deep = iterate doubled (ExFormation [BiLambda (FnSymbol 3)]) !! 60 timeout 10000000 (evaluate (compare (ExApplication deep (ArTau (AtLabel "ve") deep)) (ExApplication deep (ArTau (AtLabel "ve") deep)))) `shouldReturn` Just EQ describe "countNodes counts ExFormation with non-tau bindings" $ forM_ [ ("Q", ExRoot, 1) , ("T", ExTermination, 1) , ("$", ExXi, 1) , ("dispatch on global", ExDispatch ExRoot (AtLabel "x"), 3) , ("application with globals", ExApplication ExRoot (ArTau AtRho ExRoot), 6) , ("nested expressions", ExFormation [BiTau AtRho ExRoot, BiTau AtPhi ExRoot], 9) , ("empty formation", ExFormation [], 1) , ("void binding", ExFormation [BiVoid AtRho], 5) , ("delta binding", ExFormation [BiDelta BtEmpty], 5) , ("lambda binding", ExFormation [BiLambda (Function "Func")], 5) , ("meta binding", ExFormation [BiMeta "B"], 3) , ("metalambda binding", ExFormation [BiLambda (FnMeta "F")], 5) , ("meta expression", ExMeta "e", 1) , ("deeply nested dispatch", ExDispatch (ExDispatch ExRoot (AtLabel "a")) (AtLabel "b"), 5) , ("formation with dispatch inside", ExFormation [BiTau AtRho (ExDispatch ExRoot (AtLabel "x"))], 7) , ("application with tau argument", ExApplication ExRoot (ArTau AtRho ExRoot), 6) , ("application with a named argument", ExApplication ExRoot (ArTau (AtLabel "kx") ExRoot), 6) , ("phi-meet wraps its inner count", ExPhiMeet Nothing 1 (ExDispatch ExRoot (AtLabel "x")), 3) , ("phi-meet with label wraps its inner count", ExPhiMeet (Just "m") 2 ExRoot, 1) , ("phi-again wraps its inner count", ExPhiAgain Nothing 1 (ExDispatch ExRoot (AtLabel "x")), 3) , ("phi-again with label wraps its inner count", ExPhiAgain (Just "m") 2 ExRoot, 1) , ("bare bytes falls into the catch-all", ExBytes BtEmpty, 1) ] ( \(desc, expr, expected) -> it desc $ countNodes expr `shouldBe` expected ) describe "hashExpression" $ do it "hashes every Expression/Binding/Bytes/Attribute/Argument/Function constructor without collisions" $ let exprs = [ ExFormation [] , ExFormation [BiTau AtRho ExRoot] , ExFormation [BiVoid AtPhi] , ExFormation [BiDelta BtEmpty] , ExFormation [BiDelta (BtOne "FF")] , ExFormation [BiDelta (BtMany ["00", "01"])] , ExFormation [BiDelta (BtMeta "b")] , ExFormation [BiLambda (Function "Func")] , ExFormation [BiLambda (FnMeta "F")] , ExFormation [BiMeta "M"] , ExXi , ExRoot , ExTermination , ExApplication ExRoot (ArTau AtRho ExXi) , ExApplication ExRoot (ArTau (AtLabel "x") ExXi) , ExApplication ExRoot (ArTau (AtMeta "A") ExXi) , ExDispatch ExRoot (AtLabel "x") , ExDispatch ExRoot AtPhi , ExDispatch ExRoot AtRho , ExDispatch ExRoot AtLambda , ExDispatch ExRoot AtDelta , ExDispatch ExRoot (AtMeta "m") , ExMeta "e" , ExMeta "f" , ExPhiMeet (Just "m") 2 ExRoot , ExPhiMeet Nothing 2 ExRoot , ExPhiMeet Nothing 3 ExRoot , ExPhiAgain (Just "m") 2 ExRoot , ExPhiAgain Nothing 2 ExRoot , ExPhiAgain Nothing 3 ExRoot , ExBytes BtEmpty , ExBytes (BtOne "FF") , ExBytes (BtMany ["00", "FF"]) , ExBytes (BtMeta "b") ] hashes = map hashExpression exprs in nub hashes `shouldBe` hashes it "produces different hashes for a couple of hand-picked distinct expressions" $ do hashExpression ExRoot `shouldNotBe` hashExpression ExXi hashExpression (ExDispatch ExRoot (AtLabel "x")) `shouldNotBe` hashExpression (ExDispatch ExRoot (AtLabel "y")) describe "alike" $ do let application :: [Int] -> Expression application idxs = ExApplication (ExDispatch ExRoot (AtLabel "f")) (ArTau (AtLabel "x") (ExFormation (map (BiLambda . FnSymbol) idxs))) it "does not tell apart two terms that differ by a bijective renaming of symbols" $ alike (application [1, 2]) (application [7, 3]) `shouldBe` True it "does not take two terms that differ by a datum for the same" $ alike (ExFormation [BiDelta (BtOne "01"), BiLambda (FnSymbol 4)]) (ExFormation [BiDelta (BtOne "02"), BiLambda (FnSymbol 9)]) `shouldBe` False it "does not map one symbol to two others" $ alike (application [1, 1]) (application [1, 2]) `shouldBe` False it "does not map two symbols to one other" $ alike (application [5, 6]) (application [8, 8]) `shouldBe` False it "does not take two terms that differ by a λ name for the same" $ alike (ExFormation [BiLambda (Function "L_one")]) (ExFormation [BiLambda (Function "L_two")]) `shouldBe` False it "does not take two terms that differ by an attribute for the same" $ alike (ExDispatch (application [3]) (AtLabel "a")) (ExDispatch (application [3]) (AtLabel "b")) `shouldBe` False describe "within" $ do let pair :: Expression -> Expression pair tail' = ExApplication (ExDispatch ExRoot (AtLabel "pair")) (ArTau (AtLabel "tail") tail') call :: Int -> Expression -> Expression call idx acc = ExFormation [BiTau (AtLabel "n") (ExFormation [BiLambda (FnSymbol idx)]), BiTau (AtLabel "acc") acc, BiLambda (Function "L_fact")] chain :: Int -> Expression -> Expression chain depth base = iterate (`ExDispatch` AtLabel "qv") base !! depth it "does not search a place again for a term it already failed to find there" $ do start <- getMonotonicTime _ <- evaluate (within (chain 16 ExXi) (chain 32 ExRoot)) end <- getMonotonicTime end - start `shouldSatisfy` (< 1) it "finds a round inside the next one that wraps its accumulator once more" $ within (call 3 (ExFormation [BiDelta (BtOne "2A")])) (call 8 (pair (ExFormation [BiDelta (BtOne "2A")]))) `shouldBe` True it "does not find a call inside a smaller one nested in it" $ within (call 4 (pair (pair ExRoot))) (call 4 (pair ExRoot)) `shouldBe` False it "does not find a formation inside one of other attributes" $ within (call 5 ExXi) (ExFormation [BiTau (AtLabel "m") (ExFormation [BiLambda (FnSymbol 5)]), BiTau (AtLabel "acc") ExXi, BiLambda (Function "L_fact")]) `shouldBe` False it "does not find a formation inside one naming another λ function" $ within (ExFormation [BiTau (AtLabel "x") ExXi, BiLambda (Function "L_zero")]) (ExFormation [BiTau (AtLabel "x") ExXi, BiLambda (Function "L_dec")]) `shouldBe` False it "does not find a round inside one that differs by a datum" $ within (call 1 (ExFormation [BiDelta (BtOne "07")])) (call 1 (pair (ExFormation [BiDelta (BtOne "09")]))) `shouldBe` False it "does not find a formation inside one that only holds it under an attribute" $ within (call 2 ExRoot) (ExFormation [BiTau (AtLabel "x") (call 2 ExRoot), BiLambda (Function "L_g")]) `shouldBe` False it "does not find a formation under the ρ of the next one" $ within (call 6 ExRoot) (call 6 (ExFormation [BiTau AtRho (call 6 ExRoot)])) `shouldBe` False it "finds a round holding a datum inside one holding a symbol in its place" $ within (call 7 (pair (ExFormation [BiDelta (BtOne "1F")]))) (call 7 (ExFormation [BiLambda (FnSymbol 9)])) `shouldBe` True it "finds a round inside one holding a symbol where the first holds one only under ρ" $ within (call 3 (ExFormation [BiDelta (BtOne "5C"), BiTau AtRho (call 2 ExRoot)])) (call 3 (ExFormation [BiLambda (FnSymbol 4)])) `shouldBe` True it "does not find a round holding a symbol inside one holding a bare symbol in its place" $ within (call 8 (pair (ExFormation [BiLambda (FnSymbol 2)]))) (call 8 (ExFormation [BiLambda (FnSymbol 6)])) `shouldBe` False it "does not find a term inside a bare symbol at the top" $ within (ExFormation [BiDelta (BtOne "3E")]) (ExFormation [BiLambda (FnSymbol 5)]) `shouldBe` False describe "akin" $ do let down :: Expression -> Expression down idx = ExFormation [BiTau (AtLabel "i") idx, BiTau AtPhi (ExDispatch ExRoot (AtLabel "qo"))] next :: Expression -> Expression next arg = ExApplication (ExDispatch ExRoot (AtLabel "next")) (ArTau (AtLabel "x") arg) atom :: Expression -> Int -> Expression atom side idx = ExFormation [BiTau (AtLabel "on") (ExFormation [BiLambda (FnSymbol idx)]), BiTau AtRho (ExFormation [BiTau (AtLabel "rt") side]), BiLambda (Function "L_vex")] it "finds an atom inside the next one whose boxed formation wraps its argument once more" $ akin (atom (down (next (ExFormation [BiLambda (FnSymbol 3)]))) 5) (atom (down (next (next (ExFormation [BiLambda (FnSymbol 3)])))) 8) `shouldBe` True it "does not find an atom inside one whose boxed formation lost a wrapper" $ akin (atom (down (next (next ExRoot))) 2) (atom (down (next ExRoot)) 2) `shouldBe` False it "does not find an atom inside one that differs outside its boxed formations" $ akin (ExFormation [BiTau (AtLabel "t") (ExDispatch ExRoot (AtLabel "pu")), BiLambda (Function "L_yo")]) (ExFormation [BiTau (AtLabel "t") (next (ExDispatch ExRoot (AtLabel "pu"))), BiLambda (Function "L_yo")]) `shouldBe` False it "does not find an atom inside one naming another λ function" $ akin (ExFormation [BiTau (AtLabel "w") (down ExXi), BiLambda (Function "L_ab")]) (ExFormation [BiTau (AtLabel "w") (down (next ExXi)), BiLambda (Function "L_cd")]) `shouldBe` False it "does not map one symbol outside a boxed formation to two others" $ akin (atom (down ExRoot) 4) (ExFormation [BiTau (AtLabel "on") (ExFormation [BiLambda (FnSymbol 6)]), BiTau AtRho (ExFormation [BiTau (AtLabel "rt") (down ExRoot), BiTau (AtLabel "of") (ExFormation [BiLambda (FnSymbol 6)])]), BiLambda (Function "L_vex")]) `shouldBe` False it "does not find an atom inside one whose boxed formation is not within, however alike their parts" $ akin (atom (ExFormation [BiTau AtPhi ExRoot, BiTau (AtLabel "z") ExXi]) 1) (atom (ExFormation [BiTau AtPhi ExRoot, BiTau (AtLabel "z") ExRoot]) 1) `shouldBe` False prop "finds every term inside itself, as the looser search does" $ \(Grown one) -> (akin one one, within one one) `shouldBe` (True, True) it "does not walk a name it already measured again when it compares how deep two calls are" $ do let named :: Int -> Expression named idx = ExMeta (T.pack ("n·" ++ show idx)) term :: Int -> Expression term idx = ExApplication (ExApplication (ExDispatch ExRoot (AtLabel "f")) (ArTau (AtLabel "a") (named (pred idx)))) (ArTau (AtLabel "b") (named (pred idx))) look :: T.Text -> Maybe Expression look name = case T.stripPrefix "n·" name of Just idx | idx /= "0" -> Just (term (read (T.unpack idx))) _ -> Nothing depths :: Map.Map T.Text Int depths = List.foldl' (\kept idx -> Map.insert (T.pack ("n·" ++ show idx)) (deepness look (`Map.lookup` kept) (term idx)) kept) Map.empty [1 .. 22 :: Int] start <- getMonotonicTime found <- evaluate (akin' look (`Map.lookup` depths) (ExDispatch (named 21) (AtLabel "q")) (ExDispatch (named 22) (AtLabel "q"))) end <- getMonotonicTime (found, end - start < 1) `shouldBe` (True, True) prop "answers the same with what an earlier search kept as with nothing" $ \(Grown one) (Grown two) (Grown three) -> fst (akin'' (const Nothing) (const Nothing) (snd (akin'' (const Nothing) (const Nothing) Map.empty three one)) one two) `shouldBe` akin one two describe "deepness" $ do it "counts a call one deeper than the deepest of its head and its argument" $ deepness (const Nothing) (const Nothing) (ExApplication (ExDispatch (ExDispatch ExRoot (AtLabel "f")) (AtLabel "g")) (ArTau (AtLabel "x") ExXi)) `shouldBe` 3 it "takes the depth of a name it was told instead of walking its term" $ deepness (const (Just (ExDispatch ExRoot (AtLabel "f")))) (const (Just 7)) (ExDispatch (ExMeta "n·1") (AtLabel "q")) `shouldBe` 8 prop "measures a term the same with the depths of its names kept as by walking them" $ \(Grown one) (Grown two) (Grown three) -> let terms :: Map.Map T.Text Expression terms = Map.fromList [("n·1", one), ("n·2", ExApplication two (ArTau (AtLabel "x") (ExMeta "n·1")))] look :: T.Text -> Maybe Expression look = (`Map.lookup` terms) depths :: Map.Map T.Text Int depths = List.foldl' (\kept (name, term) -> Map.insert name (deepness look (`Map.lookup` kept) term) kept) Map.empty (Map.toAscList terms) whole :: Expression whole = ExApplication three (ArTau (AtLabel "arg") (ExMeta "n·2")) in deepness look (`Map.lookup` depths) whole `shouldBe` deepness look (const Nothing) whole describe "reaches" $ do prop "tells a term reaches a depth exactly where it measures at least that deep" $ \(Grown one) -> let deep :: Int deep = deepness (const Nothing) (const Nothing) one in map (\limit -> reaches (const Nothing) (const Nothing) limit one) [0 .. deep + 1] `shouldBe` map (<= deep) [0 .. deep + 1] prop "takes the depth of a name it was told as deepness does" $ \(Grown one) (Grown two) -> let look :: T.Text -> Maybe Expression look name = if name == "n·1" then Just one else Nothing depth :: T.Text -> Maybe Int depth name = if name == "n·1" then Just (deepness look (const Nothing) one) else Nothing whole :: Expression whole = ExApplication two (ArTau (AtLabel "x") (ExMeta "n·1")) deep :: Int deep = deepness look depth whole in map (\limit -> reaches look depth limit whole) [0 .. deep + 1] `shouldBe` map (<= deep) [0 .. deep + 1] it "does not walk an earlier call deeper than the new one when it compares them" $ do let named :: Int -> Expression named idx = ExMeta (T.pack ("n·" ++ show idx)) term :: Int -> Expression term idx = ExApplication (ExApplication (ExDispatch ExRoot (AtLabel "f")) (ArTau (AtLabel "a") (named (pred idx)))) (ArTau AtRho (named (pred idx))) look :: T.Text -> Maybe Expression look name = case T.stripPrefix "n·" name of Just idx | idx /= "0" -> Just (term (read (T.unpack idx))) _ -> Nothing call :: Expression -> Expression call arg = ExApplication (ExDispatch ExRoot (AtLabel "g")) (ArTau (AtLabel "x") arg) found <- timeout 1000000 (evaluate (akin' look (const Nothing) (call (ExFormation [BiTau AtRho (named 64)])) (call ExRoot))) found `shouldBe` Just False describe "hashSkeleton" $ do it "does not tell apart two formations that bind other terms to the same attributes" $ hashSkeleton (ExFormation [BiTau (AtLabel "acc") ExRoot, BiLambda (Function "L_f")]) `shouldBe` hashSkeleton (ExFormation [BiTau (AtLabel "acc") (ExDispatch ExXi (AtLabel "q")), BiLambda (Function "L_f")]) it "does not hash two formations naming different λ functions alike" $ hashSkeleton (ExFormation [BiLambda (Function "L_f")]) `shouldNotBe` hashSkeleton (ExFormation [BiLambda (Function "L_g")]) describe "hashShape" $ do it "does not tell apart two terms that differ by symbols alone" $ hashShape (ExFormation [BiTau (AtLabel "n") (ExFormation [BiLambda (FnSymbol 3)])]) `shouldBe` hashShape (ExFormation [BiTau (AtLabel "n") (ExFormation [BiLambda (FnSymbol 5)])]) it "does not hash two terms differing by a datum alike" $ hashShape (ExFormation [BiDelta (BtOne "0A")]) `shouldNotBe` hashShape (ExFormation [BiDelta (BtOne "0B")]) it "hashes a term holding the level below it twice without walking it as a tree" $ do let doubled :: Expression -> Expression doubled inner = ExFormation [BiTau AtRho inner, BiTau (AtLabel "x") inner] deep = iterate doubled (ExFormation [BiLambda (FnSymbol 1)]) !! 60 hashShape deep `shouldBe` hashShape (doubled (iterate doubled (ExFormation [BiLambda (FnSymbol 2)]) !! 59)) describe "BaseObject pattern" $ do it "constructs a Q-dispatch expression" $ BaseObject "bytes" `shouldBe` ExDispatch ExRoot (AtLabel "bytes") forM_ [ ("matches a Q-dispatch expression, extracting the label", ExDispatch ExRoot (AtLabel "number"), Just "number") , ("does not match a non-Q-dispatch expression", ExDispatch ExXi (AtLabel "number"), Nothing) ] ( \(desc, expr, expected) -> it desc $ let matched = case expr of BaseObject label -> Just label _ -> Nothing in matched `shouldBe` expected ) describe "dataBytes" $ it "builds the bytes-object formation carrying the given bytes" $ dataBytes (BtOne "48") `shouldBe` ExApplication (ExDispatch ExRoot (AtLabel "bytes")) (ArTau AtPhi (ExFormation [BiDelta (BtOne "48")])) describe "DataObject/DataString/DataNumber pattern" $ do it "constructs the phi-named, unwrapped form" $ DataString (BtOne "48") `shouldBe` ExApplication (ExDispatch ExRoot (AtLabel "string")) (ArTau AtPhi (dataBytes (BtOne "48"))) forM_ [ ( "matches the phi-named, unwrapped form" , ExApplication (ExDispatch ExRoot (AtLabel "string")) (ArTau AtPhi (dataBytes (BtOne "48"))) , Just (BtOne "48") ) , ( "matches the legacy as-bytes name on the outer binding" , ExApplication (ExDispatch ExRoot (AtLabel "string")) (ArTau (AtLabel "as-bytes") (dataBytes (BtOne "48"))) , Just (BtOne "48") ) , ( "matches the named form with a phi-again-wrapped outer object" , ExApplication (ExPhiAgain Nothing 1 (ExDispatch ExRoot (AtLabel "string"))) (ArTau (AtLabel "as-bytes") (dataBytes (BtOne "48"))) , Just (BtOne "48") ) , ( "matches the named form with a phi-again-wrapped inner bytes formation" , ExApplication (ExDispatch ExRoot (AtLabel "number")) ( ArTau (AtLabel "as-bytes") ( ExApplication (ExDispatch ExRoot (AtLabel "bytes")) (ArTau (AtLabel "data") (ExPhiAgain Nothing 1 (ExFormation [BiDelta (BtOne "05")]))) ) ) , Just (BtOne "05") ) ] ( \(desc, expr, expected) -> it desc $ let matched = case expr of DataString bts -> Just bts DataNumber bts -> Just bts _ -> Nothing in matched `shouldBe` expected ) forM_ [ ( "does not match when the outer object is not a base object (matchOuter fails)" , ExApplication ExRoot (ArTau (AtLabel "as-bytes") (dataBytes (BtOne "48"))) ) , ( "does not match when the inner object is not an application (matchInner fails)" , ExApplication (ExDispatch ExRoot (AtLabel "string")) (ArTau (AtLabel "as-bytes") ExRoot) ) , ( "does not match when the inner base object is not 'bytes' (matchesBytes fails)" , ExApplication (ExDispatch ExRoot (AtLabel "string")) ( ArTau (AtLabel "as-bytes") ( ExApplication (ExDispatch ExRoot (AtLabel "other")) (ArTau AtPhi (ExFormation [BiDelta (BtOne "48")])) ) ) ) , ( "does not match when the bytes formation has the wrong shape (matchFormation fails)" , ExApplication (ExDispatch ExRoot (AtLabel "string")) ( ArTau (AtLabel "as-bytes") ( ExApplication (ExDispatch ExRoot (AtLabel "bytes")) (ArTau (AtLabel "data") (ExFormation [BiDelta (BtOne "48"), BiVoid AtRho])) ) ) ) ] ( \(desc, expr) -> it desc $ let matched = case expr of DataObject label bts -> Just (label, bts) _ -> Nothing in matched `shouldBe` Nothing ) describe "attributeFromBinding" $ forM_ [ ("BiTau yields its attribute", BiTau AtRho ExRoot, Just AtRho) , ("BiVoid yields its attribute", BiVoid AtPhi, Just AtPhi) , ("BiDelta yields AtDelta", BiDelta BtEmpty, Just AtDelta) , ("BiLambda yields AtLambda", BiLambda (Function "F"), Just AtLambda) , ("BiMeta yields Nothing", BiMeta "B", Nothing) ] (\(desc, binding, expected) -> it desc (attributeFromBinding binding `shouldBe` expected)) describe "inert" $ do it "takes a formation of dispatches off ξ and Φ for inert" $ inert (ExFormation [BiTau (AtLabel "kv") (ExDispatch ExXi (AtLabel "ob")), BiTau (AtLabel "zu") (ExApplication (ExDispatch ExRoot (AtLabel "ny")) (ArTau (AtLabel "q") ExXi)), BiLambda (Function "L_wy")]) `shouldBe` True it "does not take a dispatch on a formation for inert" $ inert (ExDispatch (ExFormation [BiTau (AtLabel "hm") ExRoot]) (AtLabel "hm")) `shouldBe` False it "does not take an application of ⊥ for inert" $ inert (ExApplication ExTermination (ArTau (AtLabel "zwei") ExXi)) `shouldBe` False it "does not take a formation holding both λ and Δ for inert" $ inert (ExFormation [BiDelta (BtOne "3F"), BiVoid AtRho, BiLambda (Function "L_ep")]) `shouldBe` False it "does not take a formation holding a redex deep inside for inert" $ inert (ExFormation [BiTau (AtLabel "ix") (ExFormation [BiTau (AtLabel "gu") (ExDispatch ExTermination (AtLabel "sa"))])]) `shouldBe` False it "does not take a formation holding a binding meta for inert" $ inert (ExFormation [BiVoid (AtLabel "ro"), BiMeta "B4"]) `shouldBe` False describe "distinct" $ do it "takes a formation of different attributes for distinct" $ distinct (ExFormation [BiVoid (AtLabel "ka"), BiTau (AtLabel "ak") ExXi, BiLambda (Function "L_ok")]) `shouldBe` True it "does not take a formation carrying one attribute twice for distinct" $ distinct (ExFormation [BiVoid (AtLabel "ka"), BiTau AtPhi ExXi, BiTau (AtLabel "ka") ExRoot]) `shouldBe` False describe "repeated" $ do it "finds the attribute the bindings carry for the second time" $ repeated [BiVoid (AtLabel "wz"), BiDelta (BtOne "0C"), BiTau (AtLabel "zw") ExXi, BiDelta BtEmpty] `shouldBe` Just AtDelta it "does not find a repeat among hundreds of distinct labels" $ repeated [BiVoid (AtLabel (T.pack ('q' : show idx))) | idx <- [3 :: Int, 10 .. 2900]] `shouldBe` Nothing describe "Expression equality" $ do it "does not tell apart two formations built apart of the same parts" $ ExFormation [BiTau (AtLabel "lu") (ExDispatch ExXi (AtLabel "op")), BiVoid AtRho] `shouldBe` ExFormation [BiTau (AtLabel "lu") (ExDispatch ExXi (AtLabel "op")), BiVoid AtRho] it "tells apart two formations that differ deep inside" $ ExFormation [BiTau (AtLabel "lu") (ExDispatch ExXi (AtLabel "op"))] `shouldNotBe` ExFormation [BiTau (AtLabel "lu") (ExDispatch ExXi (AtLabel "po"))] describe "symbols" $ do it "names every symbol once, in the order it first stands" $ symbols (ExFormation [BiTau (AtLabel "mo") (ExFormation [BiLambda (FnSymbol 8)]), BiTau (AtLabel "ar") (ExFormation [BiLambda (FnSymbol 2)]), BiTau (AtLabel "si") (ExFormation [BiLambda (FnSymbol 8)])]) `shouldBe` [8, 2] it "walks every distinct term once in a term holding the level below it twice" $ do let doubled :: Expression -> Expression doubled inner = ExFormation [BiTau AtRho inner, BiTau (AtLabel "ku") (ExApplication (ExDispatch inner (AtLabel "te")) (ArTau (AtLabel "of") inner))] deep :: Expression deep = iterate doubled (ExFormation [BiLambda (FnSymbol 5), BiTau (AtLabel "ny") (ExFormation [BiLambda (FnSymbol 4)])]) !! 60 timeout 10000000 (let found = symbols deep in found <$ evaluate (length found)) `shouldReturn` Just [5, 4] describe "Expression Show instance" $ it "does not show what a node carries besides its parts" $ show (ExApplication (ExDispatch ExXi (AtLabel "yb")) (ArTau (AtLabel "kl") (ExFormation [BiVoid AtRho]))) `shouldBe` "ExApplication (ExDispatch ExXi yb) (ArTau kl (ExFormation [BiVoid ρ]))" describe "lifted" $ do it "raises every symbol above the floor by the offset" $ symbols (lifted 4 7 (ExApplication (ExFormation [BiLambda (FnSymbol 5)]) (ArTau (AtLabel "wo") (ExFormation [BiTau AtPhi (ExFormation [BiLambda (FnSymbol 9)])])))) `shouldBe` [12, 16] it "does not raise a symbol at the floor or under it" $ symbols (lifted 4 7 (ExDispatch (ExFormation [BiLambda (FnSymbol 4), BiTau (AtLabel "qe") (ExFormation [BiLambda (FnSymbol 2)])]) (AtLabel "ke"))) `shouldBe` [4, 2] it "does not touch a term carrying no symbol" $ lifted 0 3 (ExFormation [BiTau (AtLabel "ul") (ExDispatch ExXi (AtLabel "ha")), BiLambda (Function "L_up"), BiDelta (BtOne "1F")]) `shouldBe` ExFormation [BiTau (AtLabel "ul") (ExDispatch ExXi (AtLabel "ha")), BiLambda (Function "L_up"), BiDelta (BtOne "1F")] describe "intact" $ do it "hands back the whole where the part is the one it had" $ intact [BiVoid (AtLabel "zu")] (ExDispatch ExXi (AtLabel "old")) ExFormation [BiVoid (AtLabel "zu")] `shouldBe` ExDispatch ExXi (AtLabel "old") it "makes the whole anew where the part differs" $ intact [BiVoid (AtLabel "zu")] (ExDispatch ExXi (AtLabel "old")) ExFormation [BiVoid (AtLabel "ri")] `shouldBe` ExFormation [BiVoid (AtLabel "ri")] describe "suffixed" $ do it "changes the items the walk changes and keeps the others" $ suffixed (\item -> Just (if item == BiVoid (AtLabel "ko") then BiVoid (AtLabel "mu") else item)) [BiVoid (AtLabel "ax"), BiVoid (AtLabel "ko"), BiVoid (AtLabel "ep")] `shouldBe` Just [BiVoid (AtLabel "ax"), BiVoid (AtLabel "mu"), BiVoid (AtLabel "ep")] it "answers the list it was given where the walk changes nothing" $ suffixed Just [BiVoid (AtLabel "yi"), BiDelta (BtOne "2D")] `shouldBe` Just [BiVoid (AtLabel "yi"), BiDelta (BtOne "2D")] it "fails where the walk fails" $ suffixed (\item -> if item == BiVoid (AtLabel "os") then Nothing else Just item) [BiVoid (AtLabel "ig"), BiVoid (AtLabel "os")] `shouldBe` Nothing