Prototype, extremely bad code implementation of CCHM Cubical Type Theory
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
2.7 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. module Systems where
  2. import qualified Data.Map.Strict as Map
  3. import qualified Data.Set as Set
  4. import Data.Map.Strict (Map)
  5. import Data.Set (Set)
  6. import Data.List
  7. import Presyntax
  8. newtype Face = Face { getFace :: Map String Bool }
  9. deriving (Eq, Show, Ord)
  10. evalFormula :: Formula -> Face -> Bool
  11. evalFormula (Is0 x) f =
  12. case Map.lookup x (getFace f) of
  13. Just x -> not x
  14. Nothing -> error $ "dimension not bound in formula: " ++ show x
  15. evalFormula (Is1 x) f =
  16. case Map.lookup x (getFace f) of
  17. Just x -> x
  18. Nothing -> error $ "dimension not bound in formula: " ++ show x
  19. evalFormula (And a b) f = evalFormula a f && evalFormula b f
  20. evalFormula (Or a b) f = evalFormula a f || evalFormula b f
  21. evalFormula Top _ = True
  22. evalFormula Bot _ = False
  23. freeVarsFormula :: Formula -> Set String
  24. freeVarsFormula (Is0 x) = Set.singleton x
  25. freeVarsFormula (Is1 x) = Set.singleton x
  26. freeVarsFormula (And a b) = Set.union (freeVarsFormula a) (freeVarsFormula b)
  27. freeVarsFormula (Or a b) = Set.union (freeVarsFormula a) (freeVarsFormula b)
  28. freeVarsFormula Top = mempty
  29. freeVarsFormula Bot = mempty
  30. faces :: Formula -> ([Face], [Face])
  31. faces formula = partition (evalFormula formula) allPossible where
  32. truths [] = [mempty]
  33. truths (x:xs) = uncurry Map.insert <$> [(x, True), (x, False)] <*> truths xs
  34. allPossible = Face <$> truths (Set.toList (freeVarsFormula formula))
  35. impossible, possible, tautological :: Formula -> Bool
  36. impossible = null . fst . faces
  37. possible = not . null . fst . faces
  38. tautological = not . null . snd . faces
  39. toDNF :: Formula -> Formula
  40. toDNF = orFormula . map formulaOfFace . fst . faces
  41. formulaOfFace :: Face -> Formula
  42. formulaOfFace = andFormula . map (\(x, i) -> if i then Is1 x else Is0 x) . Map.toDescList . getFace
  43. andFormula :: [Formula] -> Formula
  44. andFormula = foldr and Top where
  45. and x y =
  46. case x of
  47. Top -> case y of
  48. Bot -> Bot
  49. _ -> y
  50. Bot -> Bot
  51. _ -> case y of
  52. Top -> x
  53. _ -> And x y
  54. orFormula :: [Formula] -> Formula
  55. orFormula [] = Bot
  56. orFormula (x:xs) = or x (orFormula xs) where
  57. or x y =
  58. case x of
  59. Top -> Top
  60. Bot -> case y of
  61. Top -> Top
  62. _ -> y
  63. _ -> case y of
  64. Bot -> x
  65. _ -> Or x y
  66. notFormula :: Formula -> Formula
  67. notFormula Top = Bot
  68. notFormula Bot = Top
  69. notFormula (Is0 x) = Is1 x
  70. notFormula (Is1 x) = Is0 x
  71. notFormula (And x y) = Or (notFormula x) (notFormula y)
  72. notFormula (Or x y) = And (notFormula x) (notFormula y)
  73. newtype System a = FMap { getSystem :: Map Formula a }
  74. deriving (Eq, Show, Ord)
  75. emptySystem :: System a
  76. emptySystem = FMap mempty
  77. mapSystem :: System a -> (a -> b) -> System b
  78. mapSystem (FMap x) f = FMap (Map.filterWithKey (\f _ -> f /= Bot) (fmap f x))