Monthly Archives: February 2015

The Artist-CD-Track sample

The original Artist-CD-Track sample can be found here:  http://search.cpan.org/dist/DBIx-Class/lib/DBIx/Class/Manual/Example.pod.

The Muldis-D translation can be found here: http://muldis.com/CD.html.

The Andl implementation is below. Note that they are not strictly comparable because:

  1. Andl provides no persistence. The results are only in memory.
  2. Andl provides limited output formatting.
artist  := {{ artistid:=0, name:='' }} [false]
cd      := {{ cdid:=0, artistid:=0, title:='', year:=0 }} [false]
track   := {{ trackid:=0, cdid:= 0, title:='' }} [false]

artist_data := {
  { name := 'Michael Jackson'}, 
  { name := 'Eminem' }
} [{ *artistid := ord() }]
artist := artist union artist_data

cd_data := {
  { title := 'Thriller',                name := 'Michael Jackson' },
  { title := 'Bad',                     name := 'Michael Jackson' },
  { title := 'The Marshall Mathers LP', name := 'Eminem' }
} [{ *cdid := ord() }]
cd := cd union (cd_data join artist)[{ title, cdid, artistid, year:=0}]

track_data := {
   { title := 'Beat It'         , cd := 'Thriller' },
   { title := 'Billie Jean'     , cd := 'Thriller' },
   { title := 'Dirty Diana'     , cd := 'Bad' },
   { title := 'Smooth Criminal' , cd := 'Bad' },
   { title := 'Leave Me Alone'  , cd := 'Bad' },
   { title := 'Stan'            , cd := 'The Marshall Mathers LP' },
   { title := 'The Way I Am'    , cd := 'The Marshall Mathers LP' }
 } [{ *trackid := ord() }]
track := track union (track_data join cd[{ *cd := title }]) [{ trackid, title, cdid }]

get_tracks_by_cd(t) => cd[ title = t {*title} ] join track
get_tracks_by_artist(a) => (artist[ name = a {*name}] join cd) [{cdid}] join track
get_cd_by_track(t) => track [ title = t {cdid} ] join cd
get_cds_by_artist(a) => artist [name = a {artistid} ] join cd
get_artist_by_track(t) => (track [title = t { cdid }] join cd) [{artistid}] join artist
get_artist_by_cd(t) => (cd [title = t { cdid }] join cd) [{artistid}] join artist

get_tracks_by_cd('Bad') [{ i'Track title' := title }]
get_tracks_by_artist('Michael Jackson') [{ i'Track title' := title }]
get_cd_by_track('Stan') [{ i'CD title' := title }]
get_cds_by_artist('Michael Jackson') [{ i'CD title' := title }]
get_artist_by_track('Dirty Diana') [{ i'Artist name' := name }]
get_artist_by_cd('The Marshall Mathers LP') [{ i'Artist name' := name }]

The output looks like this.

Track title
---------------
Dirty Diana
Smooth Criminal
Leave Me Alone

Track title
---------------
Beat It
Billie Jean
Dirty Diana
Smooth Criminal
Leave Me Alone

CD title
-----------------------
The Marshall Mathers LP

CD title
----------
Thriller
Bad

Artist name
---------------
Michael Jackson

Artist name
-----------
Eminem

Leave a Comment

Filed under Code sample

Transitive closure

I figured out how to write the code for Transitive Closure. Here it is in Andl.

// define recursive function
XYT := {{X:='',Y:=''}}
TRANCLO:XYT(XY:XYT) => do {
  TTT := XY[{*Z := Y}] compose XY[{*Z := X}] union XY if(TTT = XY, TTT, TRANCLO(TTT))
}
// call it
TRANCLO(MM[ {X:=MAJOR_P#, Y:= MINOR_P# } ]) [{MAJOR_P#:=X, MINOR_P#:=Y }]

Note

  1. XYT is an expression which provides the type information for what amounts to a function call. This is structural typing, there are no type names.
  2. The symbol ‘=>’ means deferred evaluation, in this case with arguments. A function call by any other name.
  3. The do {} block allows an arbitrary number of statements, and returns the value of the last expression.

I have to confess I’m quite pleased with the outcome.  Here is the equivalent code in Tutorial-D.

/* the function */
OPERATOR TRANCLO ( XY RELATION { X P#, Y P# } ) RETURNS RELATION { X P#, Y P# } ;
    RETURN 
  ( WITH ( XY UNION ( ( XY RENAME ( Y AS Z ) ) 
              COMPOSE ( XY RENAME ( X AS Z ) ) ) ) AS TTT :
    IF TTT = XY THEN TTT /* unwind recursion */
      ELSE TRANCLO ( TTT ) /* recursive invocation */
    END IF 
  ) ;
END OPERATOR ;
/* the call */
( TRANCLO ( MM RENAME ( MAJOR_P# AS X , MINOR_P# AS Y ) ) ) RENAME ( X AS MAJOR_P# , Y AS MINOR_P# )

The similarities are reasonably obvious.

Leave a Comment

Filed under Code sample

Site under construction

Patience is a virtue.

Site under construction.

Older posts have been reconstructed from posts to the TTM mailing list, with some minor edits.

Leave a Comment

Filed under General