Skip to content

Commit

Permalink
Switch to oeis2 library and reinstate OEIS support (#427)
Browse files Browse the repository at this point in the history
Closes #367.
  • Loading branch information
byorgey authored Dec 26, 2024
1 parent c7fce10 commit f6714ce
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 33 deletions.
6 changes: 3 additions & 3 deletions disco.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ library
splitmix >= 0.1 && < 0.2,
fgl >= 5.5 && < 5.9,
optparse-applicative >= 0.12 && < 0.19,
-- oeis2 < 1.1,
oeis2 >= 1.0.9 && < 1.1,
algebraic-graphs >= 0.5 && < 0.8,
pretty-show >= 1.10 && < 1.11,
boxes >= 0.1.5 && < 0.2,
Expand All @@ -525,8 +525,8 @@ executable disco
containers >= 0.5 && < 0.8,
unbound-generics >= 0.3 && < 0.5,
lens >= 4.14 && < 5.4,
optparse-applicative >= 0.12 && < 0.19
-- oeis2 < 1.1
optparse-applicative >= 0.12 && < 0.19,
oeis2 >= 1.0.9 && < 1.1

default-language: Haskell2010

Expand Down
1 change: 1 addition & 0 deletions docs/reference/library.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ then go through each of the available standard libraries.
prop-lib
string-lib
prim-lib
oeis-lib
37 changes: 37 additions & 0 deletions docs/reference/oeis-lib.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
oeis
====

Disco provides a library for interfacing with the `Online Encyclopedia
of Integer Sequences <OEIS_>`_ (OEIS). You can :doc:`import
<import>` it with:

.. _OEIS: https://oeis.org/

::

import oeis

The library provides two functions:

* The ``lookupSequence`` function takes a list of natural numbers and
returns the URL of the first result in the OEIS. For example:

::

Disco> lookupSequence [1,1,2,3,5,8]
right("https://oeis.org/A000045")

In this example, the returned URL is in fact the OEIS page for the
`Fibonacci numbers <fib_>`_.

* The ``extendSequence`` function tries to *extend* the given list as
far as it can, using data from the first match on the OEIS. Using
the same example as before, just by putting in the first few
Fibonacci numbers we can get a lot more:

::

Disco> extendSequence [1,1,2,3,5,8]
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986, 102334155]

.. _fib: https://oeis.org/A000045
File renamed without changes.
File renamed without changes.
52 changes: 24 additions & 28 deletions src/Disco/Interpret/CESK.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import qualified Data.List.Infinite as InfList
import qualified Data.Map as M
import Data.Maybe (isJust)
import Data.Ratio
import qualified Data.Text as T
import Disco.AST.Core
import Disco.AST.Generic (
Ellipsis (..),
Expand All @@ -41,6 +42,8 @@ import Disco.AST.Generic (
import Disco.AST.Typed (AProperty)
import Disco.Compile
import Disco.Context as Ctx
import Disco.Effects.Fresh
import Disco.Effects.Input
import Disco.Enumerate
import Disco.Error
import Disco.Names
Expand All @@ -51,15 +54,12 @@ import Math.Combinatorics.Exact.Binomial (choose)
import Math.Combinatorics.Exact.Factorial (factorial)
import Math.NumberTheory.Primes (factorise, unPrime)
import Math.NumberTheory.Primes.Testing (isPrime)

-- import Math.OEIS (
-- catalogNums,
-- extendSequence,
-- lookupSequence,
-- )

import Disco.Effects.Fresh
import Disco.Effects.Input
import Math.OEIS (
SearchStatus (SubSeq),
extendSeq,
lookupSeq,
number,
)
import Polysemy
import Polysemy.Error
import Polysemy.Random
Expand Down Expand Up @@ -356,8 +356,8 @@ appConst k = \case
-- Sequences

OUntil -> arity2 $ \v1 -> out . ellipsis (Until v1)
-- OLookupSeq -> out . oeisLookup
-- OExtendSeq -> out . oeisExtend
OLookupSeq -> out . oeisLookup
OExtendSeq -> out . oeisExtend
--------------------------------------------------
-- Comparison

Expand Down Expand Up @@ -639,23 +639,19 @@ constdiff (x : xs)
-- OEIS
------------------------------------------------------------

-- -- | Looks up a sequence of integers in OEIS.
-- -- Returns 'left()' if the sequence is unknown in OEIS,
-- -- otherwise 'right "https://oeis.org/<oeis_sequence_id>"'
-- oeisLookup :: Value -> Value
-- oeisLookup (vlist vint -> ns) = maybe VNil parseResult (lookupSequence ns)
-- where
-- parseResult r = VInj R (listv charv ("https://oeis.org/" ++ seqNum r))
-- seqNum = getCatalogNum . catalogNums

-- getCatalogNum [] = error "No catalog info"
-- getCatalogNum (n : _) = n

-- -- | Extends a Disco integer list with data from a known OEIS
-- -- sequence. Returns a list of integers upon success, otherwise the
-- -- original list (unmodified).
-- oeisExtend :: Value -> Value
-- oeisExtend = listv intv . extendSequence . vlist vint
-- | Looks up a sequence of integers in OEIS.
-- Returns 'left()' if the sequence is unknown in OEIS,
-- otherwise 'right "https://oeis.org/<oeis_sequence_id>"'
oeisLookup :: Value -> Value
oeisLookup (vlist vint -> ns) = maybe VNil parseResult (lookupSeq (SubSeq ns))
where
parseResult r = VInj R (listv charv ("https://oeis.org/" ++ T.unpack (number r)))

-- | Extends a Disco integer list with data from a known OEIS
-- sequence. Returns a list of integers upon success, otherwise the
-- original list (unmodified).
oeisExtend :: Value -> Value
oeisExtend = listv intv . extendSeq . vlist vint

------------------------------------------------------------
-- Normalizing bags/sets
Expand Down
3 changes: 1 addition & 2 deletions stack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ extra-deps:
- unbound-generics-0.4.4
- ansi-wl-pprint-0.6.9
- simple-enumeration-0.2.1@sha256:8625b269c1650d3dd0e3887351c153049f4369853e0d525219e07480ea004b9f,1178
# - HTTP-4000.3.16@sha256:6042643c15a0b43e522a6693f1e322f05000d519543a84149cb80aeffee34f71,5947
# HTTP-4000.3 needed for oeis-0.3.10
- oeis2-1.0.9

# Override default flag values for local packages and extra-deps
flags: {}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit f6714ce

Please sign in to comment.