Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A design of modules and interface

Tags:

ocaml

I would like to define an interface PROPERTY, and at least 2 modules Type and Formula matching it:

module type PROPERTY =
sig
  type t
  val top : t
  val bot : t
  val to_string: t -> string
  val union: t -> t -> t
  val intersection: t -> t -> t
end

module Type = (struct
  type t =
   | Tbot
   | Tint
   | Tbool
   | Ttop
  ...      
end: PROPERTY)

module Formula = (struct
  type t =
   | Fbot
   | Ftop
   | Fplus of int * Type.t
   ...
  let union = 
   ... Type.union ...
  ...      
end: PROPERTY)

There are two requirements:

1) I would like the constructors of Type can be called outside (all the programs if necessary)

2) A part of some values of Formula contain values of Types, for instance Fplus (5, Type.Tint) is of type Formula; also some functions of Formula would need to call some functions of Type, for example, Formula.union needs to call Type.union

Could anyone tell me how to amend the declaration above to fullfil my requirements? Extra modules can be added if necessary...

like image 231
SoftTimur Avatar asked Dec 18 '12 13:12

SoftTimur


People also ask

What is a module interface?

A module interface expresses the elements that are provided and required by the module. The elements defined in the interface are detectable by other modules. The implementation contains the working code that corresponds to the elements declared in the interface.

What is module design?

Modular design is basically to decompose complex systems into simple modules in order to more efficiently organize complex designs and processes. The concept was first introduced by Star (1965), in which the use of modular product in production was proposed as a new concept to develop variety.

What is a module in modular design?

Modular design, or modularity in design, is a design principle that subdivides a system into smaller parts called modules (such as modular process skids), which can be independently created, modified, replaced, or exchanged with other modules or between different systems.

What are two essential concepts in modular design?

Information hiding modules and abstract interfaces are the basic concepts needed to design multi-version programs.


1 Answers

Don't apply the : PROPERTY sealing casts to the module declaration. This hides the extra information from the returned module. You should rather use:

 module Type = struct .. end
 module Formula = struct .. end

If you still want to check that Type and Formula do satisfy the PROPERTY interface, you can do that separately:

 let () =
   ignore (module Type : PROPERTY);
   ignore (module Formula : PROPERTY);
   ()
like image 182
gasche Avatar answered Sep 26 '22 03:09

gasche