El tipo de las fórmulas proposicionales: Interpretaciones de una fórmula
Usando el tipo de las fórmulas proposicionales definido en el ejercicio anterior, definir la función
1 |
interpretaciones :: FProp -> [Interpretacion] |
tal que interpretaciones p
es la lista de las interpretaciones de la fórmula p
. Por ejemplo,
1 2 3 4 5 |
λ> interpretaciones (Impl (Var 'A') (Conj (Var 'A') (Var 'B'))) [[('A',False),('B',False)], [('A',False),('B',True)], [('A',True),('B',False)], [('A',True),('B',True)]] |
Soluciones
A continuación se muestran las soluciones en Haskell y las soluciones en Python.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import Tipo_de_formulas (FProp(..)) import Variables_de_una_formula (variables) import Valor_de_una_formula (Interpretacion) import Data.List (nub) interpretaciones :: FProp -> [Interpretacion] interpretaciones p = [zip vs i | i <- interpretacionesVar (length vs)] where vs = nub (variables p) -- (interpretacionesVar n) es la lista de las interpretaciones de n -- variables. Por ejemplo, -- λ> interpretacionesVar 2 -- [[False,False], -- [False,True], -- [True,False], -- [True,True]] interpretacionesVar :: Int -> [[Bool]] interpretacionesVar 0 = [[]] interpretacionesVar n = map (False:) bss ++ map (True:) bss where bss = interpretacionesVar (n-1) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
from src.tipo_de_formulas import Conj, Const, FProp, Impl, Neg, Var from src.valor_de_una_formula import Interpretacion from src.variables_de_una_formula import variables # interpretacionesVar(n) es la lista de las interpretaciones de n # variables. Por ejemplo, # >>> interpretacionesVar 2 # [[False, False], # [False, True], # [True, False], # [True, True]] def interpretacionesVar(n: int) -> list[list[bool]]: if n == 0: return [[]] bss = interpretacionesVar(n-1) return [[False] + x for x in bss] + [[True] + x for x in bss] def interpretaciones(f: FProp) -> list[Interpretacion]: vs = list(set(variables(f))) return [list(zip(vs, i)) for i in interpretacionesVar(len(vs))] |