Type checking string literal in Haskell?

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

On Sat, 1 Mar 2014 21:35:09 +0900, KwangYul Seo
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 Non-text part: text/html _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
I think QuasiQuoters were designed for pretty much this purpose. They're basically functions from Strings to expressions (or types/declarations/patterns, depending on the context). A quasiquoter like [url|http://example.com] could parse the string passed to it ("http://example.com") at compile time and return an expression of type URL (or throw an error or something else), or maybe even some other data structure that will end up being useful.

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
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

On 3/03/2014, at 4:59 PM, John Lato wrote:
Yes. In Haskell, types are your friends. You should define new types liberally.
I believe the original poster was asking about a *static* check. Newtypes with "smart constructors" are a good way to plug in a *dynamic* check, which is valuable but different.

Ah yes, I'd missed the "string literal" part. In that case, I agree with
Niklas that quasi-quoters seem the best option. Although, they require
pretty much the same framework to operate (a custom data type and parser),
so once you have a quasi-quoter it's usually easy to implement dynamic
checks with the same tooling.
On Mon, Mar 3, 2014 at 12:29 PM, Richard A. O'Keefe
On 3/03/2014, at 4:59 PM, John Lato wrote:
Yes. In Haskell, types are your friends. You should define new types liberally.
I believe the original poster was asking about a *static* check.
Newtypes with "smart constructors" are a good way to plug in a *dynamic* check, which is valuable but different.
participants (4)
-
John Lato
-
KwangYul Seo
-
Niklas Haas
-
Richard A. O'Keefe