Advertisement
Guest User

Untitled

a guest
Jun 28th, 2021
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 1.28 KB | None | 0 0
  1. (* reactive.ml *)
  2. module Declarative = struct
  3.   module Map = Hmap
  4.  
  5.   type ex_index = Map.Key.t
  6.  
  7.   type _ type_ =
  8.   | T_String : string type_
  9.   | T_Tuple_2 : ('a type_ * 'b type_) -> ('a * 'b) type_
  10.  
  11.   type _ constructor =
  12.   | C_String : string -> string constructor
  13.   | C_Tuple_2 : ('a constructor * 'b constructor) -> ('a * 'b) constructor
  14.  
  15.   type _ reactive =
  16.   | R_String : string Variable.t -> string reactive
  17.   | R_Tuple_2 : (('a , 'b) Tuple_2.t * 'a reactive * 'b reactive) -> ('a * 'b) reactive
  18.  
  19.   let get_var : type a . a reactive -> a Variable.t = function
  20.   | R_String x -> x
  21.   | R_Tuple_2 (tpl , _ , _) -> (
  22.     let updater_forth (a , b) = (a , b) in
  23.     let updater_back (a , b) = (a , b) in
  24.     Tuple_2.map_from ~updater_forth ~updater_back tpl
  25.   )
  26.  
  27.   let rec make : type a . a constructor -> a reactive = function
  28.   | C_String str -> (
  29.     let v = Variable.make str in
  30.     R_String v
  31.   )
  32.   | C_Tuple_2 (a , b) -> (
  33.     let a' = make a in
  34.     let b' = make b in
  35.     let tpl = Tuple_2.make ~bidirectional:true (get_var a' , get_var b') in
  36.     R_Tuple_2 (tpl , a' , b')
  37.   )
  38.  
  39. end
  40.  
  41. (* main.ml *)
  42. let () =
  43.   let (R_Tuple_2 (fullName , R_String firstName , R_String lastName)) = R.Declarative.make @@ C_Tuple_2 (C_String "Will" , C_String "Smith") in
  44.   ...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement