Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: simple utils to facilitate dev-ing at the REPL #418

Merged
merged 4 commits into from
Aug 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/haskell/natural4/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ ghc-options: -Wdefault -Wno-missed-extra-shared-lib -fconstraint-solver-iteratio

library:
source-dirs: src
dependencies:
- filepath
- filemanip
# the file stuff is for the interactive repl utils
# it's a bit misleading to list them as dependencies of the lib, but there doesn't seem to be any real alternative if we're using stack, at least according to discord

executables:
natural4-exe:
Expand Down
101 changes: 101 additions & 0 deletions lib/haskell/natural4/src/LS/UtilsREPLDev.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
{-# LANGUAGE ImportQualifiedPost #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE LambdaCase #-}

{-|
Simple utils / convenience functions for prototyping / dev-ing at the REPL.
-}


module LS.UtilsREPLDev
( filesInDirWithExtn,
findFileByNameInDir,
csvsInDir,
l4csv2rules,
leTestcasesDir,
leTestCSVs
)
where

import Flow ((|>))
import Data.Maybe (fromMaybe, listToMaybe)

import System.FilePath ((</>))
import System.FilePath.Find
( always,
fileName,
find,
(==?),
extension
)
import Text.Pretty.Simple ( pShowNoColor, pPrint )

import LS qualified
import LS.Lib (NoLabel (..), Opts (..))
import LS.Utils ((|$>))


-- Getting file paths

type FileExtn = String
type BaseFileName = String
type StartDir = String

filesInDirWithExtn :: FileExtn -> StartDir -> IO [FilePath]
filesInDirWithExtn fextn = find always (extension ==? fextn)

csvsInDir :: StartDir -> IO [FilePath]
csvsInDir = filesInDirWithExtn ".csv"

{-|
==== __Examples__
>>> findFileByNameInDir leTestcasesDir "indentation-databreach.csv"
Just "test/Testcases/LogicalEnglish/indentation-propn-databreach/indentation-databreach.csv"
-}
findFileByNameInDir :: StartDir -> BaseFileName -> IO (Maybe FilePath)
findFileByNameInDir startdir basefnm =
startdir
|> find always (fileName ==? basefnm)
|$> listToMaybe


-- Getting rules from L4 CSVs
--- TODO: Would be convenient to also have a function that allows you to match just on a part of the base filename

{-|
Util function for getting raw rules from a L4 CSV filepath
Not meant for production apps!
Adapted from Joe's code for testing LP Programs
==== __Examples__
>>> l4csv2rules "test/Testcases/LogicalEnglish/" "indentation-databreach.csv"
-}
l4csv2rules :: StartDir -> BaseFileName -> IO [LS.Rule]
l4csv2rules startdir csvFpath =
findFileByNameInDir startdir csvFpath >>=
\case
Nothing -> error "Can't find file"
-- remember, this is meant to be for internal use by a developer in the REPL
Just file -> LS.dumpRules
Opts

Check warning on line 79 in lib/haskell/natural4/src/LS/UtilsREPLDev.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

• Fields of ‘Opts’ not initialised:

Check warning on line 79 in lib/haskell/natural4/src/LS/UtilsREPLDev.hs

View workflow job for this annotation

GitHub Actions / build (macos-11)

• Fields of ‘Opts’ not initialised:

Check warning on line 79 in lib/haskell/natural4/src/LS/UtilsREPLDev.hs

View workflow job for this annotation

GitHub Actions / build (macos-12)

• Fields of ‘Opts’ not initialised:
{ file = NoLabel [file],
dbug = False,
dstream = False
}

{-|
Util function for __pretty printing (in color)__ raw rules from a L4 CSV filepath
==== __Examples__
>>> pRules "test/Testcases/LogicalEnglish/" "indentation-databreach.csv"
-}
pRules :: StartDir -> BaseFileName -> IO ()
pRules startdir csvFpath = l4csv2rules startdir csvFpath >>= pPrint


------- TODO: LE-specific things; to be moved into a better place when I have more time
-- | Convenient, tho not the best practice to put this in this module
leTestcasesDir :: FilePath
leTestcasesDir = "test" </> "Testcases" </> "LogicalEnglish"

-- | Returns a list of csvs in the LE subdir of test/Testcases
leTestCSVs :: IO [FilePath]
leTestCSVs = csvsInDir leTestcasesDir
Loading