analyse operation declarations
anaAttr :: GlobalAnnos -> TypeScheme -> OpAttr -> State Env (Maybe OpAttr)
anaAttr ga (TypeScheme tvs (_ :=> ty) _) (UnitOpAttr trm ps) =
FunType (ProductType [t1, t2] _) _ t3 _ ->
FunType t1 _ (FunType t2 _ t3 _) _ ->
mapM_ (addTypeVarDecl False) tvs
Nothing -> do addDiags [mkDiag Error
"unexpected type of operation" ty]
mt <- resolveTerm ga Nothing trm
Nothing -> return Nothing
Just t -> return $ Just $ UnitOpAttr t ps
do if t1 == t2 && t2 == t3 then
else addDiags [mkDiag Error
"unexpected type of operation" ty]
mt <- resolveTerm ga (Just t3) trm
case mt of Nothing -> return Nothing
Just t -> return $ Just $ UnitOpAttr t ps
anaAttr _ _ b = return $ Just b
filterVars :: Assumps -> Assumps
filterVars = filterAssumps (not . isVarDefn)
patternsToType :: [[VarDecl]] -> Type -> Type
patternsToType (p: ps) t = FunType (tuplePatternToType p) PFunArr
tuplePatternToType :: [VarDecl] -> Type
tuplePatternToType vds = mkProductType (map ( \ (VarDecl _ t _ _) -> t) vds) []
anaOpItem :: GlobalAnnos -> OpBrand -> OpItem -> State Env OpItem
anaOpItem ga br ods@(OpDecl is sc attr ps) =
do mSc <- anaTypeScheme sc
mAttrs <- mapM (anaAttr ga nSc) attr
us <- mapM (anaOpId br nSc attr) is
return $ OpDecl (catMaybes us) nSc (catMaybes mAttrs) ps
anaOpItem ga br (OpDefn o oldPats sc partial trm ps) =
do let (op@(OpId i _ _), extSc) = getUninstOpId sc o
mSc <- anaTypeScheme extSc
checkUniqueVars $ concat oldPats
putAssumps $ filterVars as
mPats <- mapM (mapM anaVarDecl) oldPats
let newPats = map catMaybes mPats
monoPats = map (map makeMonomorph) newPats
toQualVar (VarDecl v t _ qs) = QualVar v t qs
pats = map (\ l -> mkTupleTerm (map toQualVar l) []) monoPats
Just newSc@(TypeScheme tArgs (qu :=> scTy) qs) -> do
ty <- toEnvState $ freshInst newSc
mapM (mapM addVarDecl) monoPats
mt <- resolveTerm ga (Just ty) trm
Nothing -> return $ OpDefn op newPats
let lastSc = TypeScheme tArgs
(qu :=> patternsToType newPats scTy) qs
lamTrm = case (pats, partial) of
_ -> LambdaTerm pats partial
addOpId i lastSc [] $ Definition br lamTrm
return $ OpDefn op [] lastSc
mt <- resolveTerm ga Nothing trm
return $ OpDefn op newPats extSc partial
(case mt of Nothing -> trm
getUninstOpId :: TypeScheme -> OpId -> (OpId, TypeScheme)
getUninstOpId (TypeScheme tvs q ps) (OpId i args qs) =
(OpId i [] qs, TypeScheme (args ++ tvs) q ps)
anaOpId :: OpBrand -> TypeScheme -> [OpAttr] -> OpId -> State Env (Maybe OpId)
do let (OpId i _ _, newSc) = getUninstOpId sc o
mo <- addOpId i newSc attrs $ NoOpDefn br
return $ fmap (const o) mo
-- ----------------------------------------------------------------------------
-- ----------------------------------------------------------------------------
anaProgEq :: GlobalAnnos -> ProgEq -> State Env ProgEq
anaProgEq ga pe@(ProgEq pat trm qs) =
putAssumps $ filterVars as
mp <- checkPattern ga pat
(newPat, exbs) <- extractBindings np
mt <- resolveTerm ga Nothing trm
let (topPat, args) = getApplConstr newPat
defTrm = if null args then newTerm
else LambdaTerm (reverse args)
QualOp _ (InstOpId i _tys _) sc _ -> do
addOpId i sc [] $ Definition Op defTrm
return $ ProgEq newPat newTerm qs
_ -> do addDiags $ [mkDiag Error
"illegal toplevel pattern"
_ -> return $ ProgEq newPat trm qs
getApplConstr :: Pattern -> (Pattern, [Pattern])
let (tp, args) = getApplConstr p1 in (tp, p2:args)
TypedTerm tp _ _ _ -> getApplConstr tp