Created
November 26, 2012 13:05
-
-
Save dyokomizo/4148099 to your computer and use it in GitHub Desktop.
Glob in Haskell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE EmptyDataDecls, GADTs #-} | |
module Glob where | |
data L -- Witness for globs that begin/end with a literal | |
data S -- Witness for globs that begin/end with a star | |
data Glob a b e where -- Globs over [a] that begin with b and end with e | |
L :: [a] -> Glob a L L -- Literal glob | |
S :: Glob a S S -- Star glob | |
(:>) :: Glob a b S -> Glob a L e -> Glob a b e -- Concatenation of a glob that ends with a star and a glob that starts with a literal | |
(:<) :: Glob a b L -> Glob a S e -> Glob a b e -- Concatenation of a glob that ends with a literal and a glob that starts with a star | |
infixl 0 :>, :< | |
instance Eq a => Eq (Glob a b e) where | |
L as == L bs = as == bs | |
S == S = True | |
(gs1 :> gl1) == (gs2 :> gl2) = gs1 == gs2 && gl1 == gl2 | |
(gs1 :< gl1) == (gs2 :< gl2) = gs1 == gs2 && gl1 == gl2 | |
instance Show a => Show (Glob a b e) where | |
show (L ls) = show ls | |
show S = "*" | |
show (gs1 :> gl1) = show gs1 ++ " " ++ show gl1 | |
show (gl1 :< gs1) = show gl1 ++ " " ++ show gs1 | |
-- to be continued... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample GHC output: