23 Aug 2016

Question#1:  (15)
We need to specify an abstract data type to represent music that can be sung by a single voice. In Keeping with standard musical terminology, we will call this ADT property, but in our simplified view of the world, a voice is basically just a sequence of notes & rests. The signature part along with informal specification of the algebraic specification for the ADT is given below:

1.       Emptyvoice: Creates an empty voice that is a voice without any notes in it.
2.       oneNote: Creates a voice made up of only one note
3.       Concatenate :Creates a voice by concatenating two voices  together
4.       Is_empty :Checks if a voice is empty
5.       firstNote :Returns the first note of voice.

Solution:

Emptyvoice:   => voice
oneNote:   Note => voice
Concatenate:  voice,voice  => voice
Is_empty :  voice => Boolean
firstNote : voice => Note
otherNotes: voice => Note

   Write Axioms to complete specification?

Specifying isEmpty is straightforward:
isEmpty (emptyVoice ()) = true
isEmpty (oneNote (n)) = false
isEmpty (concatenated (v1,v2)) = isEmpty (v1) & isEmpty (v2)

Specifying firstNote is just a bit more interesting:
firstNote (emptyVoice ()) = error
firstNote (oneNote (n)) = n
firstNote (concatenated (v1,v2)) =
              firstNote (v2) if isEmpty(v1)
               firstNote (v1)           otherwise
firstNote when applied to an empty voice. it should be an error.

otherNotes (oneNote (n)) = emptyVoice ()
otherNotes (concatenated (v1,v2)) =
                  otherNotes (v2) if isEmpty(v1)
                  concatenated (otherNotes (v1),v2)          otherwise

firstNote (otherNotes (concatenated (oneNote (n1), concatenated (oneNote (n2), oneNote (n3)))))
= firstNote (concatenated (otherNodes (oneNote (n1)), concatenated (oneNote (n2), oneNote (n3))))
= firstNote (concatenated (emptyVoice (), concatenated (oneNote (n2), oneNote (n3))))
= firstNote (concatenated (oneNote (n2), oneNote (n3)))
= firstNote (oneNote (n2))
= n2


No comments:

Post a Comment