Skip to content

Commit

Permalink
Static type system (#17)
Browse files Browse the repository at this point in the history
* Static typing with value types.

* Implemented assignability check.

* Added Object printing and Supply as a test.

* Detecting conflicting declarations in patterns.
  • Loading branch information
V0ldek authored Apr 28, 2020
1 parent 924c465 commit 03bf877
Show file tree
Hide file tree
Showing 152 changed files with 4,115 additions and 1,777 deletions.
32 changes: 21 additions & 11 deletions Harper.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ cabal-version: 1.12
--
-- see: https://github.com/sol/hpack
--
-- hash: 8808b263b33fc260c705b749d1b5a47c0e5222ab529cc62f16efa67bc62a25f4
-- hash: 81f3a1df12643b675b2d1d354b41ffca5253eec2f3fed4cf8ac438ab06595179

name: Harper
version: 0.1.0.0
Expand All @@ -29,19 +29,29 @@ library
ErrM
Harper.Abs
Harper.Abs.Pos
Harper.Engine
Harper.Engine.Alloc
Harper.Engine.Conditionals
Harper.Engine.Core
Harper.Engine.Declarations
Harper.Engine.Error
Harper.Engine.Expressions
Harper.Engine.Output
Harper.Engine.Snapshots
Harper.Engine.Thunk
Harper.Abs.Typed
Harper.Error
Harper.Expressions
Harper.Interpreter
Harper.Interpreter.Alloc
Harper.Interpreter.Conditionals
Harper.Interpreter.Core
Harper.Interpreter.Declarations
Harper.Interpreter.Snapshots
Harper.Interpreter.Thunk
Harper.Lexer
Harper.Output
Harper.Parser
Harper.Printer
Harper.TypeChecker
Harper.TypeSystem.Alloc
Harper.TypeSystem.Core
Harper.TypeSystem.Declarations
Harper.TypeSystem.GlobalTypes
Harper.TypeSystem.StaticAnalysis
Harper.TypeSystem.Traits
Harper.TypeSystem.Typing
Harper.Utility
OutputM
other-modules:
Paths_Harper
Expand Down
32 changes: 22 additions & 10 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ import System.Exit ( exitFailure
)
import Control.Monad ( when )

import Harper.Abs
import Harper.Abs.Pos
import Harper.Interpreter
import Harper.Lexer
import Harper.Parser
import Harper.Printer
import Harper.Abs
import Harper.Engine
import Harper.TypeChecker


import ErrM
Expand All @@ -38,7 +40,8 @@ runFile v p f = putStrLn f >> readFile f >>= run v p
run :: Verbosity -> ParseFun (Program Pos) -> String -> IO ()
run v p s =
let ts = myLLexer s
in case p ts of
in
case p ts of
Bad s -> do
putStrLn "\nParse Failed...\n"
putStrV v "Tokens:"
Expand All @@ -48,10 +51,24 @@ run v p s =
Ok tree -> do
putStrLn "\nParse Successful!"
showTree v tree
let Out out res = runInterpreter tree
let Out out res = runTypeChecker tree
putStrLn $ out ""
case res of
Ok v -> putStrLn $ "\nExecution ended with value: " ++ show v
Ok (tree', tenv) -> do
putStrLn "\nType check successful."
showTree v tree'
let Out out res = runInterpreter tree' tenv
putStrLn $ out ""
case res of
Ok v ->
putStrLn
$ "\nExecution ended with value:\n"
++ v ""
Bad s -> do
putStrLn
$ "\nExecution terminated with an error: "
++ s
exitFailure
Bad s -> do
putStrLn $ "\nExecution terminated with an error: " ++ s
exitFailure
Expand Down Expand Up @@ -80,8 +97,3 @@ main = do
[] -> getContents >>= run 2 pProgram
"-s" : fs -> mapM_ (runFile 0 pProgram) fs
fs -> mapM_ (runFile 2 pProgram) fs





36 changes: 18 additions & 18 deletions lang/DocHarper.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,19 @@ All other symbols are terminals.
| //[TopLvlDecl]// | -> | **eps**
| | **|** | //TopLvlDecl// ``;`` //[TopLvlDecl]//
| //TypeHint// | -> | //Ident// ``::`` //TypeExpr//
| //TypeExpr3// | -> | //Ident//
| //TypeExpr2// | -> | //Ident//
| | **|** | //UIdent//
| | **|** | //TypePurity//
| | **|** | ``()``
| | **|** | //TypeExpr4//
| //TypeExpr2// | -> | ``(`` //TupleType// ``)``
| | **|** | ``(`` //TupleType// ``)``
| | **|** | ``{`` //[FieldTypeExpr]// ``}``
| | **|** | //TypeExpr3//
| //TypeExpr1// | -> | //TypeExpr1// //TypeExpr2//
| | **|** | ``(`` //TypeExpr// ``)``
| //TypeExpr1// | -> | //UIdent// //[TypeExpr2]//
| | **|** | //TypeExpr2//
| //TypeExpr// | -> | //TypeExpr1// ``->`` //TypeExpr//
| | **|** | //TypeExpr1//
| //TypeExpr4// | -> | ``(`` //TypeExpr// ``)``
| //[TypeExpr2]// | -> | //TypeExpr2//
| | **|** | //TypeExpr2// //[TypeExpr2]//
| //TupleType// | -> | //TypeExpr// ``,`` //TupleType//
| | **|** | //TypeExpr// ``,`` //TypeExpr//
| //TypePurity// | -> | ``impure``
Expand All @@ -96,13 +96,13 @@ All other symbols are terminals.
| //[FieldTypeExpr]// | -> | **eps**
| | **|** | //FieldTypeExpr//
| | **|** | //FieldTypeExpr// ``,`` //[FieldTypeExpr]//
| //FunDecl// | -> | //Ident// //[FunArg]// ``=`` //FunBody//
| //FunArg// | -> | //Ident//
| //[FunArg]// | -> | **eps**
| | **|** | //FunArg// //[FunArg]//
| //LambdaArg// | -> | //Pattern//
| //[LambdaArg]// | -> | **eps**
| | **|** | //LambdaArg// //[LambdaArg]//
| //FunDecl// | -> | //Ident// //[FunParam]// ``=`` //FunBody//
| //FunParam// | -> | //Ident//
| //[FunParam]// | -> | **eps**
| | **|** | //FunParam// //[FunParam]//
| //LambdaParam// | -> | //Pattern//
| //[LambdaParam]// | -> | **eps**
| | **|** | //LambdaParam// //[LambdaParam]//
| //FunBody// | -> | //Expression//
| | **|** | //Statement5//
| //BoolLiteral// | -> | ``true``
Expand Down Expand Up @@ -157,7 +157,7 @@ All other symbols are terminals.
| | **|** | //Expression3//
| //Expression1// | -> | //Expression2// ``|`` //Expression1//
| | **|** | //Expression2//
| //Expression// | -> | ``\`` //[LambdaArg]// ``=>`` //FunBody//
| //Expression// | -> | ``\`` //[LambdaParam]// ``=>`` //FunBody//
| | **|** | //Expression1//
| //TupleExpression// | -> | //Expression11// ``,`` //TupleExpression//
| | **|** | //Expression11// ``,`` //Expression11//
Expand Down Expand Up @@ -241,13 +241,13 @@ All other symbols are terminals.
| | **|** | //Declaration//
| //[LocalFunDecl]// | -> | //LocalFunDecl// ``;``
| | **|** | //LocalFunDecl// ``;`` //[LocalFunDecl]//
| //TypeSignature// | -> | //UIdent// //[TypeArgument]//
| //TypeSignature// | -> | //UIdent// //[TypeParameter]//
| //TypeDecl// | -> | ``value`` //TypeSignature// ``=`` ``{`` //TypeBody// ``}``
| | **|** | ``value`` //TypeSignature// ``=`` ``{`` //[TypeVariantDecl]// ``}``
| | **|** | ``ref`` //TypeSignature// ``=`` ``{`` //TypeBody// ``}``
| //TypeArgument// | -> | //Ident//
| //[TypeArgument]// | -> | **eps**
| | **|** | //TypeArgument// //[TypeArgument]//
| //TypeParameter// | -> | //Ident//
| //[TypeParameter]// | -> | **eps**
| | **|** | //TypeParameter// //[TypeParameter]//
| //TypeVariantDecl// | -> | ``variant`` //UIdent// ``=`` ``{`` //TypeBody// ``}``
| //[TypeVariantDecl]// | -> | //TypeVariantDecl// ``;``
| | **|** | //TypeVariantDecl// ``;`` //[TypeVariantDecl]//
Expand Down
35 changes: 18 additions & 17 deletions lang/Harper.cf
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@ terminator TopLvlDecl ";" ;

THint. TypeHint ::= Ident "::" TypeExpr ;

TVar. TypeExpr3 ::= Ident ;
TCtor. TypeExpr3 ::= UIdent ;
TPur. TypeExpr3 ::= TypePurity ;
TUnit. TypeExpr3 ::= "()" ;
TVar. TypeExpr2 ::= Ident ;
TCtor. TypeExpr2 ::= UIdent ;
TPur. TypeExpr2 ::= TypePurity ;
TUnit. TypeExpr2 ::= "()" ;
TTup. TypeExpr2 ::= "(" TupleType ")" ;
TAdHoc. TypeExpr2 ::= "{" [FieldTypeExpr] "}" ;
TApp. TypeExpr1 ::= TypeExpr1 TypeExpr2 ;
TApp. TypeExpr1 ::= UIdent [TypeExpr2] ;
TFun. TypeExpr ::= TypeExpr1 "->" TypeExpr ;

coercions TypeExpr 4 ;
separator nonempty TypeExpr2 "";
coercions TypeExpr 2 ;

TTupList. TupleType ::= TypeExpr "," TupleType ;
TTupTail. TupleType ::= TypeExpr "," TypeExpr ;
Expand All @@ -39,13 +40,13 @@ separator FieldTypeExpr "," ;

-- Functions

FDecl. FunDecl ::= Ident [FunArg] "=" FunBody ;
FDecl. FunDecl ::= Ident [FunParam] "=" FunBody ;

FArg. FunArg ::= Ident ;
separator FunArg "" ;
FParam. FunParam ::= Ident ;
separator FunParam "" ;

LamArg. LambdaArg ::= Pattern ;
separator LambdaArg "" ;
LamParam. LambdaParam ::= Pattern ;
separator LambdaParam "" ;

FExprBody. FunBody ::= Expression ;
FStmtBody. FunBody ::= Statement5 ;
Expand Down Expand Up @@ -111,7 +112,7 @@ OrExpr. Expression2 ::= Expression2 "or" Expression3 ;

SeqExpr. Expression1 ::= Expression2 "|" Expression1 ;

LamExpr. Expression ::= "\\" [LambdaArg] "=>" FunBody ;
LamExpr. Expression ::= "\\" [LambdaParam] "=>" FunBody ;

coercions Expression 12;

Expand All @@ -123,7 +124,7 @@ TupExprTail. TupleExpression ::= Expression11 "," Expression11 ;
-- Match expressions.

MatchExprClause. MatchExpressionClause ::= Pattern "=>" Expression ;
separator MatchExpressionClause "," ;
separator nonempty MatchExpressionClause "," ;

-- Value construction expressions.

Expand Down Expand Up @@ -176,7 +177,7 @@ separator nonempty Statement "" ;
-- Match statements.

MatchStmtClause. MatchStatementClause ::= Pattern "=>" Statement4 ;
separator MatchStatementClause "" ;
separator nonempty MatchStatementClause "" ;

-- Conditional statements.

Expand Down Expand Up @@ -228,14 +229,14 @@ terminator nonempty LocalFunDecl ";" ;

-- Type declarations.

TSig. TypeSignature ::= UIdent [TypeArgument] ;
TSig. TypeSignature ::= UIdent [TypeParameter] ;

ValTDecl. TypeDecl ::= "value" TypeSignature "=" "{" TypeBody "}" ;
ValTUDecl. TypeDecl ::= "value" TypeSignature "=" "{" [TypeVariantDecl] "}" ;
RefTDecl. TypeDecl ::= "ref" TypeSignature "=" "{" TypeBody "}" ;

TArg. TypeArgument ::= Ident ;
separator TypeArgument "" ;
TParam. TypeParameter ::= Ident ;
separator TypeParameter "" ;

TVarDecl. TypeVariantDecl ::= "variant" UIdent "=" "{" TypeBody "}";
terminator nonempty TypeVariantDecl ";" ;
Expand Down
36 changes: 17 additions & 19 deletions src/Harper/Abs.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ module Harper.Abs where

-- Haskell module generated by the BNF converter

type Pos = Maybe (Int, Int)

newtype Ident = Ident String deriving (Eq, Ord, Show, Read)
newtype UIdent = UIdent String deriving (Eq, Ord, Show, Read)
data Program a = Prog a [TopLvlDecl a]
Expand Down Expand Up @@ -36,7 +34,7 @@ data TypeExpr a
| TUnit a
| TTup a (TupleType a)
| TAdHoc a [FieldTypeExpr a]
| TApp a (TypeExpr a) (TypeExpr a)
| TApp a UIdent [TypeExpr a]
| TFun a (TypeExpr a) (TypeExpr a)
deriving (Eq, Ord, Show, Read)

Expand All @@ -48,7 +46,7 @@ instance Functor TypeExpr where
TUnit a -> TUnit (f a)
TTup a tupletype -> TTup (f a) (fmap f tupletype)
TAdHoc a fieldtypeexprs -> TAdHoc (f a) (map (fmap f) fieldtypeexprs)
TApp a typeexpr1 typeexpr2 -> TApp (f a) (fmap f typeexpr1) (fmap f typeexpr2)
TApp a uident typeexprs -> TApp (f a) uident (map (fmap f) typeexprs)
TFun a typeexpr1 typeexpr2 -> TFun (f a) (fmap f typeexpr1) (fmap f typeexpr2)
data TupleType a
= TTupList a (TypeExpr a) (TupleType a)
Expand All @@ -72,24 +70,24 @@ data FieldTypeExpr a = TFld a (TypeHint a)
instance Functor FieldTypeExpr where
fmap f x = case x of
TFld a typehint -> TFld (f a) (fmap f typehint)
data FunDecl a = FDecl a Ident [FunArg a] (FunBody a)
data FunDecl a = FDecl a Ident [FunParam a] (FunBody a)
deriving (Eq, Ord, Show, Read)

instance Functor FunDecl where
fmap f x = case x of
FDecl a ident funargs funbody -> FDecl (f a) ident (map (fmap f) funargs) (fmap f funbody)
data FunArg a = FArg a Ident
FDecl a ident funparams funbody -> FDecl (f a) ident (map (fmap f) funparams) (fmap f funbody)
data FunParam a = FParam a Ident
deriving (Eq, Ord, Show, Read)

instance Functor FunArg where
instance Functor FunParam where
fmap f x = case x of
FArg a ident -> FArg (f a) ident
data LambdaArg a = LamArg a (Pattern a)
FParam a ident -> FParam (f a) ident
data LambdaParam a = LamParam a (Pattern a)
deriving (Eq, Ord, Show, Read)

instance Functor LambdaArg where
instance Functor LambdaParam where
fmap f x = case x of
LamArg a pattern -> LamArg (f a) (fmap f pattern)
LamParam a pattern -> LamParam (f a) (fmap f pattern)
data FunBody a
= FExprBody a (Expression a) | FStmtBody a (Statement a)
deriving (Eq, Ord, Show, Read)
Expand Down Expand Up @@ -162,7 +160,7 @@ data Expression a
| AndExpr a (Expression a) (Expression a)
| OrExpr a (Expression a) (Expression a)
| SeqExpr a (Expression a) (Expression a)
| LamExpr a [LambdaArg a] (FunBody a)
| LamExpr a [LambdaParam a] (FunBody a)
deriving (Eq, Ord, Show, Read)

instance Functor Expression where
Expand Down Expand Up @@ -195,7 +193,7 @@ instance Functor Expression where
AndExpr a expression1 expression2 -> AndExpr (f a) (fmap f expression1) (fmap f expression2)
OrExpr a expression1 expression2 -> OrExpr (f a) (fmap f expression1) (fmap f expression2)
SeqExpr a expression1 expression2 -> SeqExpr (f a) (fmap f expression1) (fmap f expression2)
LamExpr a lambdaargs funbody -> LamExpr (f a) (map (fmap f) lambdaargs) (fmap f funbody)
LamExpr a lambdaparams funbody -> LamExpr (f a) (map (fmap f) lambdaparams) (fmap f funbody)
data TupleExpression a
= TupExprList a (Expression a) (TupleExpression a)
| TupExprTail a (Expression a) (Expression a)
Expand Down Expand Up @@ -376,12 +374,12 @@ instance Functor LocalObjDecl where
fmap f x = case x of
LocVarDecl a declaration -> LocVarDecl (f a) (fmap f declaration)
LocValDecl a declaration -> LocValDecl (f a) (fmap f declaration)
data TypeSignature a = TSig a UIdent [TypeArgument a]
data TypeSignature a = TSig a UIdent [TypeParameter a]
deriving (Eq, Ord, Show, Read)

instance Functor TypeSignature where
fmap f x = case x of
TSig a uident typearguments -> TSig (f a) uident (map (fmap f) typearguments)
TSig a uident typeparameters -> TSig (f a) uident (map (fmap f) typeparameters)
data TypeDecl a
= ValTDecl a (TypeSignature a) (TypeBody a)
| ValTUDecl a (TypeSignature a) [TypeVariantDecl a]
Expand All @@ -393,12 +391,12 @@ instance Functor TypeDecl where
ValTDecl a typesignature typebody -> ValTDecl (f a) (fmap f typesignature) (fmap f typebody)
ValTUDecl a typesignature typevariantdecls -> ValTUDecl (f a) (fmap f typesignature) (map (fmap f) typevariantdecls)
RefTDecl a typesignature typebody -> RefTDecl (f a) (fmap f typesignature) (fmap f typebody)
data TypeArgument a = TArg a Ident
data TypeParameter a = TParam a Ident
deriving (Eq, Ord, Show, Read)

instance Functor TypeArgument where
instance Functor TypeParameter where
fmap f x = case x of
TArg a ident -> TArg (f a) ident
TParam a ident -> TParam (f a) ident
data TypeVariantDecl a = TVarDecl a UIdent (TypeBody a)
deriving (Eq, Ord, Show, Read)

Expand Down
Loading

0 comments on commit 03bf877

Please sign in to comment.