Skip to content

Commit

Permalink
[ #20 ] Initial work
Browse files Browse the repository at this point in the history
  • Loading branch information
ice1000 committed Jan 27, 2019
1 parent 354087b commit b0aa0c8
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 10 deletions.
25 changes: 19 additions & 6 deletions src/full/OwO/Syntax/Abstract.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE TypeOperators #-}
Expand Down Expand Up @@ -31,7 +32,7 @@ module OwO.Syntax.Abstract
, AstDeclaration
-- Declarations

, ULevel(..)
, ULevel'(..)
) where

import Control.Applicative
Expand All @@ -49,7 +50,7 @@ import qualified OwO.Util.StrictMaybe as Strict
data AstTerm' c
= AstLiteral Loc LiteralInfo
-- ^ Constants, same as in Psi
| AstTypeLit c ULevel
| AstTypeLit c (ULevel' (AstTerm' c))
-- ^ Type literal
| AstApp (AstTerm' c) (AstTerm' c)
-- ^ Application
Expand Down Expand Up @@ -112,14 +113,26 @@ data AstDeclaration' t c
-- ^ Functions with types but no implementations
deriving (Eq, Ord, Show)

data ULevel
data ULevel' c
= ULevelLit Int
-- ^ Like Type0, Type1
| ULevelVar String Int
-- ^ Level variables. Should be already computed.
| ULevelVar c
-- ^ Level variables
| ULevelMax
-- ^ TypeInf, TypeOmega
deriving (Eq, Ord, Show)
deriving (Eq, Functor, Ord, Show)

instance Enum (ULevel' c) where
succ (ULevelLit i) = ULevelLit $ succ i
succ ULevelMax = ULevelMax
succ _ = error __TODO__
pred (ULevelLit i) = ULevelLit $ pred i
pred ULevelMax = ULevelMax
pred _ = error __TODO__
toEnum = ULevelLit
fromEnum (ULevelLit i) = i
fromEnum ULevelMax = -1
fromEnum _ = -2

type AstDeclaration = AstDeclaration' AstTerm' Name
type AstTerm = AstTerm' Name
Expand Down
28 changes: 28 additions & 0 deletions src/full/OwO/TypeChecking.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import Control.Monad.State
import Data.Functor ((<&>))
import Data.Maybe (catMaybes)

import OwO.Syntax.Abstract
import OwO.Syntax.Concrete
( LiteralInfo
, PsiFile (..)
Expand All @@ -37,6 +38,33 @@ import OwO.TypeChecking.Reduce
literalType :: LiteralInfo -> Type
literalType _ = Var __TODO__

-- | Check if some @AstTerm@ is a type-correct term of type @Type@
checkExpr :: TCM m => AstTerm -> Type -> m Term
checkExpr term ty = case term of
AstTypeLit name level -> do
typeLevel <- case ty of
TType l -> pure l
_ -> throwError $ TypeMismatch "Type mismatch"
termLevel <- case level of
ULevelLit i -> pure $ ULevelLit i
ULevelVar v -> ULevelVar <$> checkInfer v
ULevelMax -> pure ULevelMax
case typeLevel of
ULevelLit _ | succ termLevel == typeLevel -> pure $ TType termLevel
ULevelVar var -> pure __TODO__
ULevelMax -> pure $ TType termLevel
_ -> throwError __TODO__

-- | Infer the type of an @AstTerm@
checkInfer :: TCM m => AstTerm -> m Term
checkInfer = \case
AstTypeLit name level -> case level of
ULevelLit lit -> pure . TType $ ULevelLit lit
ULevelVar var -> TType . ULevelVar <$> checkInfer var
ULevelMax -> pure $ TType ULevelMax
AstLocalRef name index -> pure $ Var index
_ -> pure __TODO__

typeCheckFile :: TCM m => PsiFile -> m ()
typeCheckFile file = do
let decls = declarations file
Expand Down
11 changes: 7 additions & 4 deletions src/full/OwO/TypeChecking/Core.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DuplicateRecordFields #-}

-- | Core language
Expand All @@ -12,6 +11,7 @@ module OwO.TypeChecking.Core
, Type
, typeUniverseOfLevel
, typeUniverseOfLevel'
, typeOfLevel
, definitionType

, Definition(..)
Expand Down Expand Up @@ -39,10 +39,10 @@ data Term' i
| Bind i !(BinderInfo (Term' i)) (Term' i)
-- ^ Name binding
-}
| TType ULevel
| TType (ULevel' (Term' i))
-- ^ Type of Type, including type omega
| Const LiteralInfo
deriving (Eq, Functor, Ord, Show)
deriving (Eq, Ord, Show)

-- TODO

Expand Down Expand Up @@ -75,4 +75,7 @@ typeUniverseOfLevel :: Int -> Definition
typeUniverseOfLevel = uncurry SimpleDefinition . typeUniverseOfLevel'

typeUniverseOfLevel' :: Int -> (Term, Type)
typeUniverseOfLevel' i = (TType . ULevelLit $ succ i, TType $ ULevelLit i)
typeUniverseOfLevel' i = (typeOfLevel $ succ i, typeOfLevel i)

typeOfLevel :: Int -> Term
typeOfLevel = TType . ULevelLit
1 change: 1 addition & 0 deletions src/full/OwO/TypeChecking/Monad.hs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ data TCEnv = TypeCheckingEnv
-- | TypeChecking Error
data TCError
= DesugarErr DesugarError
| TypeMismatch String
| OtherErr String
deriving (Eq, Show)

Expand Down

0 comments on commit b0aa0c8

Please sign in to comment.