aboutsummaryrefslogtreecommitdiff
path: root/semestr-3/pf/lista1/zad6.ml
blob: 46e7f6d71387df792e8e8b94725c798b2bffc6ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
type cbool = { cbool : 'a. 'a -> 'a -> 'a }
type cnum = { cnum : 'a. ('a -> 'a) -> 'a -> 'a }


let even n =
  { cbool = fun tt ff -> fst (n.cnum (fun (a, b) -> (b, a)) (tt, ff))}


let ctrue = 
  { cbool = fun a b -> a}
let cfalse = 
  { cbool = fun a b -> b}
let cand f1 f2 a b = f1.cbool (f2.cbool a b) b
let cor f1 f2 a b = f1.cbool a (f2.cbool a b)
let cbool_of_bool b = if b then ctrue else cfalse
let bool_of_cbool f = f.cbool true false

let zero = { cnum = fun f x -> x }
let succ fn = { cnum = fun f x -> fn.cnum f (f x) }
let add f1 f2 = { cnum = fun f x -> f1.cnum f (f2.cnum f x) }
let mul f1 f2 = { cnum = fun f x -> f1.cnum (f2.cnum f) x }
(* let is_zero f =  *)
let rec cnum_of_int = function
  | 0 -> zero
  | n -> succ (cnum_of_int (n - 1))
let is_zero f = 
  let g x = x + 1 
  in if (f.cnum g 0) = 0 then ctrue else cfalse
let int_of_cnum f = f.cnum (fun x -> x + 1) 0

let one = succ zero
let two = succ one
let three = succ two
let four = succ three
let five = succ four

let func x = x * 2