How to implement `forall` (mathematics) in a procedural or OO language

安稳与你 提交于 2019-12-06 05:24:39

If I'm allowed to reason in Smalltalk, where blocks are objects of the class BlockClosure, I would assume that you represent the property you want to quantify as a block p.

For the sake of simplicity let's assume that the property depends on one single parameter x. Then p(x) would correspond to the Smalltalk expression

p value: x

which evaluates the block p using the argument x.

In this way, you could implement the Smalltalk method forAll: in the class BlockClosure as:

forAll: aCollection
  aCollection do: [:x | (self value: x) ifFalse: [^false]].
  ^true

which checks that the property p represented by the receiver block evaluates to true for all the elements in aCollection (your universe).

If your universe doesn't change (the usual case in the context of a problem), and what changes is the property, you could define the class Universe, which would hold the collection of elements in its instance variable contents. Then, you could implement in Universe

forAll: aProperty
  ^aProperty forAll: contents

where the inner forAll: message is the one implemented in BlockClosure.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!