Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell type and pattern matching question: extracting fields from a data type

I'm new to Haskell and working my way through the Write Yourself a Scheme in 48 Hours project and I came upon an instance where I wanted to get the underlying type out of a data type and I'm not sure how to do it without writing conversions for each variant in the type. For example, in the data type

data LispVal = Atom String
             | List [LispVal]
             | DottedList [LispVal] LispVal
             | Number Integer
             | String String
             | Bool Bool
             | Double Double

I want to write something like: (I know this doesn't work)

extractLispVal :: LispVal -> a
extractLispVal (a val) = val

or even

extractLispVal :: LispVal -> a
extractLispVal (Double val) = val
extractLispVal (Bool val) = val

Is it possible to do this? Basically I want to be able to cast back out of the LispVal if I need to use the basic type.

Thanks! Simon

like image 670
Simon Avatar asked May 18 '11 15:05

Simon


Video Answer


2 Answers

Unfortunately this sort of generic matching on constructors isn't possible directly, but even if it was yours wouldn't work--the extractLispVal function doesn't have a well-defined type, because the type of the result depends on the value of the input. There are various kinds of advanced type-system nonsense that can do things sort of like this, but they're not really something you'd want to use here anyway.

In your case, if you're only interested in extracting particular kinds of values, or if you can convert them to a single type, you can write a function like extractStringsAndAtoms :: LispVal -> Maybe String, for instance.

The only way to return one of several possible types is by combining them into a data type and pattern matching on that--the generic form of this being Either a b, which is either a or b distinguished by constructors. You could create a data type that would permit all possible types to extract... and it would be pretty much the same as LispVal itself, so that's not helpful.

If you really want to work with various types outside of a LispVal you could also look at the Data.Data module, which provides some means for reflection on data types. I doubt that's really what you want, though.


EDIT: Just to expand on things a bit, here's a few examples of extraction functions you can write:

  • Create single-constructor extraction functions, as in Don's first example, that assume you already know which constructor was used:

    extractAtom :: LispVal -> String
    extractAtom (Atom a) = a
    

    This will produce runtime errors if applied to something other than the Atom constructor, so be cautious with that. In many cases, though, you know by virtue of being at some point in an algorithm what you've got, so this can be used safely. A simple example would be if you've got a list of LispVals that you've filtered every other constructor out of.

  • Create safe single-constructor extraction functions, which serve as both a "do I have this constructor?" predicate and an "if so, give me the contents" extractor:

    extractAtom :: LispVal -> Maybe String
    extractAtom (Atom a) = Just a
    extractAtom _ = Nothing
    

    Note that this is more flexible than the above, even if you're confident of what constructor you have. For example, it makes defining these easy:

    isAtom :: LispVal -> Bool
    isAtom = isJust . extractAtom
    
    assumeAtom :: LispVal -> String
    assumeAtom x = case extractAtom x of 
                       Just a  -> a
                       Nothing -> error $ "assumeAtom applied to " ++ show x
    
  • Use record syntax when defining the type, as in Don's second example. This is a bit of language magic, for the most part, defines a bunch of partial functions like the first extractAtom above and gives you a fancy syntax for constructing values. You can also reuse names if the result is the same type, e.g. for Atom and String.

    That said, the fancy syntax is more useful for records with many fields, not types with many single-field constructors, and the safe extraction functions above are generally better than ones that produce errors.

  • Getting more abstract, sometimes the most convenient way is actually to have a single, all-purpose deconstruction function:

    extractLispVal :: (String -> r) -> ([LispVal] -> r) -> ([LispVal] -> LispVal -> r) 
                   -> (Integer -> r) -> (String -> r) -> (Bool -> r) -> (Double -> r)
                   -> LispVal -> r
    extractLispVal f _ _ _ _ _ _ (Atom x) = f x
    extractLispVal _ f _ _ _ _ _ (List xs) = f xs
    ...
    

    Yeah, it looks horrendous, I know. An example of this (on a simpler data type) in the standard libraries are the functions maybe and either, which deconstruct the types of the same names. Essentially, this is a function that reifies the pattern matching and lets you work with that more directly. It may be ugly, but you only have to write it once, and it can be useful in some situations. For instance, here's one thing you could do with the above function:

    exprToString :: ([String] -> String) -> ([String] -> String -> String) 
                 -> LispVal -> String
    exprToString f g = extractLispVal id (f . map recur) 
                                      (\xs x -> g (map recur xs) $ recur x)
                                      show show show show
      where recur = exprToString f g
    

    ...i.e., A simple recursive pretty-printing function, parameterized by how to combine the elements of a list. You can also write isAtom and the like easily:

    isAtom = extractLispVal (const True) no (const no) no no no no
      where no = const False
    
  • On the other hand, sometimes what you want to do is match one or two constructors, with nested pattern matches, and a catch-all case for the constructors you don't care about. This is exactly what pattern matching is best at, and all the above techniques would just make things far more complicated. So don't tie yourself to just one approach!

like image 192
C. A. McCann Avatar answered Sep 28 '22 11:09

C. A. McCann


You can always extract the fields from your data type, either by pattern matching on individual constructors:

extractLispValDouble (Double val) = val

or using a record selector:

data LispVal = Atom { getAtom :: String }
             ...          
             | String { getString :: String }
             | Bool   { getBool :: Bool }
             | Double { getDouble :: Double }

However, you can't write a function that returns a String or a Bool or a Double (and whatever else) naively, since you can't write a type for that.

like image 37
Don Stewart Avatar answered Sep 28 '22 13:09

Don Stewart