From b42ea5e994de56ffc7d4ec0e723261a147179b15 Mon Sep 17 00:00:00 2001 From: Felipe Bañados Schwerter Date: Mon, 24 Oct 2022 10:46:18 -0600 Subject: Specified implementation in coq --- entries/fbanados/fib.v | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 entries/fbanados/fib.v diff --git a/entries/fbanados/fib.v b/entries/fbanados/fib.v new file mode 100644 index 0000000..e9fe796 --- /dev/null +++ b/entries/fbanados/fib.v @@ -0,0 +1,90 @@ +Require Import Coq.Arith.Wf_nat. (* import strong induction on naturals *) +Require Import Lia. (* Linear Integer Arithmetic solver *) + +(* First, simple specification *) + +Fixpoint fib_simpl (n : nat) {struct n} : nat := + match n with + | 0 => 1 + | S n => match n with + | 0 => 1 + | S m => fib_simpl n + fib_simpl m + end + end. + +Example fib_3 : (fib_simpl 2 = 2). reflexivity. Qed. +Example fib_4 : (fib_simpl 3 = 3). reflexivity. Qed. +Example fib_5 : (fib_simpl 4 = 5). reflexivity. Qed. + +Lemma fib_simpl_spec : forall n, (* useful lemma for rewriting *) + fib_simpl (S (S n)) = fib_simpl (S n) + fib_simpl n. +Proof. + destruct n. + - (* case n := 0 *) + reflexivity. + - (* case n := (S n) *) + destruct n; reflexivity. +Qed. + +(* For a (slightly) faster version *) + +Fixpoint fib_acc_aux (n : nat) (acc : nat) (prev : nat) {struct n} : nat := + match n with + | 0 => acc + prev + | S n => fib_acc_aux n (acc + prev) acc + end. + +Definition fib_faster (n : nat) : nat := + match n with + | 0 => 1 + | S n => match n with + | 0 => 1 + | S m => (fib_acc_aux m 1 1) + end + end. + +Example fib_faster_3 : (fib_faster 2 = 2). reflexivity. Qed. +Example fib_faster_4 : (fib_faster 3 = 3). reflexivity. Qed. +Example fib_faster_5 : (fib_faster 4 = 5). reflexivity. Qed. + +Lemma fib_acc_aux_rewrite : forall n a b c d, + fib_acc_aux n a c + fib_acc_aux n b d = fib_acc_aux n (a + b) (c + d). +Proof. + induction n; intros. + - (* case n := 0 *) + simpl; lia. + - (* case n := (S n) *) + simpl. + rewrite IHn. + f_equal. + lia. +Qed. + +Theorem fib_faster_spec: forall n, fib_simpl n = fib_faster n. +Proof. + induction n using lt_wf_ind. (* use strong induction *) + rename H into strong_induction_hypothesis. + destruct n. (* lt_wf_ind does not deal immediately with cases *) + - (* case n := 0 *) + reflexivity. + - (* case n := (S n) *) + destruct n. + + (* case n := 1 *) + reflexivity. + + (* case n := (S (S n)) *) + rewrite ?fib_simpl_spec. + do 2 rewrite strong_induction_hypothesis by lia. + enough (forall n, fib_faster (S n) + fib_faster n = fib_faster (S (S n))) as lemma by apply lemma. + clear. + destruct n. + * (* for lemma, case n := 0 *) + reflexivity. + * (* for lemma, case n := S n *) + destruct n. + -- (* for lemma, case n := 1 *) + reflexivity. + -- (* for lemma, case n := S (S n) *) + simpl. + apply fib_acc_aux_rewrite. +Qed. + -- cgit v1.2.3-70-g09d2 From d642394b47cadfa4003369c8e0d2c828c9091bb8 Mon Sep 17 00:00:00 2001 From: Felipe Bañados Schwerter Date: Mon, 24 Oct 2022 10:48:14 -0600 Subject: Added fbanados --- people.json | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/people.json b/people.json index 248c644..2b78064 100644 --- a/people.json +++ b/people.json @@ -187,5 +187,16 @@ "link": "https://github.com/wilbowma/fib-lang/tree/2ec2d1dfd141220882d824cf3dac5b374ed291f3" } ] - } + }, + { + "github": "fbanados", + "name": "Felipe Bañados Schwerter", + "title": "PhD Candidate, UBC", + "entries": [ + { + "name": "coq", + "link": "./entries/fbanados/fib.v" + } + ] + }, ] -- cgit v1.2.3-70-g09d2 From b0644202ce201774445a09f103908498f3838820 Mon Sep 17 00:00:00 2001 From: Felipe Bañados Schwerter Date: Mon, 24 Oct 2022 11:27:43 -0600 Subject: Base smalltalk export --- entries/fbanados/fib.st | 1 + 1 file changed, 1 insertion(+) create mode 100644 entries/fbanados/fib.st diff --git a/entries/fbanados/fib.st b/entries/fbanados/fib.st new file mode 100644 index 0000000..8431a09 --- /dev/null +++ b/entries/fbanados/fib.st @@ -0,0 +1 @@ +'From Pharo10.0.0 of 15 June 2022 [Build information: Pharo-10.0.0+build.521.sha.14f541319d443f4261f84f4fa19fbb34460a5edb (64 Bit)] on 24 October 2022 at 11:26:20.436554 am'! !Integer methodsFor: '*Fibonacci' stamp: 'FelipeBanados 10/24/2022 11:13'! fibonacciNumber self <= 2 ifTrue: [ ^ 1 ]. ^ (self - 1) fibonacciNumber + (self - 2) fibonacciNumber.! ! \ No newline at end of file -- cgit v1.2.3-70-g09d2 From bd22843d89bbf1966aca51f5b81f44f5d6766b1b Mon Sep 17 00:00:00 2001 From: Felipe Bañados Schwerter Date: Mon, 24 Oct 2022 11:28:47 -0600 Subject: Fixed people --- people.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/people.json b/people.json index 2b78064..dc0453e 100644 --- a/people.json +++ b/people.json @@ -198,5 +198,5 @@ "link": "./entries/fbanados/fib.v" } ] - }, + } ] -- cgit v1.2.3-70-g09d2 From ac7935ad693adac61f7fe0a6c9ec1344f3297c48 Mon Sep 17 00:00:00 2001 From: Felipe Bañados Schwerter Date: Mon, 24 Oct 2022 11:33:02 -0600 Subject: Ok made it readable --- entries/fbanados/fib.st | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/entries/fbanados/fib.st b/entries/fbanados/fib.st index 8431a09..099407c 100644 --- a/entries/fbanados/fib.st +++ b/entries/fbanados/fib.st @@ -1 +1,8 @@ -'From Pharo10.0.0 of 15 June 2022 [Build information: Pharo-10.0.0+build.521.sha.14f541319d443f4261f84f4fa19fbb34460a5edb (64 Bit)] on 24 October 2022 at 11:26:20.436554 am'! !Integer methodsFor: '*Fibonacci' stamp: 'FelipeBanados 10/24/2022 11:13'! fibonacciNumber self <= 2 ifTrue: [ ^ 1 ]. ^ (self - 1) fibonacciNumber + (self - 2) fibonacciNumber.! ! \ No newline at end of file +'From Pharo10.0.0 of 15 June 2022 [Build information: Pharo-10.0.0+build.521.sha.14f541319d443f4261f84f4fa19fbb34460a5edb (64 Bit)] on 24 October 2022 at 11:26:20.436554 am'! + +!Integer methodsFor: '*Fibonacci' stamp: 'FelipeBanados 10/24/2022 11:13'! +fibonacciNumber + self <= 2 ifTrue: [ ^ 1 ]. + ^ (self - 1) fibonacciNumber + (self - 2) fibonacciNumber. + +! ! -- cgit v1.2.3-70-g09d2 From 3f2544bf59ebd4839a6e545cd067a4e0c8cb349a Mon Sep 17 00:00:00 2001 From: Felipe Bañados Schwerter Date: Mon, 24 Oct 2022 11:35:21 -0600 Subject: Added second entry --- people.json | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/people.json b/people.json index dc0453e..2543c07 100644 --- a/people.json +++ b/people.json @@ -196,6 +196,10 @@ { "name": "coq", "link": "./entries/fbanados/fib.v" + }, + { + "name": "pharo-smalltalk", + "link": "./entries/fbanados/fib.st" } ] } -- cgit v1.2.3-70-g09d2