[Haskell-cafe] Proposal: Sum type branches as extended types (as Type!Constructor)

Gabriel Riba griba2001 at gmail.com
Thu Jun 3 11:14:22 EDT 2010


Extending sum types with data constructors would spare runtime errors or
exception control, 

when applying functions to inappropriate branches, as in the example ...

   data List a = Nil | Cons a (List a)  -- List!Nil and List!Cons 
                                        -- as extended types


* Actual system, with runtime errors (as in GHC Data.List head) or 
exception throwing

   hd :: List a -> a
   hd (Cons x _) -> x
   hd Nil -> error "error: hd: empty list" -- error or exception throwing


* Proposed system extending types with constructors as Type!Constructor:

User must do pattern matching before applying the constructor-specific 
type function.

In ''var @ (Constructor _ _)'' the compiler should append the constructor
to the type as a pair (Type, Constructor) as an extended type for ''var'' 

No need for runtime errors or exception control

   hd :: List!Cons a -> a

   hd (Cons x _) = x

using it:

   headOf :: List a -> Maybe a

   headOf list = case list of

        li @ (Cons _ _) -> Just hd li  -- extTypeOf li == ( 'List', 'Cons')
                                  -- should pass typechecker for List!Cons

        li @ Nil  -> Just hd li     -- compiler error !! 
                                -- extTypeOf ('List','Nil') don't match

        _ -> Just hd list           -- compiler error !! 
                                -- extTypeOf ('List',Nothing) don't match


Maybe we could take out importance on the number of _ wildcards (constructor
arity) with a syntax like.
         li @ (Cons ...)
         li @ (Nil ...)


Cheers!

Gabriel Riba Faura.




More information about the Haskell-Cafe mailing list