a silly little programming language inspired by datalog.
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.

88 lines
2.1KB

  1. module EDSL where
  2. import Relude
  3. import Test.Hspec
  4. import Language.Logi
  5. import Utils
  6. tests :: Spec
  7. tests = do
  8. describe "edsl" $ do
  9. it "fact and query asum" $ do
  10. shouldBe
  11. ( evalE'
  12. [ fact_ "a" (int_ 1)
  13. , fact_ "a" (int_ 2)
  14. , fact_ "a" (int_ 3)
  15. , expr_ $ asum_ "a"
  16. ]
  17. )
  18. ( resExpr $ int_ 6
  19. )
  20. it "fact and query acount" $ do
  21. shouldBe
  22. ( evalE'
  23. [ fact_ "a" (int_ 1)
  24. , fact_ "a" (int_ 2)
  25. , fact_ "a" (int_ 3)
  26. , expr_ $ acount_ "a"
  27. ]
  28. )
  29. ( resExpr $ int_ 3
  30. )
  31. it "negative join" $ do
  32. shouldBe
  33. ( evalE'
  34. [ fact_ "a" (int_ 1)
  35. , fact_ "a" (int_ 2)
  36. , fact_ "b" (int_ 2)
  37. , fact_ "b" (int_ 3)
  38. , rule_ "c" (rec_ ["a" .= var_ "a", "b" .= var_ "b"])
  39. [ atomR_ "a" (var_ "a")
  40. , atomR_ "b" (var_ "b")
  41. , exprR_ (not_ (equals_ [var_ "a", var_ "b"]))
  42. ]
  43. , query_ "c" (var_ "x")
  44. ]
  45. )
  46. ( resE
  47. [ atom_ "c" $ rec_ ["a" .= int_ 1, "b" .= int_ 2]
  48. , atom_ "c" $ rec_ ["a" .= int_ 2, "b" .= int_ 3]
  49. , atom_ "c" $ rec_ ["a" .= int_ 1, "b" .= int_ 3]
  50. ]
  51. )
  52. it "access label" $ do
  53. shouldBe
  54. ( evalE'
  55. [ expr_ $ rec_ ["a" .= int_ 1] # "a"
  56. ]
  57. )
  58. ( resExpr $ int_ 1
  59. )
  60. it "udf between" $ do
  61. shouldBe
  62. ( evalE'
  63. [ fact_ "a" (int_ 1)
  64. , rule_ "a" (sum_ [var_ "x", int_ 1])
  65. [ atomR_ "a" (var_ "x")
  66. , exprR_ (lesser_ (var_ "x") (int_ 10))
  67. ]
  68. , fun_ "between" ["low", "n", "high"] $
  69. and_ [lesser_ (var_ "low") (var_ "n"), greater_ (var_ "high") (var_ "n")]
  70. , rule_ "b" (var_ "x")
  71. [ atomR_ "a" (var_ "x")
  72. , exprR_ $ app_ "between" [int_ 3, var_ "x", int_ 7]
  73. ]
  74. , query_ "b" (var_ "x")
  75. ]
  76. )
  77. ( resE $ map (atom_ "b" . int_) [4..6]
  78. )