Skip to content

Commit 013b2fb

Browse files
committed
more documentation
1 parent f726ae0 commit 013b2fb

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

app/Main.hs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,35 @@ import Exprs
1616
-- TODO argparse for something like check -i fma.unit
1717
-- TODO release packaged binaries on github
1818

19+
-- | Either runs a file or runs the REPL
1920
main :: IO ()
2021
main = do
2122
args <- getArgs
2223
case args of
24+
-- no file, run the repl
2325
[] -> repl
26+
-- run the file
2427
filePath:_ -> do
2528
source <- readFile filePath
2629
runString filePath source
2730

31+
-- | Run the given file name and string, printing output
2832
runString :: String -> String -> IO ()
2933
runString filePath source = case parseProgram filePath source of
34+
-- report parse error
3035
Left err -> hPrint stderr err
36+
-- run the program
3137
Right prog ->
3238
case checkProgram prog of
39+
-- report well-formedness/type checking errors
3340
Left errs -> sequence_ (print <$> errs)
41+
-- success! show definitions in their file (don't include the prelude)
3442
Right env -> print (envDifference env (initialEnv dummySS))
3543

44+
-- | Stateful REPL for running statements and viewing definitions
3645
type Repl a = HaskelineT (StateT (TyEnv SS) IO) a
3746

47+
-- | run the input as a statement and print any new definitions. The environment is maintained throughout the REPL
3848
cmd :: String -> Repl ()
3949
cmd input = do
4050
env <- get
@@ -47,6 +57,7 @@ cmd input = do
4757
put env'
4858
liftIO (print (envDifference env' env))
4959

60+
-- | (Stateful) Autocomplete based on keywords and names in scope
5061
comp :: (Monad m, MonadState (TyEnv SS) m) => WordCompleter m
5162
comp n = do
5263
env <- get
@@ -57,38 +68,48 @@ comp n = do
5768
]
5869
return $ filter (isPrefixOf n) names
5970

71+
-- | :help displays help text
6072
help :: [String] -> Repl ()
6173
help _ = liftIO $ putStrLn (unlines helpLines)
6274

75+
-- | :quit exits the repl
6376
quit :: [String] -> Repl ()
6477
quit _ = abort
6578

79+
-- | :list shows all names and their units
6680
listEnv :: [String] -> Repl ()
6781
listEnv _ = do
6882
env <- get
6983
liftIO (print env)
7084

85+
-- | Data for a @:help@-like command
7186
data Opt = Opt{ synonyms :: [String]
7287
, arguments :: [String]
7388
, runOpt :: [String] -> Repl ()
7489
, usage :: String
7590
}
91+
92+
-- | @:help@-like commands
7693
opts :: [Opt]
7794
opts =
7895
[ Opt ["help", "h"] [] help "display this help information"
7996
, Opt ["quit", "q"] [] quit "exit the repl"
8097
, Opt ["list", "l"] [] listEnv "list all names currently in scope"
8198
]
8299

100+
-- | Help text lines describing available @:help@-like commands
83101
helpLines :: [String]
84102
helpLines = "command -> description":[unwords ((":"++) <$> synonyms opt)++" -> "++usage opt | opt <- opts]
85103

104+
-- | @:help@-like commands for defining the repl
86105
opts' :: [(String, [String] -> Repl ())]
87106
opts' = [(name, runOpt opt) | opt <- opts, name <- synonyms opt]
88107

108+
-- | Welcome message
89109
ini :: Repl ()
90110
ini = liftIO $ putStrLn "Welcome to UnitChecker!" >> putStrLn "for help, use :h or :help. to quit, use :q or :quit"
91111

112+
-- | Actual repl definition. Evaluating this runs the repl until the user exits
92113
repl :: IO ()
93114
repl = flip evalStateT (initialEnv dummySS)
94115
$ evalRepl (pure "UnitChecker> ") cmd opts' (Just ':') (Word comp) ini

src/Check.hs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ instance Show a => Show (Error a) where
4242
-- from the new program.
4343
data TyEnv a = TyEnv {derivedMap :: Map String (Unit a, Int), varMap :: Map String (Unit a, Int), funMap :: Map String (Signature a, Int), count :: Int}
4444

45+
-- | Shows environment entries in the order that they were defined (inserted) separated by newlines. Uses ID's to sort
4546
instance Show (TyEnv a) where
4647
show env =
4748
[deriveds, vars, funs]
@@ -50,16 +51,25 @@ instance Show (TyEnv a) where
5051
|> fmap fst
5152
|> unlines
5253
where
54+
-- | derived unit names with their insertion ID
55+
deriveds :: [(String, Int)]
5356
deriveds = mapToVersionedLines " = " (derivedMap env)
57+
-- | variable names with their insertion ID
58+
vars :: [(String, Int)]
5459
vars = mapToVersionedLines " :: " (varMap env)
60+
-- | function names with their insertion ID
61+
funs :: [(String, Int)]
5562
funs = mapToVersionedLines " :: " (funMap env)
63+
-- | takes in a separator and an environmental map and returns each entry rendered and ID'ed
64+
mapToVersionedLines :: Show a => String -> Map.Map String (a, Int) -> [(String, Int)]
5665
mapToVersionedLines sep m = --showPair sep . swap . unassoc <$> Map.toList m
5766
Map.toList m
5867
|> fmap unassoc
5968
|> fmap (\(ab,n) -> (showPair sep ab, n))
6069
unassoc (a, (b, c)) = ((a, b), c)
6170
showPair sep (name, val) = name++sep++show val
6271

72+
-- | Totally empty type environment
6373
emptyEnvironment :: TyEnv a
6474
emptyEnvironment = TyEnv {derivedMap=Map.empty, varMap=Map.empty, funMap=Map.empty, count = 0}
6575

src/Exprs.hs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import Data.Ratio
99
type Map k v = Map.Map k v
1010

1111
-- TODO rm radians
12-
-- | Either an SI unit or a derived unit
12+
-- | Either an SI unit or a derived unit.
13+
-- Equality ignores tags
1314
data BaseUnit a = Meter a | Second a | Kilogram a | Ampere a | Kelvin a | Mole a | Candela a | Radian a | Derived String a
1415

1516
-- | All SI units, but they need a tag
@@ -20,11 +21,13 @@ siUnits = [Meter, Second, Kilogram, Ampere, Kelvin, Mole, Candela, Radian]
2021
siUnitNames :: [String]
2122
siUnitNames = [show (b()) | b <- siUnits]
2223

24+
-- equality ignores tags
2325
instance Eq (BaseUnit a) where
2426
(Derived name1 _) == (Derived name2 _) = name1 == name2
2527
-- jank, but I don't want to write all those cases and it works
2628
base1 == base2 = show base1 == show base2
2729

30+
-- comparison ignores tags
2831
instance Ord (BaseUnit a) where
2932
compare (Derived name1 _) (Derived name2 _) = compare name1 name2
3033
compare Derived{} _ = GT

0 commit comments

Comments
 (0)