@@ -140,9 +140,13 @@ elab ist info pattern tcgen fn tm
140140 pimp (MN 0 "P") Placeholder,
141141 pexp l, pexp r])
142142 elab' ina (PAlternative True as)
143− = let as' = pruneAlt as in
144− try (tryAll (zip (map (elab' ina) as') (map showHd as')))
145− (tryAll (zip (map (elab' ina) as) (map showHd as)))
143+ = do ty <- goal
144+ ctxt <- get_context
145+ let (tc, _) = unApply ty
146+ let as' = pruneByType tc ctxt as
147+ let as'' = as' -- pruneAlt as'
148+ try (tryAll (zip (map (elab' ina) as'') (map showHd as'')))
149+ (tryAll (zip (map (elab' ina) as') (map showHd as')))
146150 where showHd (PApp _ h _) = show h
147151 showHd x = show x
148152 elab' ina (PAlternative False as)
@@ -356,6 +360,29 @@ pruneAlt xs = map prune xs
356360 headIs f (PApp _ f' _) = headIs f f'
357361 headIs f _ = True -- keep if it's not an application
358362
363+-- Rule out alternatives that don't return the same type as the head of the goal
364+-- (If there are none left as a result, do nothing)
365+
366+pruneByType :: Term -> Context -> [PTerm] -> [PTerm]
367+pruneByType (P _ n _) c as
368+ = let as' = filter (headIs n) as in
369+ case as' of
370+ [] -> as
371+ _ -> as'
372+ where
373+ headIs f (PApp _ (PRef _ f') _) = typeHead f f'
374+ headIs f (PApp _ f' _) = headIs f f'
375+ headIs f (PPi _ _ _ sc) = headIs f sc
376+ headIs _ _ = True -- keep if it's not an application
377+
378+ typeHead f f' = case lookupTy Nothing f' c of
379+ [ty] -> case unApply (getRetTy ty) of
380+ (P _ ftyn _, _) -> ftyn == f
381+ _ -> False
382+ _ -> False
383+
384+pruneByType t _ as = as
385+
359386trivial :: IState -> ElabD ()
360387trivial ist = try (do elab ist toplevel False False (MN 0 "tac") (PRefl (FC "prf" 0))
361388 return ())