summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorhallgren <hallgren@chalmers.se>2013-02-28 15:13:20 +0000
committerhallgren <hallgren@chalmers.se>2013-02-28 15:13:20 +0000
commit95d77e3c37fa97f770d845dd0d05890455f8119e (patch)
treeea9507b9e73fa0c18d8accc0ce6923340e494e21 /src
parent0feb386691bb82e13c3dcc01e27ae33d8865f2ca (diff)
pattern match length estimation code simplication
Diffstat (limited to 'src')
-rw-r--r--src/compiler/GF/Grammar/PatternMatch.hs35
1 files changed, 15 insertions, 20 deletions
diff --git a/src/compiler/GF/Grammar/PatternMatch.hs b/src/compiler/GF/Grammar/PatternMatch.hs
index 8ea388f76..e1b5f904f 100644
--- a/src/compiler/GF/Grammar/PatternMatch.hs
+++ b/src/compiler/GF/Grammar/PatternMatch.hs
@@ -144,30 +144,25 @@ matchPSeq' b1@(min1,max1) p1 b2@(min2,max2) p2 s =
return (concat matches)
-- | Estimate the minimal length of the string that a pattern will match
-minLength p =
- case p of
- PString s -> length s
- PSeq p1 p2 -> minLength p1+minLength p2
- PAlt p1 p2 -> min (minLength p1) (minLength p2)
- PChar -> 1
- PChars _ -> 1
- PAs x p' -> minLength p'
- PT t p' -> minLength p'
- _ -> 0 -- safe underestimate
+minLength = matchLength 0 id (+) min -- safe underestimate
-- | Estimate the maximal length of the string that a pattern will match
-maxLength = maybe maxBound id . maxl -- safe overestimate
+maxLength =
+ maybe maxBound id . matchLength Nothing Just (liftM2 (+)) (liftM2 max)
+ -- safe overestimate
+
+matchLength unknown known seq alt = len
where
- maxl p =
+ len p =
case p of
- PString s -> Just (length s)
- PSeq p1 p2 -> liftM2 (+) (maxl p1) (maxl p2)
- PAlt p1 p2 -> liftM2 max (maxl p1) (maxl p2)
- PChar -> Just 1
- PChars _ -> Just 1
- PAs x p' -> maxl p'
- PT t p' -> maxl p'
- _ -> Nothing -- unknown length
+ PString s -> known (length s)
+ PSeq p1 p2 -> seq (len p1) (len p2)
+ PAlt p1 p2 -> alt (len p1) (len p2)
+ PChar -> known 1
+ PChars _ -> known 1
+ PAs x p' -> len p'
+ PT t p' -> len p'
+ _ -> unknown
lengthBounds p = (minLength p,maxLength p)