$SPAD/src/input cachedf.input

Ralf Hemmecke December 3, 2016

Abstract

Contents

2

— cachedf.spad —

)abbrev domain CACHEDF CachedFunction
++ Author: Ralf Hemmecke
++ Date Created: 2009
++ Date Last Updated: Oct 27, 2009
++ Related Domains: Table, AssociationList
++ Description:
++ This domain is a domain of functions with associated cache
CachedFunction(A: SetCategory, B:SetCategory): Exports == Implementation where
  Exports ==> with
    function: % -> (A -> B)
    cachedFunction: (A -> B) -> %
    apply: (%,A) -> B
    recursiveDefine: (%,f:A -> B) -> %
  Implementation ==> add
    Rep := Record(cache: Table(A,B), fun: A -> B)
    function(x:%):(A->B) == x.fun
    cachedFunction(f:A->B):% == [empty()$Table(A,B),f]
    apply(x:%,a:A):B ==
      c := x.cache
      u: Union(B,"failed") := search(a,c)
      if u case B
        then u::B
        else
          f:A->B := x.fun
          c.a := f(a)
    recursiveDefine(x:%,f:A->B):% ==
      x.fun := f
      x

———-

— * —

)set break resume
)sys rm -f cachedf.output
)spool cachedf.output
)set message test on
)set message auto off
)clear all
)sys cp $AXIOM/../../src/input/cachedf.input.pamphlet .
)lisp (tangle "cachedf.input.pamphlet" "cachedf.spad" "cachedf.spad")
)set message time on
--S 1 of 8

)co cachedf
--R
--R   Compiling AXIOM source code from file
--I      /research/test/int/input/cachedf.spad using old system compiler.
--R   CACHEDF abbreviates domain CachedFunction
--R   processing macro definition Exports ==> -- the constructor category
--R   processing macro definition Implementation ==> -- the constructor capsule
--R------------------------------------------------------------------------
--R   initializing nrlib CACHEDF for CachedFunction
--R   compiling into nrlib CACHEDF
--R   compiling exported function : $ -> A -> B
--R      CACHEDF;function;$M;1 is replaced by QCDR
--ITime: 0.01 SEC.
--R
--R   compiling exported cachedFunction : A -> B -> $
--ITime: 0.01 SEC.
--R
--R   compiling exported apply : ($,A) -> B
--ITime: 0 SEC.
--R
--R   compiling exported recursiveDefine : ($,A -> B) -> $
--ITime: 0 SEC.
--R
--I(time taken in buildFunctor:  0 . NIL)
--R
--R;;;     ***       |CachedFunction| REDEFINED
--R
--R;;;     ***       |CachedFunction| REDEFINED
--ITime: 0 SEC.
--R
--R
--R   Cumulative Statistics for Constructor CachedFunction
--I      Time: 0.02 seconds
--R
--R   finalizing nrlib CACHEDF
--R   Processing CachedFunction for Browser database:
--R--->-->CachedFunction((function ((Mapping B A) %))): Not documented!!!!
--R--->-->CachedFunction((cachedFunction (% (Mapping B A)))): Not documented!!!!
--R--->-->CachedFunction((apply (B % A))): Not documented!!!!
--R--->-->CachedFunction((recursiveDefine (% % (Mapping B A)))): Not documented!!!!
--R--------constructor---------
--R------------------------------------------------------------------------
--I   CachedFunction is now explicitly exposed in frame frame0
--R   CachedFunction will be automatically loaded when needed from
--I      /research/test/int/input/CACHEDF.nrlib/code
--R
--E 1
--S 2 of 8
I := Integer

--R
--R
--R   (1)  Integer
--R                                                                 Type: Domain
--I                                                                  Time: 0 sec
--E 2
--S 3 of 8
f(n:I):I == 1
--R
--R   Function declaration f : Integer -> Integer has been added to
--R      workspace.
--R                                                                   Type: Void
--I                                                                  Time: 0 sec
--E 3
--S 4 of 8
fib: CachedFunction(I,I) := cachedFunction(f)
--R
--R   Compiling function f with type Integer -> Integer
--R
--R LISP output:
--I(#<hash-table 092ec78c> *1;f;1;frame0)
--R                                        Type: CachedFunction(Integer,Integer)
--I                                                                  Time: 0 sec
--E 4
--S 5 of 8
recursiveDefine(fib,(n:I):I +-> if n<2 then 1 else fib(n-1)+fib(n-2))
--R
--R
--R LISP output:
--I(#<hash-table 092ec78c> (LAMBDA-CLOSURE  ...
--R                                        Type: CachedFunction(Integer,Integer)
--I                                       Time: 0.02 (IN) + 0.01 (GC) = 0.03 sec
--E 5
--S 6 of 8
fib 40
--R
--R
--R   (5)  165580141
--R                                                        Type: PositiveInteger
--I                                                   Time: 0.01 (IN) = 0.01 sec
--E 6
--S 7 of 8
g(n:I):I == if n<2 then 1 else g(n-1)+g(n-2)
--R
--R   Function declaration g : Integer -> Integer has been added to

--R      workspace.
--R                                                                   Type: Void
--I                                                                  Time: 0 sec
--E 7
--S 8 of 8
g 40
--R
--R   Compiling function g with type Integer -> Integer
--R
--R   (7)  165580141
--R                                                        Type: PositiveInteger
--I                                     Time: 36.42 (EV) + 0.02 (GC) = 36.44 sec
--E 8
)spool
)lisp (bye)

———-

References^{[1] nothing}

7