From f9e70ca13142f4848003a4cd7928d5ec6d6c80d3 Mon Sep 17 00:00:00 2001 From: William Ball Date: Tue, 12 Nov 2024 11:32:05 -0800 Subject: [PATCH] reorganized and started working on unit tests --- dependent-lambda.cabal | 76 ++++++++++++++++-------------------------- {app => lib}/Check.hs | 0 {app => lib}/Expr.hs | 3 +- {app => lib}/Parser.hs | 0 tests/CheckTests.hs | 1 + tests/ExprTests.hs | 52 +++++++++++++++++++++++++++++ tests/ParserTests.hs | 1 + tests/Tests.hs | 13 ++++++++ 8 files changed, 98 insertions(+), 48 deletions(-) rename {app => lib}/Check.hs (100%) rename {app => lib}/Expr.hs (98%) rename {app => lib}/Parser.hs (100%) create mode 100644 tests/CheckTests.hs create mode 100644 tests/ExprTests.hs create mode 100644 tests/ParserTests.hs create mode 100644 tests/Tests.hs diff --git a/dependent-lambda.cabal b/dependent-lambda.cabal index e04ad28..9e8c94c 100644 --- a/dependent-lambda.cabal +++ b/dependent-lambda.cabal @@ -1,22 +1,6 @@ cabal-version: 3.0 --- The cabal-version field refers to the version of the .cabal specification, --- and can be different from the cabal-install (the tool) version and the --- Cabal (the library) version you are using. As such, the Cabal (the library) --- version used must be equal or greater than the version stated in this field. --- Starting from the specification version 2.2, the cabal-version field must be --- the first thing in the cabal file. - --- Initial package description 'dependent-lambda' generated by --- 'cabal init'. For further documentation, see: --- http://haskell.org/cabal/users-guide/ --- --- The name of the package. name: dependent-lambda --- The package version. --- See the Haskell package versioning policy (PVP) for standards --- guiding when and how versions should be incremented. --- https://pvp.haskell.org -- PVP summary: +-+------- breaking API changes -- | | +----- non-breaking API additions -- | | | +--- code changes with no API change @@ -28,56 +12,54 @@ version: 0.1.0.0 -- A longer description of the package. -- description: --- The license under which the package is released. license: GPL-3.0-or-later - --- The file containing the license text. license-file: LICENSE --- The package author(s). author: William Ball - --- An email address to which users can send suggestions, bug reports, and patches. maintainer: williampi103@gmail.com --- A copyright notice. --- copyright: category: Math build-type: Simple --- Extra doc files to be distributed with the package, such as a CHANGELOG or a README. extra-doc-files: CHANGELOG.md + , README.md -- Extra source files to be distributed with the package, such as examples, or a tutorial module. -- extra-source-files: -common warnings - ghc-options: -Wall - -executable dependent-lambda - -- Import common warning flags. - import: warnings - - -- .hs or .lhs file containing the Main module. - main-is: Main.hs - - -- Modules included in this executable, other than Main. - other-modules: Expr - Check +library dependent-lambda-lib + exposed-modules: Check Parser + Expr - -- LANGUAGE extensions used by modules in this package. - -- other-extensions: - - -- Other library packages from which modules are imported. + hs-source-dirs: lib build-depends: base ^>=4.19.1.0 , megaparsec , text , parser-combinators , mtl - - -- Directories containing source files. - hs-source-dirs: app - - -- Base language which the package is written in. + default-language: Haskell2010 + +common warnings + ghc-options: -Wall + +executable dependent-lambda + import: warnings + main-is: Main.hs + + build-depends: base ^>=4.19.1.0 + , dependent-lambda-lib + hs-source-dirs: app + default-language: Haskell2010 + +test-suite tests + type: exitcode-stdio-1.0 + main-is: Tests.hs + other-modules: ExprTests + , ParserTests + , CheckTests + build-depends: base ^>=4.19.1.0 + , HUnit + , dependent-lambda-lib + hs-source-dirs: tests default-language: Haskell2010 diff --git a/app/Check.hs b/lib/Check.hs similarity index 100% rename from app/Check.hs rename to lib/Check.hs diff --git a/app/Expr.hs b/lib/Expr.hs similarity index 98% rename from app/Expr.hs rename to lib/Expr.hs index cb4bbff..310ceb6 100644 --- a/app/Expr.hs +++ b/lib/Expr.hs @@ -118,7 +118,8 @@ incIndices = shiftIndices 1 0 subst :: Integer -> Expr -> Expr -> Expr subst k s (Var n x) | k == n = s - | otherwise = Var (n - 1) x + | n > k = Var (n - 1) x + | otherwise = Var n x subst _ _ Star = Star subst _ _ Square = Square subst k s (App m n) = App (subst k s m) (subst k s n) diff --git a/app/Parser.hs b/lib/Parser.hs similarity index 100% rename from app/Parser.hs rename to lib/Parser.hs diff --git a/tests/CheckTests.hs b/tests/CheckTests.hs new file mode 100644 index 0000000..35a7584 --- /dev/null +++ b/tests/CheckTests.hs @@ -0,0 +1 @@ +module CheckTests where diff --git a/tests/ExprTests.hs b/tests/ExprTests.hs new file mode 100644 index 0000000..1c2a990 --- /dev/null +++ b/tests/ExprTests.hs @@ -0,0 +1,52 @@ +module ExprTests where + +import Expr +import Test.HUnit + +inner :: Expr +inner = Abs "x" (Var 0 "A") $ Var 2 "f" <.> Var 0 "x" + +e1 :: Expr +e1 = Abs "A" Star inner + +fFree :: Test +fFree = TestCase $ assertBool "f free" $ occursFree 0 e1 + +incE1 :: Test +incE1 = + TestCase $ + assertEqual + "incIndices e1" + ( Abs "A" Star $ + Abs "x" (Var 0 "A") $ + Var 3 "f" <.> Var 0 "x" + ) + (incIndices e1) + +after :: Expr +after = Abs "x" (Var 2 "B") $ Var 1 "f" <.> Var 0 "x" + +substE1 :: Test +substE1 = + TestCase $ + assertEqual + "e1[A := B]" + after + (subst 0 (Var 2 "B") inner) + +betaNFe1 :: Test +betaNFe1 = + TestCase $ + assertEqual + "e1 B" + after + (betaNF $ e1 <.> Var 2 "B") + +tests :: Test +tests = + TestList + [ TestLabel "fFree" fFree + , TestLabel "incE1" incE1 + , TestLabel "substE1" substE1 + , TestLabel "betaNFe1" betaNFe1 + ] diff --git a/tests/ParserTests.hs b/tests/ParserTests.hs new file mode 100644 index 0000000..0614b09 --- /dev/null +++ b/tests/ParserTests.hs @@ -0,0 +1 @@ +module ParserTests where diff --git a/tests/Tests.hs b/tests/Tests.hs new file mode 100644 index 0000000..af21945 --- /dev/null +++ b/tests/Tests.hs @@ -0,0 +1,13 @@ +module Main where + +import qualified ExprTests as E +import qualified System.Exit as Exit +import Test.HUnit + +tests :: Test +tests = TestList [TestLabel "ExprTests" E.tests] + +main :: IO () +main = do + result <- runTestTT tests + if failures result > 0 then Exit.exitFailure else Exit.exitSuccess