doc-src/Intro/advanced.tex
changeset 1387 9bcad9c22fd4
parent 1366 3f3c25d3ec04
child 1467 3d19a5ddc21e
equal deleted inserted replaced
1386:cf066d9b4c4f 1387:9bcad9c22fd4
   393 \indexbold{constants!declaring}\index{rules!declaring}
   393 \indexbold{constants!declaring}\index{rules!declaring}
   394 
   394 
   395 Most theories simply declare constants, definitions and rules.  The {\bf
   395 Most theories simply declare constants, definitions and rules.  The {\bf
   396   constant declaration part} has the form
   396   constant declaration part} has the form
   397 \begin{ttbox}
   397 \begin{ttbox}
   398 consts  \(c@1\) :: "\(\tau@1\)"
   398 consts  \(c@1\) :: \(\tau@1\)
   399         \vdots
   399         \vdots
   400         \(c@n\) :: "\(\tau@n\)"
   400         \(c@n\) :: \(\tau@n\)
   401 \end{ttbox}
   401 \end{ttbox}
   402 where $c@1$, \ldots, $c@n$ are constants and $\tau@1$, \ldots, $\tau@n$ are
   402 where $c@1$, \ldots, $c@n$ are constants and $\tau@1$, \ldots, $\tau@n$ are
   403 types.  Each type {\em must\/} be enclosed in quotation marks.  Each
   403 types.  The types must be enclosed in quotation marks if they contain
       
   404 user-declared infix type constructors like {\tt *}.  Each
   404 constant must be enclosed in quotation marks unless it is a valid
   405 constant must be enclosed in quotation marks unless it is a valid
   405 identifier.  To declare $c@1$, \ldots, $c@n$ as constants of type $\tau$,
   406 identifier.  To declare $c@1$, \ldots, $c@n$ as constants of type $\tau$,
   406 the $n$ declarations may be abbreviated to a single line:
   407 the $n$ declarations may be abbreviated to a single line:
   407 \begin{ttbox}
   408 \begin{ttbox}
   408         \(c@1\), \ldots, \(c@n\) :: "\(\tau\)"
   409         \(c@1\), \ldots, \(c@n\) :: \(\tau\)
   409 \end{ttbox}
   410 \end{ttbox}
   410 The {\bf rule declaration part} has the form
   411 The {\bf rule declaration part} has the form
   411 \begin{ttbox}
   412 \begin{ttbox}
   412 rules   \(id@1\) "\(rule@1\)"
   413 rules   \(id@1\) "\(rule@1\)"
   413         \vdots
   414         \vdots
   429 \index{examples!of theories} 
   430 \index{examples!of theories} 
   430 This theory extends first-order logic by declaring and defining two constants,
   431 This theory extends first-order logic by declaring and defining two constants,
   431 {\em nand} and {\em xor}:
   432 {\em nand} and {\em xor}:
   432 \begin{ttbox}
   433 \begin{ttbox}
   433 Gate = FOL +
   434 Gate = FOL +
   434 consts  nand,xor :: "[o,o] => o"
   435 consts  nand,xor :: [o,o] => o
   435 defs    nand_def "nand(P,Q) == ~(P & Q)"
   436 defs    nand_def "nand(P,Q) == ~(P & Q)"
   436         xor_def  "xor(P,Q)  == P & ~Q | ~P & Q"
   437         xor_def  "xor(P,Q)  == P & ~Q | ~P & Q"
   437 end
   438 end
   438 \end{ttbox}
   439 \end{ttbox}
   439 
   440 
   491 \index{examples!of theories}
   492 \index{examples!of theories}
   492 \begin{ttbox}
   493 \begin{ttbox}
   493 Bool = FOL +
   494 Bool = FOL +
   494 types   bool
   495 types   bool
   495 arities bool    :: term
   496 arities bool    :: term
   496 consts  tt,ff   :: "bool"
   497 consts  tt,ff   :: bool
   497 end
   498 end
   498 \end{ttbox}
   499 \end{ttbox}
   499 A $k$-place type constructor may have arities of the form
   500 A $k$-place type constructor may have arities of the form
   500 $(s@1,\ldots,s@k)c$, where $s@1,\ldots,s@n$ are sorts and $c$ is a class.
   501 $(s@1,\ldots,s@k)c$, where $s@1,\ldots,s@n$ are sorts and $c$ is a class.
   501 Each sort specifies a type argument; it has the form $\{c@1,\ldots,c@m\}$,
   502 Each sort specifies a type argument; it has the form $\{c@1,\ldots,c@m\}$,
   519 \index{examples!of theories}
   520 \index{examples!of theories}
   520 \begin{ttbox}
   521 \begin{ttbox}
   521 List = FOL +
   522 List = FOL +
   522 types   'a list
   523 types   'a list
   523 arities list    :: (term)term
   524 arities list    :: (term)term
   524 consts  Nil     :: "'a list"
   525 consts  Nil     :: 'a list
   525         Cons    :: "['a, 'a list] => 'a list" 
   526         Cons    :: ['a, 'a list] => 'a list
   526 end
   527 end
   527 \end{ttbox}
   528 \end{ttbox}
   528 Multiple arity declarations may be abbreviated to a single line:
   529 Multiple arity declarations may be abbreviated to a single line:
   529 \begin{ttbox}
   530 \begin{ttbox}
   530 arities \(tycon@1\), \ldots, \(tycon@n\) :: \(arity\)
   531 arities \(tycon@1\), \ldots, \(tycon@n\) :: \(arity\)
   539 \subsection{Type synonyms}\indexbold{type synonyms}
   540 \subsection{Type synonyms}\indexbold{type synonyms}
   540 Isabelle supports {\bf type synonyms} ({\bf abbreviations}) which are similar
   541 Isabelle supports {\bf type synonyms} ({\bf abbreviations}) which are similar
   541 to those found in \ML.  Such synonyms are defined in the type declaration part
   542 to those found in \ML.  Such synonyms are defined in the type declaration part
   542 and are fairly self explanatory:
   543 and are fairly self explanatory:
   543 \begin{ttbox}
   544 \begin{ttbox}
   544 types gate       = "[o,o] => o"
   545 types gate       = [o,o] => o
   545       'a pred    = "'a => o"
   546       'a pred    = 'a => o
   546       ('a,'b)nuf = "'b => 'a"
   547       ('a,'b)nuf = 'b => 'a
   547 \end{ttbox}
   548 \end{ttbox}
   548 Type declarations and synonyms can be mixed arbitrarily:
   549 Type declarations and synonyms can be mixed arbitrarily:
   549 \begin{ttbox}
   550 \begin{ttbox}
   550 types nat
   551 types nat
   551       'a stream = "nat => 'a"
   552       'a stream = nat => 'a
   552       signal    = "nat stream"
   553       signal    = nat stream
   553       'a list
   554       'a list
   554 \end{ttbox}
   555 \end{ttbox}
   555 A synonym is merely an abbreviation for some existing type expression.  Hence
   556 A synonym is merely an abbreviation for some existing type expression.  Hence
   556 synonyms may not be recursive!  Internally all synonyms are fully expanded.  As
   557 synonyms may not be recursive!  Internally all synonyms are fully expanded.  As
   557 a consequence Isabelle output never contains synonyms.  Their main purpose is
   558 a consequence Isabelle output never contains synonyms.  Their main purpose is
   558 to improve the readability of theories.  Synonyms can be used just like any
   559 to improve the readability of theories.  Synonyms can be used just like any
   559 other type:
   560 other type:
   560 \begin{ttbox}
   561 \begin{ttbox}
   561 consts and,or :: "gate"
   562 consts and,or :: gate
   562        negate :: "signal => signal"
   563        negate :: signal => signal
   563 \end{ttbox}
   564 \end{ttbox}
   564 
   565 
   565 \subsection{Infix and mixfix operators}
   566 \subsection{Infix and mixfix operators}
   566 \index{infixes}\index{examples!of theories}
   567 \index{infixes}\index{examples!of theories}
   567 
   568 
   568 Infix or mixfix syntax may be attached to constants.  Consider the
   569 Infix or mixfix syntax may be attached to constants.  Consider the
   569 following theory:
   570 following theory:
   570 \begin{ttbox}
   571 \begin{ttbox}
   571 Gate2 = FOL +
   572 Gate2 = FOL +
   572 consts  "~&"     :: "[o,o] => o"         (infixl 35)
   573 consts  "~&"     :: [o,o] => o         (infixl 35)
   573         "#"      :: "[o,o] => o"         (infixl 30)
   574         "#"      :: [o,o] => o         (infixl 30)
   574 defs    nand_def "P ~& Q == ~(P & Q)"    
   575 defs    nand_def "P ~& Q == ~(P & Q)"    
   575         xor_def  "P # Q  == P & ~Q | ~P & Q"
   576         xor_def  "P # Q  == P & ~Q | ~P & Q"
   576 end
   577 end
   577 \end{ttbox}
   578 \end{ttbox}
   578 The constant declaration part declares two left-associating infix operators
   579 The constant declaration part declares two left-associating infix operators
   588 
   589 
   589 \bigskip\index{mixfix declarations}
   590 \bigskip\index{mixfix declarations}
   590 {\bf Mixfix} operators may have arbitrary context-free syntaxes.  Let us
   591 {\bf Mixfix} operators may have arbitrary context-free syntaxes.  Let us
   591 add a line to the constant declaration part:
   592 add a line to the constant declaration part:
   592 \begin{ttbox}
   593 \begin{ttbox}
   593         If :: "[o,o,o] => o"       ("if _ then _ else _")
   594         If :: [o,o,o] => o       ("if _ then _ else _")
   594 \end{ttbox}
   595 \end{ttbox}
   595 This declares a constant $If$ of type $[o,o,o] \To o$ with concrete syntax {\tt
   596 This declares a constant $If$ of type $[o,o,o] \To o$ with concrete syntax {\tt
   596   if~$P$ then~$Q$ else~$R$} as well as {\tt If($P$,$Q$,$R$)}.  Underscores
   597   if~$P$ then~$Q$ else~$R$} as well as {\tt If($P$,$Q$,$R$)}.  Underscores
   597 denote argument positions.  
   598 denote argument positions.  
   598 
   599 
   605     {Chap.\ts\ref{Defining-Logics}}.
   606     {Chap.\ts\ref{Defining-Logics}}.
   606 
   607 
   607 Mixfix declarations can be annotated with priorities, just like
   608 Mixfix declarations can be annotated with priorities, just like
   608 infixes.  The example above is just a shorthand for
   609 infixes.  The example above is just a shorthand for
   609 \begin{ttbox}
   610 \begin{ttbox}
   610         If :: "[o,o,o] => o"       ("if _ then _ else _" [0,0,0] 1000)
   611         If :: [o,o,o] => o       ("if _ then _ else _" [0,0,0] 1000)
   611 \end{ttbox}
   612 \end{ttbox}
   612 The numeric components determine priorities.  The list of integers
   613 The numeric components determine priorities.  The list of integers
   613 defines, for each argument position, the minimal priority an expression
   614 defines, for each argument position, the minimal priority an expression
   614 at that position must have.  The final integer is the priority of the
   615 at that position must have.  The final integer is the priority of the
   615 construct itself.  In the example above, any argument expression is
   616 construct itself.  In the example above, any argument expression is
   616 acceptable because priorities are non-negative, and conditionals may
   617 acceptable because priorities are non-negative, and conditionals may
   617 appear everywhere because 1000 is the highest priority.  On the other
   618 appear everywhere because 1000 is the highest priority.  On the other
   618 hand, the declaration
   619 hand, the declaration
   619 \begin{ttbox}
   620 \begin{ttbox}
   620         If :: "[o,o,o] => o"       ("if _ then _ else _" [100,0,0] 99)
   621         If :: [o,o,o] => o       ("if _ then _ else _" [100,0,0] 99)
   621 \end{ttbox}
   622 \end{ttbox}
   622 defines concrete syntax for a conditional whose first argument cannot have
   623 defines concrete syntax for a conditional whose first argument cannot have
   623 the form {\tt if~$P$ then~$Q$ else~$R$} because it must have a priority
   624 the form {\tt if~$P$ then~$Q$ else~$R$} because it must have a priority
   624 of at least~100.  We may of course write
   625 of at least~100.  We may of course write
   625 \begin{quote}\tt
   626 \begin{quote}\tt
   673 $term$ and add the three polymorphic constants of this class.
   674 $term$ and add the three polymorphic constants of this class.
   674 \index{examples!of theories}\index{constants!overloaded}
   675 \index{examples!of theories}\index{constants!overloaded}
   675 \begin{ttbox}
   676 \begin{ttbox}
   676 Arith = FOL +
   677 Arith = FOL +
   677 classes arith < term
   678 classes arith < term
   678 consts  "0"     :: "'a::arith"                  ("0")
   679 consts  "0"     :: 'a::arith                  ("0")
   679         "1"     :: "'a::arith"                  ("1")
   680         "1"     :: 'a::arith                  ("1")
   680         "+"     :: "['a::arith,'a] => 'a"       (infixl 60)
   681         "+"     :: ['a::arith,'a] => 'a       (infixl 60)
   681 end
   682 end
   682 \end{ttbox}
   683 \end{ttbox}
   683 No rules are declared for these constants: we merely introduce their
   684 No rules are declared for these constants: we merely introduce their
   684 names without specifying properties.  On the other hand, classes
   685 names without specifying properties.  On the other hand, classes
   685 with rules make it possible to prove {\bf generic} theorems.  Such
   686 with rules make it possible to prove {\bf generic} theorems.  Such
   691 \index{examples!of theories}
   692 \index{examples!of theories}
   692 \begin{ttbox}
   693 \begin{ttbox}
   693 BoolNat = Arith +
   694 BoolNat = Arith +
   694 types   bool  nat
   695 types   bool  nat
   695 arities bool, nat   :: arith
   696 arities bool, nat   :: arith
   696 consts  Suc         :: "nat=>nat"
   697 consts  Suc         :: nat=>nat
   697 \ttbreak
   698 \ttbreak
   698 rules   add0        "0 + n = n::nat"
   699 rules   add0        "0 + n = n::nat"
   699         addS        "Suc(m)+n = Suc(m+n)"
   700         addS        "Suc(m)+n = Suc(m+n)"
   700         nat1        "1 = Suc(0)"
   701         nat1        "1 = Suc(0)"
   701         or0l        "0 + x = x::bool"
   702         or0l        "0 + x = x::bool"
   783 identifier, and could not otherwise be written in terms:
   784 identifier, and could not otherwise be written in terms:
   784 \begin{ttbox}\index{mixfix declarations}
   785 \begin{ttbox}\index{mixfix declarations}
   785 Nat = FOL +
   786 Nat = FOL +
   786 types   nat
   787 types   nat
   787 arities nat         :: term
   788 arities nat         :: term
   788 consts  "0"         :: "nat"                              ("0")
   789 consts  "0"         :: nat                              ("0")
   789         Suc         :: "nat=>nat"
   790         Suc         :: nat=>nat
   790         rec         :: "[nat, 'a, [nat,'a]=>'a] => 'a"
   791         rec         :: [nat, 'a, [nat,'a]=>'a] => 'a
   791         "+"         :: "[nat, nat] => nat"                (infixl 60)
   792         "+"         :: [nat, nat] => nat                (infixl 60)
   792 rules   Suc_inject  "Suc(m)=Suc(n) ==> m=n"
   793 rules   Suc_inject  "Suc(m)=Suc(n) ==> m=n"
   793         Suc_neq_0   "Suc(m)=0      ==> R"
   794         Suc_neq_0   "Suc(m)=0      ==> R"
   794         induct      "[| P(0);  !!x. P(x) ==> P(Suc(x)) |]  ==> P(n)"
   795         induct      "[| P(0);  !!x. P(x) ==> P(Suc(x)) |]  ==> P(n)"
   795         rec_0       "rec(0,a,f) = a"
   796         rec_0       "rec(0,a,f) = a"
   796         rec_Suc     "rec(Suc(m), a, f) = f(m, rec(m,a,f))"
   797         rec_Suc     "rec(Suc(m), a, f) = f(m, rec(m,a,f))"
  1075 rules of~{\tt FOL}.
  1076 rules of~{\tt FOL}.
  1076 \begin{ttbox}
  1077 \begin{ttbox}
  1077 Prolog = FOL +
  1078 Prolog = FOL +
  1078 types   'a list
  1079 types   'a list
  1079 arities list    :: (term)term
  1080 arities list    :: (term)term
  1080 consts  Nil     :: "'a list"
  1081 consts  Nil     :: 'a list
  1081         ":"     :: "['a, 'a list]=> 'a list"            (infixr 60)
  1082         ":"     :: ['a, 'a list]=> 'a list            (infixr 60)
  1082         app     :: "['a list, 'a list, 'a list] => o"
  1083         app     :: ['a list, 'a list, 'a list] => o
  1083         rev     :: "['a list, 'a list] => o"
  1084         rev     :: ['a list, 'a list] => o
  1084 rules   appNil  "app(Nil,ys,ys)"
  1085 rules   appNil  "app(Nil,ys,ys)"
  1085         appCons "app(xs,ys,zs) ==> app(x:xs, ys, x:zs)"
  1086         appCons "app(xs,ys,zs) ==> app(x:xs, ys, x:zs)"
  1086         revNil  "rev(Nil,Nil)"
  1087         revNil  "rev(Nil,Nil)"
  1087         revCons "[| rev(xs,ys); app(ys,x:Nil,zs) |] ==> rev(x:xs,zs)"
  1088         revCons "[| rev(xs,ys); app(ys,x:Nil,zs) |] ==> rev(x:xs,zs)"
  1088 end
  1089 end