login  home  contents  what's new  discussion  bug reports     help  links  subscribe  changes  refresh  edit

On Tue, Oct 27, 2009 at 4:19 PM, Franz Lehner wrote:

  ok here is a minimal example how to get remembering function.
  I am however not sure if hash tables are actually used.

This example sub-classes Table to create a domain whose values are cached functions.

fricas
(1) -> <spad>
fricas
)abbrev domain FWC FunctionWithCache
FunctionWithCache(A:SetCategory,F:Partition->A):Exports == Implementation where
    Exports ==> TableAggregate(SExpression,A) with 
        apply:(%,Partition)-> A
        ++ \spad{f(p)} computes the value of the function f
        ++ remembering its values
    Implementation ==> Table(SExpression,A) add 
        Rep := Table(SExpression,A)
rep(x:%):Rep == x :: Rep per(r:Rep):% == r :: %
part2expr:Partition -> SExpression -- should go to Partition
part2sexpr(p:Partition):SExpression == pl:List Integer := coerce(p) convert( [convert(k)@SExpression for k in pl])@SExpression
apply(f:%,p:Partition):A == repf:Rep := rep f import Rep ps:SExpression := part2sexpr(p) res:= search(ps,rep f)$Rep res case "failed" => output("F called")$OutputPackage res1:A :=F(p) -- repf(ps) := res1 -- method 1 HPUT(rep f,ps,res1)$Lisp -- method 2 (faster?) res1 res pretend A</spad>
fricas
Compiling FriCAS source code from file 
      /var/lib/zope2.10/instance/axiom-wiki/var/LatexWiki/7804888174701501931-25px001.spad
      using old system compiler.
   FWC abbreviates domain FunctionWithCache 
------------------------------------------------------------------------
   initializing NRLIB FWC for FunctionWithCache 
   compiling into NRLIB FWC 
   compiling local rep : % -> Rep
      FWC;rep is replaced by x 
Time: 0 SEC.
compiling local per : Rep -> % FWC;per is replaced by r Time: 0 SEC.
compiling local part2sexpr : Partition -> SExpression Time: 0 SEC.
compiling exported apply : (%,Partition) -> A Time: 0 SEC.
****** Domain: (Record (: key (SExpression)) (: entry A)) already in scope augmenting (Record (: key (SExpression)) (: entry A)): (Evalable (Record (: key (SExpression)) (: entry A))) ****** Domain: % already in scope augmenting %: (finiteAggregate) ****** Domain: % already in scope augmenting %: (finiteAggregate) ****** Domain: (Record (: key (SExpression)) (: entry A)) already in scope augmenting (Record (: key (SExpression)) (: entry A)): (OrderedSet) ****** Domain: % already in scope augmenting %: (finiteAggregate) ****** Domain: % already in scope augmenting %: (finiteAggregate) ****** Domain: A already in scope augmenting A: (OrderedSet) ****** Domain: A already in scope augmenting A: (Evalable A) ****** Domain: (Record (: key (SExpression)) (: entry A)) already in scope augmenting (Record (: key (SExpression)) (: entry A)): (ConvertibleTo (InputForm)) ****** Domain: (SExpression) already in scope augmenting (SExpression): (OrderedSet) ****** Domain: % already in scope augmenting %: (finiteAggregate) (time taken in buildFunctor: 4336)
;;; *** |FunctionWithCache| REDEFINED
;;; *** |FunctionWithCache| REDEFINED Time: 0.01 SEC.
Warnings: [1] apply: pretendA -- should replace by @
Cumulative Statistics for Constructor FunctionWithCache Time: 0.03 seconds
--------------non extending category---------------------- .. FunctionWithCache(#1,#2) of cat (|Join| (|TableAggregate| (|SExpression|) |#1|) (CATEGORY |domain| (SIGNATURE |apply| (|#1| % (|Partition|))))) has no (|finiteAggregate|) finalizing NRLIB FWC Processing FunctionWithCache for Browser database: --->-->FunctionWithCache(constructor): Not documented!!!! --------(apply (A % (Partition)))--------- --->-->FunctionWithCache(): Missing Description ; compiling file "/var/aw/var/LatexWiki/FWC.NRLIB/FWC.lsp" (written 01 NOV 2024 12:49:18 AM):
; wrote /var/aw/var/LatexWiki/FWC.NRLIB/FWC.fasl ; compilation finished in 0:00:00.020 ------------------------------------------------------------------------ FunctionWithCache is now explicitly exposed in frame initial FunctionWithCache will be automatically loaded when needed from /var/aw/var/LatexWiki/FWC.NRLIB/FWC

How to use:

fricas
f:FunctionWithCache(Integer,pdct):=empty()

\label{eq1} \mbox{\rm table} \left({}\right)(1)
Type: FunctionWithCache?(Integer,theMap(PRTITION;pdct;%I;18,655))
fricas
p:Partition := partition [1,1]

\label{eq2}\left({1}^{2}\right)(2)
Type: Partition
fricas
f(p)
F called

\label{eq3}2(3)
Type: PositiveInteger?
fricas
f(p)

\label{eq4}2(4)
Type: PositiveInteger?




  Subject:   Be Bold !!
  ( 15 subscribers )  
Please rate this page: