Debian Codegraph @

reasoning/law-of-cosines/law-of-cosines.n3

raw
1 @prefix : <http://example.org/cosine#> .
2 @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
3 @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
4 @prefix math: <http://www.w3.org/2000/10/swap/math#> .
5 @prefix log: <http://www.w3.org/2000/10/swap/log#> .
6 @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
7
8 ############################################################
9 # P3: Prompt → Program → Proof (Answer, Reason, Check)
10 # Law of Cosines (radians):
11 # a^2 = b^2 + c^2 - 2 b c cos(A)
12 # b^2 = c^2 + a^2 - 2 c a cos(B)
13 # c^2 = a^2 + b^2 - 2 a b cos(C)
14 ############################################################
15
16 # --- Vocabulary ---
17 :Triangle a rdfs:Class .
18 :Side a rdfs:Class .
19 :Check a rdfs:Class .
20
21 # Triangle data
22 :a a rdf:Property .
23 :b a rdf:Property .
24 :c a rdf:Property .
25 :A a rdf:Property . # radians
26 :B a rdf:Property . # radians
27 :C a rdf:Property . # radians
28 :answer a rdf:Property .
29 :why a rdf:Property .
30 :check a rdf:Property .
31 :status a rdf:Property .
32 :which a rdf:Property .
33 :value a rdf:Property .
34
35 # Reason data
36 :a2 a rdf:Property .
37 :b2 a rdf:Property .
38 :c2 a rdf:Property .
39 :cosAngle a rdf:Property .
40 :rhs a rdf:Property . # the computed x^2 term
41
42 # Check data
43 :kind a rdf:Property .
44 :ok a rdf:Property .
45 :residual a rdf:Property .
46
47 # Tolerance
48 :val a rdf:Property .
49 :eps :val 0.000000001 . # 1e-9
50
51 ############################################################
52 # Solve side c from a, b, C
53 ############################################################
54 {
55 ?t a :Triangle; :a ?a; :b ?b; :C ?C .
56 (?a 2) math:exponentiation ?a2 .
57 (?b 2) math:exponentiation ?b2 .
58 ?C math:cos ?cosC .
59
60 (?a2 ?b2) math:sum ?sumab .
61 (2 ?a) math:product ?tw_a .
62 (?tw_a ?b) math:product ?tw_ab .
63 (?tw_ab ?cosC) math:product ?twabcos .
64
65 (?sumab ?twabcos) math:difference ?c2 .
66 ?c2 math:notLessThan 0 .
67 (?c2 0.5) math:exponentiation ?c .
68 }
69 =>
70 {
71 ?t :answer [ a :Side; :which "c"; :value ?c ] ;
72 :why [ :a2 ?a2; :b2 ?b2; :cosAngle ?cosC; :rhs ?c2 ] ;
73 :status "solve-c" .
74 } .
75
76 ############################################################
77 # Solve side a from b, c, A
78 ############################################################
79 {
80 ?t a :Triangle; :b ?b; :c ?c; :A ?A .
81 (?b 2) math:exponentiation ?b2 .
82 (?c 2) math:exponentiation ?c2 .
83 ?A math:cos ?cosA .
84
85 (?b2 ?c2) math:sum ?s .
86 (2 ?b) math:product ?tw_b .
87 (?tw_b ?c) math:product ?tw_bc .
88 (?tw_bc ?cosA) math:product ?twbccos .
89
90 (?s ?twbccos) math:difference ?a2 .
91 ?a2 math:notLessThan 0 .
92 (?a2 0.5) math:exponentiation ?a .
93 }
94 =>
95 {
96 ?t :answer [ a :Side; :which "a"; :value ?a ] ;
97 :why [ :b2 ?b2; :c2 ?c2; :cosAngle ?cosA; :rhs ?a2 ] ;
98 :status "solve-a" .
99 } .
100
101 ############################################################
102 # Solve side b from c, a, B
103 ############################################################
104 {
105 ?t a :Triangle; :c ?c; :a ?a; :B ?B .
106 (?c 2) math:exponentiation ?c2 .
107 (?a 2) math:exponentiation ?a2 .
108 ?B math:cos ?cosB .
109
110 (?c2 ?a2) math:sum ?s .
111 (2 ?c) math:product ?tw_c .
112 (?tw_c ?a) math:product ?tw_ca .
113 (?tw_ca ?cosB) math:product ?twcacos .
114
115 (?s ?twcacos) math:difference ?b2 .
116 ?b2 math:notLessThan 0 .
117 (?b2 0.5) math:exponentiation ?b .
118 }
119 =>
120 {
121 ?t :answer [ a :Side; :which "b"; :value ?b ] ;
122 :why [ :c2 ?c2; :a2 ?a2; :cosAngle ?cosB; :rhs ?b2 ] ;
123 :status "solve-b" .
124 } .
125
126 ############################################################
127 # Checks — plug back the Law of Cosines for each solved side
128 ############################################################
129
130 # Check for side c answers
131 {
132 ?t a :Triangle; :a ?a; :b ?b; :C ?C; :answer ?ans .
133 ?ans a :Side; :which "c"; :value ?c .
134
135 (?a 2) math:exponentiation ?a2 .
136 (?b 2) math:exponentiation ?b2 .
137 (?c 2) math:exponentiation ?c2 .
138 ?C math:cos ?cosC .
139
140 (?a2 ?b2) math:sum ?lhs0 .
141 (2 ?a) math:product ?tw_a .
142 (?tw_a ?b) math:product ?tw_ab .
143 (?tw_ab ?cosC) math:product ?twabcos .
144
145 (?lhs0 ?twabcos) math:difference ?lhs .
146 (?lhs ?c2) math:difference ?d .
147 ?d math:absoluteValue ?abs .
148 :eps :val ?eps .
149 ?abs math:notGreaterThan ?eps .
150 }
151 =>
152 {
153 ?t :check [ a :Check; :kind "plug-in/c"; :residual ?abs; :ok true ] .
154 } .
155
156 # Check for side a answers
157 {
158 ?t a :Triangle; :b ?b; :c ?c; :A ?A; :answer ?ans .
159 ?ans a :Side; :which "a"; :value ?a .
160
161 (?b 2) math:exponentiation ?b2 .
162 (?c 2) math:exponentiation ?c2 .
163 (?a 2) math:exponentiation ?a2 .
164 ?A math:cos ?cosA .
165
166 (?b2 ?c2) math:sum ?lhs0 .
167 (2 ?b) math:product ?tw_b .
168 (?tw_b ?c) math:product ?tw_bc .
169 (?tw_bc ?cosA) math:product ?twbccos .
170
171 (?lhs0 ?twbccos) math:difference ?lhs .
172 (?lhs ?a2) math:difference ?d .
173 ?d math:absoluteValue ?abs .
174 :eps :val ?eps .
175 ?abs math:notGreaterThan ?eps .
176 }
177 =>
178 {
179 ?t :check [ a :Check; :kind "plug-in/a"; :residual ?abs; :ok true ] .
180 } .
181
182 # Check for side b answers
183 {
184 ?t a :Triangle; :c ?c; :a ?a; :B ?B; :answer ?ans .
185 ?ans a :Side; :which "b"; :value ?b .
186
187 (?c 2) math:exponentiation ?c2 .
188 (?a 2) math:exponentiation ?a2 .
189 (?b 2) math:exponentiation ?b2 .
190 ?B math:cos ?cosB .
191
192 (?c2 ?a2) math:sum ?lhs0 .
193 (2 ?c) math:product ?tw_c .
194 (?tw_c ?a) math:product ?tw_ca .
195 (?tw_ca ?cosB) math:product ?twcacos .
196
197 (?lhs0 ?twcacos) math:difference ?lhs .
198 (?lhs ?b2) math:difference ?d .
199 ?d math:absoluteValue ?abs .
200 :eps :val ?eps .
201 ?abs math:notGreaterThan ?eps .
202 }
203 =>
204 {
205 ?t :check [ a :Check; :kind "plug-in/b"; :residual ?abs; :ok true ] .
206 } .
207
208 ############################################################
209 # ----------------------- HARNESS -------------------------
210 # Angles are in radians.
211 ############################################################
212
213 # H1: Right triangle (3,4,?) with C = pi/2 → c = 5
214 :T1 a :Triangle; :a 3; :b 4; :C 1.5707963267948966 .
215
216 # H2: a=5, b=7, C = pi/3 → c = sqrt(39) ≈ 6.244997998
217 :T2 a :Triangle; :a 5; :b 7; :C 1.0471975511965976 .
218
219 # H3: Solve a from b=5, c=6, A = pi/3 → a = sqrt(31)
220 :T3 a :Triangle; :b 5; :c 6; :A 1.0471975511965976 .
221
222 # H4: Solve b from a=2, c=3, B = pi/6
223 :T4 a :Triangle; :a 2; :c 3; :B 0.5235987755982988 .
224
225 # H5: a=8, b=15, C = 75° = 1.3089969389957472 → c ≈ 15.06438
226 :T5 a :Triangle; :a 8; :b 15; :C 1.3089969389957472 .
227
228 # H6: Right angle at A, b=5, c=12 → a=13
229 :T6 a :Triangle; :b 5; :c 12; :A 1.5707963267948966 .
230