{-# LANGUAGE LambdaCase #-}{-# LANGUAGE OverloadedStrings #-}{-# LANGUAGE ScopedTypeVariables #-}{-# LANGUAGE TupleSections #-}-- SPDX-FileCopyrightText: Copyright (c) 2025 Objectionary.com-- SPDX-License-Identifier: MITmodule Grown (Grown (..), Shuffled (..), reversed) whereimport ASTimport Bytes (numToBts, strToBts)import Data.Functor.Identity (Identity (..))import Data.List (nub)import Data.Text qualified as Timport Test.QuickCheck (Arbitrary (..), Gen, chooseInt, elements, frequency, shrinkList, shuffle, sized, suchThat, vectorOf)newtype Grown = Grown Expression deriving (Show)newtype Shuffled = Shuffled (Expression, Expression) deriving (Show)instance Arbitrary Grown where arbitrary = Grown <$> sized (expression . (`div` 20)) where expression :: Int -> Gen Expression expression depth = frequency ( [ (2, pure ExXi) , (2, pure ExRoot) , (1, pure ExTermination) , (1, ExMeta <$> meta "enk") , (1, DataNumber . numToBts <$> arbitrary) , (1, DataString . strToBts <$> arbitrary) , (2, ExFormation <$> bindings 0) ] ++ [ (weight, grown) | depth > 0 , (weight, grown) <- [ (4, ExFormation <$> bindings depth) , (3, ExDispatch <$> expression (depth - 1) <*> attribute) , (3, (ExApplication <$> expression (depth - 1) <*> argument depth) `suchThat` spelled) ] ] ) spelled :: Expression -> Bool spelled (ExApplication (DataNumber _) (ArTau AtRho _)) = False spelled (ExApplication (DataString _) (ArTau AtRho _)) = False spelled _ = True argument :: Int -> Gen Argument argument depth = ArTau <$> attribute <*> expression (depth - 1) bindings :: Int -> Gen [Binding] bindings depth = do attrs <- nub <$> (chooseInt (0, 4) >>= (`vectorOf` attribute)) named <- mapM (\attr -> frequency [(1, pure (BiVoid attr)), (3, BiTau attr <$> expression (depth - 1))]) attrs wildcard <- frequency [(3, pure []), (1, pure . BiMeta <$> meta "B")] delta <- frequency [(2, pure []), (1, pure . BiDelta <$> bytes)] lambda <- frequency [(2, pure []), (1, pure . BiLambda <$> function)] (++ delta ++ lambda) <$> shuffle (named ++ wildcard) attribute :: Gen Attribute attribute = frequency [(6, AtLabel <$> label), (1, pure AtPhi), (1, pure AtRho), (1, AtMeta <$> meta "t")] label :: Gen T.Text label = T.pack <$> spelt ['a' .. 'z'] (['a' .. 'z'] ++ ['0' .. '9'] ++ "-_") function :: Gen Function function = frequency [(4, Function . T.pack <$> spelt ['A' .. 'Z'] (['a' .. 'z'] ++ ['0' .. '9'] ++ "_")), (1, FnMeta <$> meta "F"), (2, FnSymbol <$> chooseInt (1, 99))] bytes :: Gen Bytes bytes = frequency [(1, pure BtEmpty), (2, BtOne <$> byte), (3, BtMany <$> (chooseInt (2, 9) >>= (`vectorOf` byte))), (1, BtMeta <$> meta "d")] byte :: Gen String byte = vectorOf 2 (elements (['0' .. '9'] ++ ['A' .. 'F'])) meta :: String -> Gen T.Text meta heads = T.pack <$> ((:) <$> elements heads <*> (chooseInt (1, 3) >>= (`vectorOf` elements (['1' .. '9'] ++ ['a' .. 'z'])))) spelt :: String -> String -> Gen String spelt heads rest = (:) <$> elements heads <*> (chooseInt (0, 5) >>= (`vectorOf` elements rest)) shrink (Grown expr) = Grown <$> smaller expr where smaller :: Expression -> [Expression] smaller = \case ExFormation bds -> [inner | BiTau _ inner <- bds] ++ (ExFormation <$> shrinkList binding bds) ExDispatch inner attr -> inner : (ExDispatch <$> smaller inner <*> pure attr) ExApplication inner arg -> inner : held arg : (ExApplication <$> smaller inner <*> pure arg) ++ (ExApplication inner <$> argued arg) _ -> [] binding :: Binding -> [Binding] binding (BiTau attr inner) = BiTau attr <$> smaller inner binding _ = [] held :: Argument -> Expression held (ArTau _ inner) = inner argued :: Argument -> [Argument] argued (ArTau attr inner) = ArTau attr <$> smaller innerinstance Arbitrary Shuffled where arbitrary = do Grown term <- arbitrary Shuffled . (term,) <$> stirred shuffle term shrink (Shuffled (term, _)) = [Shuffled (smaller, reversed smaller) | Grown smaller <- shrink (Grown term)]reversed :: Expression -> Expressionreversed = runIdentity . stirred (Identity . reverse)stirred :: forall stir. (Monad stir) => ([Binding] -> stir [Binding]) -> Expression -> stir Expressionstirred turn = goExpr where goExpr :: Expression -> stir Expression goExpr = \case ExFormation bds -> ExFormation <$> (mapM goBinding bds >>= turn) ExDispatch inner attr -> (`ExDispatch` attr) <$> goExpr inner ExApplication inner (ArTau attr arg) -> ExApplication <$> goExpr inner <*> (ArTau attr <$> goExpr arg) term -> pure term goBinding :: Binding -> stir Binding goBinding (BiTau attr inner) = BiTau attr <$> goExpr inner goBinding bd = pure bd