Skip to Content.
Sympa Menu

maude-help - Re: [[Maude-help] ] higher order FILTER function

maude-help AT lists.cs.illinois.edu

Subject: Maude-help mailing list

List archive

Re: [[Maude-help] ] higher order FILTER function


Chronological Thread 
  • From: Traian Florin Şerbănuţă <traian.serbanuta AT fmi.unibuc.ro>
  • To: Francesoc Bongiovanni <bongiovanni AT gmail.com>
  • Cc: maude-help AT lists.cs.illinois.edu
  • Subject: Re: [[Maude-help] ] higher order FILTER function
  • Date: Fri, 22 Apr 2016 22:39:38 +0300

Hi Francesco,

Just a quick thought:  as you probably already know, unlike map, filter does not modify the list, so you would have only one parameter, and have List{X} wherever you have List{Y} now.

So, you could write it kind of like his:

fmod FILTER{X :: TRIV} is
  inc AP{X,Bool} .
  pr LIST{X} .

  var E : X$Elt .
  var L : List{X} .
  var F : Func{X,Bool} .

  op filter : List{X} Func{X,Bool} -> List{X} .
  eq filter(nil, F) = nil .
  eq filter(E L, F) = if F[E] then E  else nil fi
                      filter(L, F) .
endfm

I also wrote a test to convince myself it works:

fmod TEST is including FILTER{Nat} .
  op odd : -> Func{Nat,Bool} .
  var X : Nat .
  eq odd[X] = not(2 divides X) .
 
  op odds : List{Nat} -> List{Nat} .
  var L : List{Nat} .
  eq odds(L) = filter(L, odd) .
endfm

red odds(1 2 3 4 5 6 7 8 9 0) .

Hope this helps!
Traian

2016-04-22 17:42 GMT+03:00 Francesoc Bongiovanni <bongiovanni AT gmail.com>:
Hi there,

I am playing a bit with Maude and trying to implement the filter higher order
function based on some code for the MAP function I found on this archive a
long time ago.

fmod AP{X :: TRIV, Y :: TRIV} is
*** one param function
  sort Func{X,Y} .
  op _[_] : Func{X,Y} X$Elt -> Y$Elt [prec 17] .
*** no param function
  sort FFunc{X} .
  op _[] : FFunc{X} -> X$Elt [prec 17] .
endfm


fmod FILTER{X :: TRIV, Y :: TRIV} is
    inc AP{X,Y} .
    inc BOOL .
    pr LIST{X} .
    pr LIST{Y} .

    var E : X$Elt .
    var L : List{X} .
    var F : Func{X,Y} .

    op filter : List{X} Func{X,Y} -> List{Y} .
    eq filter(nil, F) = nil .
   ceq filter(E L, F) = E  filter(L, F) if F[E] == true .

endfm

When I load the module, the interpreter does not expect E there...why? what I
am missing ?

Thanks in advance for your insights

- Francesco




Archive powered by MHonArc 2.6.16.

Top of Page