<编程珠玑>笔记(二) 程序验证

在芯片设计(IC)领域有专门的职位叫做芯片验证工程师,其中的一种方法叫形式验证(Formal Verification),具体包括等价性检查,模型检查和定理证明。

本章所讲的程序验证方法(不要与软件测试混为一谈),与芯片行业的形式验证法非常相似。参考芯片行业,随着分工的细化,软件也会有专门的验证工程师。

1  Binary search

determine whether the sorted array x[0..n-1] contains the target element t

mustbe(range): the key idea is that we always know that if t is anywhere in x[0..n-1], then it must be in a certain range of x

1) sketch

initialize range to 0..n-1
loop
    { invariant: mustbe(range) }
    if range is empty,
        break and report that t is not in the array
    compute m, the middle of the range
    use m as a probe to shrink the range
        if t is found during the shrinking process
        break and report its position 

2) refine

lo = 0, up = n-1
loop
    { mustbe(lo, up) }
    if lo > up
        p = -1; break
    m = lo/2 + (up - lo)/2;
    case
        x[m] < t:    lo = m + 1;
        x[m] == t:  p = m; break
        x[m] > t:    up = m -1; 

3) program

 1 { musbe(0, n-1) }
 2 lo = 0; up = n-1
 3 { mustbe(lo, up) }
 4 loop
 5     { mustbe(lo, up) }
 6     if lo > up
 7         { lo > up && mustbe(lo, up) }
 8         { t is not in the array }
 9         p = -1; break
10     { mustbe(lo, up) && lo <= up }
11     m = lo/2 + (up - lo)/2;
12     {mustbe(lo, up) && lo <= m <= up}
13     case
14         x[m] < t:
15                 { mustbe(lo, up) && cantbe(0, m)}
16                 { mustbe(m+1, up) }
17                 lo = m +1
18                 { mustbe(lo, up) }
19         x[m] == t:
20                 { x[m] == t}
21                 p = m; break
22         x[m] > t:
23                 { mustbe(lo, up) && cantbe(m. n-1) }
24                 { mustbe(lo, m-1) }
25                 u = m-1
26                 { mustbe(lo, up) }
27     { mustbe(lo, up) }

2  Program verification

1) assertions

inputs, variables, and output

2) sequential control structures

"do this statement and then that statement"

place assertions between them and analyze each step of the program‘ progress individually

3) selection control structures

"if", "case": one of many choices is selected

consider each of the several choices individually

4) iteration control structures

initialization: invariation is true when the loop is executed for the first time

preservation: invariation is true before and after each iteration of loop

termination: the desired result is true whenever execution of the loop terminates

5) functions

precondition: the state(inputs, variables) must be true before it is called

postcondition: what the function will guarantee on termination

int  bsearch( int t, int x[], int n )
/*  precondition: x[0] <= x[1] <= ... <= x[n-1]
     postcondition:
          result == -1       => t not present in x
          0 <= result < n  => x[result] == t
*/  
时间: 2024-08-08 11:01:33

<编程珠玑>笔记(二) 程序验证的相关文章

&lt;编程珠玑&gt;笔记(二) 三个算法

在第二章里,作者提出了三个问题,然后慢慢引出对应的算法实现. 1  Binary search 二分查找 Given a sequential file that contain at most 4x109 32-bit integers in random order, find a 32-bit integer that is not in the file. How would you solve this problem with ample main memory? How would

&lt;编程珠玑&gt;笔记(三) 四条原则

第三章作者重在阐述一种编程观念, 即 “data does indeed strcture programs” 这一章貌似没什么干货,只好把作者的几个例子贴出来,反复看看了. 1  A survey program   Total US Citi Perm Visa Temp Visa Male Female African American 1289 1239 17 2 684 593 Mexican American 675 577 80 11 448 219 Native American

FFmpeg编程学习笔记二:音频重采样

ffmpeg实现音频重采样的核心函数swr_convert功能非常强大,可是ffmpeg文档对它的注释太过简单,在应用中往往会出这样那样的问题,其实在读取数据->重采样->编码数据的循环中在第一次执行swr_convert后还应用swr_convert再作个缓存检测看看是否还有数据,如果有就要把它写到FIFO中去,留在下次再使用,这点在转码和由低向高转换采样率时特别重要. 下面一段简单的代码,摘自我自已写的测试程序. const int frame_size = FFMIN(fifo_size

linux sheel编程学习笔记(二) --- grep命令

Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全局正则表达式版本,它的使用权限是所有用户. grep的工作方式是这样的,它在一个或多个文件中搜索字符串模板.如果模板包括空格,则必须被引用,模板后的所有字符串被看作文件名.搜索的结果被送到标准输出,不影响原文件内容. grep可用于shell脚本,因为grep通过返回一个状态值来说明搜索的状态,如果模板搜索成

《编程珠玑》---笔记。浏览此文,一窥此书。

第一章: 磁盘排序:对于一个提出的问题,不要未经思考就直接给出答案.要先深入研究问题,搞清楚这个问题的特点,根据这个特点,可能有更好的解决方案. 比如:文中:最初的需求只是"我如何对磁盘文件排序". 我们首先想到了经典的归并排序. 但,进一步了解到排序的内容是10000000个记录,每条记录都是一个7位整数,且只有1M可用的内存.每条记录不相同. [位示图法,详见我的关于排序的博文] 第二章: 三个问题: 1.给定一个包含32位整数的顺序文件,它至多只能包含40亿个这样的整数,并且整数

《编程珠玑》笔记:数组循环左移

问题描述:数组元素循环左移,将包含 num_elem 个元素的一维数组 arr[num_elem] 循环左移 rot_dist 位.能否仅使用数十个额外字节的存储空间,在正比于num_elem的时间内完成数组的旋转? 一:Bentley's Juggling Alogrithm 移动变量 arr[0] 到临时变量 tmp,移动 arr[rot_dist] 到 arr[0],arr[2rot_dist] 到 arr[rot_dist],依此类推,直到返回到取 arr[0] 中的元素,此时改为从 t

linux网络编程学习笔记之二 -----错误异常处理和各种碎碎(更新中)

errno 在unix系统中对大部分系统调用非正常返回时,通常返回值为-1,并设置全局变量errno(errno.h),如socket(), bind(), accept(), listen().erron存放一个正整数来保存上次出错的错误值. 对线程而言,每个线程都有专用的errno变量,不必考虑同步问题. strerror converts to English (Note: use strerror_r for thread safety) perror is simplified str

Win8 HTML5与JS编程学习笔记(二)

近期一直受到win8应用的Grid布局困扰,经过了半下午加半个晚上的奋斗,终于是弄明白了Grid布局方法的规则.之前我是阅读的微软官方的开发教程,书中没有详细说明CSS3的布局规则,自己鼓捣了半天也是一头雾水,于是又找到了官方的启蒙教程的布局一张,仔细阅读了一遍,又思考了代码,并在代码的基础上实验,终于是明白了布局方法.官方教程地址是:http://msdn.microsoft.com/zh-cn/library/windows/apps/jj841108.aspx 微软基于CSS3开发了Gri

多线程编程学习笔记——async和await(二)

接上文 多线程编程学习笔记——async和await(一) 三.   对连续的异步任务使用await操作符 本示例学习如何阅读有多个await方法方法时,程序的实际流程是怎么样的,理解await的异步调用 . 1.示例代码如下. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Thread