简体   繁体   English

谁能解释这个简单服务器代码所基于的评估模型?

[英]Can anyone explain the evaluation model underlying this simple server code?

I can across http://therning.org/magnus/archives/698 while looking for some examples of networking with Haskell. 我可以在http://therning.org/magnus/archives/698上查找与Haskell联网的一些示例。 It's probably about as simple an example as there is. 它可能只是一个简单的例子。

While I can use this as a template, and it works, I don't fully understand the semantics and how the code is evaluated. 虽然我可以将其用作模板并且可以正常工作,但我还没有完全理解语义以及如何评估代码。 I can see that it's laziness that prevents the forever $ do block from being constantly evaluated, but what forces it to be evaluated at all? 我可以看到,懒惰阻止了forever $ do块的不断评估,但是到底是什么迫使它进行评估呢?

Laziness has nothing to do here. 懒惰在这里无关。 And there's nothing "preventing" the subject forever $ do -block from anything either. 而且,也没有任何事情可以forever $ do “预防”这个问题。 It is infinitely evaluated, in fact you can see it as analogue to while (true) of common imperative languages. 它是无限评估的,实际上,您可以将其视为类似于通用命令性语言的while (true) The implementation of this server revolves around the accept serv_sock function call, on which the program waits for a connection thus synchronously blocking the forever loop execution until it receives the connection, does what it's supposed to with it and then loops again. 该服务器的实现围绕于accept serv_sock函数调用,在该调用上程序等待连接,从而同步阻止forever循环的执行,直到接收到该连接,然后执行应有的处理,然后再次循环。

It all may seem very much like a standard imperative program, the difference though is that instead telling the computer what to do with every expression, you are composing instructions from other instructions. 这看起来似乎非常像一个标准的命令性程序,尽管区别在于告诉计算机如何处理每个表达式,而是由其他指令组成指令。 Thus you can see the main function definition as a single composite instruction. 因此,您可以将main函数定义视为单个复合指令。

What triggers the evaluation is the fact that you assign this instruction with a name main and a type signature IO () (which got inferred in your case). 触发评估的事实是,您为该指令分配了名称main和类型签名IO () (在您的情况下可以得出)。 By this you tell the Haskell compiler to treat this instruction as an entry point of your program, and thus to begin the evaluation from it when the program is launched. 这样,您就可以告诉Haskell编译器将该指令视为程序的入口点,从而在程序启动时从该指令开始评估。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM