Haskell: How to apply partial order to a substring function?

178 views Asked by At

I have implemented the class POrd and a substring function, which takes 2 strings and determines if the first string is a substring of the second input string.

Now I want to apply the POrd class to the substring function to make it so that the substrings can be partially ordered.

However I am stuck on how to correctly apply the partial order class (POrd) to the substring function.

 class Eq a => POrd a where

     pocompare :: a -> a -> Maybe Ordering
     (~<), (~>), (~<=), (~>=) :: a -> a -> Bool

       -- Minimal complete definition: (~<=) | pocompare
 
     pocompare x y | x == y       = Just EQ
                  | x ~<= y      = Just LT
                  | y ~<= x      = Just GT
                  | otherwise    = Nothing

     x ~<= y = pocompare x y ==  Just LT
     x ~<  y = x ~<= y && x /= y
     x ~>= y = y ~<= x
     x ~>  y = x ~>= y && x /= y

 substring :: Eq a => [a] -> [a] -> Bool
 substring []_= True
 substring _[]     = False
 substring p@(x:xs) (y:ys) = x == y && prefix xs ys || substring p ys
  where
    prefix []     _= True
    prefix _[]     = False
    prefix (a:as) (b:bs) = a == b && prefix as bs
 instance POrd a => POrd [a] where
  (~<=) = substring

  xs ~<= ys = substring xs ys
  xs ~>= ys = substring ys xs
0

There are 0 answers