1 {-# LANGUAGE MultiParamTypeClasses, FlexibleInstances #-} 2 3 module Text.RegExp.Matching.Leftmost.Type where 4 5 import Data.Semiring 6 import Text.RegExp.Data 7 8 -- | Semiring used for leftmost matching. 9 -- 10 data Leftmost = Zero | One | Leftmost !Int 11 deriving (Eq,Show) 12 13 instance Semiring Leftmost where 14 zero = Zero; one = One 15 16 Zero .+. y = y 17 x .+. Zero = x 18 One .+. y = y 19 x .+. One = x 20 Leftmost a .+. Leftmost b = Leftmost (min a b) 21 22 Zero .*. _ = Zero 23 _ .*. Zero = Zero 24 One .*. y = y 25 x .*. One = x 26 Leftmost a .*. Leftmost b = Leftmost (min a b) 27 28 instance Weight c (Int,c) Leftmost where 29 symWeight p (n,c) = p c .*. Leftmost n