on 796fa25 help sign in
Idris-dev/src/Core/CaseTree.hs
190 lines 6.8 kB history raw
1module Core.CaseTree(CaseDef(..), SC(..), CaseAlt(..), CaseTree,
2 simpleCase) where
4import Core.TT
5-- import Core.Evaluate
7import Control.Monad.State
9data CaseDef = CaseDef [Name] SC
10 deriving Show
12data SC = Case Name [CaseAlt]
13 | STerm Term
14 | UnmatchedCase String -- error message
15 deriving Show
17data CaseAlt = ConCase Name Int [Name] SC
18 | ConstCase Const SC
19 | DefaultCase SC
20 deriving Show
22type CaseTree = SC
23type Clause = ([Pat], Term)
24type CS = Int
26simpleCase :: [(Term, Term)] -> CaseDef
27simpleCase [] = CaseDef [] (UnmatchedCase "No pattern clauses")
28simpleCase cs = let pats = map (\ (l, r) -> (toPats l, r)) cs
29 numargs = length (fst (head pats))
30 ns = take numargs args in
31 CaseDef ns (evalState (match ns pats (UnmatchedCase "Error")) numargs)
32 where args = map (\i -> MN i "e") [0..]
34data Pat = PCon Name Int [Pat]
35 | PConst Const
36 | PV Name
37 | PAny
38 deriving Show
40-- If there are repeated variables, take the *last* one (could be name shadowing
41-- in a where clause, so take the most recent).
43toPats :: Term -> [Pat]
44toPats f = reverse (toPat (getArgs f)) where
45 getArgs (App f a) = a : getArgs f
46 getArgs _ = []
48toPat :: [Term] -> [Pat]
49toPat tms = evalState (mapM (\x -> toPat' x []) tms) []
50 where
51 toPat' (P (DCon t a) n _) args = return $ PCon n t args
52 toPat' (P Bound n _) [] = do ns <- get
53 if n `elem` ns
54 then return PAny
55 else do put (n : ns)
56 return (PV n)
57 toPat' (App f a) args = do a' <- toPat' a []
58 toPat' f (a' : args)
59 toPat' (Constant c) args = return $ PConst c
60 toPat' _ _ = return PAny
62data Partition = Cons [Clause]
63 | Vars [Clause]
65isVarPat (PV _ : ps , _) = True
66isVarPat (PAny : ps , _) = True
67isVarPat _ = False
69isConPat (PCon _ _ _ : ps, _) = True
70isConPat (PConst _ : ps, _) = True
71isConPat _ = False
73partition :: [Clause] -> [Partition]
74partition [] = []
75partition ms@(m : _)
76 | isVarPat m = let (vars, rest) = span isVarPat ms in
77 Vars vars : partition rest
78 | isConPat m = let (cons, rest) = span isConPat ms in
79 Cons cons : partition rest
80partition xs = error $ "Partition " ++ show xs
82match :: [Name] -> [Clause] -> SC -- error case
83 -> State CS SC
84match [] (([], ret) : _) err = return $ STerm ret -- run out of arguments
85match vs cs err = mixture vs (partition cs) err
87mixture :: [Name] -> [Partition] -> SC -> State CS SC
88mixture vs [] err = return err
89mixture vs (Cons ms : ps) err = do fallthrough <- mixture vs ps err
90 conRule vs ms fallthrough
91mixture vs (Vars ms : ps) err = do fallthrough <- mixture vs ps err
92 varRule vs ms fallthrough
94data ConType = CName Name Int -- named constructor
95 | CConst Const -- constant, not implemented yet
97data Group = ConGroup ConType -- Constructor
98 [([Pat], Clause)] -- arguments and rest of alternative
100conRule :: [Name] -> [Clause] -> SC -> State CS SC
101conRule (v:vs) cs err = do groups <- groupCons cs
102 caseGroups (v:vs) groups err
104caseGroups :: [Name] -> [Group] -> SC -> State CS SC
105caseGroups (v:vs) gs err = do g <- altGroups gs
106 return $ Case v g
107 where
108 altGroups [] = return [DefaultCase err]
109 altGroups (ConGroup (CName n i) args : cs)
110 = do g <- altGroup n i args
111 rest <- altGroups cs
112 return (g : rest)
113 altGroups (ConGroup (CConst c) args : cs)
114 = do g <- altConstGroup c args
115 rest <- altGroups cs
116 return (g : rest)
118 altGroup n i gs = do (newArgs, nextCs) <- argsToAlt gs
119 matchCs <- match (newArgs ++ vs) nextCs err
120 return $ ConCase n i newArgs matchCs
121 altConstGroup n gs = do (_, nextCs) <- argsToAlt gs
122 matchCs <- match vs nextCs err
123 return $ ConstCase n matchCs
125argsToAlt :: [([Pat], Clause)] -> State CS ([Name], [Clause])
126argsToAlt [] = return ([], [])
127argsToAlt rs@((r, m) : _)
128 = do newArgs <- getNewVars r
129 return (newArgs, addRs rs)
130 where
131 getNewVars [] = return []
132 getNewVars ((PV n) : ns) = do nsv <- getNewVars ns
133 return (n : nsv)
134 getNewVars (_ : ns) = do v <- getVar
135 nsv <- getNewVars ns
136 return (v : nsv)
137 addRs [] = []
138 addRs ((r, (ps, res)) : rs) = ((r++ps, res) : addRs rs)
140getVar :: State CS Name
141getVar = do v <- get; put (v+1); return (MN v "e")
143groupCons :: [Clause] -> State CS [Group]
144groupCons cs = gc [] cs
145 where
146 gc acc [] = return acc
147 gc acc ((p : ps, res) : cs) =
148 do acc' <- addGroup p ps res acc
149 gc acc' cs
150 addGroup p ps res acc = case p of
151 PCon con i args -> return $ addg con i args (ps, res) acc
152 PConst cval -> return $ addConG cval (ps, res) acc
153 pat -> fail $ show pat ++ " is not a constructor or constant (can't happen)"
155 addg con i conargs res [] = [ConGroup (CName con i) [(conargs, res)]]
156 addg con i conargs res (g@(ConGroup (CName n j) cs):gs)
157 | i == j = ConGroup (CName n i) (cs ++ [(conargs, res)]) : gs
158 | otherwise = g : addg con i conargs res gs
160 addConG con res [] = [ConGroup (CConst con) [([], res)]]
161 addConG con res (g@(ConGroup (CConst n) cs) : gs)
162 | con == n = ConGroup (CConst n) (cs ++ [([], res)]) : gs
163 | otherwise = g : addConG con res gs
165varRule :: [Name] -> [Clause] -> SC -> State CS SC
166varRule (v : vs) alts err =
167 do let alts' = map (repVar v) alts
168 match vs alts' err
169 where
170 repVar v (PV p : ps , res) = (ps, subst p (P Bound v undefined) res)
171 repVar v (PAny : ps , res) = (ps, res)
173{-
174mkOperator :: CaseDef -> [Value] -> Maybe Value
175mkOperator (CaseDef ns casetree) args
176 | length args /= length ns = Nothing
177 | otherwise = evalArgs (zip ns args) casetree
178 where
179 evalArgs map (Case n alts) =
180 do v <- lookup n map
181 (altmap, tm) <- findAlt (getValArgs v) alts map
182 evalArgs altmap tm
183 evalArgs map (STerm tm) =
184 evalArgs map (UnmatchedCase _) = Nothing
186 getValArgs tm = getValArgs' tm []
187 getValArgs' (VApp f a) as = getValArgs' f (a:as)
188 getValArgs' f as = (f, as)
190-}