functor

OCaml: Why does renaming a type fail with “Their kinds differ”

依然范特西╮ 提交于 2020-01-05 01:12:21
问题 I'm building an universal container for for pairs of a type witness and a value of the witnessed type. This I want to use for several different types, which gives me errors because the types are all named the same. So I'm trying to rename types in the result of a functor like this: module type Witness = sig type 'a key type 'a value end module type Witnessed = sig type 'a key type 'a value type t type ('a, 'b) conv = { key : 'c . 'c key -> 'a; value : 'c . 'c value -> 'b; } val box : 'a key -

Convert overloaded function to template functor

放肆的年华 提交于 2020-01-03 13:35:17
问题 I have a few overloaded functions, e.g. int a(int) {/*...*/} float a(float) {/*...*/} /* ... */ int b(int) {/*...*/} float b(float) {/*...*/} /* ... */ My goal is to wrap these function into a functor object: template <typename T> struct func_a { auto T operator()(T t) -> decltype(a(t)) {return a(t);} }; Is there a way to define the above template struct over some other template taking the overloaded function as argument? Something like this: template </* pointer to an overloaded function f *

Convert overloaded function to template functor

旧城冷巷雨未停 提交于 2020-01-03 13:33:27
问题 I have a few overloaded functions, e.g. int a(int) {/*...*/} float a(float) {/*...*/} /* ... */ int b(int) {/*...*/} float b(float) {/*...*/} /* ... */ My goal is to wrap these function into a functor object: template <typename T> struct func_a { auto T operator()(T t) -> decltype(a(t)) {return a(t);} }; Is there a way to define the above template struct over some other template taking the overloaded function as argument? Something like this: template </* pointer to an overloaded function f *

Example of Invariant Functor?

拟墨画扇 提交于 2020-01-03 12:28:19
问题 I'm reading documentation on monad layers package and my brain is going to boil up. In the mmtl section of this document the author talks about invariant functor . It's method invmap is like fmap of Functor but it takes an inverse morphism (b -> a) also. I understand why author says that hoist of MFunctor is more powerful than tmap of Invariant but i don't see what's the point of that inverse morphism. Is there any example of an Invariant which can't be an instance of Functor ? 回答1: Here's a

OCaml recursive modules across compilation units

纵然是瞬间 提交于 2020-01-02 10:09:12
问题 I'm trying to split the following recursive modules into separate compilation units. Specifically, I'd like B to be in its own b.ml, to be able to reuse it with other A's. module type AT = sig type b type t = Foo of b | Bar val f : t -> b list end module type BT = sig type a type t = { aaa: a list; bo: t option } val g : t -> t list end module rec A : (AT with type b = B.t) = struct type b = B.t type t = Foo of b | Bar let f = function Foo b -> [ b ] | Bar -> [] end and B : (BT with type a =

Type sharing in OCaml - typechecker error

江枫思渺然 提交于 2020-01-02 07:07:14
问题 When compiling this program: module type Inc = sig type t val inc : t -> t end module type Dec = sig type t val dec : t -> t end module Merger (I : Inc) (D : Dec with type t = I.t) = struct let merge x = x |> I.inc |> D.dec end module IntInc : Inc = struct type t = int let inc x = x + 10 end module IntDec : Dec = struct type t = int let dec x = x - 8 end module Combiner = Merger (IntInc) (IntDec) I get the following error: File "simple.ml", line 30, characters 35-41: Error: Signature mismatch

std::for_each usage on member function with two args

瘦欲@ 提交于 2020-01-01 05:41:10
问题 Here's a general idea of how my class is defined as ( it performs other operations than what is mentioned below) struct Funktor { Funktor(int val):m_val(val){} bool operator()(int arg1, int arg2) { return m_val==arg1*arg2; } int m_val; }; And now I have a vector of the above objects, and I am trying to call operator() using for_each, is there a way to do this? I know it can be done using bind2nd and mem_func_ref but when there's only one argument but for two arguments I haven't found a way.

Haskell Functor implied law

夙愿已清 提交于 2020-01-01 01:16:29
问题 Typeclassopedia says: "A similar argument also shows that any Functor instance satisfying the first law (fmap id = id) will automatically satisfy the second law as well. Practically, this means that only the first law needs to be checked (usually by a very straightforward induction) to ensure that a Functor instance is valid." If this is the case, why do we even mention the second functor law? Law 1: fmap id = id Law 2: fmap (g . h) = (fmap g) . (fmap h) 回答1: While I can't give a proof, I

Where do std::bind-created functors live?

人走茶凉 提交于 2019-12-30 18:09:04
问题 A function pointer can point to anything from a free function, a function object, a wrapper over a member function call. However, the std::bind created functors can have state, as well as custom-created ones. Where that state is allocated, and who is deleting it? Consider the below example - will the state ( the number 10) be deleted when the vector is deleted? Who know to call a deleter on the functor, and no deleter on the function pointer? #include <iostream> #include <functional> #include

Where do std::bind-created functors live?

你说的曾经没有我的故事 提交于 2019-12-30 18:08:19
问题 A function pointer can point to anything from a free function, a function object, a wrapper over a member function call. However, the std::bind created functors can have state, as well as custom-created ones. Where that state is allocated, and who is deleting it? Consider the below example - will the state ( the number 10) be deleted when the vector is deleted? Who know to call a deleter on the functor, and no deleter on the function pointer? #include <iostream> #include <functional> #include