packages feed

phino-0.0.80 / test / DataizeSpec.hs

{-# LANGUAGE OverloadedRecordDot #-}{-# LANGUAGE OverloadedStrings #-}-- SPDX-FileCopyrightText: Copyright (c) 2025 Objectionary.com-- SPDX-License-Identifier: MITmodule DataizeSpec (spec) whereimport ASTimport Control.Monadimport Data.List (nub)import Data.List.NonEmpty (NonEmpty (..))import Dataize (DataizeContext (DataizeContext), dataize, dataize', execBuildTerm, morph)import Deps (Term (TeExpression), dontSaveStep)import Functions (buildTerm)import Matcher (substEmpty)import Parser (parseExpressionThrows, parseProgramThrows)import Rewriter (Rewritten)import Test.Hspecimport Yaml qualifieddefaultDataizeContext :: Expression -> Program -> DataizeContextdefaultDataizeContext loc prog = DataizeContext loc prog 25 25 False buildTerm dontSaveSteptest :: (Eq a, Show a) => ((Expression, NonEmpty Rewritten) -> DataizeContext -> IO (Maybe a, [Rewritten])) -> [(String, Expression, Expression, Maybe a)] -> Spectest func useCases =  forM_ useCases $ \(desc, input, expr, output) ->    it desc $ do      let prog = Program expr      (res, _) <- func (input, (prog, Nothing) :| []) (defaultDataizeContext ExGlobal prog)      res `shouldBe` outputtest' :: (Eq a, Show a) => ((Expression, NonEmpty Rewritten) -> DataizeContext -> IO (a, NonEmpty Rewritten)) -> [(String, Expression, Expression, a)] -> Spectest' func useCases =  forM_ useCases $ \(desc, input, expr, output) ->    it desc $ do      let prog = Program expr      (res, _) <- func (input, (prog, Nothing) :| []) (defaultDataizeContext ExGlobal prog)      res `shouldBe` outputtestDataize :: [(String, String, String, Bytes)] -> SpectestDataize useCases =  forM_ useCases $ \(name, loc, prog, res) ->    it name $ do      prog' <- parseProgramThrows prog      loc' <- parseExpressionThrows loc      (value, _) <- dataize (defaultDataizeContext loc' prog')      value `shouldBe` Just resspec :: Specspec = do  describe "morph" $    test'      morph      [ ("[[ D> 00- ]] => [[ D> 00- ]]", ExFormation [BiDelta (BtOne "00")], ExGlobal, ExFormation [BiDelta (BtOne "00")])      , ("T => T", ExTermination, ExGlobal, ExTermination)      , ("$ => X", ExThis, ExGlobal, ExTermination)      , ("Q => X", ExGlobal, ExGlobal, ExTermination)      ,        ( "Q.x (Q -> [[ x -> [[]] ]]) => [[]]"        , ExDispatch ExGlobal (AtLabel "x")        , ExFormation [BiTau (AtLabel "x") (ExFormation [])]        , ExFormation []        )      ]  describe "dataize" $    test      dataize'      [ ("[[ D> 00- ]] => 00-", ExFormation [BiDelta (BtOne "00")], ExGlobal, Just (BtOne "00"))      , ("T => X", ExTermination, ExGlobal, Nothing)      ,        ( "[[ @ -> [[ D> 00-]] ]] => 00-"        , ExFormation [BiTau AtPhi (ExFormation [BiDelta (BtOne "00"), BiVoid AtRho]), BiVoid AtRho]        , ExGlobal        , Just (BtOne "00")        )      ,        ( "[[ x -> [[ D> 01- ]] ]].x => 01-"        , ExDispatch (ExFormation [BiTau (AtLabel "x") (ExFormation [BiDelta (BtOne "01"), BiVoid AtRho]), BiVoid AtRho]) (AtLabel "x")        , ExGlobal        , Just (BtOne "01")        )      ,        ( "[[ @ -> [[ 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                                    ]                                )                                (BiTau (AtLabel "y") (ExFormation []))                            )                        ]                    )                    (AtLabel "x")                )            ]        , ExGlobal        , Just (BtOne "01")        )      ]  describe "execBuildTerm" $    it "resolves global dispatch from the universe Q" $ do      prog <- parseProgramThrows "Q -> [[ x -> [[ D> 42- ]] ]]"      expected <- parseExpressionThrows "[[ D> 42- ]]"      term <- execBuildTerm (defaultDataizeContext ExGlobal prog) "global" [Yaml.ArgAttribute (AtLabel "x")] substEmpty      case term of        TeExpression actual -> actual `shouldBe` expected        _ -> expectationFailure "global() did not return an expression"  describe "labels every step with a defined rule or operation" $ do    let funcs = maybe [] (map Yaml.function)        allowed =          map (.name) Yaml.morphingRules            ++ map (.name) Yaml.dataizationRules            ++ map (.name) Yaml.normalizationRules            ++ concatMap (funcs . (.where_)) Yaml.morphingRules            ++ concatMap (funcs . (.where_)) Yaml.dataizationRules    it "uses no step label without a defining rule or operation" $ do      prog <-        parseProgramThrows          ( unlines              [ "Q -> [["              , "  bytes(data) -> [[ @ -> $.data ]],"              , "  number(as-bytes) -> [[ @ -> $.as-bytes, plus(x) -> [[ L> L_number_plus ]] ]],"              , "  @ -> 5.plus(6)"              , "]]"              ]          )      loc <- parseExpressionThrows "Q"      (_, chain) <- dataize (defaultDataizeContext loc prog)      let orphans = nub [label | (_, Just label) <- chain, label `notElem` allowed]      unless        (null orphans)        (expectationFailure ("Dataization emitted step labels with no defining rule or operation: " ++ show orphans))  describe "preserves the reduction label sequence" $ do    let labelsOf loc src = do          prog <- parseProgramThrows src          loc' <- parseExpressionThrows loc          (_, chain) <- dataize (defaultDataizeContext loc' prog)          pure [label | (_, Just label) <- chain]    it "dataizes 5.plus(6) through the expected rules" $ do      labels <-        labelsOf          "Q"          "Q -> [[ bytes(data) -> [[ @ -> $.data ]], number(as-bytes) -> [[ @ -> $.as-bytes, plus(x) -> [[ L> L_number_plus ]] ]], @ -> 5.plus(6) ]]"      labels        `shouldBe` [ "contextualize"                   , "Mphi"                   , "alpha"                   , "copy"                   , "dot"                   , "copy"                   , "alpha"                   , "copy"                   , "Mlambda"                   , "Mphi"                   , "alpha"                   , "copy"                   , "Mprim"                   , "contextualize"                   , "dot"                   , "Mphi"                   , "alpha"                   , "copy"                   , "copy"                   , "Mprim"                   , "contextualize"                   , "dot"                   , "copy"                   , "Mprim"                   ]    it "dataizes a located reference through the expected rules" $ do      labels <- labelsOf "Q.foo.bar" "Q -> [[ foo -> [[ bar -> [[ @ -> Q.x ]] ]], x -> [[ D> 42- ]] ]]"      labels `shouldBe` ["contextualize", "Mphi", "Mprim"]  testDataize    [      ( "5.plus(6)"      , "Q"      , unlines          [ "Q -> [["          , "  bytes(data) -> [["          , "    @ -> $.data"          , "  ]],"          , "  number(as-bytes) -> [["          , "    @ -> $.as-bytes,"          , "    plus(x) -> [[ L> L_number_plus ]]"          , "  ]],"          , "  @ -> 5.plus(6)"          , "]]"          ]      , BtMany ["40", "26", "00", "00", "00", "00", "00", "00"]      )    ,      ( "Fahrenheit"      , "Q"      , unlines          [ "Q -> [["          , "  bytes -> [["          , "    data -> ?,"          , "    @ -> $.data"          , "  ]],"          , "  number -> [["          , "    as-bytes -> ?,"          , "    @ -> $.as-bytes,"          , "    plus -> [[ x -> ?, L> L_number_plus ]],"          , "    times -> [[ x -> ?, L> L_number_times ]]"          , "  ]],"          , "  @ -> $.c.times(1.8).plus(32),"          , "  c -> 25"          , "]]"          ]      , BtMany ["40", "53", "40", "00", "00", "00", "00", "00"]      )    ,      ( "Factorial"      , "Q"      , unlines          [ "Q -> [["          , "  bytes -> [["          , "    data -> ?,"          , "    @ -> $.data"          , "  ]],"          , "  number -> [["          , "    as-bytes -> ?,"          , "    @ -> $.as-bytes,"          , "    times -> [[ x -> ?, L> L_number_times ]],"          , "    plus -> [[ x -> ?, L> L_number_plus ]],"          , "    eq -> [[ x -> ?, y -> ?, L> L_number_eq ]]"          , "  ]],"          , "  fac -> [["          , "    x -> ?,"          , "    @ -> $.x.eq("          , "      1,"          , "      $.x.times($.^.fac($.x.plus(-1)))"          , "    )"          , "  ]],"          , "  @ -> $.fac(3)"          , "]]"          ]      , BtMany ["40", "18", "00", "00", "00", "00", "00", "00"]      )    ,      ( "Located"      , "Q.foo.bar"      , unlines          [ "Q -> [["          , "  foo -> [["          , "    bar -> [["          , "      @ -> Q.x"          , "    ]]"          , "  ]],"          , "  x -> [[ D> 42- ]]"          , "]]"          ]      , BtOne "42"      )    ,      ( "Five"      , "Q.x"      , unlines          [ "Q -> [["          , "  number(as-bytes) -> [[ @ -> as-bytes ]],"          , "  bytes(data) -> [[ @ -> data ]],"          , "  x -> 5"          , "]]"          ]      , BtMany ["40", "14", "00", "00", "00", "00", "00", "00"]      )    ]