···11+module Fields = Stdlib.Map.Make (String)
22+33+type t =
44+ | Null
55+ | Unit
66+ | Bool of bool
77+ | Int of int
88+ | Int64 of int64
99+ | Float of float
1010+ | Char of char
1111+ | String of string
1212+ | Pair of t * t
1313+ | List of t list
1414+ | Constr of string * t option
1515+ | Record of fields
1616+1717+and fields = t Fields.t
1818+1919+type 'a conv = 'a -> t
2020+2121+let use f conv x = conv (f x)
2222+2323+module Lit = struct
2424+ let null = Null
2525+ let unit = Unit
2626+ let true_ = Bool true
2727+ let false_ = Bool false
2828+ let pair' (a, b) = Pair (a, b)
2929+ let pair a b = pair' (a, b)
3030+ let triple' (a, b, c) = pair a (pair b c)
3131+ let triple a b c = triple' (a, b, c)
3232+ let quad' (a, b, c, d) = pair a (pair b (pair c d))
3333+ let quad a b c d = quad' (a, b, c, d)
3434+ let quint' (a, b, c, d, e) = pair a (pair b (pair c (pair d e)))
3535+ let quint a b c d e = quint' (a, b, c, d, e)
3636+ let constr ?value key = Constr (String.(trim @@ lowercase_ascii key), value)
3737+ let list xs = List xs
3838+ let left value = constr ~value "left"
3939+ let right value = constr ~value "right"
4040+ let ok value = constr ~value "ok"
4141+ let error value = constr ~value "error"
4242+end
4343+4444+let null _ = Lit.null
4545+let unit _ = Lit.unit
4646+let bool b = Bool b
4747+let int i = Int i
4848+let int64 i = Int64 i
4949+let float f = Float f
5050+let string s = String s
5151+let char c = Char c
5252+let pair' conv_a conv_b (a, b) = Lit.pair (conv_a a) (conv_b b)
5353+let pair conv_a conv_b a b = pair' conv_a conv_b (a, b)
5454+5555+let triple' conv_a conv_b conv_c (a, b, c) =
5656+ pair' conv_a (pair' conv_b conv_c) (a, (b, c))
5757+;;
5858+5959+let quad' conv_a conv_b conv_c conv_d (a, b, c, d) =
6060+ pair' conv_a (pair' conv_b (pair' conv_c conv_d)) (a, (b, (c, d)))
6161+;;
6262+6363+let quint' conv_a conv_b conv_c conv_d conv_e (a, b, c, d, e) =
6464+ pair'
6565+ conv_a
6666+ (pair' conv_b (pair' conv_c (pair' conv_d conv_e)))
6767+ (a, (b, (c, (d, e))))
6868+;;
6969+7070+let triple conv_a conv_b conv_c a b c = triple' conv_a conv_b conv_c (a, b, c)
7171+7272+let quad conv_a conv_b conv_c conv_d a b c d =
7373+ quad' conv_a conv_b conv_c conv_d (a, b, c, d)
7474+;;
7575+7676+let quint conv_a conv_b conv_c conv_d conv_e a b c d e =
7777+ quint' conv_a conv_b conv_c conv_d conv_e (a, b, c, d, e)
7878+;;
7979+8080+let sum f x =
8181+ let name, value = f x in
8282+ Lit.constr ?value name
8383+;;
8484+8585+let list conv xs = xs |> List.map conv |> Lit.list
8686+let record fields = Record (Fields.of_list fields)
8787+let option some = Option.fold ~none:Lit.null ~some
8888+8989+let result ok err =
9090+ sum (function
9191+ | Ok x -> "ok", Some (ok x)
9292+ | Error x -> "error", Some (err x))
9393+;;
9494+9595+let either left right =
9696+ sum (function
9797+ | Either.Left x -> "left", Some (left x)
9898+ | Right x -> "right", Some (right x))
9999+;;
100100+101101+module Infix = struct
102102+ let ( <$> ) = use
103103+end
104104+105105+include Infix
+119
lib/pidgin/repr.mli
···11+(** Representation of generic language. *)
22+33+(** {1 Types} *)
44+55+(** All the terms of the language. *)
66+type t = private
77+ | Null
88+ | Unit
99+ | Bool of bool
1010+ | Int of int
1111+ | Int64 of int64
1212+ | Float of float
1313+ | Char of char
1414+ | String of string
1515+ | Pair of t * t
1616+ | List of t list
1717+ | Constr of string * t option
1818+ | Record of fields
1919+2020+(** Map for record fields. *)
2121+and fields
2222+2323+(** A converter from regular OCaml value to {!type:t}. *)
2424+type 'a conv = 'a -> t
2525+2626+(** {1 Building values} *)
2727+2828+(** {2 Literal values}
2929+ Describes literal terms, without helpers *)
3030+3131+module Lit : sig
3232+ val null : t
3333+ val unit : t
3434+ val true_ : t
3535+ val false_ : t
3636+ val pair' : t * t -> t
3737+ val pair : t -> t -> t
3838+ val triple' : t * t * t -> t
3939+ val triple : t -> t -> t -> t
4040+ val quad' : t * t * t * t -> t
4141+ val quad : t -> t -> t -> t -> t
4242+ val quint' : t * t * t * t * t -> t
4343+ val quint : t -> t -> t -> t -> t -> t
4444+ val list : t list -> t
4545+ val constr : ?value:t -> string -> t
4646+ val left : t -> t
4747+ val right : t -> t
4848+ val ok : t -> t
4949+ val error : t -> t
5050+end
5151+5252+(** {2 Converters} *)
5353+5454+val null : 'a conv
5555+val unit : 'a conv
5656+val bool : bool conv
5757+val int : int conv
5858+val int64 : int64 conv
5959+val float : float conv
6060+val string : string conv
6161+val char : char conv
6262+val pair' : 'a conv -> 'b conv -> ('a * 'b) conv
6363+val pair : 'a conv -> 'b conv -> 'a -> 'b conv
6464+val triple' : ('a -> t) -> ('b -> t) -> ('c -> t) -> ('a * 'b * 'c) conv
6565+val triple : ('a -> t) -> ('b -> t) -> ('c -> t) -> 'a -> 'b -> 'c conv
6666+6767+val quad'
6868+ : ('a -> t)
6969+ -> ('b -> t)
7070+ -> ('c -> t)
7171+ -> ('d -> t)
7272+ -> ('a * 'b * 'c * 'd) conv
7373+7474+val quad
7575+ : ('a -> t)
7676+ -> ('b -> t)
7777+ -> ('c -> t)
7878+ -> ('d -> t)
7979+ -> 'a
8080+ -> 'b
8181+ -> 'c
8282+ -> 'd conv
8383+8484+val quint'
8585+ : ('a -> t)
8686+ -> ('b -> t)
8787+ -> ('c -> t)
8888+ -> ('d -> t)
8989+ -> ('e -> t)
9090+ -> ('a * 'b * 'c * 'd * 'e) conv
9191+9292+val quint
9393+ : ('a -> t)
9494+ -> ('b -> t)
9595+ -> ('c -> t)
9696+ -> ('d -> t)
9797+ -> ('e -> t)
9898+ -> 'a
9999+ -> 'b
100100+ -> 'c
101101+ -> 'd
102102+ -> 'e conv
103103+104104+val sum : ('a -> string * t option) -> 'a conv
105105+val list : 'a conv -> 'a list conv
106106+val record : (string * t) list conv
107107+val option : 'a conv -> 'a option conv
108108+val result : 'ok conv -> 'err conv -> ('ok, 'err) result conv
109109+val either : 'left conv -> 'right conv -> ('left, 'right) Either.t conv
110110+111111+(** {1 Helpers} *)
112112+113113+val use : ('b -> 'a) -> 'a conv -> 'b conv
114114+115115+module Infix : sig
116116+ val ( <$> ) : ('b -> 'a) -> 'a conv -> 'b conv
117117+end
118118+119119+include module type of Infix