* [Caml-list] Reasoning about categories at compile-time. @ 2011-03-30 22:27 Guillaume Yziquel 2011-03-31 6:02 ` Gabriel Scherer 0 siblings, 1 reply; 5+ messages in thread From: Guillaume Yziquel @ 2011-03-30 22:27 UTC (permalink / raw) To: caml-list Hello. I have a small problem that I would wish to encode in the type system, and I would like some advice on how to do that using Camlp4. You have a finite category (in the sense of a finite number of objects), and a finite set of arrows that generates all the arrows. Let's assume that you have tokens (Camlp4 a_LIDENT) for all of these. I want my Camlp4 syntax extension to operate on an .mli interface file and an .ml file. The .mli interface should contain all the relations you want to enforce concerning composition of arrows. The .ml file should contain some relations about composition of arrows. If I compile the preprocessed .ml file against the preprocessed .mli file, I want it to type check if and only if all relations in the .mli file can be deduced from relations in the .mli file. If, for instance the unprocessed .mli file contains f o g o h = i o j o k and the unprocessed .ml file contains f o g = i h = j o k I want it to type check fine. I you ommit h = j o k, I want the type checker to fail. This is purely type-system-ish, and I really do not care how arrows are encoded (types, type parameters, fun (type arrow) -> stuff). Is there a way to use Camlp4 to encode this in the type system? Intuitively, I'd say yes, but I currently do not see how. -- Guillaume Yziquel ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] Reasoning about categories at compile-time. 2011-03-30 22:27 [Caml-list] Reasoning about categories at compile-time Guillaume Yziquel @ 2011-03-31 6:02 ` Gabriel Scherer 2011-03-31 23:46 ` Guillaume Yziquel 0 siblings, 1 reply; 5+ messages in thread From: Gabriel Scherer @ 2011-03-31 6:02 UTC (permalink / raw) To: Guillaume Yziquel; +Cc: caml-list [-- Attachment #1: Type: text/plain, Size: 3234 bytes --] First, one obvious solution : parse the equations, run a solver, then output "()" if it can deduce the queries from the equations, and "1 = true" otherwise. Why isn't this solution satisfying ? With the type system there is one thing that is easy and natural to do : you could encode your arrows as elements with a specific type (eg. for an arrow `f` of source `src` and destination `dst` you would write `(src, f, dst) arrow` with `src, f, dst` abstract types) : arrow_f : (foo, f, bar) arrow then write both a composition function compose : ('a, 'f, 'b) arrow -> ('b, 'g, 'c) arrow -> ('a, ('f, 'g) comp, 'c) arrow and terms representing your equational theory: assoc : ('a, (('f, 'g) comp, 'h) comp, 'b) arrow -> ('a, ('f, ('g, 'h) comp) comp, 'b) and for each equation f o g = h (not that I use `g` and not `'g`) witness_fg_h : ('a, (f, g) comp, 'b) arrow -> ('a, h, 'b) arrow Then, for each deducible relation, there exist a typed term that is a proof witness of the relation deductibility (you just unify the type of the two arrows that should be equal). If you have a solver for equation deductibility, you can ask him to output the proof witness in this format. This does not use the inference system to solve your problem, which is what you asked for. But this is a solid design that is going to still work if you change your language, eg. can still be used on non decidable questions. I think this is a good first step; feel free to improve it by whatever advanced type hackery. On Thu, Mar 31, 2011 at 12:27 AM, Guillaume Yziquel < guillaume.yziquel@citycable.ch> wrote: > Hello. > > I have a small problem that I would wish to encode in the type system, > and I would like some advice on how to do that using Camlp4. > > You have a finite category (in the sense of a finite number of objects), > and a finite set of arrows that generates all the arrows. Let's assume > that you have tokens (Camlp4 a_LIDENT) for all of these. > > I want my Camlp4 syntax extension to operate on an .mli interface file > and an .ml file. > > The .mli interface should contain all the relations you want to enforce > concerning composition of arrows. > > The .ml file should contain some relations about composition of arrows. > > If I compile the preprocessed .ml file against the preprocessed .mli > file, I want it to type check if and only if all relations in the .mli > file can be deduced from relations in the .mli file. > > If, for instance the unprocessed .mli file contains > > f o g o h = i o j o k > > and the unprocessed .ml file contains > > f o g = i > h = j o k > > I want it to type check fine. I you ommit h = j o k, I want the type > checker to fail. > > This is purely type-system-ish, and I really do not care how arrows are > encoded (types, type parameters, fun (type arrow) -> stuff). > > Is there a way to use Camlp4 to encode this in the type system? > Intuitively, I'd say yes, but I currently do not see how. > > -- > Guillaume Yziquel > > -- > Caml-list mailing list. Subscription management and archives: > https://sympa-roc.inria.fr/wws/info/caml-list > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs > > [-- Attachment #2: Type: text/html, Size: 4101 bytes --] ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] Reasoning about categories at compile-time. 2011-03-31 6:02 ` Gabriel Scherer @ 2011-03-31 23:46 ` Guillaume Yziquel 2011-04-03 7:25 ` Andrej Bauer 0 siblings, 1 reply; 5+ messages in thread From: Guillaume Yziquel @ 2011-03-31 23:46 UTC (permalink / raw) To: Gabriel Scherer; +Cc: caml-list Le Thursday 31 Mar 2011 à 08:02:30 (+0200), Gabriel Scherer a écrit : > First, one obvious solution : parse the equations, run a solver, then > output "()" if it can deduce the queries from the equations, and "1 = > true" otherwise. Why isn't this solution satisfying ? Because that would mean, in Camlp4, doing a processing of the whole module X : sig (* category constraints / proof goals *) end = struct (* category axioms *) end I want to be able to deal separately with .ml and .mli files. Or with a module structure and a module signature. Therefore I need it to be such that it is the type checker that is in charge of being the solver. I'd love a 'type system extension' mechanism... this way I could include the solver in it. > With the type system there is one thing that is easy and natural to do > : you could encode your arrows as elements with a specific type (eg. > for an arrow `f` of source `src` and destination `dst` you would write > `(src, f, dst) arrow` with `src, f, dst` abstract types) : > > arrow_f : (foo, f, bar) arrow > > then write both a composition function > > compose : ('a, 'f, 'b) arrow -> ('b, 'g, 'c) arrow -> ('a, ('f, 'g) comp, 'c) arrow > > and terms representing your equational theory: > > assoc : ('a, (('f, 'g) comp, 'h) comp, 'b) arrow -> ('a, ('f, ('g, 'h) comp) comp, 'b) > > and for each equation f o g = h (not that I use `g` and not `'g`) > > witness_fg_h : ('a, (f, g) comp, 'b) arrow -> ('a, h, 'b) arrow > > Then, for each deducible relation, there exist a typed term that is a > proof witness of the relation deductibility (you just unify the type of > the two arrows that should be equal). If you have a solver for equation > deductibility, you can ask him to output the proof witness in this > format. I thought of that, though not as precisely as you're exposing it. But keeping the .ml / .mli distinction and compiling them separately, you have a problem: Essentially, the witnesses live in the .ml file, and the proof witness also lives in the .ml file. In the .mli file, you simply have to declare the existence of a proof witness term. However, in order to construct the proof witness, you need to know the goal that you wish to prove, which means providing to camlp4 some information that lives in the .mli file. This would break separate compilation. Or rather separate preprocessing. You could not preprocess the .ml file before the .mli file. Worse: you could not do: module X with syntax extension = struct ... end module type S with syntax extension = sig ... end let m = (module X : S) (which is what I want to do, specifically). > This does not use the inference system to solve your problem, which is > what you asked for. But this is a solid design that is going to still > work if you change your language, eg. can still be used on non > decidable questions. I think this is a good first step; feel free to > improve it by whatever advanced type hackery. Unfortunately, this doesn't fit the bill. I had this picture in mind intuitively but not formalised, and I saw the limitation I explained above. That's what prompted me to ask on the list in the first place. But thanks for clarifying my hazy thoughts. What bugs me is that, to put it simply, the category axioms are generated by Camlp4 in a non-deterministic fashion, dependent on the outside world. You only have control in user code on the proof goals. What you propose means linking module struct and module type syntactically. However, I'd need them decoupled as I'd like in the long run to do the following: You have two categories, C1 and C2. You take the set union of both categories C1 U C2. (Set union of objects, and set unions of arrows). This gives two OCaml modules. You include both modules into a bigger module, add some arrows and arrow composition axioms. You then want to match them against a module signature describing the proof goals. Do the same thing with first-class modules (OCaml 3.13 doesn't need so much module signatures as OCaml 3.12 for first-class modules, which goes in the sense of 'decoupling' preprocessed modules and preprocessed signatures), and you get an idea of what I'm up to. That's why I'm looking for a purely type-system encoding of this problem. Somehow lifting the solver for equational deducibility you mentionned into the type inference algo itself. So instead of having arrows as private types, I was thinking of having arrows as type parameters. And have unification doing the solving. Unfortunately, if you put equational constraints in the module signature, they leak into the module implementation (funny to have a signature leaking in an implementation by the way). Just think of the simple module X : sig val f : 'a -> 'a end = struct let f : 'a -> 'b = Obj.magic end This is somehow reversed: module X : sig val f : 'a -> 'b end = struct let f : 'a -> 'a = assert false end fails to type-check, which could perhaps be understood as 'given two types not known to be equal, I fail to conclude that 'a = 'a and 'b = 'a, i.e. I fail to conclude that 'b = 'a. This is tautological, but it's interesting to see that the axioms are now in the signature and the goal in the module structure. I was wondering perhaps naïvely how much truth there is in the above paragraph, and if it could be used to encode the equational solver into the unification algo in one way or another. -- Guillaume Yziquel ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] Reasoning about categories at compile-time. 2011-03-31 23:46 ` Guillaume Yziquel @ 2011-04-03 7:25 ` Andrej Bauer 2011-04-03 21:03 ` Guillaume Yziquel 0 siblings, 1 reply; 5+ messages in thread From: Andrej Bauer @ 2011-04-03 7:25 UTC (permalink / raw) To: Guillaume Yziquel; +Cc: caml-list I would be interested to hear why the problem must be solved with camlp4 and ocaml, because this looks a lot like an instance of someone with a hammer looking for some nails to hit. With kind regards, Andrej ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] Reasoning about categories at compile-time. 2011-04-03 7:25 ` Andrej Bauer @ 2011-04-03 21:03 ` Guillaume Yziquel 0 siblings, 0 replies; 5+ messages in thread From: Guillaume Yziquel @ 2011-04-03 21:03 UTC (permalink / raw) To: Andrej Bauer; +Cc: caml-list Le Sunday 03 Apr 2011 à 09:25:54 (+0200), Andrej Bauer a écrit : > I would be interested to hear why the problem must be solved with > camlp4 and ocaml, because this looks a lot like an instance of someone > with a hammer looking for some nails to hit. Because it's fun and interesting. -- Guillaume Yziquel ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2011-04-03 21:05 UTC | newest] Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2011-03-30 22:27 [Caml-list] Reasoning about categories at compile-time Guillaume Yziquel 2011-03-31 6:02 ` Gabriel Scherer 2011-03-31 23:46 ` Guillaume Yziquel 2011-04-03 7:25 ` Andrej Bauer 2011-04-03 21:03 ` Guillaume Yziquel
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox