summaryrefslogtreecommitdiff
path: root/src/GF/UseGrammar/MatchTerm.hs
diff options
context:
space:
mode:
authoraarne <aarne@cs.chalmers.se>2006-03-16 21:26:22 +0000
committeraarne <aarne@cs.chalmers.se>2006-03-16 21:26:22 +0000
commit5d772cbfa20abfb84b9925a5670ac5dd28312251 (patch)
tree506b36e8f931ad341cb3eb9f08bc86a4aa894d05 /src/GF/UseGrammar/MatchTerm.hs
parente658e345fd225308cdf7c2c131198f85927b1dc3 (diff)
MatchTerm: testing conditions on terms, e.g. nodup
Diffstat (limited to 'src/GF/UseGrammar/MatchTerm.hs')
-rw-r--r--src/GF/UseGrammar/MatchTerm.hs50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/GF/UseGrammar/MatchTerm.hs b/src/GF/UseGrammar/MatchTerm.hs
new file mode 100644
index 000000000..9acffd44c
--- /dev/null
+++ b/src/GF/UseGrammar/MatchTerm.hs
@@ -0,0 +1,50 @@
+----------------------------------------------------------------------
+-- |
+-- Module : MatchTerm
+-- Maintainer : AR
+-- Stability : (stable)
+-- Portability : (portable)
+--
+--
+-- functions for matching with terms. AR 16/3/2006
+-----------------------------------------------------------------------------
+
+module GF.UseGrammar.MatchTerm where
+
+import GF.Data.Operations
+import GF.Data.Zipper
+
+import GF.Grammar.Grammar
+import GF.Grammar.PrGrammar
+import GF.Infra.Ident
+import GF.Grammar.Values
+import GF.Grammar.Macros
+import GF.Grammar.MMacros
+
+import Control.Monad
+import Data.List
+
+-- test if a term has duplicated idents, either any or just atoms
+
+hasDupIdent, hasDupAtom :: Exp -> Bool
+hasDupIdent = (>1) . maximum . map length . group . sort . allConstants True
+hasDupAtom = (>1) . maximum . map length . group . sort . allConstants False
+
+-- test if a certain ident occurs in term
+
+grepIdent :: Ident -> Exp -> Bool
+grepIdent c = elem c . allConstants True
+
+-- form the list of all constants, optionally ignoring all but atoms
+
+allConstants :: Bool -> Exp -> [Ident]
+allConstants alsoApp = err (const []) snd . flip appSTM [] . collect where
+ collect e = case e of
+ Q _ c -> add c e
+ QC _ c -> add c e
+ Cn c -> add c e
+ App f a | not alsoApp -> case f of
+ App g b -> collect b >> collect a
+ _ -> collect a
+ _ -> composOp collect e
+ add c e = updateSTM (c:) >> return e