SF习题答案(1)(LF-Basic)

本文是关于《Software Foundations》第一卷《Logical Foundations》的习题解答,涉及布尔运算、自然数乘法、比较、迭代定义等概念,通过Coq进行形式化证明。
摘要由CSDN通过智能技术生成

介绍

Coq是一个定理证明辅助助手,在对一些关键系统进行形式化证明时十分重要的证明工具。本博客时针对自《Software Foundation》的第一卷《LOGICAL FOUNDATIONS》中的习题进行解答,在阅读的过程中请参考书籍进行阅读。

Bsaic

Exercise 1: 1 star, standard (nandb)

Remove “Admitted.” and complete the definition of the following function; then make sure that the Example assertions below can each be verified by Coq. (I.e., fill in each proof, following the model of the orb tests above.) The function should return true if either or both of its inputs are false.
Definition nandb (b1:bool) (b2:bool) : bool
(* REPLACE THIS LINE WITH “:= your_definition .” ). Admitted.
Example test_nandb1: (nandb true false) = true.
(
FILL IN HERE ) Admitted.
Example test_nandb2: (nandb false false) = true.
(
FILL IN HERE ) Admitted.
Example test_nandb3: (nandb false true) = true.
(
FILL IN HERE ) Admitted.
Example test_nandb4: (nandb true true) = false.
(
FILL IN HERE *) Admitted.

解析:参考讲解中andb和orb的定义,根据几个测试的参数及输出得出定义,并更改测试中的证明语句,自动验证定义的正确。

Definition nandb (b1:bool) (b2:bool) : bool :=
  match b1 with
  | true => match b2 with
            | true => false
            | false => true
            end
  | false => match b2 with
            | true => true
            | false => true
            end
  end.

Example test_nandb1: (nandb true false) = true.
Proof. simpl. reflexivity. Qed.
Example test_nandb2: (nandb false false) = true.
Proof. simpl. reflexivity. Qed.
Example test_nandb3: (nandb false true) = true.
Proof. simpl. reflexivity. Qed.
Example test_nandb4: (nandb true true) = false.
Proof. simpl. reflexivity. Qed.

Exercise 2: 1 star, standard (andb3)

Do the same for the andb3 function below. This function should return true when all of its inputs are true, and false otherwise.
Definition andb3 (b1:bool) (b2:bool) (b3:bool) : bool
(* REPLACE THIS LINE WITH “:= your_definition .” ). Admitted.
Example test_andb31: (andb3 true true true) = true.
(
FILL IN HERE ) Admitted.
Example test_andb32: (andb3 false true true) = false.
(
FILL IN HERE ) Admitted.
Example test_andb33: (andb3 true false true) = false.
(
FILL IN HERE ) Admitted.
Example test_andb34: (andb3 true true false) = false.
(
FILL IN HERE *) Admitted.

解析:和上一题思考过程相似。

Definition andb3 (b1:bool) (b2:bool) (b3:bool) : bool :=
  match b1 with
  | false => false
  | true => match b2 with
            | false => false
            | true => b3
            end
  end.

Example test_andb31: (andb3 true true true) = true.
Proof. simpl. reflexivity.  Qed.
Example test_andb32: (andb3 false true true) = false.
Proof. simpl. reflexivity.  Qed.
Example test_andb33: (andb3 true false true) 
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值