Advertisement
Guest User

Untitled

a guest
Jan 21st, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 5.41 KB | None | 0 0
  1. type ('a,'b) plus = Inl of 'a | Inr of 'b
  2.  
  3. type 'a next = Later of (unit -> 'a)
  4. type 'a omega = Cons of 'a * 'a omega next
  5. type 'a delta = 'a
  6. type ('a, 'b) arr = 'a -> 'b
  7.  
  8. type 'a ty =
  9.   Tconst : 'a delta ty
  10. | Tomega : 'a omega ty
  11. | Tnext : 'a ty -> 'a next ty
  12. | Tprod : 'a ty * 'b ty -> ('a * 'b) ty
  13. | Tarr : 'a ty * 'b ty -> ('a, 'b) arr ty
  14. | Tplus : 'a ty * 'b ty -> ('a, 'b) plus ty
  15.  
  16. type ('a, 'b) tm =
  17.   Id : ('a, 'a) tm
  18. | Pair : ('a, 'b) tm * ('a, 'c) tm -> ('a, 'b * 'c) tm
  19. | Proj1 : ('a * 'b, 'a) tm
  20. | Proj2 : ('a * 'b, 'b) tm
  21. | Delta : ('a -> 'b) -> ('a delta, 'b delta) tm
  22. | Omega : ('a -> 'b) -> ('a omega, 'b omega) tm
  23. | Comp : ('b, 'c) tm * ('a, 'b) tm -> ('a, 'c) tm
  24. | Fold : ('a delta * 'a omega next, 'a omega) tm
  25. | Unfold : ('a omega, 'a delta * 'a omega next) tm
  26. | Fix : (('a next, 'a) arr, 'a) tm
  27. | Lam : ('a * 'b, 'c) tm -> ('a, ('b, 'c) arr) tm
  28. | Nextfun : ('a, 'b) tm -> ('a next, 'b next) tm
  29. | Nextprod : ('a next * 'b next, ('a * 'b) next) tm
  30. | App : (('a, 'b) arr * 'a, 'b) tm
  31. | Tt : ('a, unit delta) tm
  32. | Case : ('a -> ('b, 'c) tm) -> ('b * 'a delta, 'c) tm
  33. | Next : ('a, 'a next) tm (* redondant, mais le typechecker me déteste *)
  34.  
  35. let next (x : 'a) : 'a next = Later (fun () -> x)
  36.  
  37. let rec interp : type a b. (a, b) tm -> a -> b = function
  38.   Id -> fun x -> x
  39. | Pair (t, u) -> fun x -> (interp t x, interp u x)
  40. | Proj1 -> fst
  41. | Proj2 -> snd
  42. | Delta f -> f
  43. | Fold -> fun (a, b) -> Cons (a, b)
  44. | Unfold -> (function Cons (a, b) -> (a, b))
  45. | Fix -> let rec fix f =
  46.            f (Later (fun () -> fix f)) in fix
  47. | Lam t -> fun x y -> interp t (x, y)
  48. | App -> fun (f, x) -> f x
  49. | Comp (f, g) -> fun x -> interp f (interp g x)
  50. | Tt -> fun _ -> ()
  51. | Nextprod -> (function (Later f, Later g) -> Later (fun () -> f (), g ()))
  52. | Nextfun t -> (function Later f -> Later (fun () -> interp t (f ())))
  53. | Next -> next
  54.  
  55. let tm_times : ('a, 'b) tm -> ('c, 'd) tm -> ('a * 'c, 'b * 'd) tm =
  56.   fun t u -> Pair (Comp (t, Proj1), Comp (u, Proj2))
  57.  
  58. let tm_ilam : type a b c. ((a * b, c) arr, (a, (b, c) arr) arr) tm =
  59.   Lam (Lam (Comp (App, (Pair (Comp (Proj1, Proj1),
  60.                               Pair (Comp (Proj2, Proj1), Proj2))))))
  61.  
  62. let tm_fixparam : (('a * 'b next, 'b) arr, ('a, 'b) arr) tm =
  63.   Comp (Lam (Comp (Fix, App)), tm_ilam)
  64.  
  65. let tm_elam (t : (('a,'b) arr, ('c, 'd) arr) tm) (u : ('a, 'b) tm) : ('c, 'd) tm =
  66.   Comp (App, Pair (Comp (t, Lam (Comp (u, Proj2))) , Id))
  67.  
  68. let tm_Fixparam : type a b. (a * b next, b) tm -> (a, b) tm =
  69.   fun x -> tm_elam tm_fixparam x
  70.  
  71. (* On simule un compteur c : ω → ω par la fonction synchrone f : Σ^ω → Σ^ω telle que
  72. f(x)(n) = x_{c(n)}
  73.  
  74. On prend en paramètre un stream bool option omega ≡ 3^omega pour contrôler les
  75. montées/descentes. Le compteur paramétré C : 3^ω × Σ^ω → Σ^ω satisfait les équations suivantes
  76. C(c, x)(0) = x_0
  77. C(c, x)(n+1) = C(c,x)(n) si c(n) = None
  78. C(c, x)(n+1) = C(c,<x)(n) si c(n) = Some true (incrément)
  79. C(c, x)(n+1) = C(c,x_0.x)(n) si c(n) = Some false (décrémentation)
  80.  
  81. Pour tester l'implèm, on prend Σ ≡ int, mais clairement, Σ ≡ 2 est suffisant pour
  82. fabriquer une machine avec compteurs.
  83. *)
  84.  
  85. let tm_nextarr : (('a, 'b) arr next, ('a next, 'b next) arr) tm =
  86.   Lam (Comp (Nextfun App, Nextprod))
  87.  
  88. (*let tm_next : type a. (a omega, a omega next) tm =
  89.   Comp (Proj2, ((tm_Fixparam (Pair (Proj1, Comp (Nextfun Proj1, Proj2)) : (a omega * (a omega * a omega next) next, a omega * a omega next) tm))))
  90. *)
  91.  
  92. let tm_next = Next
  93.  
  94. let tm_const (d : 'a) : ('b, 'a delta) tm = Comp (Delta (fun () -> d), Tt)
  95.  
  96. let tm_ncons (d : 'a) : ('a omega, 'a omega) tm =
  97.   Comp (Fold, Pair (tm_const d, tm_next))
  98.  
  99. let caseom (f : 'a -> ('b,'c) tm) : ('b * 'a omega, 'c) tm =
  100.   Comp (Case f, tm_times Id (Comp (Proj1, Unfold)))
  101.  
  102. let tm_tail : ('a omega, 'a omega next) tm = Comp (Proj2, Unfold)
  103.  
  104. let tm_cmpt (d : 'a) : (bool option omega, ('a omega, 'a omega) arr) tm =
  105.   tm_Fixparam (
  106.         Lam (
  107.         Comp (Fold,
  108.          Pair (Comp (Proj1, Comp (Unfold, Proj2)),
  109.                Comp (Comp (
  110.                       Comp (App,
  111.                             caseom (function None -> tm_times Id tm_next
  112.                                            (* (assert false : ((('a omega next, 'a omega next) arr * 'a omega), ('a omega next, 'a omega next) arr * 'a omega next) tm)*)
  113.                                           | Some true -> tm_times Id tm_tail
  114.                                           | Some false -> tm_times Id (Comp (tm_next, tm_ncons d))
  115.                                  )
  116.                            ),
  117.                             Pair (Pair (Comp (Proj2, Proj1), Proj2), Comp (Proj1, Proj1))
  118.                            )
  119.                      ,
  120.                      tm_times (tm_times Id tm_nextarr) Id)
  121.               )
  122.              )))
  123.  
  124. let up (l : 'a list) : int -> 'a =
  125.   let rec aux p = function
  126.     [] -> aux [] (List.rev p)
  127.   | h :: t -> function 0 -> h
  128.                      | n -> aux (h :: p) t (n - 1)
  129.   in aux [] l
  130.  
  131. let listn =
  132.   let rec aux k = function 0 -> [] | n -> k :: (aux (k+1) (n-1)) in
  133.   aux 0
  134.  
  135. let rec om_of_fun f = Cons (f 0, Later (fun () -> om_of_fun (fun k -> f (k+1))))
  136. let rec fun_of_om (Cons (a, Later f)) = function 0 -> a | k -> fun_of_om (f ()) (k - 1)
  137.  
  138. let test l =
  139.   let c = interp (tm_cmpt (-1)) in
  140.   let n = List.length l in
  141.   let ln = listn (n+1) in
  142.   List.map (fun_of_om @@ c (om_of_fun (up l)) (om_of_fun (fun n -> n))) ln
  143.  
  144. let _ = test [None]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement