1-----------------------------------------------------------------------------
3-- Module : Language.PureScript.CaseDeclarations
4-- Copyright : (c) Phil Freeman 2013
7-- Maintainer : Phil Freeman <paf31@cantab.net>
8-- Stability : experimental
13-----------------------------------------------------------------------------
15module Language.PureScript.CaseDeclarations (
19import Data.List (groupBy)
20import Control.Monad (join, unless)
21import Control.Monad.Error.Class
22import Language.PureScript.Names
23import Language.PureScript.Values
24import Language.PureScript.Declarations
26desugarCases :: [Declaration] -> Either String [Declaration]
27desugarCases = fmap join . mapM toDecls . groupBy inSameGroup
29inSameGroup :: Declaration -> Declaration -> Bool
30inSameGroup (ValueDeclaration ident1 _ _ _) (ValueDeclaration ident2 _ _ _) = ident1 == ident2
31inSameGroup _ _ = False
33toDecls :: [Declaration] -> Either String [Declaration]
34toDecls d@[ValueDeclaration _ [] Nothing _] = return d
35toDecls ds@(ValueDeclaration ident bs _ _ : _) = do
36 let tuples = map toTuple ds
37 unless (all ((== map length bs) . map length . fst) tuples) $
38 throwError $ "Argument list lengths differ in declaration " ++ show ident
39 return [makeCaseDeclaration ident tuples]
42toTuple :: Declaration -> ([[Binder]], (Maybe Guard, Value))
43toTuple (ValueDeclaration _ bs g val) = (bs, (g, val))
44toTuple _ = error "Not a value declaration"
46makeCaseDeclaration :: Ident -> [([[Binder]], (Maybe Guard, Value))] -> Declaration
47makeCaseDeclaration ident alternatives =
49 argPattern = map length . fst . head $ alternatives
50 args = map (map (('_' :) . show)) $ toArgs argPattern
51 vars = concatMap (map (\arg -> Var (Qualified global (Ident arg)))) args
52 binders = [ (join bs, g, val) | (bs, (g, val)) <- alternatives ]
53 value = foldr (\args' ret -> Abs (map Ident args') ret) (Case vars binders) args
55 ValueDeclaration ident [] Nothing value
57toArgs :: [Int] -> [[Int]]
61 go start (n:ns) = [start..start + n - 1] : go (start + n) ns
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list