What is a <cycle> in data?

(I am using OCaml version 4.02.3)

I determined the type self

# type self = Self of self;;
type self = Self of self 

and its copy s

# let rec s = Self s;;
val s : self = Self <cycle>

Since OCaml is a strict language , I expected the definition to sfall into infinite recursion. But the interpreter said sit matters and Self <cycle>.

I also applied the function to s.

# let f (s: self) = 1;;
val f : self -> int = <fun> 
# f s;;
- : int = 1 

It sdoes not seem to be evaluated before the application of the function (e.g. in a non-strict language).

How does OCaml handle cyclic data such as s? Self <cycle>- normal form?

+4
source share
1 answer

OCaml , s , . , :

let f (Self Self x) = x
f s == s;; 

, n :

โ‹…โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ‹…
| header | field[0] | field[1] | โ‹ฏ | fiekd[n] |
โ‹…โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ‹…

, field[k] - OCaml, . s, Self , , field[0]. field[0] . , s OCaml.

, <cycle>, s. <cycle>, , <abstr> <fun>, , .

, , , f s = s, (=) - (i.e. (==)) โ€‹โ€‹,

let rec ones = 1 :: ones;; (* prints [1;<cycle>] *)
let twos = List.map ((+) 1) ones;; (* falls in an infinite recursion *)
+4

Source: https://habr.com/ru/post/1678544/


All Articles