Skip to content

Commit 7605821

Browse files
author
Alex Gryzlov
committed
second draft of Imp
1 parent 2986e4c commit 7605821

File tree

2 files changed

+37
-42
lines changed

2 files changed

+37
-42
lines changed

src/Imp.lidr

Lines changed: 37 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -323,24 +323,24 @@ _Proof_: By induction on `a`. Most cases follow directly from the `IH`. The
323323
remaining cases are as follows:
324324
325325
- Suppose `a = ANum n` for some `n`. We must show
326-
```
326+
```
327327
aeval (optimize_0plus (ANum n)) = aeval (ANum n).
328-
```
328+
```
329329
This is immediate from the definition of `optimize_0plus`.
330330
331331
- Suppose `a = APlus a1 a2` for some `a1` and `a2`. We must show
332-
```
332+
```
333333
aeval (optimize_0plus (APlus a1 a2)) = aeval (APlus a1 a2).
334334
```
335-
335+
336336
Consider the possible forms of `a1`. For most of them, `optimize_0plus`
337337
simply calls itself recursively for the subexpressions and rebuilds a new
338338
expression of the same form as `a1`; in these cases, the result follows
339339
directly from the `IH`. The interesting case is when `a1 = ANum n` for some
340340
`n`. If `n = ANum 0`, then
341-
```
341+
```
342342
optimize_0plus (APlus a1 a2) = optimize_0plus a2
343-
```
343+
```
344344
and the `IH` for `a2` is exactly what we need. On the other hand, if `n = S
345345
n'` for some `n'`, then again `optimize_0plus` simply calls itself
346346
recursively, and the result follows from the `IH`. $\square$
@@ -571,13 +571,13 @@ data AEvalR : AExp0 -> Nat -> Type where
571571
AEvalR e1 n1 ->
572572
AEvalR e2 n2 ->
573573
AEvalR (AMult0 e1 e2) (n1 * n2)
574-
```
574+
```
575575
576576
\todo[inline]{Edit}
577577
578578
It will be convenient to have an infix notation for \idr{AEvalR}. We'll write
579579
\idr{e |/ n} to mean that arithmetic expression \idr{e} evaluates to value
580-
\idr{n}.
580+
\idr{n}.
581581
582582
In fact, Idris provides a way to use this notation in the definition of
583583
\idr{AevalR} itself. This reduces confusion by avoiding situations where we're
@@ -1304,9 +1304,9 @@ definitions. This section explores some examples.
13041304
13051305
\todo[inline]{Edit}
13061306
1307-
Inverting Heval essentially forces Idris to expand one step of the ceval
1308-
computation — in this case revealing that st' must be st extended with the new
1309-
value of X, since plus2 is an assignment
1307+
Inverting `Heval` essentially forces Idris to expand one step of the `CEval`
1308+
computation — in this case revealing that `st'` must be st extended with the new
1309+
value of `X`, since `plus2` is an assignment
13101310
13111311
> plus2_spec prf (E_Ass aev) = rewrite sym aev in rewrite prf in Refl
13121312
@@ -1323,12 +1323,16 @@ $\square$
13231323
==== Exercise: 3 stars, recommended (loop_never_stops)
13241324
13251325
> loop_never_stops : Not (Imp.loop / st |/ st')
1326-
> loop_never_stops = ?loop_never_stops_rhs
1326+
> loop_never_stops contra = ?loop_never_stops_rhs
13271327
1328+
\todo[inline]{Edit}
1329+
1330+
```coq
13281331
Proof.
13291332
intros st st' contra. unfold loop in contra.
13301333
remember (WHILE BTrue DO SKIP END) as loopdef
13311334
eqn:Heqloopdef.
1335+
```
13321336
13331337
Proceed by induction on the assumed derivation showing that loopdef terminates.
13341338
Most of the cases are immediately contradictory (and so can be solved in one
@@ -1526,18 +1530,12 @@ with an additional case.
15261530
> CIfB : BExp -> ComB -> ComB -> ComB
15271531
> CWhileB : BExp -> ComB -> ComB
15281532
1529-
Notation "'SKIP'" :=
1530-
CSkip.
1531-
Notation "'BREAK'" :=
1532-
CBreak.
1533-
Notation "x '::=' a" :=
1534-
(CAss x a) (at level 60).
1535-
Notation "c1 ;; c2" :=
1536-
(CSeq c1 c2) (at level 80, right associativity).
1537-
Notation "'WHILE' b 'DO' c 'END'" :=
1538-
(CWhile b c) (at level 80, right associativity).
1539-
Notation "'IFB' c1 'THEN' c2 'ELSE' c3 'FI'" :=
1540-
(CIf c1 c2 c3) (at level 80, right associativity).
1533+
> syntax SKIP' = CSkipB
1534+
> syntax BREAK' = CBreakB
1535+
> syntax [x] "::='" [a] = CAssB x a
1536+
> syntax [c1] ";;'" [c2] = CSeqB c1 c2
1537+
> syntax WHILE' [b] DO [c] END = CWhileB b c
1538+
> syntax IFB' [c1] THEN [c2] ELSE [c3] FI = CIfB c1 c2 c3
15411539
15421540
Next, we need to define the behavior of \idr{BREAK}. Informally, whenever
15431541
\idr{BREAK} is executed in a sequence of commands, it stops the execution of
@@ -1550,7 +1548,7 @@ One important point is what to do when there are multiple loops enclosing a
15501548
given \idr{BREAK}. In those cases, \idr{BREAK} should only terminate the
15511549
_innermost_ loop. Thus, after executing the following...
15521550
1553-
```idris
1551+
```
15541552
X ::= 0;;
15551553
Y ::= 1;;
15561554
WHILE 0 ≠ Y DO
@@ -1572,18 +1570,15 @@ evaluation relation that specifies whether evaluation of a command executes a
15721570
> SContinue : Result
15731571
> SBreak : Result
15741572
1575-
Reserved Notation "c1 '/' st '||//' s '/' st'"
1576-
(at level 40, st, s at level 39).
1577-
1578-
Intuitively, `c / st ||// s / st'` means that, if \idr{c} is started in state
1573+
Intuitively, \idr{c // st |/ s / st'} means that, if \idr{c} is started in state
15791574
\idr{st}, then it terminates in state \idr{st'} and either signals that the
15801575
innermost surrounding loop (or the whole program) should exit immediately
15811576
(\idr{s = SBreak}) or that execution should continue normally (\idr{s =
15821577
SContinue}).
15831578
1584-
The definition of the "`c / st ||// s / st'`" relation is very similar to the
1585-
one we gave above for the regular evaluation relation (`c / st ||// st'`) — we
1586-
just need to handle the termination signals appropriately:
1579+
The definition of the "\idr{c // st |/ s / st'}" relation is very similar to the
1580+
one we gave above for the regular evaluation relation (\idr{c / st |/ st'}) —
1581+
we just need to handle the termination signals appropriately:
15871582
15881583
- If the command is \idr{SKIP}, then the state doesn't change and execution of
15891584
any enclosing loop can continue normally.
@@ -1622,37 +1617,38 @@ relation.
16221617
> E_SkipB : CEvalB CSkipB st SContinue st
16231618
> -- FILL IN HERE
16241619
1625-
where "c1 '/' st '||//' s '/' st'" := (ceval c1 st s st').
1620+
> syntax [c1] "//" [st] "|/" [s] "/" [st'] = CEvalB c1 st s st'
16261621
16271622
Now prove the following properties of your definition of \idr{CEvalB}:
16281623
1629-
> break_ignore : CEvalB (CSeqB CBreakB c) st s st' -> st = st'
1624+
> break_ignore : (((BREAK') ;;' (c)) // st |/ s / st') -> st = st'
16301625
> break_ignore x = ?break_ignore_rhs
16311626
1632-
> while_continue : CEvalB (CWhileB b c) st s st' -> s = SContinue
1627+
> while_continue : ((WHILE' b DO c END) // st |/ s / st') -> s = SContinue
16331628
> while_continue x = ?while_continue_rhs
16341629
1635-
> while_stops_on_break : beval st b = True -> CEvalB c st SBreak st' ->
1636-
> CEvalB (CWhileB b c) st SContinue st'
1630+
> while_stops_on_break : beval st b = True ->
1631+
> (c // st |/ SBreak / st') ->
1632+
> ((WHILE' b DO c END) // st |/ SContinue / st')
16371633
> while_stops_on_break prf x = ?while_stops_on_break_rhs
16381634
16391635
$\square$
16401636
16411637
16421638
==== Exercise: 3 stars, advanced, optional (while_break_true)
16431639
1644-
> while_break_true : CEvalB (CWhileB b c) st SContinue st' ->
1640+
> while_break_true : ((WHILE' b DO c END) // st |/ SContinue / st') ->
16451641
> beval st' b = True ->
1646-
> (st'' ** CEvalB c st'' SBreak st')
1642+
> (st'' ** c // st'' |/ SBreak / st')
16471643
> while_break_true x prf = ?while_break_true_rhs
16481644
16491645
$\square$
16501646
16511647
16521648
==== Exercise: 4 stars, advanced, optional (cevalB_deterministic)
16531649
1654-
> cevalB_deterministic : CEvalB c st s1 st1 ->
1655-
> CEvalB c st s2 st2 ->
1650+
> cevalB_deterministic : (c // st |/ s1 / st1) ->
1651+
> (c // st |/ s2 / st2) ->
16561652
> (st1 = st2, s1 = s2)
16571653
> cevalB_deterministic x y = ?cevalB_deterministic_rhs
16581654

src/Maps.lidr

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ https://github.com/idris-lang/Idris-dev/pull/3925 happens}
9393
Uninhabited (False = True) where
9494
uninhabited Refl impossible
9595

96-
9796
> beq_id_true_iff : (beq_id x y = True) <-> x = y
9897
> beq_id_true_iff = (bto, bfro)
9998
> where

0 commit comments

Comments
 (0)