From 4f162814483aa0ab4e11cab51e23f807bece906c Mon Sep 17 00:00:00 2001 From: pimjager Date: Thu, 7 Apr 2016 15:41:35 +0200 Subject: [PATCH] Sem --- ontwerpbesluiten.txt | 8 +++++- sem.dcl | 1 + sem.icl | 62 +++++++++++++++++++++++++++----------------- 3 files changed, 46 insertions(+), 25 deletions(-) diff --git a/ontwerpbesluiten.txt b/ontwerpbesluiten.txt index 4c245aa..8cbcf17 100644 --- a/ontwerpbesluiten.txt +++ b/ontwerpbesluiten.txt @@ -4,4 +4,10 @@ Waarom? Mooiere puurdere lambda-calculus. Is chill straks met hogere orde functies (denken we) Staat mooie shizzle zoals in Haskell toe (f :: a -> a, g :: a -> a, f = g) Simpeler om te parsen -Con: wellicht kut met code generatie, wie weet. \ No newline at end of file +Con: wellicht kut met code generatie, wie weet. + +-- +Functies en variabelen delen 1 namespace. +Dat is met het oog op hogere orde functies, dan kan een parameter voor een functie een andere +functie zijn, of een variabele en als die 2 aparte namespaces hebben is dat moeilijk onduidelijk / +praktisch onmogelijke met type inteference \ No newline at end of file diff --git a/sem.dcl b/sem.dcl index 5c01ba0..6351c04 100644 --- a/sem.dcl +++ b/sem.dcl @@ -2,6 +2,7 @@ definition module sem from Data.Either import :: Either from AST import :: AST, :: Pos, :: Type +from StdOverloaded import class toString :: SemError = ParseError Pos String diff --git a/sem.icl b/sem.icl index 99aa44a..50b67ea 100644 --- a/sem.icl +++ b/sem.icl @@ -22,6 +22,15 @@ from parse import :: ParserOutput, :: Error :: Gamma :== 'Map'.Map String Type :: Env a :== (State Gamma (Either SemError a)) +get = state $ \s -> (s,s) + +putIdent :: String Type -> Env Void +putIdent i t = gets ('Map'.get i) >>= \mt -> case mt of + Nothing = pure <$> modify ('Map'.put i t) + Just t2 = unify t t2 >>= \r -> case r of + Left e = pure $ Left e + Right t3 = pure <$> modify ('Map'.put i t3) + instance toString SemError where toString (ParseError p e) = concat [ toString p,"SemError: ParseError: ", e] @@ -57,31 +66,36 @@ semVarDecl v = pure $ Right v // //TODO ident in de environment // Right e = Right $ pure vd // -//typeExpr :: Expr -> Env Type -//typeExpr (IntExpr _ _) = Right $ pure IntType -//typeExpr (CharExpr _ _) = Right $ pure CharType -//typeExpr (BoolExpr _ _) = Right $ pure BoolType -//typeExpr (Op1Expr p UnNegation expr) = undef//typeExpr expr -//// >>= \exprtype->case exprtype of -//// Right BoolType = Right $ pure BoolType -//// t = Left $ UnifyError p BoolType exprtype -//typeExpr (Op1Expr p UnMinus expr) = undef// typeExpr expr -//// >>= \exprtype->case exprtype of -//// IntType = Right $ pure IntType -//// t = Left $ UnifyError p IntType exprtype -//// typeExpr (Op2Expr Pos Expr Op2 Expr) = undef -////typeExpr (FunExpr Pos FunCall -////typeExpr (EmptyListExpr Pos -////typeExpr (TupleExpr Pos (Expr, Expr) -////typeExpr (VarExpr Pos VarDef) = undef +typeExpr :: Expr -> Env Type +typeExpr (IntExpr _ _) = pure $ Right IntType +typeExpr (CharExpr _ _) = pure $ Right CharType +typeExpr (BoolExpr _ _) = pure $ Right BoolType +typeExpr (Op1Expr p UnNegation expr) = typeExpr expr + >>= \exprtype->case exprtype of + Left e = pure $ Left e + Right BoolType = pure $ Right BoolType + Right (IdType ident) = putIdent ident BoolType >>| pure (Right BoolType) + Right t = pure $ Left $ UnifyError p BoolType t +//typeExpr (Op1Expr p UnMinus expr) = typeExpr expr +// >>= \exprtype->case exprtype of +// IntType = pure $ Right IntType +// t = Left $ UnifyError p IntType exprtype +//typeExpr (Op2Expr Pos Expr Op2 Expr) = undef +//typeExpr (FunExpr Pos FunCall) = undef +//typeExpr (EmptyListExpr Pos) = undef +//typeExpr (TupleExpr Pos (Expr, Expr)) = undef +//typeExpr (VarExpr Pos VarDef) = undef + + + //// -//class unify a :: Type a -> Env a -// -//instance unify Type where -// unify IntType IntType = Right $ pure IntType -// unify BoolType BoolType = Right $ pure BoolType -// unify CharType CharType = Right $ pure CharType -// unify _ _ = undef +class unify a :: Type a -> Env Type + +instance unify Type where + unify IntType IntType = pure $ Right IntType + unify BoolType BoolType = pure $ Right BoolType + unify CharType CharType = pure $ Right CharType + unify _ _ = undef // //instance unify Expr where // unify type expr = case type of -- 2.20.1