From a8c9802f5dd2575aa99274d8237174cd5454ed07 Mon Sep 17 00:00:00 2001 From: Neil Brown Date: Tue, 7 Apr 2009 15:52:06 +0000 Subject: [PATCH] Changed the occam type checker to handle checking user defined operators better They are now recursed into with no type context, then afterwards the type is deduced. This seems to be how they were meant to work, and is also much faster than what I was doing. --- frontends/OccamTypes.hs | 35 ++++++++++++++--------------------- 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/frontends/OccamTypes.hs b/frontends/OccamTypes.hs index bfdb505..0f36d1e 100644 --- a/frontends/OccamTypes.hs +++ b/frontends/OccamTypes.hs @@ -719,36 +719,29 @@ inferTypes = occamOnlyPass "Infer types" 1 -> "unary" 2 -> "binary" n -> show n ++ "-ary" - + + es' <- noTypeContext $ mapM recurse es + tes <- sequence [astTypeOf e `catchError` (const $ return A.Infer) | e <- es'] + cs <- getCompState -- The nubBy will ensure that only one definition remains for each -- set of type-arguments, and will keep the first definition in the -- list (which will be the most recent) - possibles <- sequence - [ (do es' <- sequence - [do e' <- doActual m direct t e - checkActual (A.Formal A.ValAbbrev t (A.Name m "x")) - (A.ActualExpression e') - return e' - | (t, e) <- zip ts es] - return $ Right ((opFuncName, es'), ts) - ) `catchError` (return . Left) + possibles <- return + [ ((opFuncName, es'), ts) | (raw, opFuncName, ts) <- nubBy ((==) `on` (\(op,_,ts) -> (op,ts))) $ csOperators cs -- Must be right operator: , raw == A.nameName n -- Must be right arity: - , length ts == length es] - case splitEither possibles of - -- We want to be helpful and give the user an idea - -- of what we thought the types were, but we must - -- also be careful not to die while getting the - -- types (and thus missing the real error!) - (errs,[]) -> do tes <- sequence [astTypeOf e `catchError` (const $ return A.Infer) | e <- es] - diePC m $ formatCode ("No matching " ++ opDescrip ++ " operator definition found for types: %" - ++ " errors were: " ++ show errs) tes - (_, [poss]) -> return $ fst poss - (_, posss) -> dieP m $ "Ambigious " ++ opDescrip ++ " operator, matches definitions: " + , length ts == length es + -- Must have right types: + , ts == tes + ] + case possibles of + [] -> diePC m $ formatCode ("No matching " ++ opDescrip ++ " operator definition found for types: %") tes + [poss] -> return $ fst poss + posss -> dieP m $ "Ambigious " ++ opDescrip ++ " operator, matches definitions: " ++ show (map (transformPair (A.nameMeta . fst) showOccam) posss) else do (_, fs) <- checkFunction m n