Skip to content

Commit 64fd826

Browse files
committed
Substitution.
1 parent 430277d commit 64fd826

File tree

2 files changed

+43
-47
lines changed

2 files changed

+43
-47
lines changed

src/Smallstep.lidr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ We can use this terminology to generalize the observation we made
486486
> iff : (p,q : Type) -> Type
487487
> iff p q = (p -> q, q -> p)
488488
489-
> infixl 6 <->
489+
> infixl 9 <->
490490
> (<->) : (p: Type) -> (q:Type) -> Type
491491
> (<->) = iff
492492

src/Stlc.lidr

Lines changed: 42 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -327,34 +327,33 @@ spelled the same as some global name `x`.
327327

328328
Here is the definition, informally...
329329

330-
`x:=s`x = s
331-
`x:=s`y = y if x <> y
332-
`x:=s`(\x:T11. t12) = \x:T11. t12
333-
`x:=s`(\y:T11. t12) = \y:T11. `x:=s`t12 if x <> y
334-
`x:=s`(t1 t2) = (`x:=s`t1) (`x:=s`t2)
335-
`x:=s`true = true
336-
`x:=s`false = false
337-
`x:=s`(if t1 then t2 else t3) =
338-
if `x:=s`t1 then `x:=s`t2 else `x:=s`t3
330+
[x:=s]x = s
331+
[x:=s]y = y if x <> y
332+
[x:=s](\x:T11. t12) = \x:T11. t12
333+
[x:=s](\y:T11. t12) = \y:T11. [x:=s]t12 if x <> y
334+
[x:=s](t1 t2) = ([x:=s]t1) ([x:=s]t2)
335+
[x:=s]true = true
336+
[x:=s]false = false
337+
[x:=s](if t1 then t2 else t3) =
338+
if [x:=s]t1 then [x:=s]t2 else [x:=s]t3
339339

340340
... and formally:
341341

342-
> mutual
343-
>
344342
> subst : String -> Tm -> Tm -> Tm
345-
> subst x s t =
346-
> case t of
347-
> Tvar x' => if x == x' then s else t
348-
> Tabs x' ty t1 =>
349-
> Tabs x' ty (if x == x' then t1 else subst x s t1)
350-
> t1 # t2 => subst x s t1 # subst x s t2
351-
> Ttrue => Ttrue
352-
> Tfalse => Tfalse
353-
> Tif t1 t2 t3 => Tif (subst x s t1) (subst x s t2) (subst x s t3)
354-
355-
> infixl 5 :=
356-
> (:=) : String -> Tm -> Tm -> Tm
357-
> (:=) x s = subst x s
343+
> subst x s (Tvar x') =
344+
> case decEq x x' of
345+
> Yes _ => s
346+
> No _ => (Tvar x')
347+
> subst x s (Tabs x' ty t1) =
348+
> Tabs x' ty (case decEq x x' of
349+
> Yes p => t1
350+
> No p => subst x s t1)
351+
> subst x s (t1 # t2) = subst x s t1 # subst x s t2
352+
> subst x s Ttrue = Ttrue
353+
> subst x s Tfalse = Tfalse
354+
> subst x s (Tif t1 t2 t3) = Tif (subst x s t1) (subst x s t2) (subst x s t3)
355+
356+
> syntax "[" [p] ":=" [q] "]" [r] = subst p q r
358357

359358
_Technical note_: Substitution becomes trickier to define if we
360359
consider the case where `s`, the term being substituted for a
@@ -375,9 +374,9 @@ been "captured" by the binder at the beginning of `t`.
375374
Why would this be bad? Because it violates the principle that the
376375
names of bound variables do not matter. For example, if we rename
377376
the bound variable in `t`, e.g., let `t' = \w:Bool. z`, then
378-
``x:=s`t'` is `\w:Bool. \x:Bool. r`, which does not behave the
379-
same as ``x:=s`t = \r:Bool. \x:Bool. r`. That is, renaming a
380-
bound variable changes how `t` behaves under substitution. *)
377+
``x:=s[t'] is `\w:Bool. \x:Bool. r`, which does not behave the
378+
same as `[x:=s]t = \r:Bool. \x:Bool. r`. That is, renaming a
379+
bound variable changes how `t` behaves under substitution.
381380

382381
See, for example, `Aydemir 2008` for further discussion
383382
of this issue.
@@ -392,25 +391,22 @@ one of the constructors; your job is to fill in the rest of the
392391
constructors and prove that the relation you've defined coincides
393392
with the function given above.
394393

395-
-- > data substi : Tm -> Tm -> Type where -- (s:Tm) (x:string) : :=
396-
-- > S_Var1 : (s:Tm) -> (x:string) -> substi s x (Tvar x) s
397-
-- > S_Var1 : (s:Tm) -> (x:string) -> substi s x (Tvar x) s
398-
--
399-
--
400-
-- > Tvar x' => if x == x' then s else t
401-
-- > Tabs x' ty t1 =>
402-
-- > Tabs x' ty (if x == x' then t1 else subst x s t1)
403-
-- > t1 # t2 => subst x s t1 # subst x s t2
404-
-- > Ttrue => Ttrue
405-
-- > Tfalse => Tfalse
406-
-- > Tif t1 t2 t3 => Tif (subst x s t1) (subst x s t2) (subst x s t3)
407-
408-
409-
Theorem substi_correct : forall s x t t',
410-
`x:=s`t = t' <-> substi s x t t'.
411-
Proof.
412-
(* FILL IN HERE *) Admitted.
413-
(** `` *)
394+
> data Substi : (s:Tm) -> (x:String) -> Tm -> Tm -> Type where
395+
> S_True : Substi s x Ttrue Ttrue
396+
> S_False : Substi s x Tfalse Tfalse
397+
> S_App : {l', r':Tm} -> Substi s x l l' -> Substi s x r r' -> Substi s x (l # r) (l' # r')
398+
> S_If : {b', p',n':Tm} -> Substi s x b b' -> Substi s x p p'
399+
> -> Substi s x n n' -> Substi s x (Tif b p n) (Tif b' p' n')
400+
> S_Var1 : Substi s x (Tvar x) s
401+
> S_Var2 : Substi s x (Tvar y) (Tvar y)
402+
> S_Abs1 : Substi s x t t' -> Substi s x (Tabs x' ty t) (Tabs x' ty t')
403+
> S_Abs2 : Substi s x (Tabs y ty t) (Tabs y ty t)
404+
405+
406+
> substi_correct : (s:Tm) -> (x: String) -> (t : Tm) -> (t' : Tm) ->
407+
> (([ x := s ] t) = t') <-> Substi s x t t'
408+
> substi_correct s x t t' = ?substi_correct_rhs1
409+
414410

415411
(* ================================================================= *)
416412
(** ** Reduction *)

0 commit comments

Comments
 (0)