1
1
mirror of https://github.com/github/semantic.git synced 2025-01-08 08:30:27 +03:00

Document the declarations in Term.hs

This commit is contained in:
Matt Diephouse 2016-01-13 15:35:40 -05:00
parent a2643cef82
commit c0c3a69bcf

View File

@ -5,8 +5,11 @@ import Data.Maybe
import Control.Comonad.Cofree
import Syntax
-- | An annotated node (Syntax) in an abstract syntax tree.
type Term a annotation = Cofree (Syntax a) annotation
-- | Zip two terms by combining their annotations into a pair of annotations.
-- | If the structure of the two terms don't match, then Nothing will be returned.
zipTerms :: Term a annotation -> Term a annotation -> Maybe (Term a (annotation, annotation))
zipTerms (annotation1 :< a) (annotation2 :< b) = annotate $ zipUnwrap a b
where
@ -18,9 +21,11 @@ zipTerms (annotation1 :< a) (annotation2 :< b) = annotate $ zipUnwrap a b
zipUnwrap _ _ = Nothing
zipUnwrapMaps a' b' key = (,) key <$> zipTerms (a' ! key) (b' ! key)
-- | Fold a term into some other value from the bottom up.
cata :: (annotation -> Syntax a b -> b) -> Term a annotation -> b
cata f (annotation :< syntax) = f annotation $ cata f <$> syntax
-- | Return the number of leaves in the node.
termSize :: Term a annotation -> Integer
termSize = cata size where
size _ (Leaf _) = 1