我正在阅读this文章,该文章解释了Haskell的非严格语义。我理解,直到作者开始在Haskell中讨论部分和无限列表。
作者说: -
这个想法是将无限列表理解为部分列表的限制。
之后,作者继续解释表达式的执行: -
17:36:12 - DEBUG: Preparing Injection Hibernate Session and Transaction process: /addStudent - Method: com.struts2hibernatepagination.action.AddStudentAction.execute()
17:36:12 - DEBUG: Full Hibernate Plugin's Session Factory: destroy factory required...
17:36:12 - DEBUG: Full Hibernate Plugin's Session Factory: C3P0 not found
17:36:12 - DEBUG: Full Hibernate Plugin's Session Factory: All SessionFactories Destroyed sucessful
17:36:12 - DEBUG: Hibernate Session Required (from current Thread) - SessionFactory required: (default)
17:36:12 - DEBUG: No Hibernate Session in current thread. New Hibernate Session will be created and returned (SessionFactory "(default)")
17:36:12 - DEBUG: New Hibernate Session required - SessionFactory required: (default)
17:36:12 - DEBUG: Full Hibernate Plugin's Session Factory build started...
17:36:12 - DEBUG: Full Hibernate Plugin's Session Factory using Hibernate Annotation Configuration
17:36:13 - DEBUG: Full Hibernate Plugin's Session Factory configuration file "/hibernate.cfg.xml" configured
17:36:14 - DEBUG: SessionFactory "" configured from "/hibernate.cfg.xml" file
17:36:14 - DEBUG: "" configured as the *default* SessionFactory of the Full Hibernate Plugin's Session Factory
17:36:14 - DEBUG: Full Hibernate Plugin's Session Factory built successful
17:36:14 - DEBUG: New Hibernate Session created and returned (SessionFactory "")
17:36:14 - DEBUG: Hibernate Session from Full Hibernate Plugin's Hibernate Session Factory
17:36:14 - DEBUG: Hibernate Session injected (by annotation) into Action. Field "session". Class "com.struts2hibernatepagination.dao.StudentDAO"
17:36:14 - DEBUG: Full Hibernate Plugin Validation in class com.struts2hibernatepagination.action.AddStudentAction
17:36:14 - DEBUG: Full Hibernate Plugin Validation found no erros.
Hibernate: insert into student (last_name, first_name, marks) values (?, ?, ?)
**TinaDutta44Inside execute method**
17:36:15 - DEBUG: Hibernate Transation org.hibernate.transaction.JDBCTransaction@1d4fbd1 rolledback by Full Hibernate Plugin
17:36:15 - DEBUG: Hibernate Session closed
17:36:15 - DEBUG: Hibernate Session closed by Full Hibernate Plugin's Hibernate Session Factory
17:36:15 - DEBUG: Hibernate Transaction Committed
17:36:15 - DEBUG: Injection Hibernate Session and Transaction process for /addStudent - Method: com.struts2hibernatepagination.action.AddStudentAction.execute() finished
17:36:15 - DEBUG: Preparing Injection Hibernate Session and Transaction process: /listStudents - Method: com.struts2hibernatepagination.action.AddStudentAction.listStudents()
17:36:15 - DEBUG: Hibernate Session Required (from current Thread) - SessionFactory required: (default)
17:36:15 - DEBUG: No Hibernate Session in current thread. New Hibernate Session will be created and returned (SessionFactory "(default)")
17:36:15 - DEBUG: New Hibernate Session required - SessionFactory required: (default)
17:36:15 - DEBUG: New Hibernate Session created and returned (SessionFactory "")
17:36:15 - DEBUG: Hibernate Session from Full Hibernate Plugin's Hibernate Session Factory
17:36:15 - DEBUG: Hibernate Session injected (by annotation) into Action. Field "session". Class "com.struts2hibernatepagination.dao.StudentDAO"
17:36:15 - DEBUG: Full Hibernate Plugin Validation in class com.struts2hibernatepagination.action.AddStudentAction
17:36:15 - DEBUG: Full Hibernate Plugin Validation found no erros.
**null
Inside validate method!!**
17:36:15 - DEBUG: Full Hibernate Plugin found custom validation errors: {} [Please Enter First Name !!!]
17:36:16 - DEBUG: Hibernate Transation org.hibernate.transaction.JDBCTransaction@6ee964 rolledback by Full Hibernate Plugin
17:36:16 - DEBUG: Hibernate Session closed
17:36:16 - DEBUG: Hibernate Session closed by Full Hibernate Plugin's Hibernate Session Factory
17:36:16 - DEBUG: Hibernate Transaction Committed
17:36:16 - DEBUG: Injection Hibernate Session and Transaction process for /listStudents - Method: com.struts2hibernatepagination.action.AddStudentAction.listStudents() finished
结果有点违背了我对预期输出的直觉。我认为答案只是列表: - 虽然作者的解释足以理解执行过程以及我们如何得到最终结果,但它并没有解释为什么它的工作方式如此。filter (< 3) [1..]
。但是,不!
所以,我的问题是为什么是无限列表,表示为一堆部分列表的限制?有人可以解释这一点而不必深入研究复杂的数学术语吗?
由于
答案 0 :(得分:5)
简单地说,Haskell编译器并不神奇,但有时可能会显得神奇。虽然与其他编程语言相比,某些表达式似乎非常具有声明性,但Haskell的评估语义实际上非常简单。
出于这个原因,在你提到的例子filter (< 3) [1..]
中,GHC并不“知道”有关上述表达式的含义的任何内容。虽然对于一个人来说很明显,2
之后永远不会有任何元素满足(< 3)
谓词,但filter
没有理由意识到不会有最终成为一些元素。因此,尝试评估结果列表的前两个元素以外的任何内容都将产生无限循环。
这是解释Haskell中的无限列表实际上只是“限制”的理念。真正的分析系统可以使用无限列表,并且可以对其元素的所有进行断言。从数学角度来说,可以简单地证明Haskell表达式[1..]
表示的无限列表只包含两个小于3的元素,但Haskell没有任何这样的分析能力 - 它只是一种函数式编程语言。
使用模拟到数学极限,我们可以说在给定无限量的时间和空间的情况下,评估[1..]
接近无限列表,但没有它,它只是一个计算 - 我们总能产生的承诺如果我们想要更多元素,但与数学无限集不同,它不是对真正无限元素集的一些高级描述。它只是一组有限的元素,具有任意大小,并描述了如何获得更多。