-
Notifications
You must be signed in to change notification settings - Fork 0
/
blocks_field_proof.go
183 lines (156 loc) · 4.06 KB
/
blocks_field_proof.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package questions
import (
"errors"
"strings"
"github.com/benoitkugler/maths-online/server/src/maths/expression"
"github.com/benoitkugler/maths-online/server/src/maths/questions/client"
)
type ProofFieldBlock struct {
Answer ProofSequence
}
// ID is only used by answer fields
func (pr ProofFieldBlock) instantiate(params expression.Vars, ID int) (instance, error) {
ins, err := pr.Answer.instantiate(params)
if err != nil {
return nil, err
}
return ProofFieldInstance{
Answer: ins.(proofSequenceIns),
ID: ID,
}, nil
}
func (pr ProofFieldBlock) setupValidator(*expression.RandomParameters) (validator, error) {
err := pr.Answer.validate()
return noOpValidator{}, err
}
// Placeholder used when editing the proof
type ProofInvalid struct{}
type ProofStatement struct {
Content Interpolated
}
type ProofEquality struct {
Terms string // always interpreted as LaTeX, splitted by equals
}
type ProofNode struct {
Left, Right ProofAssertion `gomacro-data:"ignore"`
Op client.Binary
}
type ProofAssertions []ProofAssertion
type ProofSequence struct {
Parts ProofAssertions `gomacro-data:"ignore"`
}
type ProofAssertion interface {
instantiate(params expression.Vars) (proofAssertionIns, error)
validate() error
}
func (ProofInvalid) instantiate(params expression.Vars) (proofAssertionIns, error) {
return nil, errors.New("La preuve ne doit pas contenir d'assertion en construction.")
}
func (ProofInvalid) validate() error {
return errors.New("La preuve ne doit pas contenir d'assertion en construction.")
}
func (v ProofStatement) instantiate(params expression.Vars) (proofAssertionIns, error) {
out, err := v.Content.instantiate(params)
return proofStatementIns(out), err
}
func (v ProofStatement) validate() error {
_, err := v.Content.parse()
return err
}
func ensureLaTeX(s string) string {
s = strings.TrimSpace(s)
if s != "" && !strings.HasPrefix(s, "$") { // ensure LaTeX
s = "$" + s + "$"
}
return s
}
func (v ProofEquality) terms() (members []Interpolated, avecDef Interpolated) {
// start by the optional avec separator
const avecSep = "avec"
chunks := strings.SplitN(v.Terms, avecSep, 2)
if len(chunks) >= 2 {
avecDef = Interpolated(chunks[1])
}
terms := strings.Split(chunks[0], "=")
members = make([]Interpolated, len(terms))
for i, t := range terms {
t = ensureLaTeX(t)
members[i] = Interpolated(t)
}
return members, avecDef
}
func (v ProofEquality) instantiate(params expression.Vars) (proofAssertionIns, error) {
members, avecDef := v.terms()
out := proofEqualityIns{
Terms: make([]client.TextLine, len(members)),
}
var err error
for i, term := range members {
line, err := term.instantiate(params)
if err != nil {
return nil, err
}
out.Terms[i] = line
}
out.Def, err = avecDef.instantiate(params)
if err != nil {
return nil, err
}
out.WithDef = len(out.Def) != 0
return out, nil
}
func (v ProofEquality) validate() error {
members, avecDef := v.terms()
for _, term := range members {
if term == "" {
return errors.New("Les membres d'une égalité ne peuvent pas être vides.")
}
_, err := term.parse()
if err != nil {
return err
}
}
if _, err := avecDef.parse(); err != nil {
return err
}
return nil
}
func (v ProofNode) instantiate(params expression.Vars) (proofAssertionIns, error) {
left, err := v.Left.instantiate(params)
if err != nil {
return nil, err
}
right, err := v.Right.instantiate(params)
if err != nil {
return nil, err
}
return proofNodeIns{Op: v.Op, Left: left, Right: right}, nil
}
func (v ProofNode) validate() error {
if err := v.Left.validate(); err != nil {
return err
}
if err := v.Right.validate(); err != nil {
return err
}
return nil
}
func (v ProofSequence) instantiate(params expression.Vars) (proofAssertionIns, error) {
out := make(proofSequenceIns, len(v.Parts))
var err error
for i, p := range v.Parts {
out[i], err = p.instantiate(params)
if err != nil {
return nil, err
}
}
return out, nil
}
func (v ProofSequence) validate() error {
for _, p := range v.Parts {
if err := p.validate(); err != nil {
return err
}
}
return nil
}