Skip to content

Commit

Permalink
use unbound's instantiate
Browse files Browse the repository at this point in the history
  • Loading branch information
sweirich committed Jun 27, 2022
1 parent 866fc7e commit 69419e8
Show file tree
Hide file tree
Showing 32 changed files with 105 additions and 67 deletions.
15 changes: 14 additions & 1 deletion full/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ each other.
Installation
----------

Compiling pi-forall requires GHC and cabal or stack
Compiling pi-forall requires GHC and stack

Recommended tools (see links for instructions):

Expand Down Expand Up @@ -48,6 +48,19 @@ Each implementation has the following structure:
```

To build each version, go to that directory and type:

```
stack build
```

and to typecheck a source file:

```
stack exec -- pi-forall <sourcefile>
```


Acknowledgement
---------------

Expand Down
10 changes: 5 additions & 5 deletions full/pi/Equality.pi
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ k = \ [A][x] p .
k2 : [A:Type] -> [x:A] -> [P : (x = x) -> Type] -> P Refl -> (h:x = x) -> P h
k2 = \ [A][x][P] d h . subst d by h

-- here's another version of j
-- here's j
-- https://ncatlab.org/nlab/show/identity+type#ExplicitDefinition

j : [A:Type]
Expand All @@ -108,10 +108,10 @@ j : [A:Type]
-> (x:A) -> (y:A) -> (p : x = y) -> C x y p
j = \[A][C] t x y p . subst (t x) by p

-- Can we use this version of j to derive k ?
-- I don't think so. At least not without some
-- form of heterogenous equality
-- Can't derive k from j

{- -- Doesn't type check
k' : [A:Type] -> (x:A) -> (p : x = x) -> (p = Refl)
k' = \ [A] x p .
j [A] [\ x y p . p = Refl] x x Refl
j [A] [\ x y p . p = Refl] x x Refl
-}
2 changes: 1 addition & 1 deletion full/pi/Hw1.pi
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Hw1 where

-- HW #1: get this file to type check by adding typing rules
-- for booleans and products to TypeCheck.hs in 'version1'
-- for booleans and sigma types to TypeCheck.hs in 'version1'

-- prelude operations on boolean values

Expand Down
4 changes: 2 additions & 2 deletions full/pi/Hw2.pi
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ neg = \ A . ( (A) -> void )
not : Bool -> Bool
not = \ x . if x then False else True

-- to be or not to be, that is the question
-- show that true is not false

not_not_equal : (b : Bool) -> (b = not b) -> void
not_not_equal = \b pf.
Expand All @@ -31,7 +31,7 @@ not_false_then_true : (b : Bool) -> neg (b = False) -> b = True
not_false_then_true = \b nb.
if b then Refl else nb Refl (b = True)

-- showing that decidable equality for booleans is correct.
-- show that decidable equality for booleans is correct.

eq_bool : Bool -> Bool -> Bool
eq_bool = \x y. if x then y else not y
Expand Down
2 changes: 1 addition & 1 deletion full/pi/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ SHELL=/bin/bash
PASS=Logic Equality Product Nat Fin Vec BoolLib Hw1 Hw2 FinHw \
Lambda Lambda0 Lambda1 Lambda2 \
Lec1 Lec2 Lec3 Lec4 \
List NatChurch Product1
List NatChurch Product1 Lennart

# TODO_FAIL are tests that *should* fail but currently pass.
TODO_FAIL=
Expand Down
7 changes: 3 additions & 4 deletions full/src/Equal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ whnf (App t1 t2) = do
nf <- whnf t1
case nf of
(Lam ep bnd) -> do
whnf (Unbound.substBind bnd (unArg t2) )
whnf (Unbound.instantiate bnd [unArg t2] )
_ -> do
return (App nf t2)

Expand All @@ -201,8 +201,7 @@ whnf (LetPair a bnd) = do
nf <- whnf a
case nf of
Prod b1 c -> do
((x,y), body) <- Unbound.unbind bnd
whnf (Unbound.substs [(x, b1), (y, c)] body)
whnf (Unbound.instantiate bnd [b1, c])
_ -> return (LetPair nf bnd)

-- ignore/remove type annotations and source positions when normalizing
Expand All @@ -211,7 +210,7 @@ whnf (Pos _ tm) = whnf tm

whnf (Let rhs bnd) = do
-- (x,body) <- Unbound.unbind bnd
whnf (Unbound.substBind bnd rhs)
whnf (Unbound.instantiate bnd [rhs])
whnf (Subst tm pf) = do
pf' <- whnf pf
case pf' of
Expand Down
2 changes: 1 addition & 1 deletion full/src/Syntax.hs
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ instance Unbound.Alpha SourcePos where
acompare' _ _ _ = EQ

-- Substitutions ignore source positions
instance Unbound.Subst b SourcePos where subst _ _ = id; substs _ = id; substPat _ _ = id
instance Unbound.Subst b SourcePos where subst _ _ = id; substs _ = id; substBvs _ _ = id

-- Internally generated source positions
internalPos :: SourcePos
Expand Down
2 changes: 1 addition & 1 deletion full/src/TypeCheck.hs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ tcTerm (App t1 t2) Nothing = do
-- if the argument is Irrelevant, resurrect the context
(if ep1 == Irr then Env.extendCtx (Demote Rel) else id) $
checkType (unArg t2) tyA
return (Unbound.substBind bnd (unArg t2))
return (Unbound.instantiate bnd [unArg t2])


-- i-ann
Expand Down
2 changes: 1 addition & 1 deletion full/stack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ packages:

extra-deps:
- git: [email protected]:lambdageek/unbound-generics.git
commit: 04f940740d95aecd105fd87ed9f49ee0b81da408
commit: a2a558058012b09bb313b3eb03cddb734fcf4a98


flags: {}
Expand Down
8 changes: 3 additions & 5 deletions main/pi/Equality.pi
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ k = \ [A][x] p .
k2 : [A:Type] -> [x:A] -> [P : (x = x) -> Type] -> P Refl -> (h:x = x) -> P h
k2 = \ [A][x][P] d h . subst d by h

-- here's another version of j
-- here's j
-- https://ncatlab.org/nlab/show/identity+type#ExplicitDefinition

j : [A:Type]
Expand All @@ -108,11 +108,9 @@ j : [A:Type]
-> (x:A) -> (y:A) -> (p : x = y) -> C x y p
j = \[A][C] t x y p . subst (t x) by p

-- Can we use this version of j to derive k ?
-- I don't think so. At least not without some
-- form of heterogenous equality
-- Can't derive k from j

{- -- Don't work, and I don't think it ever will.
{- -- Doesn't type check
k' : [A:Type] -> (x:A) -> (p : x = x) -> (p = Refl)
k' = \ [A] x p .
j [A] [\ x y p . p = Refl] x x Refl
Expand Down
2 changes: 1 addition & 1 deletion main/pi/Hw1.pi
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Hw1 where

-- HW #1: get this file to type check by adding typing rules
-- for booleans and products to TypeCheck.hs in 'version1'
-- for booleans and sigma types to TypeCheck.hs in 'version1'

-- prelude operations on boolean values

Expand Down
4 changes: 2 additions & 2 deletions main/pi/Hw2.pi
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ neg = \ A . ( (A) -> void )
not : Bool -> Bool
not = \ x . if x then False else True

-- to be or not to be, that is the question
-- show that true is not false

not_not_equal : (b : Bool) -> (b = not b) -> void
not_not_equal = {- SOLN DATA -} \b pf.
Expand All @@ -31,7 +31,7 @@ not_false_then_true : (b : Bool) -> neg (b = False) -> b = True
not_false_then_true = {- SOLN DATA -} \b nb.
if b then Refl else nb Refl (b = True) {- STUBWITH TRUSTME -}

-- showing that decidable equality for booleans is correct.
-- show that decidable equality for booleans is correct.

eq_bool : Bool -> Bool -> Bool
eq_bool = \x y. if x then y else not y
Expand Down
15 changes: 14 additions & 1 deletion version1/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ each other.
Installation
----------

Compiling pi-forall requires GHC and cabal or stack
Compiling pi-forall requires GHC and stack

Recommended tools (see links for instructions):

Expand Down Expand Up @@ -48,6 +48,19 @@ Each implementation has the following structure:
```

To build each version, go to that directory and type:

```
stack build
```

and to typecheck a source file:

```
stack exec -- pi-forall <sourcefile>
```


Acknowledgement
---------------

Expand Down
2 changes: 1 addition & 1 deletion version1/pi/Hw1.pi
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Hw1 where

-- HW #1: get this file to type check by adding typing rules
-- for booleans and products to TypeCheck.hs in 'version1'
-- for booleans and sigma types to TypeCheck.hs in 'version1'

-- prelude operations on boolean values

Expand Down
5 changes: 2 additions & 3 deletions version1/src/Equal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ whnf (App t1 t2) = do
nf <- whnf t1
case nf of
(Lam bnd) -> do
whnf (Unbound.substBind bnd t2)
whnf (Unbound.instantiate bnd [t2])
_ -> do
return (App nf t2)
whnf (If t1 t2 t3) = do
Expand All @@ -118,8 +118,7 @@ whnf (LetPair a bnd) = do
nf <- whnf a
case nf of
Prod b1 c -> do
((x, y), body) <- Unbound.unbind bnd
whnf (Unbound.substs [(x, b1), (y, c)] body)
whnf (Unbound.instantiate bnd [b1, c])
_ -> return (LetPair nf bnd)

-- ignore/remove type annotations and source positions when normalizing
Expand Down
2 changes: 1 addition & 1 deletion version1/src/Syntax.hs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ instance Unbound.Alpha SourcePos where
acompare' _ _ _ = EQ

-- Substitutions ignore source positions
instance Unbound.Subst b SourcePos where subst _ _ = id; substs _ = id; substPat _ _ = id
instance Unbound.Subst b SourcePos where subst _ _ = id; substs _ = id; substBvs _ _ = id

-- Internally generated source positions
internalPos :: SourcePos
Expand Down
18 changes: 5 additions & 13 deletions version1/src/TypeCheck.hs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ tcTerm (App t1 t2) Nothing = do
ensurePi ty = Env.err [DS "Expected a function type but found ", DD ty]
(tyA, bnd) <- ensurePi ty1
checkType t2 tyA
return (Unbound.substBind bnd t2)
return (Unbound.instantiate bnd [t2])

-- i-ann
tcTerm (Ann tm ty) Nothing = do
Expand All @@ -85,20 +85,12 @@ tcTerm TrustMe (Just ty) = return ty
tcTerm TyUnit Nothing = return Type
tcTerm LitUnit Nothing = return TyUnit
-- i-bool
tcTerm TyBool Nothing = return Type
tcTerm TyBool Nothing = Env.err [DS "unimplemented"]
-- i-true/false
tcTerm (LitBool b) Nothing = return TyBool
tcTerm (LitBool b) Nothing = Env.err [DS "unimplemented"]
-- c-if
tcTerm t@(If t1 t2 t3) mty = do
checkType t1 TyBool
ty <- inferType t2
checkType t3 ty
return ty
tcTerm (Let rhs bnd) mty = do
ty <- inferType rhs
(x,body) <- Unbound.unbind bnd
Env.extendCtx (mkSig x ty) $ tcTerm body mty

tcTerm t@(If t1 t2 t3) mty = Env.err [DS "unimplemented"]
tcTerm (Let rhs bnd) mty = Env.err [DS "unimplemented"]
tcTerm t@(Sigma tyA bnd) Nothing = Env.err [DS "unimplemented"]
tcTerm t@(Prod a b) (Just ty) = Env.err [DS "unimplemented"]
tcTerm t@(LetPair p bnd) (Just ty) = Env.err [DS "unimplemented"]
Expand Down
2 changes: 1 addition & 1 deletion version1/stack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ packages:

extra-deps:
- git: [email protected]:lambdageek/unbound-generics.git
commit: 04f940740d95aecd105fd87ed9f49ee0b81da408
commit: a2a558058012b09bb313b3eb03cddb734fcf4a98


flags: {}
Expand Down
15 changes: 14 additions & 1 deletion version2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ each other.
Installation
----------

Compiling pi-forall requires GHC and cabal or stack
Compiling pi-forall requires GHC and stack

Recommended tools (see links for instructions):

Expand Down Expand Up @@ -48,6 +48,19 @@ Each implementation has the following structure:
```

To build each version, go to that directory and type:

```
stack build
```

and to typecheck a source file:

```
stack exec -- pi-forall <sourcefile>
```


Acknowledgement
---------------

Expand Down
2 changes: 1 addition & 1 deletion version2/pi/Hw1.pi
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Hw1 where

-- HW #1: get this file to type check by adding typing rules
-- for booleans and products to TypeCheck.hs in 'version1'
-- for booleans and sigma types to TypeCheck.hs in 'version1'

-- prelude operations on boolean values

Expand Down
4 changes: 2 additions & 2 deletions version2/pi/Hw2.pi
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ neg = \ A . ( (A) -> void )
not : Bool -> Bool
not = \ x . if x then False else True

-- to be or not to be, that is the question
-- show that true is not false

not_not_equal : (b : Bool) -> (b = not b) -> void
not_not_equal = TRUSTME
Expand All @@ -28,7 +28,7 @@ not_not_equal = TRUSTME
not_false_then_true : (b : Bool) -> neg (b = False) -> b = True
not_false_then_true = TRUSTME

-- showing that decidable equality for booleans is correct.
-- show that decidable equality for booleans is correct.

eq_bool : Bool -> Bool -> Bool
eq_bool = \x y. if x then y else not y
Expand Down
7 changes: 3 additions & 4 deletions version2/src/Equal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ whnf (App t1 t2) = do
nf <- whnf t1
case nf of
(Lam bnd) -> do
whnf (Unbound.substBind bnd t2)
whnf (Unbound.instantiate bnd [t2])
_ -> do
return (App nf t2)

Expand All @@ -153,8 +153,7 @@ whnf (LetPair a bnd) = do
nf <- whnf a
case nf of
Prod b1 c -> do
((x,y), body) <- Unbound.unbind bnd
whnf (Unbound.substs [(x, b1), (y, c)] body)
whnf (Unbound.instantiate bnd [b1, c])
_ -> return (LetPair nf bnd)

-- ignore/remove type annotations and source positions when normalizing
Expand All @@ -163,7 +162,7 @@ whnf (Pos _ tm) = whnf tm

whnf (Let rhs bnd) = do
-- (x,body) <- Unbound.unbind bnd
whnf (Unbound.substBind bnd rhs)
whnf (Unbound.instantiate bnd [rhs])
whnf (Subst tm pf) = do
pf' <- whnf pf
case pf' of
Expand Down
2 changes: 1 addition & 1 deletion version2/src/Syntax.hs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ instance Unbound.Alpha SourcePos where
acompare' _ _ _ = EQ

-- Substitutions ignore source positions
instance Unbound.Subst b SourcePos where subst _ _ = id; substs _ = id; substPat _ _ = id
instance Unbound.Subst b SourcePos where subst _ _ = id; substs _ = id; substBvs _ _ = id

-- Internally generated source positions
internalPos :: SourcePos
Expand Down
Loading

0 comments on commit 69419e8

Please sign in to comment.