logo
Tags down

shadow

Would you please explain OCaml functors to me?


By : user3851087
Date : October 17 2020, 11:12 PM
With these it helps If you come from an OOP universe, then it probably helps to think of a module as analogous to a static class. Similar to .NET static classes, OCaml module have constructors; unlike .NET, OCaml modules can accept parameters in their constructors. A functor is a scary sounding name for the object you pass into the module constructor.
So using the canonical example of a binary tree, we'd normally write it in F# like this:
code :


Share : facebook icon twitter icon

Functors in Ocaml


By : aris
Date : March 29 2020, 07:55 AM
To fix this issue I am having a bit of a problem with a functor (and it's resultant type). Below, I have a Set functor that uses an Ordered type. I actually used the set.ml that comes with ocaml for some guidance, but I seem to be doing everything ahhem right. I created an Ordered module with integers and applied it to the Set functor to get the last module on this code sample, IntSet. , You need to change these two lines
code :
module Set (Elt:Ordered) : S = 
module OrdInt : Ordered =
module Set (Elt:Ordered) : S with type elt = Elt.t = 
module OrdInt : Ordered with type t = int =
module OrdInt =
module IntSet = Set (struct
 type t = int
 let lt a b = a < b
 let eq a b = a = b
 let leq a b = a <= b
end) 

A design of functors in OCaml


By : user166147
Date : March 29 2020, 07:55 AM
With these it helps In Option (1), do you apply ZoneFun inside ZonesFun?
Assuming that, I think the choice is between (1) and (3)/(4) (which seem to be the same). Which one to pick depends on whether you need to be able to access the created Zone modules outside ZoneFun (you need (4)) or not ((1) works fine).
code :
val make: ZoneFun(X).t -> ZonesFun(X).t
module type PROP = sig type t end
module type ZONE = sig type t end
module MakeZone (P : PROP) = struct type t = {p : P.t} end
module MakeZones (Z : ZONE) = struct type t = ZS of Z.t list end

module MakeMisc (P : PROP) :
sig
  val make : MakeZone(P).t -> MakeZones(MakeZone(P)).t
end =
struct
  module Zone = MakeZone(P)
  module Zones = MakeZones(Zone)
  let make z = Zones.ZS [z]
end

module Type = struct type t = T end
module Formula = struct type t = F end
module ZoneType = MakeZone(Type)
module ZoneFormula = MakeZone(Formula)
module ZonesType = MakeZones(ZoneType)
module ZonesFormula = MakeZones(ZoneFormula)
module MiscType = MakeMisc(Type)
module MiscFormula = MakeMisc(Formula)
let zst = MiscType.make {ZoneType.p = Type.T}
let zsf = MiscFormula.make {ZoneFormula.p = Formula.F}
module MakeMisc (P : PROP) :
sig
  val make : MakeZone(P).t -> MakeZones(MakeZone(P)).t
end =
struct
  module Zone : sig type t = MakeZone(P).t end = MakeZone(P)
  module Zones :
    sig type t = MakeZones(MakeZone(P)).t = ZS of MakeZone(P).t list end =
    MakeZones(MakeZone(P))
  let make z = Zones.ZS [z]
end
module type PROP = sig type t end
module type ZONE = sig type prop type t = {p : prop} end
module type ZONES = sig type zone type t = ZS of zone list end
module MakeZone (P : PROP) = struct type prop = P.t type t = {p : prop} end
module MakeZones (Z : ZONE) = struct type zone = Z.t type t = ZS of zone list end

module MakeMisc
  (P : PROP) (Z : ZONE with type prop = P.t) (Zs : ZONES with type zone = Z.t) :
sig
  val make : Z.t -> Zs.t
end =
struct
  let make z = Zs.ZS [z]
end

module Type = struct type t = T end
module Formula = struct type t = F end
module ZoneType = MakeZone(Type)
module ZoneFormula = MakeZone(Formula)
module ZonesType = MakeZones(ZoneType)
module ZonesFormula = MakeZones(ZoneFormula)
module MiscType = MakeMisc(Type)(ZoneType)(ZonesType)
module MiscFormula = MakeMisc(Formula)(ZoneFormula)(ZonesFormula)
let zst = MiscType.make {ZoneType.p = Type.T}
let zsf = MiscFormula.make {ZoneFormula.p = Formula.F}

OCaml - When To Use Functors


By : Miguel Saiz
Date : March 29 2020, 07:55 AM
To fix the issue you can do Functors don't really apply here since there are no modules, but you could attack this quite directly with higher order functions.
The key is to observe that you need one transform, and thus one function argument, for each constructor. This pattern is similar in some ways to to List.fold_right, which can be thought of as an operation that replaces the list constructors.
code :
type expr =
  | Add of expr * expr
  | Sub of expr * expr
  | Mul of expr * expr
  | Div of expr * expr
  | Int of int

let transform ~add ~sub ~mul ~div ~int expr =
  let rec tx = function
    | Add (x, y) -> add (tx x) (tx y)
    | Sub (x, y) -> sub (tx x) (tx y)
    | Mul (x, y) -> mul (tx x) (tx y)
    | Div (x, y) -> div (tx x) (tx y)
    | Int x -> int x in
  tx expr

let binary_op_str sep a b = "(" ^ a ^ sep ^ b ^ ")"

let display_expr = transform
  ~add:(binary_op_str " + ")
  ~sub:(binary_op_str " - ")
  ~mul:(binary_op_str " * ")
  ~div:(binary_op_str " / ")
  ~int:string_of_int

let cal_expr = transform
  ~add:(+)
  ~sub:(-)
  ~mul:( * )
  ~div:(/)
  ~int:(fun x -> x)

OCaml Signature Functors


By : Charanjit Singh
Date : March 29 2020, 07:55 AM
around this issue Unfortunately, to my knowledge this is not possible. You will have to do
code :
module type Y = functor (A : ModuleA) ->
  sig
    include I with type t := A.t
    val blah : A.t -> int
  end
module type Y = functor (A : ModuleA) ->
  sig
    include module type of XX(A)
    val blah : A.t -> int
  end

OCaml - functors - how to use?


By : Alex
Date : March 29 2020, 07:55 AM
hop of those help? Functors, essentially are a way to write modules in terms of other modules.
A pretty classic example is the Map.Make functor from the standard library. This functor lets you define a map with a specific key type.
shadow
Privacy Policy - Terms - Contact Us © bighow.org