Bloody Haskell: it's like a scab I can't stop picking at.
Prompted by something on Reddit, I started thinking about libraries to pluralize (English) words, like the table-name generator in Ruby on Rails or Damian Conway's (rather more complete-looking) Lingua::EN::Inflect for Perl. Such things seem to be called "inflectors". My question is, what would be the idiomatic interface for a Haskell inflector? The obvious thing would be something like
Prompted by something on Reddit, I started thinking about libraries to pluralize (English) words, like the table-name generator in Ruby on Rails or Damian Conway's (rather more complete-looking) Lingua::EN::Inflect for Perl. Such things seem to be called "inflectors". My question is, what would be the idiomatic interface for a Haskell inflector? The obvious thing would be something like
pluralize :: Inflector -> String -> Stringwhere
Inflector
is some sort of data structure holding configuration data: classical versus modern plurals, any custom cases, etc. But following on from our earlier discussion of high- and low-level type-checking, it occurred to me that there's a constraint here: only singular nouns should be pluralized. So should we then havenewtype Singular = String newtype Plural = String pluralize :: Inflector -> Singular -> Plural? Or even
module Inflect (makeSingular, pluralize) data Singular = Singular String data Plural = Plural String makeSingular :: String -> Singular -- error-checking code goes here, to check that the string passed -- is a single word, or something pluralize :: Inflector -> Singular -> Pluralso we don't export the constructors for Singular or Plural, ensuring that one can only construct them using our interface? But wouldn't this make client code far uglier than necessary? Should one then provide both options, tagging one as unsafe?
module Inflect (makeSingular, pluralize, unsafePluralize) data Singular = Singular String data Plural = Plural String makeSingular :: String -> Singular -- error-checking code goes here unsafePluralize ::Inflector -> String -> String pluralize :: Inflector -> Singular -> Plural pluralize i (Singular s) = Plural (unsafePluralize i s)Thoughts?
Tags: