Yes.  In Haskell, types are your friends.  You should define new types liberally.

I think the usual approach is to create newtype wrapper:

> module EmailAddr (EmailAddr, mkEmailAddr) where
>
> newtype EmailAddr = EmailAddr String
>
> mkEmailAddr :: String -> Maybe EmailAddr
> mkEmailAddr str = if isEmailAddr then Just (EmailAddr str) else Nothing

The only way to make an EmailAddr is via the mkEmailAddr function, which checks that the string is actually a valid address (implementation omitted).  Therefore, there's a guarantee that any EmailAddr is actually a well-formed email address, and any functions that operate on an EmailAddr can rely upon this.

Of course, it might be useful to create an actual algebraic type instead:

> data EmailAddr = EmailAddr { address :: String, domain :: String }

(and the domain could similarly be an algebraic type instead of a plain string)

In general, you should be working with types that closely reflect the domain you're working in.  This will make your functions more clear, and the compiler/type checker will be able to provide more help during development.

John L.


On Sat, Mar 1, 2014 at 4:35 AM, KwangYul Seo <kwangyul.seo@gmail.com> wrote:
In Java, the Checker Framework (http://types.cs.washington.edu/checker-framework/) provides a way to type check string literals. For example, Java signatures type system differentiates strings literals in different forms:

1. Unqualified strings : 
"Hello, world!" -> @Unqualified String

2. Fully qualified names: 
"package.Outer.Inner" -> @FullyQualifiedString String

3. Binary names: 
"package.Outer$Inner" -> @BinaryName String

4. Field descriptors: 
"Lpackage/Outer$Inner;" -> @FieldDescriptor String

It can do the similar checks with regular expressions or SQL statements.

Is it possible to type check string literals in Haskell? I think it would be nice if we can check if a given string literal is a valid URL or an email address at compile time.

Regards,
Kwang Yul Seo


_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe