Skip to content

Commit c4f7697

Browse files
committed
Initial commit
0 parents  commit c4f7697

File tree

8 files changed

+210
-0
lines changed

8 files changed

+210
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
dist/
2+
dist-newstyle/
3+
.ghc.*

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Revision history for qtt
2+
3+
## 0.1.0.0 -- YYYY-mm-dd
4+
5+
* First version. Released on an unsuspecting world.

LICENSE

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Copyright (c) 2019, Isaac Elliott
2+
3+
All rights reserved.
4+
5+
Redistribution and use in source and binary forms, with or without
6+
modification, are permitted provided that the following conditions are met:
7+
8+
* Redistributions of source code must retain the above copyright
9+
notice, this list of conditions and the following disclaimer.
10+
11+
* Redistributions in binary form must reproduce the above
12+
copyright notice, this list of conditions and the following
13+
disclaimer in the documentation and/or other materials provided
14+
with the distribution.
15+
16+
* Neither the name of Isaac Elliott nor the names of other
17+
contributors may be used to endorse or promote products derived
18+
from this software without specific prior written permission.
19+
20+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Setup.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import Distribution.Simple
2+
main = defaultMain

qtt.cabal

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
cabal-version: 2.4
2+
-- Initial package description 'qtt.cabal' generated by 'cabal init'. For
3+
-- further documentation, see http://haskell.org/cabal/users-guide/
4+
5+
name: qtt
6+
version: 0.1.0.0
7+
-- synopsis:
8+
-- description:
9+
-- bug-reports:
10+
license: BSD-3-Clause
11+
license-file: LICENSE
12+
author: Isaac Elliott
13+
maintainer: [email protected]
14+
-- copyright:
15+
category: Language
16+
extra-source-files: CHANGELOG.md
17+
18+
library
19+
exposed-modules: Syntax
20+
, Context
21+
build-depends: base ^>=4.12.0.0
22+
, bound
23+
, deriving-compat
24+
, semirings
25+
ghc-options: -Wall -Werror
26+
hs-source-dirs: src
27+
default-language: Haskell2010

src/Context.hs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{-# language DeriveFunctor #-}
2+
module Context where
3+
4+
import Data.Semiring (times, plus)
5+
import Syntax
6+
7+
data Entry v a
8+
= Entry
9+
{ _entryVar :: v
10+
, _entryUsage :: Usage
11+
, _entryType :: Term a
12+
} deriving (Eq, Show, Functor)
13+
14+
newtype Context a = Context { unContext :: [Entry a a] }
15+
deriving (Eq, Show)
16+
17+
scaleC :: Usage -> Context a -> Context a
18+
scaleC u = Context . go . unContext
19+
where
20+
go ctx =
21+
case ctx of
22+
[] -> []
23+
Entry v u' t : cs -> Entry v (times u u') t : go cs
24+
25+
zeroC :: Context a -> Context a
26+
zeroC = scaleC Zero
27+
28+
addC :: Eq a => Context a -> Context a -> Context a
29+
addC (Context a) (Context b) = Context $ go a b
30+
where
31+
go [] [] = []
32+
go (Entry v u t : cs) (Entry v' u' t' : cs')
33+
| v == v' && t == t' = Entry v (plus u u') t : go cs cs'
34+
go _ _ = error "addContext: context mismatch"
35+
36+
lookupC :: Eq a => a -> Context a -> Maybe (Entry a a)
37+
lookupC a = go . unContext
38+
where
39+
go [] = Nothing
40+
go (Entry v u t : cs)
41+
| v == a = Just $ Entry v u t
42+
| otherwise = go cs

src/Syntax.hs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{-# language DeriveFunctor, DeriveFoldable, DeriveTraversable #-}
2+
{-# language StandaloneDeriving #-}
3+
{-# language TemplateHaskell #-}
4+
module Syntax where
5+
6+
import Bound.Scope (Scope)
7+
import Bound.TH (makeBound)
8+
import Data.Deriving (deriveEq1, deriveShow1)
9+
10+
import Data.Semiring (Semiring(..))
11+
12+
data Usage = Zero | One | Many
13+
deriving (Eq, Show)
14+
15+
instance Semiring Usage where
16+
zero = Zero
17+
one = One
18+
19+
plus Zero m = m
20+
plus One Zero = One
21+
plus One One = Many
22+
plus One Many = Many
23+
plus Many m = Many
24+
25+
times Zero m = Zero
26+
times One m = m
27+
times Many Zero = Zero
28+
times Many One = Many
29+
times Many Many = Many
30+
31+
type Ty = Term
32+
data Term a
33+
= Var a
34+
| Ann (Term a) Usage (Term a)
35+
| Type
36+
| Lam (Scope () Term a)
37+
| Pi Usage (Term a) (Scope () Term a)
38+
| App (Term a) (Term a)
39+
| Pair (Term a) (Term a)
40+
| Sigma Usage (Term a) (Scope () Term a)
41+
| Fst (Term a)
42+
| Snd (Term a)
43+
deriving (Functor, Foldable, Traversable)
44+
makeBound ''Term
45+
deriveEq1 ''Term
46+
deriveShow1 ''Term
47+
48+
deriving instance Eq a => Eq (Term a)
49+
deriving instance Show a => Show (Term a)

src/Typecheck.hs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
module Typecheck where
2+
3+
import Bound.Scope (fromScope)
4+
import Bound.Var (Var(..), unvar)
5+
6+
import Context
7+
import Syntax
8+
9+
data TypeError a
10+
= NotInScope a
11+
| VariableMany a
12+
| ExpectedType (Ty a)
13+
deriving (Eq, Show)
14+
15+
eval :: Context a -> Term a -> Term a
16+
eval ctx tm =
17+
case tm of
18+
Var a -> _
19+
Ann a u b -> _
20+
Type -> Type
21+
Lam a -> _
22+
Pi u a b -> _
23+
App a b -> _
24+
Pair a b -> _
25+
Sigma u a b -> _
26+
Fst a -> _
27+
Snd a -> _
28+
29+
check :: Context a -> (x -> Entry () x) -> Term x -> Usage -> Ty x -> Either (TypeError a) Bool
30+
check ctx varCtx tm u ty =
31+
case tm of
32+
Pi u a b ->
33+
case ty of
34+
Type -> do
35+
check (zeroC ctx) varCtx a Zero Type
36+
check (zeroC ctx) (unvar (const $ Entry () Zero (F <$> a)) (fmap F . varCtx)) (fromScope b) Zero Type
37+
_ -> Left $ ExpectedType ty
38+
39+
infer :: Eq a => Context a -> Term a -> Either (TypeError a) (Usage, Ty a)
40+
infer ctx tm =
41+
case tm of
42+
Var a ->
43+
case lookupC a ctx of
44+
Nothing -> Left $ NotInScope a
45+
Just (Entry _ u t) ->
46+
case u of
47+
Many -> Left $ VariableMany a
48+
_ -> pure (u, t)
49+
Ann a u b -> do
50+
check (zeroC ctx) b Zero Type
51+
check (zeroC ctx) a Zero b
52+
pure (u, b)

0 commit comments

Comments
 (0)