package GT

  1. Overview
  2. Docs
Generic programming with extensible transformations

Install

Dune Dependency

Authors

Maintainers

Sources

0.5.1.tar.gz
sha256=fe0aa1e40afd36f7c1fcd992c408261d0b5645cea080ea98bc5dbf0b8682187f
sha512=8db0f2d8db4d7c11cf55835ba2b6c4f5ee1e8c09a4bdc2a9c57d50958ff8654688d4c8596079dfd1027d8b9e878f6e1b0d14341bcdf5f63cf96a45b2a571f3cb

doc/src/foldr/foldr.ml.html

Source file foldr.ml

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
(*
 * Generic transformers: plugins.
 * Copyright (C) 2016-2019
 *   Dmitrii Kosarev aka Kakadu
 * St.Petersburg State University, JetBrains Research
 *)

(** {i Foldr} plugin: fold all values in a type.

    Essentially is a stub that chains inherited attribute thorough all values
    in the value

    For type declaration [type ('a,'b,...) typ = ...] it will create a transformation
    function with type

    [('s -> 'a -> 's) ->
     ('s -> 'b -> 's) ->
     ... ->
     's -> ('a,'b,...) typ -> 's ]
*)

open Base
open Ppxlib
open GTCommon

let trait_name = "foldr"

module Make(AstHelpers : GTHELPERS_sig.S) = struct
open AstHelpers
module Foldl = Foldl.Make(AstHelpers)

let trait_name =  trait_name

class g initial_args tdecls = object(self: 'self)
  inherit Foldl.g initial_args tdecls

  method trait_name = trait_name

  method join_args ~loc do_typ ~init (xs: (string * core_type) list) =
    List.fold_left ~f:(fun acc (name,typ) ->
        Exp.app_list ~loc
          (do_typ typ)
          [ acc; Exp.sprintf ~loc "%s" name]
        )
        ~init
        (List.rev xs)

end

let create = (new g :> Foldl.P.plugin_constructor)

end

let register () =
  Expander.register_plugin trait_name (module Make: Plugin_intf.MAKE)

let () = register ()
OCaml

Innovation. Community. Security.