类型分析,个人理解就是(通过静态分析技术)分析出代码中,哪些地方只能是某种或某几种数据类型,这是一种约束。

 

例如,给定一个程序:

其中,我们可以很直接地得到一些约束:

最后,经过简化可以得到:

对于给定的变量类型,如果他们不符合这个约束,则说明,他们是不合法的。

那么,怎么去提取以及维护这些约束呢?

采用一种“并查集”的结构:一个有向图,每个节点有一条边指向父节点(父节点则指向自己)。如果两个节点具有相同的父节点,那么,这个两节点就认为是等价的,即含有相同的数据类型。

以下是并查集的基本算法:

The unification algorithm uses union-find by associating a node with each term (including sub-terms) in the constraint system.

For each term τ we initially invoke MakeSet(τ ).

Note that each term at this point is either a type variable or a proper type (i.e. integer, heap pointer, or function); μ terms are only produced for presenting solutions to constraints, as explained below.

For each constraint τ1 = τ2 we invoke Unify(τ1, τ2), which unifies the two terms if possible and enforces the general term equality axiom by unifiying sub-terms recursively: 

Unification fails if attempting to unify two terms with different constructor (where function constructors are considered different if they have different arity). 

再来看个例子:

对于递归:

Limitations of the Type Analysis 

例子:

 

运行的时候没问题,但是,遵循之前的方法会报错,之前的方法并不考虑程序的顺序执行给数据类型转换的影响。即X=42在X=alloc之后,因此,最终返回的一定是int型。

另一个例子:

最新文章

  1. rabbitmq使用dead letter机制来进行retry
  2. .NET基础之自定义泛型
  3. BIOS与CMOS有什么区别
  4. Google开源库-Volley
  5. macbook air 安装win7系统时,到最后一步要进入win7,需要给PC设置一个用户名,键盘没反应
  6. C++编程规范之19:总是初始化变量
  7. jsp页面判断文件上传类型
  8. Java Enum使用演示样品枚举
  9. Nlpir Parser敏感词搜索灵玖语义技术应用
  10. Activiti简介
  11. 1.httpClient和ScrollView
  12. 【转】asp.net获取当前页面的url地址
  13. NI Vision ClampRake修改
  14. spring boot学习(2) SpringBoot 项目属性配置
  15. Maven构建时跳过部分测试
  16. BZOJ3997:[TJOI2015]组合数学(DP,Dilworth定理)
  17. RabbitMQ之Queues-5
  18. Android集成百度地图SDK
  19. Part5核心初始化_lesson4---关闭mmu
  20. 泛型2(lambda表达式/参数绑定)

热门文章

  1. 单向LSTM笔记, LSTM做minist数据集分类
  2. django中数据库操作有关部分
  3. django项目的新建相关的命令及配置
  4. Vue(二十三)vuex + axios + 缓存 运用 (以登陆功能为例)
  5. js 创建Table,每行3列的方式
  6. Docker命令使用详解(转)
  7. JSP(8)—EL案例和JSTL案例
  8. SSH error ( Read from socket failed: Connection reset by peer ) and it's solution
  9. 减少apk包大小的一种思路
  10. [Python设计模式] 第21章 计划生育——单例模式