{- |
Description: Truth tables for \(n\)-ary boolean functions
Copyright: Copyright (C) 2021 Yoo Chung
License: GPL-3.0-or-later
Maintainer: dev@chungyc.org

Some solutions to "Problems.P48" of Ninety-Nine Haskell "Problems".
-}
module Solutions.P48 (tablen) where

-- | Generalizes 'Problems.P46.table' in a way that the logical expression may contain any number of logical variables.
tablen :: Int -> ([Bool] -> Bool) -> [[Bool]]
tablen :: Int -> ([Bool] -> Bool) -> [[Bool]]
tablen Int
n [Bool] -> Bool
f = ([Bool] -> [Bool]) -> [[Bool]] -> [[Bool]]
forall a b. (a -> b) -> [a] -> [b]
map (\[Bool]
xs -> [Bool]
xs [Bool] -> [Bool] -> [Bool]
forall a. [a] -> [a] -> [a]
++ [[Bool] -> Bool
f [Bool]
xs]) ([[Bool]] -> [[Bool]]) -> [[Bool]] -> [[Bool]]
forall a b. (a -> b) -> a -> b
$ Int -> [[Bool]]
enumerate Int
n

-- | Enumerate all combinations of n boolean values.
enumerate :: Int -> [[Bool]]
enumerate :: Int -> [[Bool]]
enumerate Int
0 = [[]]
enumerate Int
n = [Bool
x Bool -> [Bool] -> [Bool]
forall a. a -> [a] -> [a]
: [Bool]
xs | Bool
x <- [Bool
False, Bool
True], [Bool]
xs <- Int -> [[Bool]]
enumerate (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)]