| 1 |
-- FILE: /home/diacon/LANG/Cafe/prog/sieve.mod |
| 2 |
-- CONTENTS: equational program for computing prime numbers by |
| 3 |
-- the Eratostene sieve algorithm featuring |
| 4 |
-- evaluation strategies and computation modulo axioms (A) |
| 5 |
-- AUTHOR: Razvan Diaconescu |
| 6 |
-- DIFFICULTY: *** |
| 7 |
|
| 8 |
set tram path brute |
| 9 |
|
| 10 |
mod! LAZYLIST (X :: TRIV ) { |
| 11 |
|
| 12 |
[ Elt < List ] |
| 13 |
|
| 14 |
op nil : -> List |
| 15 |
op __ : List List -> List {assoc idr: nil strat: (0)} |
| 16 |
} |
| 17 |
|
| 18 |
mod* INFLIST { |
| 19 |
protecting(LAZYLIST(INT)) |
| 20 |
|
| 21 |
[ List < InfList ] |
| 22 |
|
| 23 |
op __ : List InfList -> InfList {strat: (0)} |
| 24 |
op ints-from_ : Int -> InfList |
| 25 |
op show_upto_ : InfList Int -> List |
| 26 |
op force : List InfList -> InfList {strat: (1 2 0)} |
| 27 |
|
| 28 |
vars E I : Int |
| 29 |
var L : List |
| 30 |
var S : InfList |
| 31 |
|
| 32 |
eq force(L,S) = L S . |
| 33 |
eq show nil upto I = nil . |
| 34 |
eq show E S upto I = if I == 0 then nil |
| 35 |
else force(E,show S upto (I - 1)) fi . |
| 36 |
eq ints-from I = I (ints-from (I + 1)) . |
| 37 |
} |
| 38 |
|
| 39 |
mod* SIEVE { |
| 40 |
protecting(INFLIST) |
| 41 |
|
| 42 |
op sieve_ : List -> List |
| 43 |
op sieve_ : InfList -> InfList |
| 44 |
op primes : -> InfList |
| 45 |
op filter_with_ : List Int -> List |
| 46 |
op filter_with_ : InfList Int -> InfList |
| 47 |
|
| 48 |
vars E I P : Int |
| 49 |
var S : InfList |
| 50 |
|
| 51 |
eq filter nil with P = nil . |
| 52 |
eq filter I S with P = if (I rem P) == 0 then filter S with P |
| 53 |
else I (filter S with P) fi . |
| 54 |
eq sieve nil = nil . |
| 55 |
eq sieve (I S) = I (sieve (filter S with I)) . |
| 56 |
eq primes = sieve (ints-from 2) . |
| 57 |
} |
| 58 |
|
| 59 |
-- tram red in SIEVE : show primes upto 200 . |
| 60 |
red in SIEVE : show primes upto 100 . |
| 61 |
|
| 62 |
-- |
| 63 |
eof |
| 64 |
-- |
| 65 |
$Id: sieve.mod,v 1.2 2010-06-17 08:23:18 sawada Exp $ |