{-# LANGUAGE OverloadedStrings #-}-- SPDX-FileCopyrightText: Copyright (c) 2025 Objectionary.com-- SPDX-License-Identifier: MITmodule MatcherSpec whereimport ASTimport Control.Exception (evaluate)import Control.Monad (forM_)import Data.Map.Strict qualified as Mapimport Data.Text qualified as Timport Matcherimport System.Timeout (timeout)import Test.Hspec (Example (Arg), Expectation, Spec, SpecWith, describe, it, shouldBe)substs :: [[(T.Text, MetaValue)]] -> [Subst]substs = map (Subst . Map.mapKeys Named . Map.fromList)test :: (a -> a -> [Subst]) -> [(String, a, a, [Subst])] -> SpecWith (Arg Expectation)test function useCases = forM_ useCases $ \(desc, ptn, tgt, expected) -> it desc $ function ptn tgt `shouldBe` expectedspec :: Specspec = do describe "matchExpressionDeep: expression => expression => [substitution]" $ test matchExpressionDeep [ ( "Q => [[ @ -> Q, ^ -> Q ]] => [(), ()]" , ExRoot , ExFormation [BiTau AtPhi ExRoot, BiTau AtRho ExRoot] , substs [[], []] ) , ( "Q.!t => [[ @ -> Q.y, ^ -> [[ a -> Q.w ]], @ -> Q.y ]] => [(t >> y), (t >> w), (t >> y)]" , ExDispatch ExRoot (AtMeta "t") , ExFormation [ BiTau AtPhi (ExDispatch ExRoot (AtLabel "y")) , BiTau AtRho (ExFormation [BiTau (AtLabel "a") (ExDispatch ExRoot (AtLabel "w"))]) , BiTau AtPhi (ExDispatch ExRoot (AtLabel "y")) ] , substs [[("t", MvAttribute (AtLabel "y"))], [("t", MvAttribute (AtLabel "w"))], [("t", MvAttribute (AtLabel "y"))]] ) , ( "[[!t -> Q.org.!t]] => [[f -> [[x -> Q.org.x]], t -> [[y -> Q.org.y]] => [(!t >> x), (!t >> y)]" , ExFormation [BiTau (AtMeta "t") (ExDispatch (ExDispatch ExRoot (AtLabel "org")) (AtMeta "t"))] , ExFormation [ BiTau (AtLabel "f") (ExFormation [BiTau (AtLabel "x") (ExDispatch (ExDispatch ExRoot (AtLabel "org")) (AtLabel "x"))]) , BiTau (AtLabel "t") (ExFormation [BiTau (AtLabel "y") (ExDispatch (ExDispatch ExRoot (AtLabel "org")) (AtLabel "y"))]) ] , substs [[("t", MvAttribute (AtLabel "x"))], [("t", MvAttribute (AtLabel "y"))]] ) , ( "!e => [[x -> Q]] => [(!e >> [[x -> Q]] ), (!e >> Q)]" , ExMeta "e" , ExFormation [BiTau (AtLabel "x") ExRoot] , substs [ [("e", MvExpression (ExFormation [BiTau (AtLabel "x") ExRoot]))] , [("e", MvExpression ExRoot)] ] ) , ( "!e.!t => Q.org.eolang => [(!e >> Q.org, !t >> eolang), (!e >> Q, !t >> org)]" , ExDispatch (ExMeta "e") (AtMeta "t") , ExDispatch (ExDispatch ExRoot (AtLabel "org")) (AtLabel "eolang") , substs [ [("e", MvExpression (ExDispatch ExRoot (AtLabel "org"))), ("t", MvAttribute (AtLabel "eolang"))] , [("e", MvExpression ExRoot), ("t", MvAttribute (AtLabel "org"))] ] ) , ( "⟦!B1, !t ↦ ∅, !B2⟧.!t => ⟦ x ↦ ξ.t, t ↦ ∅ ⟧.t(ρ ↦ ⟦ x ↦ ξ.t, t ↦ ∅ ⟧) => [(!B1 >> ⟦x ↦ ξ.t⟧, !t >> t, !B2 >> ⟦⟧ )]" , ExDispatch (ExFormation [BiMeta "B1", BiVoid (AtMeta "t"), BiMeta "B2"]) (AtMeta "t") , ExApplication ( ExDispatch ( ExFormation [ BiTau (AtLabel "x") (ExDispatch ExXi (AtLabel "t")) , BiVoid (AtLabel "t") ] ) (AtLabel "t") ) ( ArTau AtRho ( ExFormation [ BiTau (AtLabel "x") (ExDispatch ExXi (AtLabel "t")) , BiVoid (AtLabel "t") ] ) ) , substs [ [ ("B1", MvBindings [BiTau (AtLabel "x") (ExDispatch ExXi (AtLabel "t"))]) , ("t", MvAttribute (AtLabel "t")) , ("B2", MvBindings []) ] ] ) , ( "somebody" , ExFormation [ BiTau (AtLabel "i1") ( ExFormation [ BiTau (AtLabel "a") (ExMeta "e0") , BiTau (AtLabel "b") (ExMeta "e-first") ] ) , BiTau (AtLabel "i2") ( ExFormation [ BiTau (AtLabel "a") (ExMeta "e0") , BiTau (AtLabel "b") (ExMeta "e-second") ] ) ] , ExFormation [ BiTau (AtLabel "i1") ( ExFormation [ BiTau (AtLabel "a") ExRoot , BiTau (AtLabel "b") ExXi ] ) , BiTau (AtLabel "i2") ( ExFormation [ BiTau (AtLabel "a") ExRoot , BiTau (AtLabel "b") (ExFormation [BiVoid AtPhi]) ] ) ] , substs [ [ ("e0", MvExpression ExRoot) , ("e-first", MvExpression ExXi) , ("e-second", MvExpression (ExFormation [BiVoid AtPhi])) ] ] ) ] describe "matchAttribute: attribute => attribute => substitution" $ forM_ [ ("!t => ^ => [(!t >> ^)]", AtMeta "t", AtRho, substs [[("t", MvAttribute AtRho)]]) , ("!t => @ => [(!t >> @)]", AtMeta "t", AtPhi, substs [[("t", MvAttribute AtPhi)]]) ] ( \(desc, ptn, tgt, expected) -> it desc $ matchAttribute ptn tgt `shouldBe` expected ) describe "matchAlpha: alpha => alpha => substitution" $ forM_ [ ("α1 => α1 => [()]", Alpha 1, Alpha 1, substs [[]]) , ("α0 => α1 => []", Alpha 0, Alpha 1, substs []) ] ( \(desc, ptn, tgt, expected) -> it desc $ matchAlpha ptn tgt `shouldBe` expected ) describe "matchFunction: function => function => substitution" $ forM_ [ ("!f => Func => [(!f >> Func)]", FnMeta "f", Function "Func", substs [[("f", MvFunction (Function "Func"))]]) , ("Func => Func => [()]", Function "Func", Function "Func", substs [[]]) , ("Func => Other => []", Function "Func", Function "Other", substs []) ] ( \(desc, ptn, tgt, expected) -> it desc $ matchFunction ptn tgt `shouldBe` expected ) describe "matchExpression': phi-meet/phi-again nodes" $ forM_ [ ( "PhiMeet(same prefix/idx) matches its inner expression" , ExPhiMeet (Just "p") 1 ExRoot , ExPhiMeet (Just "p") 1 ExRoot , substs [[]] ) , ( "PhiMeet with different idx does not match" , ExPhiMeet (Just "p") 1 ExRoot , ExPhiMeet (Just "p") 2 ExRoot , substs [] ) , ( "PhiMeet with different prefix does not match" , ExPhiMeet (Just "p") 1 ExRoot , ExPhiMeet (Just "q") 1 ExRoot , substs [] ) , ( "PhiAgain(same prefix/idx) matches its inner expression" , ExPhiAgain Nothing 0 ExXi , ExPhiAgain Nothing 0 ExXi , substs [[]] ) , ( "PhiAgain with different idx does not match" , ExPhiAgain Nothing 0 ExXi , ExPhiAgain Nothing 1 ExXi , substs [] ) ] ( \(desc, ptn, tgt, expected) -> it desc $ matchExpression' ptn tgt `shouldBe` expected ) describe "matchBindings: [binding] => [binding] => substitution" $ test matchBindings [ ( "[[]] => [[]] => ()" , [] , [] , substs [[]] ) , ( "[[!B]] => T:[[x -> ?, D> 01-, L> Func]] => (!B >> T)" , [BiMeta "B"] , [BiVoid (AtLabel "x"), BiDelta (BtOne "01"), BiLambda (Function "Func")] , substs [[("B", MvBindings [BiVoid (AtLabel "x"), BiDelta (BtOne "01"), BiLambda (Function "Func")])]] ) , ( "[[D> 00-]] => [[D> 00-, L> Func]] => []" , [BiDelta (BtOne "00")] , [BiDelta (BtOne "00"), BiLambda (Function "Func")] , substs [] ) , ( "[[y -> ?, !t -> ?]] => [[y -> ?, x -> ?]] => (!t >> x)" , [BiVoid (AtLabel "y"), BiVoid (AtMeta "t")] , [BiVoid (AtLabel "y"), BiVoid (AtLabel "x")] , substs [[("t", MvAttribute (AtLabel "x"))]] ) , ( "[[!B, x -> ?]] => [[x -> ?]] => (!B >> [[]])" , [BiMeta "B", BiVoid (AtLabel "x")] , [BiVoid (AtLabel "x")] , substs [[("B", MvBindings [])]] ) , ( "[[!B1, x -> ?, !B2]] => [[x -> ?, y -> ?]] => (!B1 >> [[]], !B2 >> [[y -> ?]])" , [BiMeta "B1", BiVoid (AtLabel "x"), BiMeta "B2"] , [BiVoid (AtLabel "x"), BiVoid (AtLabel "y")] , substs [[("B1", MvBindings []), ("B2", MvBindings [BiVoid (AtLabel "y")])]] ) , ( "[[!B1, !x -> ?, !B2]] => [[y -> ?, D> -> 00-, L> Func]] => (!x >> y, !B1 >> [[]], !B2 >> [[D> -> 00-, L> Func]])" , [BiMeta "B1", BiVoid (AtMeta "x"), BiMeta "B2"] , [BiVoid (AtLabel "y"), BiDelta (BtOne "00"), BiLambda (Function "Func")] , substs [[("B1", MvBindings []), ("B2", MvBindings [BiDelta (BtOne "00"), BiLambda (Function "Func")]), ("x", MvAttribute (AtLabel "y"))]] ) , ( "[[!x -> ?, !y -> ?]] => [[a -> ?, b -> ?]] => (!x >> a, !y >> b)" , [BiVoid (AtMeta "x"), BiVoid (AtMeta "y")] , [BiVoid (AtLabel "a"), BiVoid (AtLabel "b")] , substs [[("x", MvAttribute (AtLabel "a")), ("y", MvAttribute (AtLabel "b"))]] ) , ( "[[t -> ?, !B]] => [[t -> ?, x -> Q, y -> $]] => (!B >> [[x -> Q, y -> $]])" , [BiVoid (AtLabel "t"), BiMeta "B"] , [BiVoid (AtLabel "t"), BiTau (AtLabel "x") ExRoot, BiTau (AtLabel "y") ExXi] , substs [[("B", MvBindings [BiTau (AtLabel "x") ExRoot, BiTau (AtLabel "y") ExXi])]] ) , ( "[[!B, z -> Q]] => [[x -> Q, y -> $, z -> Q]] => (!B >> [[x -> Q, y -> $]])" , [BiMeta "B", BiTau (AtLabel "z") ExRoot] , [BiTau (AtLabel "x") ExRoot, BiTau (AtLabel "y") ExXi, BiTau (AtLabel "z") ExRoot] , substs [[("B", MvBindings [BiTau (AtLabel "x") ExRoot, BiTau (AtLabel "y") ExXi])]] ) , ( "[[L> Func, D> 00-]] => [[D> 00-, L> Func]] => []" , [BiLambda (Function "Func"), BiDelta (BtOne "00")] , [BiDelta (BtOne "00"), BiLambda (Function "Func")] , substs [] ) , ( "[[t -> ?, !B]] => [[x -> ?, t -> ?]] => []" , [BiVoid (AtLabel "t"), BiMeta "B"] , [BiVoid (AtLabel "x"), BiVoid (AtLabel "t")] , substs [] ) , ( "[[!B, !t -> ?]] => [[x -> ?, y -> ?]] => (!t >> y, !B >> [[ x -> ? ]] )" , [BiMeta "B", BiVoid (AtMeta "t")] , [BiVoid (AtLabel "x"), BiVoid (AtLabel "y")] , substs [[("t", MvAttribute (AtLabel "y")), ("B", MvBindings [BiVoid (AtLabel "x")])]] ) , ( "[[!B1, !t -> ?, !B2]] => [[ x -> ?, y -> ?, z -> ? ]] => [(!B1 >> [[]], !t >> x, !B2 >> [[ y -> ?, z -> ? ]]), (...), (...)]" , [BiMeta "B1", BiVoid (AtMeta "t"), BiMeta "B2"] , [BiVoid (AtLabel "x"), BiVoid (AtLabel "y"), BiVoid (AtLabel "z")] , substs [ [ ("B1", MvBindings []) , ("t", MvAttribute (AtLabel "x")) , ("B2", MvBindings [BiVoid (AtLabel "y"), BiVoid (AtLabel "z")]) ] , [ ("B1", MvBindings [BiVoid (AtLabel "x")]) , ("t", MvAttribute (AtLabel "y")) , ("B2", MvBindings [BiVoid (AtLabel "z")]) ] , [ ("B1", MvBindings [BiVoid (AtLabel "x"), BiVoid (AtLabel "y")]) , ("t", MvAttribute (AtLabel "z")) , ("B2", MvBindings []) ] ] ) , ( "[[!B1, !t1 -> ?, !B2, !t2 -> ?, !B3]] => [[ a -> ?, b -> ?, x -> ?, y -> ?, z -> ? ]] => [10 substs]" , [BiMeta "B1", BiVoid (AtMeta "t1"), BiMeta "B2", BiVoid (AtMeta "t2"), BiMeta "B3"] , [ BiVoid (AtLabel "a") , BiVoid (AtLabel "b") , BiVoid (AtLabel "x") , BiVoid (AtLabel "y") , BiVoid (AtLabel "z") ] , substs [ [ ("B1", MvBindings []) , ("t1", MvAttribute (AtLabel "a")) , ("B2", MvBindings []) , ("t2", MvAttribute (AtLabel "b")) , ("B3", MvBindings [BiVoid (AtLabel "x"), BiVoid (AtLabel "y"), BiVoid (AtLabel "z")]) ] , [ ("B1", MvBindings []) , ("t1", MvAttribute (AtLabel "a")) , ("B2", MvBindings [BiVoid (AtLabel "b")]) , ("t2", MvAttribute (AtLabel "x")) , ("B3", MvBindings [BiVoid (AtLabel "y"), BiVoid (AtLabel "z")]) ] , [ ("B1", MvBindings []) , ("t1", MvAttribute (AtLabel "a")) , ("B2", MvBindings [BiVoid (AtLabel "b"), BiVoid (AtLabel "x")]) , ("t2", MvAttribute (AtLabel "y")) , ("B3", MvBindings [BiVoid (AtLabel "z")]) ] , [ ("B1", MvBindings []) , ("t1", MvAttribute (AtLabel "a")) , ("B2", MvBindings [BiVoid (AtLabel "b"), BiVoid (AtLabel "x"), BiVoid (AtLabel "y")]) , ("t2", MvAttribute (AtLabel "z")) , ("B3", MvBindings []) ] , [ ("B1", MvBindings [BiVoid (AtLabel "a")]) , ("t1", MvAttribute (AtLabel "b")) , ("B2", MvBindings []) , ("t2", MvAttribute (AtLabel "x")) , ("B3", MvBindings [BiVoid (AtLabel "y"), BiVoid (AtLabel "z")]) ] , [ ("B1", MvBindings [BiVoid (AtLabel "a")]) , ("t1", MvAttribute (AtLabel "b")) , ("B2", MvBindings [BiVoid (AtLabel "x")]) , ("t2", MvAttribute (AtLabel "y")) , ("B3", MvBindings [BiVoid (AtLabel "z")]) ] , [ ("B1", MvBindings [BiVoid (AtLabel "a")]) , ("t1", MvAttribute (AtLabel "b")) , ("B2", MvBindings [BiVoid (AtLabel "x"), BiVoid (AtLabel "y")]) , ("t2", MvAttribute (AtLabel "z")) , ("B3", MvBindings []) ] , [ ("B1", MvBindings [BiVoid (AtLabel "a"), BiVoid (AtLabel "b")]) , ("t1", MvAttribute (AtLabel "x")) , ("B2", MvBindings []) , ("t2", MvAttribute (AtLabel "y")) , ("B3", MvBindings [BiVoid (AtLabel "z")]) ] , [ ("B1", MvBindings [BiVoid (AtLabel "a"), BiVoid (AtLabel "b")]) , ("t1", MvAttribute (AtLabel "x")) , ("B2", MvBindings [BiVoid (AtLabel "y")]) , ("t2", MvAttribute (AtLabel "z")) , ("B3", MvBindings []) ] , [ ("B1", MvBindings [BiVoid (AtLabel "a"), BiVoid (AtLabel "b"), BiVoid (AtLabel "x")]) , ("t1", MvAttribute (AtLabel "y")) , ("B2", MvBindings []) , ("t2", MvAttribute (AtLabel "z")) , ("B3", MvBindings []) ] ] ) ] describe "matchExpression: expression => pattern => substitution" $ test matchExpression' [ ("$ => $ => [()]", ExXi, ExXi, substs [[]]) , ("Q => Q => [()]", ExRoot, ExRoot, substs [[]]) , ( "!e => Q => [(!e >> Q)]" , ExMeta "e" , ExRoot , substs [[("e", MvExpression ExRoot)]] ) , ( "!e => Q.org(x -> $) => [(!e >> Q.org(x -> $))]" , ExMeta "e" , ExApplication (ExDispatch ExRoot (AtLabel "org")) (ArTau (AtLabel "x") ExXi) , substs [[("e", MvExpression (ExApplication (ExDispatch ExRoot (AtLabel "org")) (ArTau (AtLabel "x") ExXi)))]] ) , ( "!e1.x => Q.org.x => [(!e1 >> Q.org)]" , ExDispatch (ExMeta "e1") (AtLabel "x") , ExDispatch (ExDispatch ExRoot (AtLabel "org")) (AtLabel "x") , substs [[("e1", MvExpression (ExDispatch ExRoot (AtLabel "org")))]] ) , ( "!e.org.!t => $.org.x => [(!e >> $, !t >> x)]" , ExDispatch (ExDispatch (ExMeta "e") (AtLabel "org")) (AtMeta "t") , ExDispatch (ExDispatch ExXi (AtLabel "org")) (AtLabel "x") , substs [[("e", MvExpression ExXi), ("t", MvAttribute (AtLabel "x"))]] ) , ( "[[!t -> !e, !B]].!t => [[x -> Q, y -> $]].x => [(!t >> x, !e >> Q, !B >> [y -> $])]" , ExDispatch (ExFormation [BiTau (AtMeta "t") (ExMeta "e"), BiMeta "B"]) (AtMeta "t") , ExDispatch ( ExFormation [ BiTau (AtLabel "x") ExRoot , BiTau (AtLabel "y") ExXi ] ) (AtLabel "x") , substs [ [ ("t", MvAttribute (AtLabel "x")) , ("e", MvExpression ExRoot) , ("B", MvBindings [BiTau (AtLabel "y") ExXi]) ] ] ) , ( "[[!B1, !t ↦ !e1, !B2]](!t ↦ !e2) => ⟦ t ↦ ξ.k, x ↦ ξ.t, k ↦ ∅ ⟧(x ↦ ξ) => [(!B1 >> [[ t -> $.k ]], !t >> x, !B2 >> [[ k -> ? ]], !e1 >> $.t, !e2 >> $)]" , ExApplication (ExFormation [BiMeta "B1", BiTau (AtMeta "t") (ExMeta "e1"), BiMeta "B2"]) (ArTau (AtMeta "t") (ExMeta "e2")) , ExApplication ( ExFormation [ BiTau (AtLabel "t") (ExDispatch ExXi (AtLabel "k")) , BiTau (AtLabel "x") (ExDispatch ExXi (AtLabel "t")) , BiVoid (AtLabel "k") ] ) (ArTau (AtLabel "x") ExXi) , substs [ [ ("B1", MvBindings [BiTau (AtLabel "t") (ExDispatch ExXi (AtLabel "k"))]) , ("t", MvAttribute (AtLabel "x")) , ("B2", MvBindings [BiVoid (AtLabel "k")]) , ("e1", MvExpression (ExDispatch ExXi (AtLabel "t"))) , ("e2", MvExpression ExXi) ] ] ) ] describe "matches an anonymous meta independently at every occurrence" $ forM_ [ ( "[[ !t -> !e, !t -> !e ]] => [[ a -> Q, b -> $ ]] => both bindings bind their own slots" , ExFormation [ BiTau (AtAny (Slot "t" 1)) (ExAny (Slot "e" 2)) , BiTau (AtAny (Slot "t" 3)) (ExAny (Slot "e" 4)) ] , ExFormation [ BiTau (AtLabel "a") ExRoot , BiTau (AtLabel "b") ExXi ] , [ Subst ( Map.fromList [ (Anon (Slot "t" 1), MvAttribute (AtLabel "a")) , (Anon (Slot "e" 2), MvExpression ExRoot) , (Anon (Slot "t" 3), MvAttribute (AtLabel "b")) , (Anon (Slot "e" 4), MvExpression ExXi) ] ) ] ) , ( "[[ !t -> Q, !t -> Q ]] => [[ a -> Q ]] => no match, since one binding cannot fill two slots" , ExFormation [ BiTau (AtAny (Slot "t" 1)) ExRoot , BiTau (AtAny (Slot "t" 3)) ExRoot ] , ExFormation [BiTau (AtLabel "a") ExRoot] , [] ) ] (\(desc, ptn, tgt, expected) -> it desc (matchExpression ptn tgt `shouldBe` expected)) describe "reachable" $ do it "reaches a redex standing deep inside a formation" $ reachable (ExDispatch (ExFormation [BiMeta "B1"]) (AtMeta "t1")) (ExFormation [BiTau (AtLabel "kw") (ExFormation [BiTau (AtLabel "zu") (ExDispatch (ExFormation []) (AtLabel "qo"))])]) `shouldBe` True it "does not reach a dispatch whose head is no formation" $ reachable (ExDispatch (ExFormation [BiMeta "B1"]) (AtMeta "t1")) (ExFormation [BiTau (AtLabel "kw") (ExDispatch (ExDispatch ExRoot (AtLabel "zu")) (AtLabel "qo"))]) `shouldBe` False it "does not reach a formation lacking the λ the pattern asks for" $ reachable (ExFormation [BiMeta "B1", BiLambda (FnMeta "f"), BiMeta "B2"]) (ExFormation [BiTau (AtLabel "kw") (ExFormation [BiDelta (BtOne "9E")])]) `shouldBe` False it "does not reach an application handing an attribute other than the one the pattern names" $ reachable (ExApplication (ExFormation [BiMeta "B1"]) (ArTau AtRho (ExMeta "e1"))) (ExApplication (ExFormation []) (ArTau (AtLabel "vy") ExRoot)) `shouldBe` False it "reaches a redex in the argument of an application" $ reachable (ExDispatch ExTermination (AtMeta "t")) (ExApplication (ExDispatch ExRoot (AtLabel "hp")) (ArTau (AtLabel "yd") (ExDispatch ExTermination (AtLabel "ob")))) `shouldBe` True it "does not reach into a term the deep matcher never enters" $ reachable (ExDispatch ExTermination (AtMeta "t")) (ExPhiMeet Nothing 3 (ExDispatch ExTermination (AtLabel "ob"))) `shouldBe` False it "reaches any term with a bare meta" $ reachable (ExMeta "e1") ExXi `shouldBe` True describe "fitting" $ do it "fits a dispatch of a formation holding the attribute the pattern names" $ fitting (ExDispatch (ExFormation [BiMeta "B1", BiTau (AtMeta "t1") (ExMeta "e1"), BiMeta "B2"]) (AtMeta "t1")) (ExDispatch (ExFormation [BiVoid (AtLabel "wu"), BiTau (AtLabel "kx") ExRoot]) (AtLabel "kx")) `shouldBe` True it "does not fit a redex standing below the root" $ fitting (ExDispatch (ExFormation [BiMeta "B1"]) (AtMeta "t1")) (ExFormation [BiTau (AtLabel "kw") (ExDispatch (ExFormation []) (AtLabel "qo"))]) `shouldBe` False it "does not fit a dispatch of another attribute" $ fitting (ExDispatch (ExMeta "e1") AtRho) (ExDispatch ExXi (AtLabel "zo")) `shouldBe` False it "does not fit a formation lacking the Δ the pattern asks for" $ fitting (ExFormation [BiDelta (BtMeta "d1"), BiMeta "B1"]) (ExFormation [BiLambda (Function "Lq"), BiVoid AtRho]) `shouldBe` False describe "combine" $ forM_ [ ("combines two empty substitutions", substEmpty, substEmpty, Just substEmpty) , ("combines two empty substs built directly from an empty map", Subst Map.empty, Subst Map.empty, Just substEmpty) , ( "combines an empty subst with a single-entry one" , substEmpty , Subst (Map.singleton (Named "at") (MvAttribute AtPhi)) , Just (Subst (Map.singleton (Named "at") (MvAttribute AtPhi))) ) , ( "combines two substs with disjoint keys" , Subst (Map.singleton (Named "first") (MvAttribute AtPhi)) , Subst (Map.singleton (Named "second") (MvBytes (BtOne "00"))) , Just (Subst (Map.fromList [(Named "first", MvAttribute AtPhi), (Named "second", MvBytes (BtOne "00"))])) ) , ( "keeps a shared key when both substs agree on its value" , Subst (Map.fromList [(Named "first", MvAttribute AtRho), (Named "second", MvAttribute AtPhi)]) , Subst (Map.singleton (Named "first") (MvAttribute AtRho)) , Just (Subst (Map.fromList [(Named "first", MvAttribute AtRho), (Named "second", MvAttribute AtPhi)])) ) , ("returns Nothing when a shared key disagrees", Subst (Map.singleton (Named "x") (MvAttribute AtPhi)), Subst (Map.singleton (Named "x") (MvAttribute AtRho)), Nothing) , ( "returns Nothing for the whole merge when any key conflicts" , Subst (Map.fromList [(Named "x", MvAttribute AtRho), (Named "y", MvBytes (BtOne "1F"))]) , Subst (Map.singleton (Named "x") (MvAttribute AtPhi)) , Nothing ) ] (\(desc, first, second, expected) -> it desc (combine first second `shouldBe` expected)) describe "matchBindings: a meta binding over a crowded formation" $ it "dont cut the target anew at every index a meta binding may end at" $ do matched <- timeout 5000000 (evaluate (length (matchExpression dot (ExDispatch (crowd 3000) (AtLabel "d1"))))) matched `shouldBe` Just 1 describe "matchExpressionDeep'" $ do it "does not look inside an inert term for a redex" $ matchExpressionDeep' True (ExMeta "e1") (ExFormation [BiTau (AtLabel "oq") (ExDispatch ExXi (AtLabel "ze"))]) `shouldBe` [] it "looks inside an inert term for what is no redex" $ length (matchExpressionDeep' False (ExMeta "e1") (ExFormation [BiTau (AtLabel "oq") (ExDispatch ExXi (AtLabel "ze"))])) `shouldBe` 3 it "finds a redex standing beside an inert term" $ matchExpressionDeep' True (ExDispatch ExTermination (AtMeta "t1")) (ExFormation [BiTau (AtLabel "ux") (ExFormation [BiVoid AtRho]), BiTau (AtLabel "xu") (ExDispatch ExTermination (AtLabel "vo"))]) `shouldBe` substs [[("t1", MvAttribute (AtLabel "vo"))]] describe "reachable'" $ it "does not reach into an inert term for a redex" $ reachable' True (ExMeta "e1") (ExDispatch (ExDispatch ExRoot (AtLabel "gh")) (AtLabel "hg")) `shouldBe` False describe "pinned" $ it "writes the attribute in place of its meta wherever the meta stands" $ pinned (AtMeta "t1") (AtLabel "wk") (ExFormation [BiMeta "B1", BiTau (AtMeta "t1") (ExDispatch ExXi (AtMeta "t1")), BiVoid (AtMeta "t2")]) `shouldBe` ExFormation [BiMeta "B1", BiTau (AtLabel "wk") (ExDispatch ExXi (AtLabel "wk")), BiVoid (AtMeta "t2")] describe "matchExpression' of a dispatch naming a meta attribute" $ it "binds the attribute the dispatch names and nothing else" $ matchExpression' dot (ExDispatch (ExFormation [BiTau (AtLabel "pa") ExRoot, BiTau (AtLabel "ap") ExXi, BiVoid AtRho]) (AtLabel "ap")) `shouldBe` substs [ [ ("B1", MvBindings [BiTau (AtLabel "pa") ExRoot]) , ("t1", MvAttribute (AtLabel "ap")) , ("n1", MvExpression ExXi) , ("B2", MvBindings [BiVoid AtRho]) ] ] describe "sites" $ do it "finds the places a rule matches at in the order the deep matcher finds them" $ map fst (sites False (\expr -> [() | ExDispatch _ _ <- [expr]]) (ExDispatch (ExFormation [BiTau (AtLabel "wq") (ExDispatch ExXi (AtLabel "h"))]) (AtLabel "r"))) `shouldBe` [ExDispatch (ExFormation [BiTau (AtLabel "wq") (ExDispatch ExXi (AtLabel "h"))]) (AtLabel "r"), ExDispatch ExXi (AtLabel "h")] it "keeps what the rule makes of a place beside it, once for every way it matches" $ sites False (\expr -> [idx | ExXi <- [expr], idx <- [7, 3 :: Int]]) (ExApplication ExRoot (ArTau (AtLabel "u") ExXi)) `shouldBe` [(ExXi, 7), (ExXi, 3)] it "never looks inside an inert term when the rule is a redex" $ sites True (\expr -> [() | ExXi <- [expr]]) (ExFormation [BiTau (AtLabel "zk") (ExFormation [BiDelta (BtOne "1F")])]) `shouldBe` [] describe "hits" $ do it "finds every rule matching deep inside the term in the order one walk meets them" $ hits [(7, False, \_ expr -> [() | ExTermination <- [expr]]), (3, False, \_ expr -> [() | ExRoot <- [expr]]), (5, False, \_ expr -> [() | ExXi <- [expr]])] Nothing (ExDispatch (ExApplication ExXi (ArAlpha (Alpha 2) ExTermination)) (AtLabel "y")) `shouldBe` [5, 7] it "never asks a rule that is a redex inside an inert term" $ hits [(4, True, \_ expr -> [() | ExXi <- [expr]]), (9, False, \_ expr -> [() | ExXi <- [expr]])] Nothing (ExFormation [BiTau (AtLabel "zk") ExXi]) `shouldBe` [9] it "hands the world to every rule it asks" $ hits [(2, False, \universe _ -> [() | Just ExRoot <- [universe]])] (Just ExRoot) (ExDispatch ExXi (AtLabel "qo")) `shouldBe` [2] it "stops the walk at the first rule it finds" $ take 1 (hits [(6, False, \_ expr -> [() | ExRoot <- [expr]]), (1, False, error "the walk went on past the first rule it found")] Nothing ExRoot) `shouldBe` [6] describe "splits" $ it "cuts the bindings in two, the shortest leading run first" $ splits [BiVoid (AtLabel "a"), BiVoid AtRho] `shouldBe` [([], [BiVoid (AtLabel "a"), BiVoid AtRho]), ([BiVoid (AtLabel "a")], [BiVoid AtRho]), ([BiVoid (AtLabel "a"), BiVoid AtRho], [])] where dot :: Expression dot = ExDispatch (ExFormation [BiMeta "B1", BiTau (AtMeta "t1") (ExMeta "n1"), BiMeta "B2"]) (AtMeta "t1") crowd :: Int -> Expression crowd size = ExFormation [BiTau (AtLabel (T.pack ("d" <> show idx))) (ExFormation [BiDelta (BtOne "00")]) | idx <- [1 .. size]]