多线程在...函数式语言中? (序言) [英] Multithreading in... functional languages? (Prolog)

查看:123
本文介绍了多线程在...函数式语言中? (序言)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的朋友在学校开始学习Prolog时,我嘲笑他学习一门毫无用处的语言。然而,他向我展示了一些我从未知道的东西;我想知道这种技术来自哪里。



技巧是这样的:

  permutation(List): -  
isAMember(X,List),
deleteFirstElement(X,List,Substring),
%等等
isAMember(X,List)
是一个函数,它返回true如果 X List 中。然而,到目前为止 X 没有被定义为一个变量 - ,所以程序会产生一堆新线程,每个线程对应于 X ,使 isAMember(X,List) true,并继续。

这允许我们以最简单,最优雅的方式创建一个多线程算法,我可以想像这是可能的。

所以我的问题是:这是Prolog特有的,还是所有逻辑和/或功能语言的特征?另外,我在哪里可以学到更多令人惊叹的多线程技术 - 这肯定是编程的未来。

解决方案

被称为Datalog的Prolog子集限于纯逻辑要素(无切),原则上,证明搜索可以并行进行。然而,你必须小心,因为完整的Prolog的语义对产生结果的顺序非常敏感,并且一些真正的Prolog程序依赖于此。



像Haskell和Clean这样的纯函数式语言的情况要好一些 - 并行评估子表达式总是安全的 - 但是性能有很多挑战。如果你做了极端的并行性(每个子表达式),由于所有的开销,你不会得到任何性能提升。目前看好的方法似乎是


  • 线程(并发Haskell)$ / $>

  • 数据并行Haskell


  • Sparks,其中 par seq 运算符。


    并行功能的东西已经存在了将近30多年来人们仍在努力使其表现良好。如果您想了解更多信息,请尝试


    • 最近关于Haskell的ACM研讨会(以及之前的Haskell研讨会)


    • 麻省理工学院的Arvind的工作是麻省理工学院的一位伟大先驱(请看R. Nikhil关于pH并行编程的书)

      >


    When my friend started learning Prolog in school, I made fun of him for learning a useless language. However, he's showed me some stuff I never even knew possible; I want to know where this technique comes from.

    The technique is this:

    permutation(List) :-
        isAMember(X, List),
        deleteFirstElement(X, List, Substring),
        % and so on
    

    In this code, isAMember(X, List) is a function that returns true if X is in List. However, up to this point X is not defined as a variable - so the program will spawn a bunch of new threads, one for each possible value of X that makes isAMember(X, List) true, and continue from there.

    This allows us to create a multi-threaded algorithm in the simplest, most elegant way I could have ever imagined possible.

    So my question is: Is this Prolog-specific, or a feature of all logical- and/or functional-languages? Also, where can I learn more amazing multithreading techniques like this - this is surely the future of programming.

    解决方案

    The subset of Prolog known as "Datalog" is restricted to pure logical features (no "cut"), and in principle, the proof search could be done in parallel. However you'd have to be careful because the semantics of full Prolog is quite sensitive to the order in which results are produced, and some real Prolog programs depend on this.

    The situation in pure functional languages like Haskell and Clean is a bit better—it is always safe to evaluate subexpressions in parallel—but there are many challenges of performance. If you do extreme parallelism (every subexpression) you don't get any performance gains because of all the overhead. The promising approaches at the moment seem to be

    • Threads (Concurrent Haskell)

    • Data Parallel Haskell

    • "Sparks" with par and seq operators.

    The parallel functional stuff has been around for almost 30 years and people are still trying to make it perform well. If you want more information, try

    • Recent proceedings of the ACM Symposium on Haskell (and before that, the Haskell workshop)

    • The work of Arvind at MIT, who was a great pioneer in this area (check out his book with R. Nikhil on parallel programming with pH)

    这篇关于多线程在...函数式语言中? (序言)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆