packages feed

hsmt-0.1.0.0: test-common/HSMT/Gen/Expr.hs

-- | Sort-directed QuickCheck generators of well-sorted core expressions.
--
-- Shared by the unit test suite (printer/parser round trips) and by the
-- differential suite against Z3. The configuration selects the sorts in play
-- and the theories whose interpreted operators may appear; literals of every
-- sort in play are always allowed.
module HSMT.Gen.Expr
  ( Theory (..)
  , GenConfig (..)
  , eufConfig
  , liaConfig
  , ufliaConfig
  , bvConfig
  , ufbvConfig
  , axConfig
  , aliaConfig
  , abvConfig
  , recordConfig
  , recordMixConfig
  , stringConfig
  , sliaConfig
  , regexConfig
  , sliaRegexConfig
  , genRegex
  , fpConfig
  , fpMixConfig
  , allTheoriesConfig
  , Signature (..)
  , genSignature
  , genTerm
  , genProblem
  , shrinkExpr
  , shrinkProblem
  , sortOfExpr
  , fpLiteral
  ) where

import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import Data.Set (Set)
import qualified Data.Set as Set
import qualified Data.Text as T
import Test.QuickCheck

import HSMT.Core.Expr
import qualified HSMT.Core.Float as F
import HSMT.Core.Op
import HSMT.Core.Sort
import HSMT.Core.Typing

data Theory = TArith | TBitVec | TArray | TString | TRecord | TFloat | TRegex
  deriving (Eq, Ord, Show, Enum, Bounded)

data GenConfig = GenConfig
  { gcSorts :: [Sort]
    -- ^ Sorts (besides Bool) of symbols and terms.
  , gcTheories :: Set Theory
    -- ^ Theories whose operators may be used.
  , gcDepth :: Int
    -- ^ Maximum term depth.
  , gcAssertions :: (Int, Int)
    -- ^ Range of the number of assertions of a problem.
  , gcFunctions :: Bool
    -- ^ Whether to declare uninterpreted functions.
  , gcDivisors :: [Integer]
    -- ^ Literal divisors of integer division and remainder.
  }
  deriving (Show)

-- | Uninterpreted functions, equalities and literals (QF_UF with literal
-- values of other sorts).
eufConfig :: GenConfig
eufConfig =
  GenConfig
    { gcSorts = [UninterpretedSort "U", UninterpretedSort "V", IntSort, BVSort 2]
    , gcTheories = Set.empty
    , gcDepth = 3
    , gcAssertions = (1, 7)
    , gcFunctions = True
    , gcDivisors = [-2, -1, 1, 2, 3]
    }

-- | Linear integer arithmetic (QF_LIA), including division by literals and
-- by zero.
liaConfig :: GenConfig
liaConfig =
  GenConfig
    { gcSorts = [IntSort]
    , gcTheories = Set.singleton TArith
    , gcDepth = 3
    , gcAssertions = (1, 6)
    , gcFunctions = False
    , gcDivisors = [-3, -2, 0, 2, 3, 5]
    }

-- | Uninterpreted functions over integers (QF_UFLIA).
ufliaConfig :: GenConfig
ufliaConfig =
  liaConfig
    { gcSorts = [IntSort, UninterpretedSort "U"]
    , gcFunctions = True
    }

-- | Fixed-width bit-vectors (QF_BV), with extraction, concatenation and
-- extension between the widths in play.
bvConfig :: GenConfig
bvConfig =
  GenConfig
    { gcSorts = [BVSort 1, BVSort 2, BVSort 3, BVSort 4, BVSort 8]
    , gcTheories = Set.singleton TBitVec
    , gcDepth = 3
    , gcAssertions = (1, 6)
    , gcFunctions = False
    , gcDivisors = []
    }

-- | Uninterpreted functions over bit-vectors (QF_UFBV).
ufbvConfig :: GenConfig
ufbvConfig =
  bvConfig
    { gcSorts = [BVSort 2, BVSort 4, BVSort 8, UninterpretedSort "U"]
    , gcFunctions = True
    }

-- | Arrays over uninterpreted sorts with uninterpreted functions (QF_AUF),
-- including arrays of Booleans and nested arrays.
axConfig :: GenConfig
axConfig =
  GenConfig
    { gcSorts =
        [ UninterpretedSort "U"
        , ArraySort (UninterpretedSort "U") (UninterpretedSort "U")
        , ArraySort (UninterpretedSort "U") BoolSort
        , ArraySort (UninterpretedSort "U") (ArraySort (UninterpretedSort "U") (UninterpretedSort "U"))
        ]
    , gcTheories = Set.singleton TArray
    , gcDepth = 3
    , gcAssertions = (1, 6)
    , gcFunctions = True
    , gcDivisors = []
    }

-- | Arrays with linear integer arithmetic (QF_ALIA).
aliaConfig :: GenConfig
aliaConfig =
  liaConfig
    { gcSorts = [IntSort, ArraySort IntSort IntSort, ArraySort IntSort BoolSort, ArraySort BoolSort IntSort]
    , gcTheories = Set.fromList [TArith, TArray]
    , gcDivisors = [-2, 2, 3]
    }

-- | Arrays with bit-vectors (QF_ABV), over index sorts that are expanded
-- (widths 2 and 4) and that are not (width 8).
abvConfig :: GenConfig
abvConfig =
  bvConfig
    { gcSorts =
        [ BVSort 2
        , BVSort 4
        , BVSort 8
        , ArraySort (BVSort 2) (BVSort 4)
        , ArraySort (BVSort 4) BoolSort
        , ArraySort (BVSort 8) (BVSort 2)
        ]
    , gcTheories = Set.fromList [TBitVec, TArray]
    }

-- | Records over uninterpreted sorts and Booleans, with uninterpreted
-- functions: nested records, the empty record and a record with only four
-- values.
recordConfig :: GenConfig
recordConfig =
  GenConfig
    { gcSorts = [u, r1, RecordSort [("in", r1), ("v", u)], RecordSort [], RecordSort [("c", BoolSort), ("d", BoolSort)]]
    , gcTheories = Set.singleton TRecord
    , gcDepth = 3
    , gcAssertions = (1, 6)
    , gcFunctions = True
    , gcDivisors = []
    }
  where
    u = UninterpretedSort "U"
    r1 = RecordSort [("u", u), ("p", BoolSort)]

-- | Records with integer and bit-vector fields, arrays of records and arrays
-- indexed by records (an infinite record sort and an expanded finite one).
recordMixConfig :: GenConfig
recordMixConfig =
  GenConfig
    { gcSorts =
        [ IntSort
        , BVSort 2
        , ri
        , rf
        , ArraySort IntSort ri
        , ArraySort rf IntSort
        , ArraySort ri (BVSort 2)
        ]
    , gcTheories = Set.fromList [TArith, TBitVec, TArray, TRecord]
    , gcDepth = 3
    , gcAssertions = (1, 6)
    , gcFunctions = True
    , gcDivisors = [-2, 2, 3]
    }
  where
    ri = RecordSort [("a", IntSort), ("b", BVSort 2)]
    rf = RecordSort [("c", BoolSort), ("d", BVSort 1)]

-- | Strings (QF_S); integer arguments are literals.
stringConfig :: GenConfig
stringConfig =
  GenConfig
    { gcSorts = [StringSort]
    , gcTheories = Set.singleton TString
    , gcDepth = 3
    , gcAssertions = (1, 5)
    , gcFunctions = False
    , gcDivisors = []
    }

-- | Strings with linear integer arithmetic and uninterpreted functions
-- (QF_SLIA with functions).
sliaConfig :: GenConfig
sliaConfig =
  stringConfig
    { gcSorts = [StringSort, IntSort]
    , gcTheories = Set.fromList [TString, TArith]
    , gcFunctions = True
    , gcDivisors = [-2, 2, 3]
    }

-- | Strings with membership in ground regular expressions.
regexConfig :: GenConfig
regexConfig = stringConfig {gcTheories = Set.fromList [TString, TRegex]}

-- | 'sliaConfig' with membership in ground regular expressions.
sliaRegexConfig :: GenConfig
sliaRegexConfig = sliaConfig {gcTheories = Set.fromList [TString, TArith, TRegex]}

-- | A ground regular expression of at most the given depth, over the
-- characters of the generated string literals.
genRegex :: Int -> Gen Expr
genRegex d
  | d <= 0 = leaf
  | otherwise =
      frequency
        [ (3, leaf)
        , (3, App ReConcat <$> (chooseInt (2, 3) >>= \k -> vectorOf k sub))
        , (3, App ReUnion <$> vectorOf 2 sub)
        , (1, App ReInter <$> vectorOf 2 sub)
        , (2, App ReStar . pure <$> sub)
        , (1, App RePlus . pure <$> sub)
        , (1, App ReOpt . pure <$> sub)
        , (1, App ReComp . pure <$> sub)
        , (1, App ReDiff <$> vectorOf 2 sub)
        , (1, do
            i <- chooseInt (0, 2)
            j <- chooseInt (0, 3)
            App (ReLoop i j) . pure <$> sub)
        , (1, chooseInt (0, 3) >>= \n -> App (RePower n) . pure <$> sub)
        ]
  where
    sub = genRegex (d - 1)
    str w = constant (StrConst w)
    leaf =
      frequency
        [ (5, App StrToRe . pure . str <$> elements ["", "a", "b", "ab", "ba", "aab", "\"", "é"])
        , (2, (\(a, b) -> App ReRange [str a, str b]) <$> elements [("a", "b"), ("a", "c"), ("b", "b"), ("b", "a"), ("ab", "c")])
        , (1, pure (constant ReAllChar))
        , (1, pure (constant ReAll))
        , (1, pure (constant ReNone))
        ]

-- | Floating point (QF_FP) over two small formats, with rounding-mode
-- constants and conversions between the formats. Z3's bit-blaster rejects
-- or answers unknown on formats whose significand is not wider than the
-- exponent, so the formats have @sb >= eb + 2@.
fpConfig :: GenConfig
fpConfig =
  GenConfig
    { gcSorts = [FPSort 3 5, FPSort 4 6, RoundingModeSort]
    , gcTheories = Set.singleton TFloat
    , gcDepth = 3
    , gcAssertions = (1, 5)
    , gcFunctions = False
    , gcDivisors = []
    }

-- | Floating point with bit-vectors and uninterpreted functions (QF_UFBVFP):
-- conversions to and from bit-vectors, and Float16.
fpMixConfig :: GenConfig
fpMixConfig =
  fpConfig
    { gcSorts = [FPSort 3 5, FPSort 5 11, RoundingModeSort, BVSort 3, BVSort 8]
    , gcTheories = Set.fromList [TFloat, TBitVec]
    , gcFunctions = True
    , gcDepth = 2
    }

-- | Every theory and a variety of sorts.
allTheoriesConfig :: GenConfig
allTheoriesConfig =
  GenConfig
    { gcSorts =
        [ IntSort
        , BVSort 1
        , BVSort 2
        , BVSort 3
        , StringSort
        , UninterpretedSort "U"
        , ArraySort IntSort IntSort
        , ArraySort (BVSort 2) BoolSort
        , rec1
        , RecordSort [("in", rec1), ("flag", BoolSort)]
        , FPSort 3 5
        , FPSort 4 6
        , RoundingModeSort
        ]
    , gcTheories = Set.fromList [minBound .. maxBound]
    , gcDepth = 3
    , gcAssertions = (1, 4)
    , gcFunctions = True
    , gcDivisors = [-2, -1, 1, 2, 3]
    }
  where
    rec1 = RecordSort [("a", IntSort), ("b", BVSort 2)]

-- | Declared constants (by sort) and functions.
data Signature = Signature
  { sigConsts :: Map Sort [Symbol]
  , sigFuns :: [Symbol]
  }
  deriving (Show)

allSorts :: GenConfig -> [Sort]
allSorts cfg = BoolSort : gcSorts cfg

genSignature :: GenConfig -> Gen Signature
genSignature cfg = do
  consts <- sequence [constsOf i s | (i, s) <- zip [0 :: Int ..] (allSorts cfg)]
  nFuns <- if gcFunctions cfg then chooseInt (0, 3) else pure 0
  funs <- sequence [mkFunction k | k <- [0 .. nFuns - 1]]
  pure (Signature (Map.fromList consts) funs)
  where
    constsOf i s = do
      n <- if s == BoolSort then chooseInt (0, 2) else chooseInt (1, 3)
      -- "f" and "p" are reserved for functions and Boolean constants.
      let prefix = if s == BoolSort then "p" else T.singleton (cycle "abcdeghijkmnqrstuvwyz" !! (i - 1))
      pure (s, [Symbol (prefix <> T.pack (show j)) [] s | j <- [0 .. n - 1]])
    mkFunction k = do
      arity <- chooseInt (1, 2)
      args <- vectorOf arity (elements (allSorts cfg))
      result <- frequency [(1, pure BoolSort), (3, elements (gcSorts cfg))]
      pure (Symbol ("f" <> T.pack (show k)) args result)

has :: GenConfig -> Theory -> Bool
has cfg t = Set.member t (gcTheories cfg)

-- | A term of the given sort with at most the given depth.
genTerm :: GenConfig -> Signature -> Int -> Sort -> Gen Expr
genTerm cfg sig = go
  where
    sorts = allSorts cfg
    inPlay s = s `elem` sorts

    go d s
      | d <= 0 = leaf s
      | otherwise = frequency ((3, leaf s) : compound (d - 1) s)

    leaf s = oneof (map (pure . constant . Uninterpreted) consts <> literal s)
      where
        consts = Map.findWithDefault [] s (sigConsts sig)

    literal = \case
      BoolSort -> [constant . BoolConst <$> arbitrary]
      IntSort -> [constant . IntConst <$> chooseInteger (-3, 3)]
      BVSort w -> [constant . BVConst w <$> chooseInteger (0, 2 ^ w - 1)]
      StringSort -> [constant . StrConst <$> elements ["", "a", "b", "ab", "ba", "aab", "\"", "\\u", "é"]]
      ArraySort i e | has cfg TArray -> [(\x -> App (ConstArray i) [x]) <$> leaf e]
      RecordSort fs -> [App (RecordMk (map fst fs)) <$> traverse (leaf . snd) fs]
      FPSort eb sb -> [constant . FPConst eb sb <$> fpLiteral eb sb]
      RoundingModeSort -> [constant . RMConst <$> elements F.allRoundingModes]
      _ -> []

    build op argSorts d = App op <$> traverse (go d) argSorts

    compound d s =
      [ (2, build (Uninterpreted f) (symArgs f) d)
      | f <- sigFuns sig
      , symResult f == s
      ]
        <> [(1, build Ite [BoolSort, s, s] d)]
        <> [(1, build Select [ArraySort i s, i] d) | has cfg TArray, ArraySort i e <- sorts, e == s]
        <> [ (1, build (RecordGet f) [r] d)
           | has cfg TRecord
           , r@(RecordSort fs) <- sorts
           , (f, fs') <- fs
           , fs' == s
           ]
        <> specific d s
        <> floating d s

    -- Floating-point operators, by result sort.
    floating d = \case
      _ | not (has cfg TFloat) -> []
      BoolSort ->
        [ (2, elements fpSorts >>= \t -> elements [FPLeq, FPLt, FPGeq, FPGt, FPEq] >>= \op -> build op [t, t] d)
        , ( 2
          , elements fpSorts >>= \t ->
              elements [FPIsNormal, FPIsSubnormal, FPIsZero, FPIsInfinite, FPIsNaN, FPIsNegative, FPIsPositive]
                >>= \op -> build op [t] d
          )
        ]
      t@(FPSort eb sb) ->
        [ (1, elements [FPAbs, FPNeg] >>= \op -> build op [t] d)
        , (4, elements [FPAdd, FPSub, FPMul, FPDiv] >>= \op -> build op [RoundingModeSort, t, t] d)
        , (1, build FPFma [RoundingModeSort, t, t, t] d)
        , (1, elements [FPSqrt, FPRoundToIntegral] >>= \op -> build op [RoundingModeSort, t] d)
        , (1, elements [FPRem, FPMin, FPMax] >>= \op -> build op [t, t] d)
        , (1, build FPFromFields [BVSort 1, BVSort eb, BVSort (sb - 1)] d)
        ]
          <> [(1, build (ToFP eb sb) [RoundingModeSort, u] d) | u <- fpSorts, u /= t]
          <> [ (1, build (ToFP eb sb) [BVSort w] d)
             | has cfg TBitVec
             , BVSort w <- sorts
             , w == eb + sb
             ]
          <> [ (1, elements [ToFP eb sb, ToFPUnsigned eb sb] >>= \op -> build op [RoundingModeSort, BVSort w] d)
             | has cfg TBitVec
             , BVSort w <- sorts
             ]
      BVSort w
        | has cfg TBitVec ->
            [ (1, elements [FPToUBV w, FPToSBV w] >>= \op -> build op [RoundingModeSort, t] d)
            | t <- fpSorts
            ]
      _ -> []

    fpSorts = [t | t@(FPSort _ _) <- sorts]

    specific d = \case
      BoolSort ->
        [ (1, build Not [BoolSort] d)
        , (1, nary And BoolSort d)
        , (1, nary Or BoolSort d)
        , (1, build Xor [BoolSort, BoolSort] d)
        , (1, build Implies [BoolSort, BoolSort] d)
        , (4, elements sorts >>= \t -> build Eq [t, t] d)
        , (2, elements sorts >>= \t -> nary Distinct t d)
        ]
          <> [ (1, elements [Le, Lt, Ge, Gt] >>= \op -> build op [IntSort, IntSort] d)
             | has cfg TArith && inPlay IntSort
             ]
          <> [ (1, elements bvPreds >>= \op -> build op [BVSort w, BVSort w] d)
             | has cfg TBitVec
             , BVSort w <- sorts
             ]
          <> [ (1, elements [StrContains, StrPrefixOf, StrSuffixOf, StrLt, StrLe] >>= \op -> build op [StringSort, StringSort] d)
             | has cfg TString && inPlay StringSort
             ]
          <> [ (3, App StrInRe <$> sequence [go d StringSort, genRegex 3])
             | has cfg TRegex && inPlay StringSort
             ]
      IntSort ->
        [ (w, g)
        | has cfg TArith
        , (w, g) <-
            [ (2, nary Add IntSort d)
            , (1, build Sub [IntSort, IntSort] d)
            , (1, build Neg [IntSort] d)
            , (1, App Mul <$> sequence [intLit, go d IntSort])
            , (1, App IntDiv <$> sequence [go d IntSort, divisor])
            , (1, App IntMod <$> sequence [go d IntSort, divisor])
            , (1, build Abs [IntSort] d)
            ]
        ]
          <> [ (w, g)
             | has cfg TString && inPlay StringSort
             , (w, g) <-
                [ (1, build StrLen [StringSort] d)
                , (1, build StrIndexOf [StringSort, StringSort, IntSort] d)
                , (1, build StrToInt [StringSort] d)
                ]
             ]
      BVSort w
        | has cfg TBitVec ->
            [ (1, elements [BVNot, BVNeg] >>= \op -> build op [BVSort w] d)
            , (3, elements bvBinary >>= \op -> build op [BVSort w, BVSort w] d)
            ]
              <> [ (1, do
                      lo <- chooseInt (0, w' - w)
                      build (BVExtract (lo + w - 1) lo) [BVSort w'] d)
                 | BVSort w' <- sorts
                 , w' > w
                 ]
              <> [ (1, build BVConcat [BVSort a, BVSort (w - a)] d)
                 | BVSort a <- sorts
                 , a < w
                 , inPlay (BVSort (w - a))
                 ]
              <> [ (1, elements [BVZeroExtend (w - w'), BVSignExtend (w - w')] >>= \op -> build op [BVSort w'] d)
                 | BVSort w' <- sorts
                 , w' < w
                 ]
      StringSort
        | has cfg TString ->
            [ (2, build StrConcat [StringSort, StringSort] d)
            , (1, build StrAt [StringSort, IntSort] d)
            , (1, build StrSubstr [StringSort, IntSort, IntSort] d)
            , (1, build StrReplace [StringSort, StringSort, StringSort] d)
            , (1, build StrFromInt [IntSort] d)
            ]
      ArraySort i e
        | has cfg TArray -> [(2, build Store [ArraySort i e, i, e] d)]
      RecordSort fs
        | has cfg TRecord && not (null fs) ->
            [(2, elements fs >>= \(f, s) -> build (RecordSet f) [RecordSort fs, s] d)]
      _ -> []

    nary op s d = chooseInt (2, 3) >>= \k -> build op (replicate k s) d

    intLit = constant . IntConst <$> elements [-2, -1, 1, 2, 3]
    divisor = constant . IntConst <$> elements (gcDivisors cfg)

    bvPreds = [BVULt, BVULe, BVUGt, BVUGe, BVSLt, BVSLe, BVSGt, BVSGe]
    bvBinary =
      [ BVAnd, BVOr, BVXor, BVAdd, BVSub, BVMul, BVUDiv, BVURem, BVSDiv, BVSRem
      , BVSMod, BVShl, BVLShr, BVAShr
      ]

-- | A floating-point literal: special values, the extremes of each kind,
-- and random patterns.
fpLiteral :: Int -> Int -> Gen Integer
fpLiteral eb sb =
  F.canonicalize eb sb
    <$> frequency
      [ (3, elements specials)
      , (4, chooseInteger (0, 2 ^ (eb + sb) - 1))
      ]
  where
    enc = F.encode eb sb
    sig = 2 ^ (sb - 1)
    expField e = e * sig
    neg = (2 ^ (eb + sb - 1) +)
    specials =
      [ enc F.NaN
      , enc (F.Inf False)
      , enc (F.Inf True)
      , enc (F.Zero False)
      , enc (F.Zero True)
      , 1 -- smallest subnormal
      , neg 1
      , sig - 1 -- largest subnormal
      , expField 1 -- smallest normal
      , expField (2 ^ eb - 2) + sig - 1 -- largest finite
      , neg (expField (2 ^ eb - 2) + sig - 1)
      , expField (2 ^ (eb - 1) - 1) -- one
      , neg (expField (2 ^ (eb - 1) - 1))
      , expField (2 ^ (eb - 1)) -- two
      , expField (2 ^ (eb - 1) - 1) + 1 -- just above one
      ]

-- | A list of assertions. Most assertions are small clauses over atoms,
-- which gives a reasonable mix of satisfiable and unsatisfiable problems;
-- the rest are arbitrary Boolean terms.
genProblem :: GenConfig -> Gen [Expr]
genProblem cfg = do
  sig <- genSignature cfg
  n <- chooseInt (gcAssertions cfg)
  vectorOf n (assertion sig)
  where
    assertion sig =
      frequency
        [ (3, clause sig)
        , (1, chooseInt (1, gcDepth cfg) >>= \d -> genTerm cfg sig d BoolSort)
        ]
    clause sig = do
      k <- frequency [(3, pure 1), (2, pure 2), (1, pure 3)]
      lits <- vectorOf k (literal sig)
      pure $ case lits of
        [l] -> l
        _ -> App Or lits
    literal sig = do
      a <- atom sig
      positive <- frequency [(2, pure True), (1, pure False)]
      pure (if positive then a else App Not [a])
    atom sig = do
      s <- elements (allSorts cfg)
      d <- chooseInt (0, gcDepth cfg - 1)
      frequency $
        [(2, App Eq <$> vectorOf 2 (genTerm cfg sig d s))]
          <> [ (3, elements [Le, Lt, Ge, Gt] >>= \op -> App op <$> vectorOf 2 (genTerm cfg sig d IntSort))
             | Set.member TArith (gcTheories cfg) && IntSort `elem` gcSorts cfg
             ]
          <> [ (3, elements bvPredicates >>= \op -> App op <$> vectorOf 2 (genTerm cfg sig d w))
             | Set.member TBitVec (gcTheories cfg)
             , w@(BVSort _) <- [s]
             ]
          <> [ (3, App StrInRe <$> sequence [genTerm cfg sig d StringSort, genRegex 3])
             | Set.member TRegex (gcTheories cfg) && StringSort `elem` gcSorts cfg
             ]
    bvPredicates = [BVULt, BVULe, BVUGt, BVUGe, BVSLt, BVSLe, BVSGt, BVSGe]

-- | Sort of a generated (hence well-sorted) expression.
sortOfExpr :: Expr -> Sort
sortOfExpr e = either (error . show) id (sortOf e)

-- | Replace the expression by a proper subterm of the same sort, or shrink
-- one argument. Both keep the expression well sorted.
shrinkExpr :: Expr -> [Expr]
shrinkExpr e@(App op args) =
  sameSortSubterms <> [App op args' | args' <- shrinkOne args]
  where
    s = sortOfExpr e
    sameSortSubterms =
      Set.toList . Set.fromList $
        [t | a <- args, t <- subtermsOf a, sortOfExpr t == s]
    shrinkOne [] = []
    shrinkOne (x : xs) = [x' : xs | x' <- shrinkExpr x] <> [x : xs' | xs' <- shrinkOne xs]

subtermsOf :: Expr -> [Expr]
subtermsOf e@(App _ args) = e : concatMap subtermsOf args

shrinkProblem :: [Expr] -> [[Expr]]
shrinkProblem = filter (not . null) . shrinkList shrinkExpr