Skip to content

Commit

Permalink
Measure size of AST
Browse files Browse the repository at this point in the history
towards #866
  • Loading branch information
kostmo committed Jan 7, 2023
1 parent dd28529 commit 7e00f61
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Swarm/Language/Syntax.hs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ module Swarm.Language.Syntax (
fvT,
fv,
mapFree1,
measureAstSize,
) where

import Control.Arrow (Arrow ((&&&)))
Expand Down Expand Up @@ -965,3 +966,17 @@ mapFree1 x f = fvT %~ (\t -> if t == TVar x then f t else t)

lowShow :: Show a => a -> Text
lowShow a = toLower (from (show a))

-- | Note that the size constants for the recursive constructors
-- are somewhat arbitrary; perhaps we should take into account
-- the other fields of those constructors.
measureAstSize :: Term -> Int
measureAstSize = \case
SLam _ _ (Syntax _ t1) -> 2 + measureAstSize t1
SApp (Syntax _ t1) (Syntax _ t2) -> measureAstSize t1 + measureAstSize t2
SLet _ _ _ (Syntax _ t1) (Syntax _ t2) -> 2 + measureAstSize t1 + measureAstSize t2
SPair (Syntax _ t1) (Syntax _ t2) -> measureAstSize t1 + measureAstSize t2
SDef _ _ _ (Syntax _ t1) -> 2 + measureAstSize t1
SBind _ (Syntax _ t1) (Syntax _ t2) -> 1 + measureAstSize t1 + measureAstSize t2
SDelay _ (Syntax _ t1) -> 1 + measureAstSize t1
_ -> 1

0 comments on commit 7e00f61

Please sign in to comment.