Repository navigation
Description
I have an idea - it'd be great if the unifier/bottom-up prover could unify on work variables more aggressively, at least optionally. This is an area where mmj2 can currently do more than metamath-lamp can.
I'm trying to re-prove syl when loading set.mm and stopped after ax-3 (that is, I only have the axioms in the context).
I want to prove, using ax-2, the following step which was created earlier by a backwards application of ax-mp:
|- ( &W1 -> ( ( ph -> ps ) -> ( ph -> ch ) ) )
However, when I select this statement and unify (prove bottom-up), ax-2 doesn't appear as an option. That's true even if I allow "everything" by changing the Length restriction to "No", allow root statements at all levels, and enable everything else. If I try to enter ax-2 directly as a label, the backwards prover won't offer ax-2 as an option - it clearly thinks this statement cannot match ax-2.
Follow this link to see the state. The full situation is also captured by this json:
{"srcs":[{"typ":"Web","fileName":"","url":"https://us.metamath.org/metamath/set.mm","readInstr":"StopAfter","label":"ax-3","resetNestingLevel":true,"allLabels":[]}],"descr":"Reprove syllogism (\"syl\"), using only axioms.","varsText":"var1 wff &W1","disjText":"","stmts":[{"label":"syl.1","typ":"e","cont":"|- ( ph -> ps )","jstfText":""},{"label":"syl.2","typ":"e","cont":"|- ( ps -> ch )","jstfText":""},{"label":"3","typ":"p","cont":"|- ( &W1 -> ( ( ph -> ps ) -> ( ph -> ch ) ) )","jstfText":""},{"label":"2","typ":"p","cont":"|- &W1","jstfText":""},{"label":"1","typ":"p","cont":"|- ( ( ph -> ps ) -> ( ph -> ch ) )","jstfText":"2 3 : ax-mp"},{"label":"syl","typ":"p","cont":"|- ( ph -> ch )","jstfText":"syl.1 1 : ax-mp"}]}The desired state is to be able to unify it with ax-2.
It also won't unify through merging. I can search for ax-2 and insert it, which is fine, and the instance has a bunch of metavariables as expected. But "merge" will not merge the original statement and the instance of ax-2, no matter what the order is.
I can replace the work variable &W1 with ( ph -> ( ps -> ch ) ) and apply "unify". Then bottom-up proving immediately unifies to that result.
This is unfortunate. If the backwards prover could handle this case (even optionally) I bet it could automatically prove a lot more. In fact, if it noticed these matches, it could probably prove some things that mmj2 struggles with (because in mmj2 you can't really control the search of just one statement very well).
I tested on this, but I don't think the version matters: https://expln.github.io/lamp/dev/index.html
This is in contrast to mmj2, which can unify some work variables and lets you specify an assertion like ax-2 to be used on such a statement. A "before" and "after" should clarify things. For example, here's an example of "syllogism" where I just typed in ax-2 in the analogous situation (the "*" comments are hints to myself of where I'm going):
$( <MM> <PROOF_ASST> THEOREM=syllogism LOC_AFTER=
hd1::syllogism.1 |- ( ph -> ps )
hd2::syllogism.2 |- ( ps -> ch )
* ! |- ( ph -> ( ps -> ch ) )
* ! |- ( ( ph -> ps ) -> ( ph -> ch ) )
!d3:: |- &W1
!d5:: |- &W2
!d6::ax-2 |- ( &W2 -> ( &W1 -> ( ph -> ch ) ) )
d4:d5,d6:ax-mp |- ( &W1 -> ( ph -> ch ) )
qed:d3,d4:ax-mp |- ( ph -> ch )
$)
In mmj2, after pressing control-U, I get a unification which helps me to keep going:
$( <MM> <PROOF_ASST> THEOREM=syllogism LOC_AFTER=
hd1::syllogism.1 |- ( ph -> ps )
hd2::syllogism.2 |- ( ps -> ch )
* ! |- ( ph -> ( ps -> ch ) )
* ! |- ( ( ph -> ps ) -> ( ph -> ch ) )
!d3:: |- ( ph -> &W3 )
!d5:: |- ( ph -> ( &W3 -> ch ) )
d6::ax-2 |- ( ( ph -> ( &W3 -> ch ) )
-> ( ( ph -> &W3 ) -> ( ph -> ch ) ) )
d4:d5,d6:ax-mp |- ( ( ph -> &W3 ) -> ( ph -> ch ) )
qed:d3,d4:ax-mp |- ( ph -> ch )
$)
Activity
expln commentedon Jun 4, 2023
Thanks for the detailed explanations. I will analyze the issue and return with some response.
david-a-wheeler commentedon Jun 4, 2023
Thanks, and as always, thanks for your time.
I did an experiment with a surprising result, which may be helpful to you. I expected that the problem was that "&W1" would only match a wff variable (which would be an understandable limitation). So I modified "Variables" to say:
I then modified my "statement 3" to delete its justification & changed the first wff variable to a work variable, resulting in this:
Even with this change, a bottom-up proof didn't find ax-2.
david-a-wheeler commentedon Jun 5, 2023
In case it's relevant, it'd also be awesome if the tool could automatically replace work variables on unification. The review posted on May 27, 2023, at 10:17 AM, by Alexander van der Vekens on the Metamath mailing list (metamath@googlegroups.com) said:
I'm not sure this is related, but since it might be, I thought I'd note it.
expln commentedon Jun 5, 2023
I analyzed this issue and yes indeed this is not a bug, this is a limitation of the mm-lamp unifier.
This case is special because of the following reason.
In order to unify
ax-2with|- ( &W1 -> ( ( ph -> ps ) -> ( ph -> ch ) ) )we have to substitute variables in both expressions.But mm-lamp's unifier is unable to find such substitutions.
What it can do is: given two expressions exprA and exprB, it can find a substitution subsUni such that by applying subsUni to exprA we are obtaining exprB, i.e.:
subsUni(exprA) = exprB.
What the case described in this issue requires is to find a substitution subsBi such that (using similar notation):
subsBi(exprA) = subsBi(exprB).
In my notation, Uni stands for "unidirectional" and Bi stands for "bidirectional".
Let me demonstrate this on real expressions.
In oder for the next example not to be misleading, I have to express
ax-2using work variables, but this should not be a problem because it doesn't matter what variable names we choose to describe an assertion as soon as variable types are correct (please correct me if I am wrong).exprA =
|- ( ( &W3 -> ( &W4 -> &W2 ) ) -> ( ( &W3 -> &W4 ) -> ( &W3 -> &W2 ) ) )exprB =
|- ( &W1 -> ( ( ph -> ps ) -> ( ph -> ch ) ) )A substitution subsBi (such that subsBi(exprA) = subsBi(exprB) ) exists, and it is as follows:
But mm-lamp searches only for subsUni, such that subsUni(exprA) = exprB. Obviously it doesn't exist because length(exprB) < length(exprA).
The workaround you've described in the guide, when you manually substitute
&W1 → ( ph -> ( ps -> ch ) )is absolutely correct. It allows to bypass this limitation using the smallest amount of logical steps.I would like to share another possible solution. It uses more logical steps, but it demonstrates that such a "bidirectional" substitution may be achieved by two "unidirectional" substitutions. I recorded a video because it would be difficult to write (and probably to read) an explanation - (no sound) https://drive.google.com/file/d/1KIr0eOEmH4VoIHOHFhqXwBn08h-xGicV/view?usp=sharing
To fix this issue, I plan to use the fact that a "bidirectional" substitution may be achieved by two "unidirectional" substitutions.
I don't have a very clear plan how to implement this in mm-lamp, but I have some ideas to begin with.
Sorry if my explanation is difficult to understand because of unusual terminology. I am ready to answer clarifying questions if you have any.
david-a-wheeler commentedon Jun 6, 2023
@expln - thanks for the explanation! I also appreciate you pointing out that it's possible to select two expressions to make replacements easier.
For now, I think the solution is to document both approaches in the guide, given the current limitations in the unification algorithm. That said, I hope we don't stop there, see my next comment here...
david-a-wheeler commentedon Jun 6, 2023
In the longer term, it'd be great if the unifier could handle this more general case, at least as an enable-able option. Unification has been studied to death, I bet there are some awesome algorithms that do much with decent performance.
@digama0 - any suggestions on how to improve the metamath-lamp unification algorithm, given the discussion above? In particular, any pointers to some of the most relevant literature?
digama0 commentedon Jun 6, 2023
There is a canonical algorithm for what @expln calls "bidirectional unification" (normally this is called "unification" and the unidirectional version is called "matching") which can be done purely recursively. There is a description on wikipedia, although I think the version I have implemented for MM1 is both more executable and simpler. I will summarize it:
unify(e1, e2)which takes two expressionse1ande2containing metavariables (work variables) and will assign those variables to makee1ande2equal.unify(e1, e2): Pattern matche1ande2.e1 = e2then successe1is a metavariable thenassign(e1, e2)e2is a metavariable thenassign(e2, e1)e1ande2are distinct variables, or one is a variable and the other is a constructor, or both are distinct constructors then faile1 = t(a1, ..., an)ande2 = t(b1,..., bn)thenunify(ai, bi)for1 <= i <= nassign(mv, e)takesmva metavariable and assigns it toe, if possible:e = mvthen successecontainsmvthen fail (this is the occurs check)mv := e; all other equality checks should be performed modulo this assignment so either you should rewrite the whole state to replace occurrences ofmvwithe, or store an mvar state and transparently use it in the pattern match inunify.david-a-wheeler commentedon Jun 6, 2023
@digama0 - thank you so much for your wonderfully clear & specific reply!
Now things make much more sense. Alexander van der Vekens earlier complained of problems using metamath-lamp, and while I'm sure part of the problem was a lack of documentation, this difference is almost certainly part of the issue as well.
I don't know how long it would take to implement full unification instead. I suspect that once it is, the tool would become much more capable AND would be easier to use for those who've used other tools like mmj2. Full unification might be slower than matching; I can see selecting matching vs. unification as a possible option in the search dialogue. That said, I suspect "full unification" would be the preferred setting, as that would greatly increase the likelihood of finding solutions.
expln commentedon Jun 6, 2023
Thank you @digama0 for your detailed answer and especially for providing the summary of the algorithm. I would not understand it from the Rust code without that summary.
In this notation
t(a1, ..., an), is my understanding correct that this represents a tree structure where a1,...,an are sibling nodes of this tree? And overall, all expressions are stored as trees (or in other format which may be transformed into a tree very fast)?@david-a-wheeler
Sure, I already have one idea what I can do to handle this and other similar cases. But yeah, that's not a simple change, so it will need some time for implementation.
Thank you for posting this issue with detailed description and explanations how it is handled in mmj2. Currently this is the only way how I can discover such non-trivial uses cases commonly used during creation of proofs.
digama0 commentedon Jun 6, 2023
Yes,
a1, ..., anare the children of a node withtas the constructor. And yes, this is presuming that the expressions are stored as trees, or else already parsed to trees. For example,( &W1 -> ( ph -> &W2 ) )is represented asapp[wi](mvar[wff, 1], app[wi](var[wph], mvar[wff, 2]))(wheret(a1,..., an)is now being written more explicitly asapp[t](a1, ..., an)); for most purposes you can probably treatvarnodes the same asapp, except for $d propagation.I would say that not doing parsing would be a big misstep for any MM proof assistant - you gain the ability to read some unusual metamath databases at the cost of not being able to reason about the proof at a high enough level to perform useful automation activities.
david-a-wheeler commentedon Jun 9, 2023
If it's not too hard, I suggest leaving the old unifier as an option, at least for bottom-up search. If the bottom up prover is too powerful, it's hard to make simple examples in the guide to explain how to deal with cases where the automation doesn't work. So having ways to disable the automation can help. That said, I expect that the default should be full unification.
david-a-wheeler commentedon Jun 9, 2023
FYI, I looked for some more resources about implementing Unification, especially in OCaml, Rescript, or JavaScript. My hope is that this would help in addition to the awesome info from Mario above. Unification is a really old well-studied problem; Robinson's unification algorithm dates from 1965.
Here are some things I found that might be useful:
BTW, there's a nice video of the Curry-Howard Correspondence here (I found it while looking for examples): https://cs3110.github.io/textbook/chapters/adv/curry-howard.html
expln commentedon Jun 10, 2023
Thanks David and Mario for the detailed explanations! The unification algorithm described by Mario looks really easy. I will analyze everything and return with my feedback.
david-a-wheeler commentedon Jun 25, 2023
I totally understand if that's deferred, but I think this one is key. I think there are two parts to this:
38 remaining items
david-a-wheeler commentedon Jul 30, 2023
Yes, please. If I click on 2 statements and click on merge. I think users would expect the tool to do it if it's possible.
I agree with @digama0 - unification is a powerful general-purpose algorithm that can do a number of actions automatically, and I'd really like to see it used so that a single change in one place can cause automatic replacements like this.
expln commentedon Sep 8, 2023
@digama0
Hi Mario,
Can you explain how MM1 builds syntax trees? Currently, I have performance problems with the algorithm I use in mm-lamp. My algorithm of building a syntax tree for an expression, for example
s1 s2 s3 ..., is as follows:class s1 s2 s3 ...,setvar s1 s2 s3 ...,wff s1 s2 s3 ...;Transforming a proof into a syntax tree is a straightforward process because the proof is already a tree. Finding a proof is much more expensive process. I am using the same algorithm as in mm-lamp's bottom-up prover to find syntax proofs, which tries to apply all possible assertions to build a proof bottom-up. If needed I can provide more details on this algorithm, but I think this should be enough to understand why it is not very fast.
This algorithm works sufficiently well for a small amount of statements. But now I want to build syntax trees for all assertions in set.mm, and that will be very slow.
digama0 commentedon Sep 9, 2023
The process of building syntax trees from expressions is called parsing. There are a few ways you can do it, but the simplest is to put the syntax axioms into a trie and do a general backtracking algorithm. This can perform poorly on highly ambiguous grammars, but set.mm is unambiguous by design, and even though some backtracking is still needed because things might only be resolved after a while, it's not so bad in practice on set.mm.
A trie node has the following data:
cto trie nodes (shown asconst cbelow)vto trie nodes (shown asvar vbelow)reduce axbelow)The top level of the trie is a map from typecodes to trie nodes. Every path through the tree represents a sequence of constants and variables, and during grammar initialization (which you can do either up front or as you encounter syntax axioms) you travel down the tree and add the syntax axiom. For example, starting from the empty tree:
we can insert axiom
wi, that iswff ( ph -> ps ), to produce the tree:If we insert
wa $a wff ( ph /\ ps )andwn $a wff -. phinto the tree as well, we get:Having prepared a data structure for parsing, now suppose we have a string of constants and variables we want to parse. Here's a rendering of the algorithm as lean code:
Described in more detail:
s1 s2 ...with expected typecodec(usuallywfffor set.mm) we runparseFmla(c, T, [s1, s2, ...]), which returns the first successful parseparseFmladepends on some mutually recursive functions:parse(tc, pos, k)parses the substring of the input starting atposas an expression of typetc, passing each successful parse tokand returning the first successparsePT(pos, args, k, N)uses tree nodeNto decide what to do: if the next symbol is a constantcthen callparseC, and if that fails then tryparseVfor all variable children, and ifN.reduceis nonempty then returnk(app(N.reduce, args), pos).parseC(q, c, pos, args, k)just callsparsePT(pos, args, k, q[c])parseV(pos, args, k, vs)loops over all the variable children, and tries to parse with each of themEach individual case is fairly straightforward given the data structure; I can explain in more detail if required. This is also a transcription of the algorithm https://github.com/digama0/mm0/blob/master/mm0-hs/src/MM0/FromMM/Parser.hs#L575-L612 , although that one assumes lazy evaluation and so hopefully this one is more suitable to be adapted to your context.
expln commentedon Sep 9, 2023
Thank you so much, Mario!
Thanks, I need some time to understand this and probably then I will ask additional questions.
david-a-wheeler commentedon Sep 9, 2023
@digama0 - as always, thanks so much for your help!!
expln commentedon Sep 15, 2023
@digama0
I understood the algorithm. But I have not implemented it yet because it will take some time. I am wondering how much time could this algorithm take to parse all assertion statements in set.mm (only the resulting statements of axioms and theorems without hypotheses). Do you have any statistics from your implementation of how much time it takes to parse set.mm? I've done some improvements to my current algorithm in mm-lamp, so now it takes about 40 seconds to parse set.mm (previously it was about an hour).
digama0 commentedon Sep 16, 2023
The lower bound of this parsing algorithm is definitely sub-second if done well. I doubt knowing that will be useful to you though.
david-a-wheeler commentedon Dec 28, 2023
Metamath-lamp has gown into a very useful tool. I personally think this issue is now of the most important issues holding it back. While there is always room for better auto.ation, full unification would automate much (compared to the current state) and is 100% general.
david-a-wheeler commentedon Dec 28, 2023
Having this would certainly simplify the tutorial! :-)
expln commentedon Dec 30, 2023
I started working on full unification a few months ago. But it turned out to be much harder than I imagined. I needed to change the parts of the code which I don't want to change (at least for now). So, I postponed this activity. Currently I am implementing automation. My current results in automation are promising, so I want to release it first. After that, I will try to do another attempt to implement full unification. Probably I will use that new automation to implement the full unification.
david-a-wheeler commentedon Nov 2, 2024
I would love to see this. This is extremely general & is itself a form of automation.
I appreciate it's not easy. If there's something I can do to help, let me know.
expln commentedon Nov 3, 2024
I guess I need help in comparing mmj2 behavior with mm-lamp, because I don’t know mmj2 well. I will try to come up with some solution to the problem you started this issue with (when you typed ax-2 and pressed Ctrl-U). When I implement some analogous solution in mm-lamp, I will ask you to come up with another example of what is possible in mmj2 and not possible in mm-lamp.
expln commentedon Jan 2, 2025
Hi David,
I have made some progress here. I implemented the unification for the simple case you described in the first comment in this issue (the case with two MMP files, before and after pressing Ctrl+U). To achieve the same in mm-lamp, do the following:
This is available in dev version currently.
Here is a corresponding demo https://drive.google.com/file/d/14Y6hV97bxytiG60wbdcAn_71m1K9VoQD/view?usp=drive_link
This functionality is implemented using macros. All of this can be achieved manually. Currently, the “Unify” macro finds a step with an error, takes its assertion and puts the assertion’s statements above. Then it uses the substitution to unify new statements with the existing one. It also merges duplicated steps (which appear after the substitution) and renames the new step to have the name of the old step (so it looks like the existing step was modified). If you open the browser’s console, you’ll see the logs of the macros interaction with mm-lamp. You can also see the macros code if you click the small
+button to the right of the MMJ2 dropdown and copy-paste the text from the small text field to any JS editor (or simply text editor). (To copy the text from the small text field, left click the text field, then press Ctrl+A and Ctrl+C)As I understand, your proposal was to make the full unification a “first-class citizen” in mm-lamp. But this requires a lot of changes in mm-lamp for which I am not ready now. However, I can try to write macros to cover mmj2 functionality using existing mm-lamp functionality. Do you have any other scenarios when mmj2 can do more than mm-lamp in terms of a more broad unification? If you can, it would be convenient for me if you send one or a few such cases in the form of MMP pairs, one before and another after pressing Ctrl+U.
david-a-wheeler commentedon Jan 2, 2025
@expln - thanks for working to implement something related to unification!
@digama0 is an expert on what full unification does and means in this context - not me. He'd be the much better person to comment on it. I'm hoping he'll have a chance to say something. Mario: I know you're busy, but this is outside my expertise.
I don't care how unification is implemented. Implementing it via macros is probably unnecessarily slow, but if it works, it's fine by me. A decent implementation should still run quickly. I'd like for it to have an easy invocation though; control+U would be the obvious keymap for it :-). Eventually it should just be something that comes with the tool and doesn't need special enablement before use.
I wouldn't call it "mmj2" or use that term anywhere. The issue is unification, a well-known algorithm. I believe the unification algorithm goes back to 1965. The fact that mmj2 is one of the many many many programs that has a unification algorithm implementation is not really relevant to anything.
expln commentedon Jan 28, 2025
Hi David,
Yes, I agree, I plan to add key shortcuts for macros. I don’t want to hardcode them though. I need some time to think on how to make it flexible.
My intention is to simulate mmj2 behavior when one invokes the “Unify” macro (I am not sure if it is possible using existing mm-lamp functionality though :) ) The “Unify” macro is not the algorithm invented back in 1965. The “Unify” macro should do whatever mmj2 does when a user presses control+U. And this is unique to mmj2. Also, as I know, the MMP format is highly coupled with mmj2 (Yamma also uses it, but if I am not mistaken it was introduced by mmj2). In the future, also an “Export to MMP” macro may be added. Then it will be possible to develop a proof in mmj2 (or Yamma, or whatever other mmj2-like assistants will appear by that time :) ), then temporarily move it to mm-lamp to do some actions which are not supported in mmj2, and after that move the proof back to mmj2 to continue working there. So, I guess it should be fine to use the “mmj2” term in that place.