less prototype, less bad code implementation of CCHM 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.

85 lines
2.7 KiB

  1. module Elab.Eval.Formula where
  2. import qualified Data.Map.Strict as Map
  3. import qualified Data.Sequence as Seq
  4. import qualified Data.Set as Set
  5. import Data.Map.Strict (Map)
  6. import Data.Set (Set)
  7. import Syntax
  8. import {-# SOURCE #-} Elab.WiredIn (inot, ior, iand)
  9. toDnf :: Value -> Maybe Value
  10. toDnf = fmap (dnf2Val . normalise) . val2Dnf where
  11. val2Dnf (VNe _ _) = Nothing
  12. val2Dnf x = toDnf x where
  13. toDnf (VIAnd x y) = idist <$> toDnf (inot x) <*> toDnf (inot y)
  14. toDnf (VIOr x y) = ior <$> toDnf x <*> toDnf y
  15. toDnf (VINot x) = inot <$> toDnf x
  16. toDnf VI0 = pure VI0
  17. toDnf VI1 = pure VI1
  18. toDnf v@(VNe _ Seq.Empty) = pure v
  19. toDnf _ = Nothing
  20. dnf2Val xs = Set.foldl ior VI0 (Set.map (Set.foldl iand VI1) xs)
  21. type Nf = Set (Set Value)
  22. normalise :: Value -> Nf
  23. normalise = normaliseOr where
  24. normaliseOr (VIOr x y) = Set.singleton (normaliseAnd x) <> normaliseOr y
  25. normaliseOr x = Set.singleton (normaliseAnd x)
  26. normaliseAnd (VIAnd x y) = Set.insert x (normaliseAnd y)
  27. normaliseAnd x = Set.singleton x
  28. compareDNFs :: Value -> Value -> Bool
  29. compareDNFs (VIOr x y) (VIOr x' y') =
  30. let (a, a') = swap x y
  31. (b, b') = swap x' y'
  32. in compareDNFs a b && compareDNFs a' b'
  33. compareDNFs (VIAnd x y) (VIAnd x' y') =
  34. let (a, a') = swap x x'
  35. (b, b') = swap y y'
  36. in compareDNFs a a' && compareDNFs b b'
  37. compareDNFs x y = x == y
  38. swap :: Ord b => b -> b -> (b, b)
  39. swap x y =
  40. if x <= y then (x, y) else (y, x)
  41. possible :: Map Head Bool -> Value -> (Bool, Map Head Bool)
  42. possible sc (VINot (VNe n Seq.Empty)) =
  43. case Map.lookup n sc of
  44. Just True -> (False, sc)
  45. _ -> (True, Map.insert n False sc)
  46. possible sc (VNe n Seq.Empty) =
  47. case Map.lookup n sc of
  48. Just False -> (False, sc)
  49. _ -> (True, Map.insert n True sc)
  50. possible sc (VIAnd x y) =
  51. let (a, sc') = possible sc x
  52. (b, sc'') = possible sc' y
  53. in (a && b, sc'')
  54. possible sc (VIOr x y) =
  55. case possible sc x of
  56. (True, sc') -> (True, sc')
  57. (False, _) -> possible sc y
  58. possible sc VI0 = (False, sc)
  59. possible sc VI1 = (True, sc)
  60. possible sc _ = (False, sc)
  61. truthAssignments :: NFEndp -> Map Name (NFType, NFEndp) -> [Map Name (NFType, NFEndp)]
  62. truthAssignments VI0 _ = []
  63. truthAssignments VI1 m = pure m
  64. truthAssignments (VIOr x y) m = truthAssignments x m ++ truthAssignments y m
  65. truthAssignments (VIAnd x y) m = truthAssignments x =<< truthAssignments y m
  66. truthAssignments (VNe (HVar x) Seq.Empty) m = pure (Map.insert x (VI, VI1) m)
  67. truthAssignments (VINot (VNe (HVar x) Seq.Empty)) m = pure (Map.insert x (VI, VI0) m)
  68. truthAssignments _ m = pure m
  69. idist :: Value -> Value -> Value
  70. idist (VIOr x y) z = (x `idist` z) `ior` (y `idist` z)
  71. idist z (VIOr x y) = (z `idist` x) `ior` (z `idist` y)
  72. idist z x = iand z x