subject-knowledge.hoon 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. =<
  2. :: a memoization for formula analysis, updated/inspected at eval
  3. :: boundaries (2/9)
  4. =| memo=(map * (list [sock (unit sock) (unit @tas)]))
  5. :: A jet dashboard
  6. ::
  7. :: The @tas tag is used only for debugging
  8. =| jute=(map * (list [sock [$-(* *) @tas]]))
  9. |%
  10. :: Partial knowledge of a noun
  11. +$ sock ^sock
  12. ++ sk .
  13. ++ lash
  14. |= [[s=sock f=*] j=$-(* *) n=@tas]
  15. =. jute
  16. ?. (~(has by jute) f)
  17. (~(put by jute) f (limo ~[[s [j n]]]))
  18. (~(jab by jute) f |=(js=(list [sock [$-(* *) @tas]]) (weld ~[[s [j n]]] js)))
  19. sk
  20. ++ entr
  21. |= [s=sock f=* nam=(unit @tas)]
  22. ^- (each _memo [r=(unit sock) n=(unit @tas)])
  23. =/ entry
  24. =/ e (~(get by memo) f) ?~ e ~ u.e
  25. |-
  26. ^- (each _memo [(unit sock) (unit @tas)])
  27. ?~ entry
  28. :- %&
  29. ?: (~(has by memo) f)
  30. (~(jab by memo) f |=(e=(list [sock (unit sock) (unit @tas)]) (weld (limo ~[[s ~ nam]]) e)))
  31. (~(put by memo) f (limo ~[[s ~ nam]]))
  32. ?: (mous -<.entry s)
  33. [%| ->.entry]
  34. $(entry +.entry)
  35. ++ exit
  36. |= [s=sock f=* r=sock]
  37. ^- _memo
  38. ~| "Should not exit where there is no entry"
  39. %:
  40. ~(jab by memo)
  41. f
  42. |= e=(list [sock (unit sock) (unit @tas)])
  43. |-
  44. ^- (list [sock (unit sock) (unit @tas)])
  45. ?< ?= %~ e
  46. ?: =(-<.e s)
  47. [[s `r ->+.e] +.e]
  48. [-.e $(e +.e)]
  49. ==
  50. ++ bord *(map * (list [sock [$-(* *) @tas]]))
  51. :: test whether a sock nests in another sock
  52. :: a=sock nests in b=sock if b has the same information as a, or
  53. :: strictly more information
  54. ::
  55. ++ mous
  56. |= [a=sock b=sock]
  57. ^- ?
  58. ?: ?= [%gues ~] a
  59. %.y
  60. ?- a
  61. ::
  62. [%hint *]
  63. ?&
  64. ?= [%hint *] b
  65. .= s.a s.b
  66. ?~ d.a
  67. ?~ d.b
  68. %.y
  69. %.n
  70. ?~ d.b
  71. %.n
  72. $(a u.d.a, b u.d.b)
  73. $(a r.a, b r.b)
  74. ==
  75. ::
  76. [%know *]
  77. ?|
  78. ?&
  79. ?= [%know *] b
  80. =(k.a k.b)
  81. ==
  82. ?&
  83. ?= [%bets * *] b
  84. ?= [* *] k.a
  85. $(a [%know -.k.a], b p.b)
  86. $(a [%know +.k.a], b q.b)
  87. ==
  88. ==
  89. ::
  90. [%bets * *]
  91. ?|
  92. ?&
  93. ?= [%know * *] b
  94. $(a p.a, b [%know -.k.b])
  95. $(a q.a, b [%know +.k.b])
  96. ==
  97. ?&
  98. ?= [%bets * *] b
  99. $(a p.a, b p.b)
  100. $(a q.a, b q.b)
  101. ==
  102. ==
  103. ==
  104. :: compute what we know of the result of a formula
  105. ::
  106. :: We may reimplement this more efficiently later, but this way we
  107. :: don't have to keep updating our experiments in 2 implementations
  108. ++ wash
  109. |= [s=sock f=*]
  110. ^- [sock _memo]
  111. =+ (pull s f)
  112. [(cort -<) ->]
  113. :: Check for a jet
  114. ++ juke
  115. |= [s=sock f=*]
  116. ^- (unit [$-(* *) @tas])
  117. =/ jets
  118. =/ j (~(get by jute) f)
  119. ^- (list [sock [$-(* *) @tas]])
  120. ?~ j ~ u.j
  121. |-
  122. ^- (unit [$-(* *) @tas])
  123. ?~ jets
  124. ~
  125. ?: (mous -<.jets s)
  126. `->.jets
  127. $(jets +.jets)
  128. :: learn a noun at an address
  129. ::
  130. :: darn discards hints if it must descend through them
  131. ::
  132. :: TODO: we use +knit everywhere, this is just +knit with [%know *], can
  133. :: we get rid of it?
  134. ++ darn
  135. |= [s=sock b=@ n=*]
  136. ^- sock
  137. ?: ?= [%hint *] s
  138. $(s r.s)
  139. ?: =(b 1)
  140. [%know n]
  141. ?: ?= [%know *] s
  142. :- %know
  143. .* [k.s n] [%10 [b [0 3]] [0 2]]
  144. ?- (cap b)
  145. ::
  146. %2
  147. ?- s
  148. ::
  149. [%gues ~]
  150. [%bets p=$(b (mas b), s [%gues ~]) q=[%gues ~]]
  151. ::
  152. [%bets p=* q=*]
  153. [%bets p=$(b (mas b), s p.s) q=q.s]
  154. ==
  155. ::
  156. %3
  157. ?- s
  158. ::
  159. [%gues ~]
  160. [%bets p=[%gues ~] q=$(b (mas b), s [%gues ~])]
  161. ::
  162. [%bets p=* q=*]
  163. [%bets p=p.s q=$(b (mas b), s q.s)]
  164. ==
  165. ==
  166. :: axis into a sock
  167. ::
  168. :: yarn will return a hint record if it is at an axis
  169. :: but will discard them as it descends through them
  170. ++ yarn
  171. |= [s=sock b=@]
  172. ^- sock
  173. ?< =(b 0)
  174. |-
  175. ?: =(b 1)
  176. s
  177. ?: ?= [%hint *] s
  178. $(s r.s) :: descend through hint records
  179. ?- s
  180. ::
  181. [%know *]
  182. :- %know
  183. .* k.s [0 b]
  184. ::
  185. [%gues ~]
  186. [%gues ~]
  187. ::
  188. [%bets p=* q=*]
  189. ?- (cap b)
  190. ::
  191. %2
  192. $(b (mas b), s p.s)
  193. ::
  194. %3
  195. $(b (mas b), s q.s)
  196. ::
  197. ==
  198. ==
  199. :: axis into a sock, retrieving a name from a fast hint
  200. ++ narn
  201. |= [s=sock b=@]
  202. ^- [sock (unit @tas)]
  203. ?< =(b 0)
  204. =| nam=(unit @tas)
  205. |-
  206. ^- [sock (unit @tas)]
  207. ?: =(b 1)
  208. ?. ?= [%hint *] s [s nam]
  209. ?. =(s.s %fast) [s nam]
  210. ?~ d.s [s nam]
  211. =/ namsock (yarn u.d.s 2)
  212. ?. ?= [%know @] namsock [s nam]
  213. [s `k.namsock]
  214. ?: ?= [%hint *] s
  215. ?. =(s.s %fast) $(s r.s)
  216. ?~ d.s $(s r.s)
  217. =/ namsock (yarn u.d.s 2)
  218. ?. ?= [%know @] namsock $(s r.s)
  219. =. nam `k.namsock
  220. $(s r.s)
  221. ?- s
  222. ::
  223. [%know *]
  224. :-
  225. :- %know
  226. .* k.s [%0 b]
  227. nam
  228. ::
  229. [%gues ~]
  230. [[%gues ~] nam]
  231. ::
  232. [%bets * *]
  233. ?- (cap b)
  234. ::
  235. %2
  236. $(b (mas b), s p.s)
  237. ::
  238. %3
  239. $(b (mas b), s q.s)
  240. ==
  241. ==
  242. :: make a new sock from 2 socks (and an axis)
  243. ++ knit
  244. |= [s=sock b=@ t=sock]
  245. ^- sock
  246. ?: =(b 1)
  247. t
  248. ?: ?= [%hint *] s
  249. [%hint s.s d.s $(s r.s)]
  250. ?- s
  251. ::
  252. [%know @]
  253. ~| %know-atom !!
  254. ::
  255. [%know * *]
  256. ?- (cap b)
  257. ::
  258. %2
  259. =/ r $(s [%know +<.s], b (mas b))
  260. ?: ?= [%know k=*] r
  261. [%know +.r +>.s]
  262. [%bets r [%know +.k.s]]
  263. ::
  264. %3
  265. =/ r $(s [%know +>.s], b (mas b))
  266. ?: ?= [%know k=*] r
  267. [%know +<.s +.r]
  268. [%bets [%know +<.s] r]
  269. ==
  270. ::
  271. [%bets p=* q=*]
  272. ?- (cap b)
  273. ::
  274. %2
  275. =/ r $(s p.s, b (mas b))
  276. ?: ?= [%know k=*] r
  277. ?: ?= [%know k=*] q.s
  278. [%know +.r +.q.s]
  279. [%bets r q.s]
  280. [%bets r q.s]
  281. ::
  282. %3
  283. =/ r $(s q.s, b (mas b))
  284. ?: ?= [%know k=*] r
  285. ?: ?= [%know k=*] p.s
  286. [%know +.p.s +.r]
  287. [%bets p.s r]
  288. [%bets p.s r]
  289. ==
  290. ::
  291. [%gues ~]
  292. =/ r $(b (mas b))
  293. ?: ?= [%gues ~] r
  294. [%gues ~]
  295. ?- (cap b)
  296. ::
  297. %2
  298. [%bets r [%gues ~]]
  299. ::
  300. %3
  301. [%bets [%gues ~] r]
  302. ==
  303. ==
  304. ++ pear
  305. |= [a=sock b=sock]
  306. ^- sock
  307. ?: ?= [%know *] a
  308. ?: ?= [%know *] b
  309. [%know =(a b)]
  310. [%gues ~]
  311. [%gues ~]
  312. :: Description of knowledge at a call site
  313. ::
  314. :: Codegen behavior
  315. :: %dyn means we will generate a full eval: check cache or codegen,
  316. :: guard on stored sock
  317. ::
  318. :: %mis means we will do fresh inline codegen of a formula with a new
  319. :: label
  320. ::
  321. :: %rec and %hit both mean we will generate to jumps to labels
  322. :: They differ because for %rec the analysis must treat the call as
  323. :: unknowable, while for %hit we do not re-analyze the call but return
  324. :: the memoized sock
  325. ::
  326. :: %jet corresponds to a jump to a jet
  327. +$ coot
  328. $%
  329. [%dyn =sock] :: we don't know the formula
  330. [%mis =foot] :: we know the formula, it's not memoized
  331. [%rec =sock f=*] :: a recursive call, the memo table has a blackhole for this sock/formula pair
  332. [%hit res=sock] :: a memoized call, the memo table has an entry for this sock/formula pair
  333. [%jet jet=@tas] :: call would be jetted
  334. ==
  335. :: Annotated Nock tree with subject knowledge
  336. +$ foot
  337. $:
  338. $%
  339. [%1 b=*]
  340. [%2 b=foot c=foot =coot]
  341. [%3 b=foot]
  342. [%4 b=foot]
  343. [%5 b=foot c=foot]
  344. [%6 b=foot c=(unit foot) d=(unit foot)]
  345. [%7 b=foot c=foot]
  346. [%8 b=foot c=foot]
  347. [%9 b=@ c=foot =coot]
  348. [%10 [b=@ c=foot] d=foot]
  349. [%11 b=@ c=foot]
  350. [%11 [b=@ c=foot] d=foot]
  351. [%12 ref=foot path=foot]
  352. [%cell p=foot q=foot]
  353. [%0 b=@]
  354. ==
  355. $= s sock
  356. $= r sock
  357. ==
  358. ++ cort
  359. |= =coot
  360. ^- sock
  361. ?- coot
  362. ::
  363. [%dyn *]
  364. [%gues ~]
  365. ::
  366. [%mis *]
  367. r.foot.coot
  368. ::
  369. [%rec *]
  370. [%gues ~]
  371. ::
  372. [%hit *]
  373. res.coot
  374. ::
  375. [%jet *]
  376. [%gues ~]
  377. ==
  378. ++ pull
  379. |= [s=sock f=*]
  380. (pull-eval s [%know f] ~)
  381. ++ pull-eval
  382. |= [s=sock f=sock nam=(unit @tas)]
  383. ^- [coot _memo]
  384. ?: ?= [%hint *] f $(f r.f)
  385. ?. ?= [%know *] f
  386. [[%dyn s] memo]
  387. =/ jet (juke s k.f)
  388. ?. ?= ~ jet
  389. :: found a jet
  390. ~& "Jet: {<+.u.jet>}"
  391. [[%jet +.u.jet] memo]
  392. =/ mem=(each _memo [r=(unit sock) n=(unit @tas)]) (entr s k.f nam)
  393. ?- mem
  394. ::
  395. [%& *]
  396. =. memo +.mem
  397. =^ res memo (pull-inner s k.f)
  398. =. memo (exit s k.f r.res)
  399. [[%mis res] memo]
  400. ::
  401. [%| *]
  402. ?~ r.p.mem
  403. :: memo blackhole
  404. ~& "Recur: sock {<[s]>} formula {<k.f>}"
  405. [[%rec s k.f] memo]
  406. :: memo hit
  407. ~& "Hit: sock {<[s]>} formula {<k.f>} result {<u.r.p.mem>} named {<n.p.mem>}"
  408. [[%hit u.r.p.mem] memo]
  409. ==
  410. ++ pull-inner
  411. |= [s=sock f=*]
  412. ^- [foot _memo]
  413. ?+ f ~| "Unrecognized nock {<f>}" ~| %pull-bonk !!
  414. ::
  415. [[* *] *]
  416. =^ pfoot memo $(f -.f)
  417. =^ qfoot memo $(f +.f)
  418. [[[%cell pfoot qfoot] s=s r=(knit [%bets r.pfoot [%gues ~]] 3 r.qfoot)] memo]
  419. ::
  420. [%0 b=@]
  421. [[[%0 b.f] s=s r=(yarn s b.f)] memo]
  422. ::
  423. [%1 b=*]
  424. [[[%1 b.f] s=s r=[%know b.f]] memo]
  425. ::
  426. [%2 b=* c=*]
  427. =^ bfoot memo $(f b.f)
  428. =^ cfoot memo $(f c.f)
  429. =/ [r=sock nam=(unit @tas)] (narn r.cfoot 1)
  430. =^ coot memo (pull-eval r.bfoot r nam)
  431. [[[%2 bfoot cfoot coot] s (cort coot)] memo]
  432. ::
  433. [%3 b=*]
  434. =^ bfoot memo $(f b.f)
  435. =/ br r.bfoot
  436. =/ r
  437. |-
  438. ?- br
  439. ::
  440. [%hint *]
  441. $(br r.br)
  442. ::
  443. [%know @]
  444. [%know 1]
  445. ::
  446. [%know * *]
  447. [%know 0]
  448. ::
  449. [%bets * *]
  450. [%know 0]
  451. ::
  452. [%gues ~]
  453. [%gues ~]
  454. ==
  455. [[[%3 bfoot] s r] memo]
  456. ::
  457. [%4 b=*]
  458. =^ bfoot memo $(f b.f)
  459. [[[%4 bfoot] s [%gues ~]] memo]
  460. ::
  461. [%5 b=* c=*]
  462. =^ bfoot memo $(f b.f)
  463. =^ cfoot memo $(f c.f)
  464. =/ br r.bfoot
  465. =/ cr r.cfoot
  466. =/ r
  467. |-
  468. ?: ?= [%hint *] br
  469. $(br r.br)
  470. ?: ?= [%hint *] cr
  471. $(cr r.cr)
  472. ?: ?= [%know *] r.bfoot
  473. ?: ?= [%know *] r.cfoot
  474. [%know =(k.r.bfoot k.r.cfoot)]
  475. [%gues ~]
  476. [%gues ~]
  477. [[[%5 bfoot cfoot] s r] memo]
  478. ::
  479. [%6 b=* c=* d=*]
  480. =^ bfoot memo $(f b.f)
  481. =/ br r.bfoot
  482. |-
  483. ?+ br ~| %pull-nest !!
  484. ::
  485. [%hint *]
  486. $(br r.br)
  487. ::
  488. [%know %0]
  489. =^ cfoot memo ^$(f c.f)
  490. [[[%6 bfoot `cfoot ~] s r.cfoot] memo]
  491. ::
  492. [%know %1]
  493. =^ dfoot memo ^$(f d.f)
  494. [[[%6 bfoot ~ `dfoot] s r.dfoot] memo]
  495. ::
  496. [%gues ~]
  497. =^ cfoot memo ^$(f c.f)
  498. =^ dfoot memo ^$(f d.f)
  499. [[[%6 bfoot `cfoot `dfoot] s [%gues ~]] memo]
  500. ==
  501. ::
  502. [%7 b=* c=*]
  503. =^ bfoot memo $(f b.f)
  504. =^ cfoot memo $(s r.bfoot, f c.f)
  505. [[[%7 bfoot cfoot] s r.cfoot] memo]
  506. ::
  507. [%8 b=* c=*]
  508. =^ bfoot memo $(f b.f)
  509. =^ cfoot memo $(s (knit [%bets [%gues ~] s] 2 r.bfoot), f c.f)
  510. [[[%8 bfoot cfoot] s=s r=r.cfoot] memo]
  511. ::
  512. [%9 b=@ c=*]
  513. =^ cfoot memo $(f c.f)
  514. =/ [r=sock nam=(unit @tas)] (narn r.cfoot b.f)
  515. =^ coot memo (pull-eval r.cfoot r nam)
  516. [[[%9 b.f cfoot coot] s (cort coot)] memo]
  517. ::
  518. [%10 [b=@ c=*] d=*]
  519. =^ cfoot memo $(f c.f)
  520. =^ dfoot memo $(f d.f)
  521. [[[%10 [b.f cfoot] dfoot] s (knit r.dfoot b.f [%gues ~])] memo]
  522. ::
  523. [%11 b=@ c=*]
  524. =^ cfoot memo $(f c.f)
  525. [[[%11 b.f cfoot] s r=[%hint b.f ~ r.cfoot]] memo]
  526. ::
  527. [%11 [b=@ c=*] d=*]
  528. =^ cfoot memo $(f c.f)
  529. :: implement the %data hint
  530. :: delete the axes which are looked up in the hint from the subject
  531. :: knowledge for the hinted computation
  532. ?: =(b.f %data)
  533. =/ c c.f
  534. |-
  535. ?: ?= [[%0 @] *] c
  536. =. s (knit s ->.c [%gues ~])
  537. $(c +.c)
  538. =^ dfoot memo ^$(f d.f)
  539. [[[%11 [b.f cfoot] dfoot] s [%hint b.f `r.cfoot r.dfoot]] memo]
  540. =^ dfoot memo $(f d.f)
  541. [[[%11 [b.f cfoot] dfoot] s [%hint b.f `r.cfoot r.dfoot]] memo]
  542. ::
  543. [%12 ref=* path=*]
  544. =^ reffoot memo $(f ref.f)
  545. =^ pathfoot memo $(f path.f)
  546. [[[%12 reffoot pathfoot] s [%gues ~]] memo]
  547. ==
  548. ++ hord
  549. |= [nam=@tas]
  550. ^- (list [s=sock f=*])
  551. =| r=(list [s=sock f=*])
  552. =/ entries ~(tap by memo)
  553. |-
  554. ^- (list [s=sock f=*])
  555. ?~ entries
  556. r
  557. =/ f p.i.entries
  558. =/ entry q.i.entries
  559. |-
  560. ^- (list [s=sock f=*])
  561. ?~ entry
  562. ^$(entries t.entries)
  563. =? r =(+>.i.entry `nam) (weld (limo ~[[-.i.entry f]]) r)
  564. $(entry t.entry)
  565. :: example nocks for testing
  566. ++ nocs
  567. |%
  568. ++ dec
  569. !=
  570. !.
  571. =>
  572. => %ed
  573. |%
  574. ++ dec
  575. ~/ %dec
  576. |= x=@
  577. ~> %data.[x ~]
  578. ^- @
  579. =| d=@
  580. |-
  581. ^- d=@
  582. ?: =(.+(d) x)
  583. d
  584. $(d .+(d))
  585. --
  586. (dec 8)
  587. ++ ad
  588. !=
  589. !.
  590. =>
  591. => %ed
  592. |%
  593. ++ dec
  594. ~/ %dec
  595. |= x=@
  596. ~> %data.[x ~]
  597. ^- @
  598. =| d=@
  599. |-
  600. ^- d=@
  601. ?: =(.+(d) x)
  602. d
  603. $(d .+(d))
  604. ++ add
  605. ~/ %add
  606. |= [x=@ y=@]
  607. ~> %data.[x y ~]
  608. ^- @
  609. |-
  610. ^- @
  611. ?: =(x 0)
  612. y
  613. $(x (dec x), y .+(y))
  614. --
  615. (add 5 8)
  616. --
  617. --
  618. |%
  619. +$ sock
  620. $%
  621. [%know k=*]
  622. [%hint s=@ d=(unit sock) r=sock]
  623. [%bets p=sock q=sock]
  624. [%gues ~]
  625. ==
  626. --