Skip to content

Commit 26d253b

Browse files
committed
Lesser than and some bug fixes
1 parent 27a05f1 commit 26d253b

File tree

8 files changed

+59
-11
lines changed

8 files changed

+59
-11
lines changed

src/Lam/Agda/Lam/Data.agda

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ data BinOpT : Set where
6161
Mul : BinOpT
6262
And : BinOpT
6363
Or : BinOpT
64+
LtInt : BinOpT
6465
MkPair : BinOpT
6566

6667
{-# COMPILE AGDA2HS BinOpT deriving (Eq, Show) #-}
@@ -72,6 +73,7 @@ instance
7273
iEqBinOp ._==_ Mul Mul = true
7374
iEqBinOp ._==_ And And = true
7475
iEqBinOp ._==_ Or Or = true
76+
iEqBinOp ._==_ LtInt LtInt = true
7577
iEqBinOp ._==_ MkPair MkPair = true
7678
iEqBinOp ._==_ _ _ = false
7779

src/Lam/Agda/Lam/Evaluator.agda

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ smallStepBinOp o e1 _ Nothing (Just e2') = Just (BinOp o e1 e2')
117117
smallStepBinOp Add (Const (NumC i1)) (Const (NumC i2)) Nothing Nothing = Just (Const (NumC (i1 + i2)))
118118
smallStepBinOp Sub (Const (NumC i1)) (Const (NumC i2)) Nothing Nothing = Just (Const (NumC (i1 - i2)))
119119
smallStepBinOp Mul (Const (NumC i1)) (Const (NumC i2)) Nothing Nothing = Just (Const (NumC (i1 * i2)))
120+
smallStepBinOp LtInt (Const (NumC i1)) (Const (NumC i2)) Nothing Nothing = Just (Const (BoolC (i1 < i2)))
120121
smallStepBinOp And (Const (BoolC i1)) (Const (BoolC i2)) Nothing Nothing = Just (Const (BoolC (i1 && i2)))
121122
smallStepBinOp Or (Const (BoolC i1)) (Const (BoolC i2)) Nothing Nothing = Just (Const (BoolC (i1 || i2)))
122123
smallStepBinOp _ _ _ _ _ = Nothing

src/Lam/Agda/Lam/FormalizationEvaluator.agda

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ open import Lam.Data
44
open import Lam.Evaluator
55
open import Lam.UtilsAgda
66

7-
open import Haskell.Prelude using (Just; Nothing; Int; Bool; _+_; _-_; _*_; _&&_; _||_; not)
7+
open import Haskell.Prelude using (Just; Nothing; Int; Bool; _+_; _-_; _*_; _&&_; _||_; not; _<_)
88

99
open import Data.Bool using (true; false)
1010
open import Data.Empty using (⊥-elim; ⊥)
@@ -58,6 +58,13 @@ data Neutral where
5858
---------------------
5959
Neutral (BinOp Mul L M)
6060

61+
noe-ltInt : {L M : Expr}
62+
Normal L
63+
Normal M
64+
( {i j : Int} ¬ (L ≡ (Const (NumC i)) × M ≡ (Const (NumC j))))
65+
---------------------
66+
Neutral (BinOp LtInt L M)
67+
6168
noe-and : {L M : Expr}
6269
Normal L
6370
Normal M
@@ -140,7 +147,7 @@ data ReducesTo : Expr → Expr → Set where
140147
Normal V1
141148
Normal V2
142149
---------------------------
143-
ReducesTo (App (Lam s ty V1) V2) (substitute V1 V2)
150+
ReducesTo (App (Lam s ty V1) V2) (substitute V2 V1)
144151
-- using a predicate to specify substitution here gets pretty ugly
145152

146153
r-l' : {s : Id} {ty : TypeL} {L L' : Expr}
@@ -168,6 +175,9 @@ data ReducesTo : Expr → Expr → Set where
168175
r-mul : {i1 i2 : Int}
169176
ReducesTo (BinOp Mul (Const (NumC i1)) (Const (NumC i2))) (Const (NumC (i1 * i2)))
170177

178+
r-ltInt : {i1 i2 : Int}
179+
ReducesTo (BinOp LtInt (Const (NumC i1)) (Const (NumC i2))) (Const (BoolC (i1 < i2)))
180+
171181
r-and : {i1 i2 : Bool}
172182
ReducesTo (BinOp And (Const (BoolC i1)) (Const (BoolC i2))) (Const (BoolC (i1 && i2)))
173183

@@ -290,6 +300,11 @@ stepNothingNormal {BinOp Mul L M} eq | Nothing | Nothing =
290300
where
291301
mulStepNothing : {i j} smallStepBinOp Mul L M Nothing Nothing ≡ Nothing ¬ ((L ≡ Const (NumC i)) × (M ≡ Const (NumC j)))
292302
mulStepNothing () ⟨ refl , refl ⟩
303+
stepNothingNormal {BinOp LtInt L M} eq | Nothing | Nothing =
304+
no-ne (noe-ltInt (stepNothingNormal eqL) (stepNothingNormal eqM) (ltStepNothing eq))
305+
where
306+
ltStepNothing : {i j} smallStepBinOp LtInt L M Nothing Nothing ≡ Nothing ¬ ((L ≡ Const (NumC i)) × (M ≡ Const (NumC j)))
307+
ltStepNothing () ⟨ refl , refl ⟩
293308
stepNothingNormal {BinOp And L M} eq | Nothing | Nothing =
294309
no-ne (noe-and (stepNothingNormal eqL) (stepNothingNormal eqM) (andStepNothing eq))
295310
where
@@ -368,6 +383,11 @@ neutralStepNothing {BinOp Mul L M} (noe-mul h1 h2 h3)
368383
| normalStepNothing h2 with smallStepBinOp Mul L M Nothing Nothing in eq
369384
neutralStepNothing {BinOp Mul L M} (noe-mul h1 h2 h3) | Nothing = refl
370385
neutralStepNothing {BinOp Mul (Const (NumC L')) (Const (NumC M'))} (noe-mul h1 h2 h3) | Just V' = ⊥-elim (h3 {L'} {M'} ⟨ refl , refl ⟩)
386+
neutralStepNothing {BinOp LtInt L M} (noe-ltInt h1 h2 h3)
387+
rewrite normalStepNothing h1
388+
| normalStepNothing h2 with smallStepBinOp LtInt L M Nothing Nothing in eq
389+
neutralStepNothing {BinOp LtInt L M} (noe-ltInt h1 h2 h3) | Nothing = refl
390+
neutralStepNothing {BinOp LtInt (Const (NumC L')) (Const (NumC M'))} (noe-ltInt h1 h2 h3) | Just V' = ⊥-elim (h3 {L'} {M'} ⟨ refl , refl ⟩)
371391
neutralStepNothing {BinOp And L M} (noe-and h1 h2 h3)
372392
rewrite normalStepNothing h1
373393
| normalStepNothing h2 with smallStepBinOp And L M Nothing Nothing in eq
@@ -421,6 +441,8 @@ step→red {BinOp Sub (Const (NumC i)) M} {N} h | Nothing | Nothing with M
421441
... | Const (NumC j) rewrite sym (Just-injective h) = r-sub
422442
step→red {BinOp Mul (Const (NumC i)) M} {N} h | Nothing | Nothing with M
423443
... | Const (NumC j) rewrite sym (Just-injective h) = r-mul
444+
step→red {BinOp LtInt (Const (NumC i)) M} {N} h | Nothing | Nothing with M
445+
... | Const (NumC j) rewrite sym (Just-injective h) = r-ltInt
424446
step→red {BinOp And (Const (BoolC i)) M} {N} h | Nothing | Nothing with M
425447
... | Const (BoolC j) rewrite sym (Just-injective h) = r-and
426448
step→red {BinOp Or (Const (BoolC i)) M} {N} h | Nothing | Nothing with M
@@ -457,6 +479,7 @@ red→step (r-binop1 h) rewrite red→step h = refl
457479
red→step (r-binop2 x h) rewrite normalStepNothing x | red→step h = refl
458480
red→step r-sub = refl
459481
red→step r-mul = refl
482+
red→step r-ltInt = refl
460483
red→step r-and = refl
461484
red→step r-or = refl
462485
red→step r-ite-true = refl

src/Lam/Agda/Lam/FormalizationTypeChecker.agda

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ data _⊢_∶_ : TypingContext → Expr → TypeL → Set where
5656
Γ ⊢ M ∶ IntT
5757
Γ ⊢ BinOp Mul L M ∶ IntT
5858

59+
⊢< : {L M : Expr} {Γ : TypingContext}
60+
Γ ⊢ L ∶ IntT
61+
Γ ⊢ M ∶ IntT
62+
Γ ⊢ BinOp LtInt L M ∶ BoolT
63+
5964
⊢ite : : TypingContext} {b t e : Expr} {A : TypeL}
6065
Γ ⊢ b ∶ BoolT
6166
Γ ⊢ t ∶ A
@@ -121,6 +126,7 @@ data _⊢_∶_ : TypingContext → Expr → TypeL → Set where
121126
⊢→tc (⊢+ h1 h2) rewrite ⊢→tc h1 | ⊢→tc h2 = refl
122127
⊢→tc (⊢- h1 h2) rewrite ⊢→tc h1 | ⊢→tc h2 = refl
123128
⊢→tc (⊢* h1 h2) rewrite ⊢→tc h1 | ⊢→tc h2 = refl
129+
⊢→tc (⊢< h1 h2) rewrite ⊢→tc h1 | ⊢→tc h2 = refl
124130
⊢→tc {Γ} {Ite b t e} {ty} (⊢ite tb tt te)
125131
rewrite
126132
⊢→tc {Γ} {b} {BoolT} tb
@@ -183,6 +189,9 @@ tc→⊢ {Γ} {BinOp Mul e1 e2} eq with typeCheck' Γ e1 in eqE1
183189
tc→⊢ {Γ} {BinOp Sub e1 e2} eq with typeCheck' Γ e1 in eqE1
184190
... | Just IntT with typeCheck' Γ e2 in eqE2
185191
... | Just IntT rewrite sym (Just-injective eq) = ⊢- (tc→⊢ eqE1) (tc→⊢ eqE2)
192+
tc→⊢ {Γ} {BinOp LtInt e1 e2} eq with typeCheck' Γ e1 in eqE1
193+
... | Just IntT with typeCheck' Γ e2 in eqE2
194+
... | Just IntT rewrite sym (Just-injective eq) = ⊢< (tc→⊢ eqE1) (tc→⊢ eqE2)
186195
tc→⊢ {Γ} {BinOp And e1 e2} eq with typeCheck' Γ e1 in eqE1
187196
... | Just BoolT with typeCheck' Γ e2 in eqE2
188197
... | Just BoolT rewrite sym (Just-injective eq) = ⊢&& (tc→⊢ eqE1) (tc→⊢ eqE2)

src/Lam/Agda/Lam/TypeChecker.agda

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,15 @@ typeCheck' gam (BinOp Mul e1 e2) =
6363
}
6464
; _ -> Nothing
6565
}
66+
typeCheck' gam (BinOp LtInt e1 e2) =
67+
myCaseOf (typeCheck' gam e1)
68+
λ { (Just IntT) ->
69+
myCaseOf (typeCheck' gam e2)
70+
λ { (Just IntT) -> Just BoolT
71+
; _ -> Nothing
72+
}
73+
; _ -> Nothing
74+
}
6675
typeCheck' gam (BinOp And e1 e2) =
6776
myCaseOf (typeCheck' gam e1)
6877
λ { (Just BoolT) ->

src/Lam/Handler.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ handleEval :: RawExpr -> Result ()
4949
handleEval rExpr = do
5050
gctx <- get
5151
expr <- liftEither (eraseNames gctx rExpr)
52-
liftIO (putStrLnFlush (show expr))
5352
isUntyped <- askUntyped
5453
if isUntyped then
5554
liftIO (putStrLnFlush (untypedPrettyPrint (eval expr)))

src/Lam/Parser/Parser.y

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,17 @@ import Lam.Parser.Lexer qualified as L
2424
%right "else"
2525
%left "."
2626
%right "=>"
27-
%right "&&" "||"
28-
%right "+" "-" "+T"
29-
%right "*" "*T"
30-
%right "!"
31-
%right "proj1"
32-
%right "proj2"
33-
%right "inl"
34-
%right "inr"
27+
%left "<" ">"
28+
%left "&&" "||"
29+
%left "+" "-"
30+
%right "+T"
31+
%left "*"
32+
%right "*T"
33+
%left "!"
34+
%left "proj1"
35+
%left "proj2"
36+
%left "inl"
37+
%left "inr"
3538

3639
%token
3740
"lam" { L.Lam }
@@ -162,6 +165,7 @@ RawExpr :: { RawExpr }
162165
| RawExpr "*" RawExpr { RawBinOp Mul $1 $3 }
163166
| RawExpr "&&" RawExpr { RawBinOp And $1 $3 }
164167
| RawExpr "||" RawExpr { RawBinOp Or $1 $3 }
168+
| RawExpr "<" RawExpr { RawBinOp LtInt $1 $3 }
165169
| "!" RawExpr { RawUnOp Not $2 }
166170
| "if" RawExpr "then" RawExpr "else" RawExpr { RawIte $2 $4 $6 }
167171
| "proj1" RawExpr { RawUnOp Proj1 $2 }

src/Lam/Utils.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ prettyPrint = go []
4040
ppBinOp Mul = "*"
4141
ppBinOp And = "&&"
4242
ppBinOp Or = "||"
43+
ppBinOp LtInt = "<"
4344
ppBinOp MkPair = undefined
4445
ppUnOp Not = "!"
4546
ppUnOp Proj1 = "proj1"

0 commit comments

Comments
 (0)