From: "L.G. Meredith" <lgreg.meredith@gmail.com>
To: caml-list <caml-list@inria.fr>
Subject: recursive modules?
Date: Wed, 31 May 2006 12:35:54 -0700 [thread overview]
Message-ID: <5de3f5ca0605311235h1f1db228t8e10f4ef65233e05@mail.gmail.com> (raw)
[-- Attachment #1: Type: text/plain, Size: 6003 bytes --]
All,
i'm mostly a neophyte with OCaml. Please find below a simple example of a
code i would like to be able to write, but that the compiler will not eat. i
build a term language over a set of variables that are themselves abstractly
characterized in terms of some identifier type. i would like to write a
recursive module in which the abstract type of identifier is essentially the
type of the joinedcompositionfunctor applied to itself. One can show that
this results in a (mathematically) well-founded definition. Is there a
version of OCaml or an OCaml compiler option that will enable this kind of
recursive definition?
Best wishes,
--greg
(* What we demand of identifiers used by variables *)
module type IDENTIFIER =
sig
type idType
val comparator : idType -> idType -> bool
end
(* Two monoids 'joined at the hip', i.e. sharing an identity and set of
'variables' *)
module type JOINEDCOMPOSITIONFUNCTOR =
functor (VarId : IDENTIFIER) ->
sig
type idType = VarId.idType
type composite =
Empty
| Var of idType
| Mult of composite list
| Sum of composite list
exception IllFormedMultiplication
exception IllFormedSummation
val zero : composite
val multiply : composite list -> composite
val sum : composite list -> composite
end
module JoinedCompositionFunctor =
functor ( VarId : IDENTIFIER ) ->
struct
type idType = VarId.idType
type composite =
Empty
| Var of idType
| Mult of composite list
| Sum of composite list
exception IllFormedMultiplication
exception IllFormedSummation
let zero = Empty
let rec multiply cList =
(match cList with
[] -> Empty
| cListHd :: cListRest ->
(let mRest = multiply cListRest in
(match cListHd with
Empty -> mRest
| _ ->
(match mRest with
Empty -> Mult( [ cListHd ] )
| Mult( mList ) -> Mult( cListHd :: mList )
| _ -> raise IllFormedMultiplication))))
let rec sum cList =
(match cList with
[] -> Empty
| cListHd :: cListRest ->
(let mRest = sum cListRest in
(match cListHd with
Empty -> mRest
| _ ->
(match mRest with
Empty -> Sum( [ cListHd ] )
| Sum( mList ) -> Sum( cListHd :: mList )
| _ -> raise IllFormedSummation))))
end
(* A structural equivalence, with an abstract characterization of
bag-equivalence *)
module Equivalence ( TFun : JOINEDCOMPOSITIONFUNCTOR ) ( VarId : IDENTIFIER
) =
struct
module JoinedComposition = TFun( VarId )
let rec structural c1 c2 =
match ( c1, c2 ) with
( JoinedComposition.Empty, JoinedComposition.Empty ) -> true
| ( JoinedComposition.Mult( [] ), JoinedComposition.Empty ) -> true
| ( JoinedComposition.Empty, JoinedComposition.Mult( [] ) ) -> true
| ( JoinedComposition.Sum( [] ), JoinedComposition.Empty ) -> true
| ( JoinedComposition.Empty, JoinedComposition.Sum( [] ) ) -> true
| ( JoinedComposition.Mult( [] ), JoinedComposition.Sum( [] ) ) -> true
| ( JoinedComposition.Sum( [] ), JoinedComposition.Mult( [] ) ) -> true
| ( JoinedComposition.Mult( multiplicands1 ), JoinedComposition.Mult(
multiplicands2 ) ) ->
(structuralBags multiplicands1 multiplicands2
JoinedComposition.multiply structural)
| ( JoinedComposition.Sum( sumands1 ), JoinedComposition.Sum( sumands2 )
) ->
(structuralBags sumands1 sumands2 JoinedComposition.sum structural)
| ( JoinedComposition.Var( id1 ), JoinedComposition.Var( id2 ) ) ->
( VarId.comparator id1 id2 )
| ( d1, d2 ) -> ( d1 = d2 )
and structuralBags opands1 opands2 compositeCtor equiv =
(match opands1 with
[] ->
(match opands2 with
[] -> true
| _ -> false)
| opands1Hd :: opands1Rest ->
let compositeCtor1Rest = (compositeCtor opands1Rest) in
let filterfn =
(fun opands2Candidate ->
(equiv opands1Hd opands2Candidate)) in
(match opands2 with
[] -> false
| opands2Hd :: opands2Rest ->
match (List.partition filterfn opands2) with
( [], _ ) -> false
| ( candidatesHd :: candidatesRest, failures ) ->
let lc = ref true in
let found = ref false in
let c = ref candidatesHd in
let cRest = ref candidatesRest in
let fails = ref failures in
((while !lc do
if (equiv compositeCtor1Rest (compositeCtor (!cRest @
!fails)))
then
(found := true;
lc := false)
else
(fails := !c :: !fails;
(match !cRest with
[] -> lc := false
| cRestHd :: cRestRest ->
(c := cRestHd;
cRest := cRestRest;)))
done);
!found)))
end
module VariableOfString =
struct
type idType = string
let comparator = (=)
end
module JoinedCompositionOfString = JoinedCompositionFunctor (
VariableOfString )
module JoinedEquivalenceOfString = Equivalence ( JoinedCompositionFunctor )
( VariableOfString )
(*
The following definition can be proven to be mathematically
well-founded, but is there a compiler that will do the right thing?
module VariableOfRJoinedComposition =
struct
module RJoinedCompositionEquivalence =
Equivalence ( JoinedCompositionFunctor )( VariableOfJoinedRComposition )
type idType = RJoinedCompositionEquivalence.JoinedComposition.idType
let comparator =
RJoinedCompositionEquivalence.JoinedComposition.comparator
end
*)
--
L.G. Meredith
Partner
Biosimilarity LLC
505 N 72nd St
Seattle, WA 98103
+1 206.650.3740
[-- Attachment #2: Type: text/html, Size: 10888 bytes --]
next reply other threads:[~2006-05-31 19:36 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-05-31 19:35 L.G. Meredith [this message]
2006-05-31 23:51 ` [Caml-list] " Jacques Garrigue
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=5de3f5ca0605311235h1f1db228t8e10f4ef65233e05@mail.gmail.com \
--to=lgreg.meredith@gmail.com \
--cc=caml-list@inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox