Skip to content

Commit bf86708

Browse files
committed
Even more documentation strings.
1 parent a838ede commit bf86708

File tree

9 files changed

+223
-76
lines changed

9 files changed

+223
-76
lines changed

src/FSM.hs

+17-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
Copyright : (C) 2022 Marek Materzok
33
License : BSD2 (see the file LICENSE)
44
Maintainer : Marek Materzok <[email protected]>
5-
|-}
5+
6+
The YieldFSM compiler.
7+
-}
68
module FSM(fsm) where
79

810
import FSM.Lang
@@ -64,6 +66,20 @@ mkFSM str = do
6466
hFlush stderr
6567
fail "FAIL"
6668

69+
{-|
70+
The YieldFSM compiler as a Template Haskell quasiquoter.
71+
It compiles YieldFSM programs to Clash definitions.
72+
73+
Example use:
74+
75+
> [fsm|countFSM :: (CP.HiddenClockResetEnable dom)
76+
> => CP.Signal dom Integer
77+
> fun f i:
78+
> yield i
79+
> ret call f (i+1)
80+
> ret call f 0
81+
> |]
82+
-}
6783
fsm :: THQ.QuasiQuoter
6884
fsm = THQ.QuasiQuoter undefined undefined undefined mkFSM
6985

src/FSM/Desc.hs

+31-17
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,44 @@ import qualified Language.Haskell.TH as TH
1313
import qualified Data.Map.Strict as M
1414
import Prelude
1515

16+
{-|
17+
Encodes a Mealy machine transition.
18+
-}
1619
data Transition = Transition {
17-
transOutput :: TH.Exp,
18-
transNextState :: TH.Name,
19-
transNextStateParams :: TH.Exp
20+
transOutput :: TH.Exp, -- ^ Output value on this transition.
21+
transNextState :: TH.Name, -- ^ The name of the next state.
22+
transNextStateParams :: TH.Exp -- ^ Parameters of the next state.
2023
}
2124

22-
data DecisionTree a = DTIf TH.Exp (DecisionTree a) (DecisionTree a)
23-
| DTLet TH.Pat TH.Exp (DecisionTree a)
24-
| DTCase TH.Exp [(TH.Pat, DecisionTree a)]
25-
| DTLeaf a
25+
{-|
26+
Decision trees. Used to represent conditions on which the next
27+
transition is selected.
28+
-}
29+
data DecisionTree a = DTIf TH.Exp (DecisionTree a) (DecisionTree a) -- ^ Boolean condition.
30+
| DTLet TH.Pat TH.Exp (DecisionTree a) -- ^ Let definition.
31+
| DTCase TH.Exp [(TH.Pat, DecisionTree a)] -- ^ Case (pattern-matching) condition.
32+
| DTLeaf a -- ^ Leaf of the decision tree.
2633

34+
{-|
35+
Encodes a Mealy machine state. A state can be parametrized, and it has
36+
a number of transitions, encoded using a decision tree.
37+
-}
2738
data FSMState = FSMState {
28-
fsmStateParams :: TH.Pat,
29-
fsmStateTrans :: DecisionTree Transition
39+
fsmStateParams :: TH.Pat, -- ^ State parameters.
40+
fsmStateTrans :: DecisionTree Transition -- ^ State transitions.
3041
}
3142

43+
{-|
44+
Encodes a Mealy machine.
45+
-}
3246
data FSM = FSM {
33-
fsmName :: TH.Name,
34-
fsmType :: TH.Type,
35-
fsmParams :: [TH.Pat],
36-
fsmStates :: M.Map TH.Name FSMState,
37-
fsmInputs :: Maybe TH.Pat,
38-
fsmInitState :: TH.Name,
39-
fsmInitStateParam :: TH.Exp,
40-
fsmConts :: M.Map TH.Name (M.Map TH.Name [TH.Name])
47+
fsmName :: TH.Name, -- ^ Machine name.
48+
fsmType :: TH.Type, -- ^ Clash type of the machine.
49+
fsmParams :: [TH.Pat], -- ^ Machine parameters.
50+
fsmStates :: M.Map TH.Name FSMState, -- ^ Machine states.
51+
fsmInputs :: Maybe TH.Pat, -- ^ Machine inputs.
52+
fsmInitState :: TH.Name, -- ^ Machine initial state.
53+
fsmInitStateParam :: TH.Exp, -- ^ Parameters of the initial state.
54+
fsmConts :: M.Map TH.Name (M.Map TH.Name [TH.Name]) -- ^ Continuation data types.
4155
}
4256

src/FSM/DescGenADT.hs

+4
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ wildUnused s (TH.ListP ps) = TH.ListP $ wildUnused s <$> ps
8484
wildUnused s (TH.SigP p t) = TH.SigP (wildUnused s p) t
8585
wildUnused s (TH.ViewP e p) = TH.ViewP e $ wildUnused s p
8686

87+
{-|
88+
Compiles the automata descriptions in the target language to Mealy machines
89+
defined in Clash. This is the final stage of YieldFSM compilation.
90+
-}
8791
compileFSM :: FSM -> TH.Q [TH.Dec]
8892
compileFSM fsm = do
8993
let nm = TH.nameBase $ fsmName fsm

src/FSM/FreeVars.hs

+39-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ Maintainer : Marek Materzok <[email protected]>
55
66
Defines helper functions related to free variables and substitutions.
77
-}
8-
module FSM.FreeVars where
8+
module FSM.FreeVars(
9+
FreeVars(..), FreeVarsPat(..), PatFV(..),
10+
patSingleton, patFreeSingleton, patFreeVars, underPat,
11+
boundVars, freeVarsFunMap,
12+
Subst(..), substSingle, rename, renameSingle, boundAsVars, substPat,
13+
isConstantExpr
14+
) where
915

1016
import qualified Language.Haskell.TH as TH
1117
import qualified Data.Set as S
@@ -16,29 +22,46 @@ import FSM.Desc
1622
import Control.Arrow
1723
import qualified FSM.Util.SetClass as SC
1824

25+
-- | Things that have free variables.
1926
class FreeVars a where
2027
freeVars :: SC.SetClass s => a -> s TH.Name
2128

29+
-- | Things that have bound and free variables.
2230
class FreeVarsPat a where
2331
freeVarsPat :: SC.SetClass s => a -> PatFV s
2432

25-
data PatFV s = PatFV { patBound :: s TH.Name, patFree :: s TH.Name }
33+
{-|
34+
Represents bound and free variables (e.g. for a pattern).
35+
A Haskell pattern can both bind variables, and have free variables (e.g.
36+
in view patterns).
37+
-}
38+
data PatFV s = PatFV {
39+
patBound :: s TH.Name, -- ^ Bound variables.
40+
patFree :: s TH.Name -- ^ Free variables.
41+
}
2642

2743
instance SC.SetClass s => Semigroup (PatFV s) where
2844
PatFV s1 s2 <> PatFV t1 t2 = PatFV (s1 `SC.union` t1) (s2 `SC.union` t2)
2945

3046
instance SC.SetClass s => Monoid (PatFV s) where
3147
mempty = PatFV mempty mempty
3248

49+
-- | A single bound variable.
3350
patSingleton :: SC.SetClass s => TH.Name -> PatFV s
3451
patSingleton n = PatFV (SC.singleton n) mempty
3552

53+
-- | A single free variable.
3654
patFreeSingleton :: SC.SetClass s => TH.Name -> PatFV s
3755
patFreeSingleton n = PatFV mempty (SC.singleton n)
3856

57+
-- | Free variables of something, as a 'PatFV'.
3958
patFreeVars :: (SC.SetClass s, FreeVars a) => a -> PatFV s
4059
patFreeVars e = PatFV mempty (freeVars e)
4160

61+
{-|
62+
Free variables under a pattern. The pattern's bound variables
63+
reduce the set of free variables; its free variables extend it.
64+
-}
4265
underPat :: SC.SetClass s => s TH.Name -> PatFV s -> s TH.Name
4366
underPat s (PatFV bs fs) = fs <> (s `SC.difference` bs)
4467

@@ -48,6 +71,7 @@ underPatFV (PatFV bs1 fs1) (PatFV bs2 fs2) = PatFV (bs1 <> bs2) (fs2 <> (fs1 `SC
4871
freeVarsUnderPat :: (SC.SetClass s, FreeVarsPat a) => s TH.Name -> a -> s TH.Name
4972
freeVarsUnderPat s p = s `underPat` freeVarsPat p
5073

74+
-- | Bound vars of something.
5175
boundVars :: (SC.SetClass s, FreeVarsPat a) => a -> s TH.Name
5276
boundVars = patBound . freeVarsPat
5377

@@ -57,6 +81,7 @@ instance FreeVars a => FreeVars (Maybe a) where
5781
instance FreeVarsPat a => FreeVarsPat (Maybe a) where
5882
freeVarsPat = maybe mempty id . fmap freeVarsPat
5983

84+
-- | Free variables of a function set.
6085
freeVarsFunMap :: (IsDesugared l, SC.SetClass s) => FunMap l -> s TH.Name
6186
freeVarsFunMap = mconcat . map (\(_, (p, s)) -> freeVars s `freeVarsUnderPat` p) . M.toList
6287

@@ -199,7 +224,9 @@ substName :: M.Map TH.Name TH.Exp -> TH.Name -> TH.Exp
199224
substName s n | Just e <- M.lookup n s = e
200225
| otherwise = TH.VarE n
201226

227+
-- | Things that can have expressions substituted for variables.
202228
class Subst a where
229+
-- | Variable substitution.
203230
subst :: M.Map TH.Name TH.Exp -> a -> a
204231

205232
instance Subst TH.Exp where
@@ -323,19 +350,23 @@ instance Subst a => Subst [a] where
323350
instance Subst a => Subst (Maybe a) where
324351
subst s = fmap (subst s)
325352

353+
-- | Perform a single variable substitution.
326354
substSingle :: Subst a => TH.Name -> TH.Exp -> a -> a
327355
substSingle n e = subst (M.singleton n e)
328356

329357
instance Subst VStmt where
330358
subst su (VExp e) = VExp (subst su e)
331359
subst su (VCall n e) = VCall n (subst su e)
332360

361+
-- | Rename variables (substitute variables for variables).
333362
rename :: Subst a => M.Map TH.Name TH.Name -> a -> a
334363
rename su = subst (M.map TH.VarE su)
335364

365+
-- | Rename a single variable.
336366
renameSingle :: Subst a => TH.Name -> TH.Name -> a -> a
337367
renameSingle n n' = substSingle n (TH.VarE n')
338368

369+
-- | Variables bound in @as@ patterns.
339370
boundAsVars :: TH.Pat -> S.Set TH.Name
340371
boundAsVars (TH.LitP _) = mempty
341372
boundAsVars (TH.VarP _) = mempty
@@ -355,6 +386,7 @@ boundAsVars (TH.ListP ps) = mconcat $ map boundAsVars ps
355386
boundAsVars (TH.SigP p _) = boundAsVars p
356387
boundAsVars (TH.ViewP _ p) = boundAsVars p
357388

389+
-- | Substitute a pattern for a bound variable in a pattern.
358390
substPat :: M.Map TH.Name TH.Pat -> TH.Pat -> TH.Pat
359391
substPat _ p@(TH.LitP _) = p
360392
substPat s p@(TH.VarP n) | Just p' <- M.lookup n s = p'
@@ -407,6 +439,11 @@ isConstructorExpr (TH.UnboundVarE _) = False
407439
isConstructorExpr (TH.LabelE _) = False
408440
isConstructorExpr (TH.ImplicitParamVarE _) = False
409441

442+
{-|
443+
Expressions that can be substituted without duplicating circuits.
444+
In digital circuits, constructors are represented as bundles of wires,
445+
and therefore can be duplicated without a performance penalty.
446+
-}
410447
isConstantExpr :: TH.Exp -> Bool
411448
isConstantExpr (TH.VarE _) = True
412449
isConstantExpr e = isConstructorExpr e

0 commit comments

Comments
 (0)