playground/coq/unfinished/chlipala-oplls.v

75 lines
1.8 KiB
Coq

Require Import ArithRing. (*Ring*)
Inductive exp : Set :=
| Constant : nat -> exp
| Plus : exp -> exp -> exp
| Times : exp -> exp -> exp.
Fixpoint eval (e : exp) : nat :=
match e with
| Constant n => n
| Plus e1 e2 => (eval e1) + (eval e2)
| Times e1 e2 => (eval e1) * (eval e2)
end.
Fixpoint commuter (e : exp) : exp :=
match e with
| Constant n => e
| Plus e1 e2 => Plus (commuter e2) (commuter e1)
| Times e1 e2 => Times (commuter e2) (commuter e1)
end.
Compute eval (Plus (Constant 3) (Constant 4)).
Compute eval (commuter (Plus (Constant 3) (Constant 4))).
Theorem commuter_equiv : forall e : exp, (eval e) = (eval (commuter e)).
Proof.
intros e.
induction e.
- reflexivity.
- simpl.
rewrite <- IHe1, <- IHe2. (* or rewrite in other direction *)
ring. (* based on properties of semi-rings *)
- simpl.
rewrite IHe1, IHe2.
ring.
Qed.
Theorem commuter_equiv' : forall e : exp, (eval e) = (eval (commuter e)).
Proof.
intros e.
induction e; simpl.
- reflexivity.
- rewrite IHe1, IHe2.
ring.
- rewrite IHe1, IHe2.
ring.
Qed.
Theorem commuter_equiv'' : forall e : exp, (eval e) = (eval (commuter e)).
(* Example of proof automation. This scales better.
When things like addition of a new constructor happens. *)
Proof.
induction e; simpl;
repeat match goal with
| [ H : _ = _ |- _ ] => rewrite H
end; ring.
Qed.
(** * Untyped lambda calculus *)
Require Import String.
Inductive term : Set :=
| Var : string -> term
| Abs : string -> term -> term
| App : term -> term -> term.
Check string_dec.
(* string_dec : forall s1 s2 : string, {s1 = s2} + {s1 <> s2} *)
Definition subst (var : string) (repl t : term) : term :=
match t with
| Var x => if string_dec x var then var else t
| Abs x t => if string
| App t1 t2 => App (subst orig repl t1) (subst orig repl t2)
end.