Skip to content

Instantly share code, notes, and snippets.

@dyokomizo
Created November 26, 2012 13:05
Show Gist options
  • Save dyokomizo/4148099 to your computer and use it in GitHub Desktop.
Save dyokomizo/4148099 to your computer and use it in GitHub Desktop.
Glob in Haskell
{-# 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...
@dyokomizo
Copy link
Author

Sample GHC output:

*Glob> L "foo" :< S :> L "bar" :< S :> L "baz"
"foo" * "bar" * "baz"
*Glob> :t L "foo" :< S :> L "bar" :< S :> L "baz"
L "foo" :< S :> L "bar" :< S :> L "baz" :: Glob Char L L
*Glob> :t L "foo" :< S :> L "bar" :< S 
L "foo" :< S :> L "bar" :< S :: Glob Char L S

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment