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.

62 lines
1.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 Data.Map.Strict (Map)
  5. import Syntax
  6. import {-# SOURCE #-} Elab.WiredIn (inot, ior, iand)
  7. import Debug.Trace (traceShow)
  8. toDnf :: Value -> Maybe Value
  9. toDnf (VNe _ _) = Nothing
  10. toDnf x = toDnf x where
  11. toDnf (VIAnd x y) = idist <$> toDnf (inot x) <*> toDnf (inot y)
  12. toDnf (VIOr x y) = ior <$> toDnf x <*> toDnf y
  13. toDnf (VINot x) = inot <$> toDnf x
  14. toDnf VI0 = pure VI0
  15. toDnf VI1 = pure VI1
  16. toDnf v@(VNe _ Seq.Empty) = pure v
  17. toDnf _ = Nothing
  18. compareDNFs :: Value -> Value -> Bool
  19. compareDNFs (VIOr x y) (VIOr x' y') =
  20. let (a, a') = swap x y
  21. (b, b') = swap x' y'
  22. in compareDNFs a b && compareDNFs a' b'
  23. compareDNFs (VIAnd x y) (VIAnd x' y') =
  24. let (a, a') = swap x x'
  25. (b, b') = swap y y'
  26. in compareDNFs a a' && compareDNFs b b'
  27. compareDNFs x y = x == y
  28. swap :: Ord b => b -> b -> (b, b)
  29. swap x y =
  30. if x <= y then (x, y) else (y, x)
  31. possible :: Map Head Bool -> Value -> (Bool, Map Head Bool)
  32. possible sc (VINot (VNe n Seq.Empty)) =
  33. case Map.lookup n sc of
  34. Just True -> (False, sc)
  35. _ -> (True, Map.insert n False sc)
  36. possible sc (VNe n Seq.Empty) =
  37. case Map.lookup n sc of
  38. Just False -> (False, sc)
  39. _ -> (True, Map.insert n True sc)
  40. possible sc (VIAnd x y) =
  41. let (a, sc') = possible sc x
  42. (b, sc'') = possible sc' y
  43. in (a && b, sc'')
  44. possible sc (VIOr x y) =
  45. case possible sc x of
  46. (True, sc') -> (True, sc')
  47. (False, _) -> possible sc y
  48. possible sc VI0 = (False, sc)
  49. possible sc VI1 = (True, sc)
  50. possible sc _ = (False, sc)
  51. idist :: Value -> Value -> Value
  52. idist (VIOr x y) z = (x `idist` z) `ior` (y `idist` z)
  53. idist z (VIOr x y) = (z `idist` x) `ior` (z `idist` y)
  54. idist z x = iand z x