[linux-l] Re: Apropos Java-Bein-Klotz

Rocco Rutte pdmef at cs.tu-berlin.de
So Mai 29 16:18:45 CEST 2005


Hi,

* Oliver Bandel [05-05-28 15:42:58 +0200] wrote:

>Schick doch mal die Beispiel-Sourcen!

[x] Done.

Aber ich war etwas voreilig. Das mit der Bindung geht nicht so ohne 
weiteres, weil es für Strings ("denotation") keinen Konstruktor gibt. 
Aber einen solchen Konstruktor braucht man, um ihn direkt als Argument 
einer Funktion zu benutzen. Zum Beispiel baut man einen neuen Datentyp 
'Bit' so:

DATA Bit == 0 1

Diese '0' und '1' kann man dann für soetwas wie "Überladen" benutzen:

FUN land : bit ** bit -> bool
DEF land (0, 0) == false
DEF land (1, 0) == false
DEF land (0, 1) == false
DEF land (1, 1) == true

Allerdings ist das Problem, dass alles, was man direkt im Argument der 
Funktion schon nutzen will, auch in obiger 'DATA Bit ==' Anweisung 
stehen muss. Und bei Denotation gibt es keinen Konstruktor, der schon 
alle möglichen Strings umfasst. Deshalb musste ich in meiner Lösung erst 
einen eigenen Datentyp 'opts' bauen, dessen Werte ich überladen kann. 
Ich spare mir zwar dann die Bindung Key->Funktion, muss aber in conv() 
trotzdem die Bindung Switch_von_Befehlszeile->Key machen... ;-(

Aber es funktioniert:

$ ./tests -a -b -c -f
handle a
handle b
handle c
-f: bad option ;-(

Wenn es soetwas noch nicht gibt, könnte man ja langfristig mal 
verschiedene Implementierungen einer Problemlösung sammeln und im Netz 
veröffentlichen...

  bye, Rocco
-- 
:wq!
-------------- nächster Teil --------------
SIGNATURE Test

IMPORT Void      ONLY void
       Com[void] ONLY com[void]

FUN tests : com[void]
-------------- nächster Teil --------------
IMPLEMENTATION Test

IMPORT Denotation  COMPLETELY
       Nat         COMPLETELY
       Void        COMPLETELY
       Com[void]   COMPLETELY
       File        COMPLETELY
       ComCompose  COMPLETELY
       ProcessArgs COMPLETELY
       Seq         COMPLETELY
       SeqMap      COMPLETELY
       SeqReduce   COMPLETELY

-- as there's no constructor for denotation we can use,
-- we need custom data type 'opts'
DATA opts == a b c BAD

-- map given command line option to items of type 'opts'
FUN conv : denotation -> opts
DEF conv (arg) ==
  IF arg = "-a" THEN a
  IF arg = "-b" THEN b
  IF arg = "-c" THEN c
  ELSE BAD FI

-- handling functionality
FUN handle : denotation ** opts -> denotation
-- the handle functions
DEF handle (_, a) == "handle a"
DEF handle (_, b) == "handle b"
DEF handle (_, c) == "handle c"
DEF handle (o, BAD) == o ++ ": bad option ;-("

FUN processArgs : seq[denotation] -> com[void]
DEF processArgs (_ :: argv) ==
  write (stdOut,
    reduce (\\a,b. a ++ "\n" ++ b, "")
    (map (\\d. handle (d, conv (d))) (argv))
  )

FUN tests : com[void]
DEF tests == args & processArgs


Mehr Informationen über die Mailingliste linux-l