在OCaml中操作循环内的不可变变量 [英] Manipulate unmutable variables inside of loop in OCaml
问题描述
我在OCaml中有下面的代码。我已经定义了所有的necesar函数,并且逐步测试了它们的评估应该可以正常工作,但是我没有成功地操作while中的变量。如何使x,vn, v改变它们的值?我认为我应该像rec循环一样重写,但不能完全确定:
下面是代码的其余部分: http://pastebin.com/Ash3xw6y
伪代码:
输入:f公式
输出:是,如果f有效
否则不是
开始:
V =变量集合
从f =>和< =>
while(V is not empty)
从V
中选择x V = V - {x}
用f替换f [x-> true]&& f [x-> false]
尽可能简化f
如果f用true求值然后返回true
else if(not f)is true true then return false
结束如果
结束而
返回false
结束
类型bexp = V
|字符串
| B的bool
|否定bexp
|和bexp * bexp
|或者是bexp * bexp
| bexp * bexp
|的Impl eqv of bexp * bexp
module StringSet = Set.make(String)
let is_valide f =
let v = stringset_of_list(ens f [])in (*消除=>和< => *)
让quit_loop = ref false在
中,而不是! quit_loop
do
let x = StringSet.choose v in
let vn = StringSet.remove xv in
if StringSet.is_empty vn = true then quit_loop:= true;
让h = And(替换x(B true)g,替换x(B false)g)in
let j =简化h in
if(only_bools j)then
if(eval j)then print_stringyes
else print_stringnot
done
( New form )
让重言式f =
让rec tautology1 xvg =
让h = And(remplace x(B true)g,remplace x(B false)g)in
let j =简化h in
if(not_bools j)then tautology( StringSet.reose(StringSet.remove xv)(StringSet.remove xv)j
else
if(eval1 j)then print_stringyes \\\
else
if(eval1(Neg(j ))然后print_stringnot \\\
;
in tautology1(StringSet.choose(stringset_of_list(ens f []))(stringset_of_list(ens f []))(elim f);;
whi le
循环属于OCaml中的命令式编程部分。
基本上,你不能修改,而
或用于
循环或任何地方。 变量是可变的,你需要定义它像 let var = ref ...
。 ref
是关键字mutables。
阅读这两章:
您可以定义 x,vn,v
as ref
s,但我猜它会很难看。
我建议你认为你的代码在
由于您没有在这里放置函数 ens
等,所以我无法提供一个示例为你。
I have the following code in OCaml.I have defined all necesar functions and tested them step by step the evalution should work good but I didn't succed to manipulate the variables inside of while.How can I make x,vn,v to change their value?I think I should rewrite the while like a rec loop but can't figure out exactly: Here is the rest of code: http://pastebin.com/Ash3xw6y
Pseudocode:
input : f formula
output: yes if f valid
else not
begin:
V =set of prop variables
eliminate from f => and <=>
while (V is not empty)
choose x from V
V =V -{x}
replace f with f[x->true]&&f[x->false]
simplify as much as possible f
if f is evaluated with true then return true
else if (not f) is evaluated true then return false
end if
end while
return false
end
type bexp = V of
| string
| B of bool
| Neg of bexp
| And of bexp * bexp
| Or of bexp * bexp
| Impl of bexp * bexp
| Eqv of bexp * bexp
module StringSet=Set.make(String)
let is_valide f=
let v= stringset_of_list (ens f []) in (*set of all variables of f *)
let g= elim f in (*eliminate => and <=> *)
let quit_loop=ref false in
while not !quit_loop
do
let x=StringSet.choose v in
let vn=StringSet.remove x v in
if StringSet.is_empty vn=true then quit_loop:=true;
let h= And( replace x (B true) g ,replace x (B false) g)in
let j=simplify h in
if (only_bools j) then
if (eval j) then print_string "yes"
else print_string "not"
done
(New form)
let tautology f =
let rec tautology1 x v g =
let h= And( remplace x (B true) g ,remplace x (B false) g)in
let j= simplify h in
if not (only_bools j) then tautology (StringSet.choose (StringSet.remove x v) (StringSet.remove x v) j
else
if (eval1 j) then print_string "yes \n " else
if (eval1 (Neg (j))) then print_string "not \n";
in tautology1 (StringSet.choose (stringset_of_list (ens f [])) (stringset_of_list (ens f [])) (elim f);;
while
loop belongs to imperative programming part in OCaml.
Basically, you can't modify immutable variables in while
or for
loops or anywhere.
To let a variable to be mutable, you need to define it like let var = ref ...
. ref
is the keyword for mutables.
Read these two chapters:
- https://realworldocaml.org/v1/en/html/a-guided-tour.html#imperative-programming
- https://realworldocaml.org/v1/en/html/imperative-programming-1.html
You can define x,vn,v
as ref
s, but I guess it will be ugly.
I suggest you think your code in a functional way.
Since you haven't placed functions ens
etc here, I can't produce an example refine for u.
这篇关于在OCaml中操作循环内的不可变变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!