ProgTutorial/FirstSteps.thy
changeset 257 ce0f60d0351e
parent 256 1fb8d62c88a0
child 258 03145998190b
equal deleted inserted replaced
256:1fb8d62c88a0 257:ce0f60d0351e
   316        ((fn x => x + 4) o fst o (fn x => (x, x)) o (fn x => x + 1)) x*}
   316        ((fn x => x + 4) o fst o (fn x => (x, x)) o (fn x => x + 1)) x*}
   317 
   317 
   318 text {* and typographically more economical than *}
   318 text {* and typographically more economical than *}
   319 
   319 
   320 ML{*fun inc_by_five x =
   320 ML{*fun inc_by_five x =
   321    let val y1 = x + 1
   321 let val y1 = x + 1
   322        val y2 = (y1, y1)
   322     val y2 = (y1, y1)
   323        val y3 = fst y2
   323     val y3 = fst y2
   324        val y4 = y3 + 4
   324     val y4 = y3 + 4
   325    in y4 end*}
   325 in y4 end*}
   326 
   326 
   327 text {* 
   327 text {* 
   328   Another reason why the let-bindings in the code above are better to be
   328   Another reason why the let-bindings in the code above are better to be
   329   avoided: it is more than easy to get the intermediate values wrong, not to 
   329   avoided: it is more than easy to get the intermediate values wrong, not to 
   330   mention the nightmares the maintenance of this code causes!
   330   mention the nightmares the maintenance of this code causes!
   337     f |> fastype_of
   337     f |> fastype_of
   338       |> binder_types 
   338       |> binder_types 
   339       |> map (pair "z")
   339       |> map (pair "z")
   340       |> Variable.variant_frees ctxt [f]
   340       |> Variable.variant_frees ctxt [f]
   341       |> map Free  
   341       |> map Free  
   342       |> (curry list_comb) f *}
   342       |> curry list_comb f *}
   343 
   343 
   344 text {*
   344 text {*
   345   This code extracts the argument types of a given function @{text "f"} and then generates 
   345   This code extracts the argument types of a given function @{text "f"} and then generates 
   346   for each argument type a distinct variable; finally it applies the generated 
   346   for each argument type a distinct variable; finally it applies the generated 
   347   variables to the function. For example:
   347   variables to the function. For example:
  1964 end"
  1964 end"
  1965 "99998, 99999, 100000, 100001, 100002, 100003, 100004, 100005, 100006, 
  1965 "99998, 99999, 100000, 100001, 100002, 100003, 100004, 100005, 100006, 
  1966 100007, 100008, 100009, 100010, 100011, 100012, 100013, 100014, 100015, 
  1966 100007, 100008, 100009, 100010, 100011, 100012, 100013, 100014, 100015, 
  1967 100016, 100017, 100018, 100019, 100020"}
  1967 100016, 100017, 100018, 100019, 100020"}
  1968 
  1968 
  1969   where @{ML upto} generates a list of integers. You can print this
  1969   where @{ML upto} generates a list of integers. You can print out this
  1970   list out as an ``set'', that means enclosed inside @{text [quotes] "{"} and
  1970   list as an ``set'', that means enclosed inside @{text [quotes] "{"} and
  1971   @{text [quotes] "}"}, and separated by commas using the function
  1971   @{text [quotes] "}"}, and separated by commas using the function
  1972   @{ML [index] enum in Pretty}. For example
  1972   @{ML [index] enum in Pretty}. For example
  1973 *}
  1973 *}
  1974 
  1974 
  1975 text {*
  1975 text {*
  1983 "{99998, 99999, 100000, 100001, 100002, 100003, 100004, 100005, 100006, 
  1983 "{99998, 99999, 100000, 100001, 100002, 100003, 100004, 100005, 100006, 
  1984   100007, 100008, 100009, 100010, 100011, 100012, 100013, 100014, 100015, 
  1984   100007, 100008, 100009, 100010, 100011, 100012, 100013, 100014, 100015, 
  1985   100016, 100017, 100018, 100019, 100020}"}
  1985   100016, 100017, 100018, 100019, 100020}"}
  1986 
  1986 
  1987   As can be seen, this function prints out the ``set'' so that starting 
  1987   As can be seen, this function prints out the ``set'' so that starting 
  1988   from the second each new line as an indentation of 2.
  1988   from the second, each new line as an indentation of 2.
  1989   
  1989   
  1990   If you print something out which goes beyond the capabilities of the
  1990   If you print out something that goes beyond the capabilities of the
  1991   standard function, you can do realatively easily the formating
  1991   standard functions, you can do realatively easily the formating
  1992   yourself. Assume you want to print out a list of items where like in ``English''
  1992   yourself. Assume you want to print out a list of items where like in ``English''
  1993   the last two items are separated by @{text [quotes] "and"}. For this you can
  1993   the last two items are separated by @{text [quotes] "and"}. For this you can
  1994   write the function
  1994   write the function
  1995 
  1995 
  1996 *}
  1996 *}
  2004         (Pretty.commas front) @ 
  2004         (Pretty.commas front) @ 
  2005         [Pretty.brk 1, Pretty.str "and", Pretty.brk 1, last]
  2005         [Pretty.brk 1, Pretty.str "and", Pretty.brk 1, last]
  2006       end *}
  2006       end *}
  2007 
  2007 
  2008 text {*
  2008 text {*
  2009   where in Line 7 we print the beginning of the list and in Line
  2009   where Line 7 prints the beginning of the list and Line
  2010   8 the last item. We have to use @{ML "Pretty.brk 1"} in order
  2010   8 the last item. We have to use @{ML "Pretty.brk 1"} in order
  2011   to insert a space (of length 1) before the 
  2011   to insert a space (of length 1) before the 
  2012   @{text [quotes] "and"}. This space is also a pleace where a linebreak 
  2012   @{text [quotes] "and"}. This space is also a pleace where a linebreak 
  2013   can occur. We do the same ater the @{text [quotes] "and"}. This gives you
  2013   can occur. We do the same ater the @{text [quotes] "and"}. This gives you
  2014   for example
  2014   for example
  2036     (pstr "The term " @ [ptrm] @ pstr " has type " 
  2036     (pstr "The term " @ [ptrm] @ pstr " has type " 
  2037       @ [pty, Pretty.str "."])))
  2037       @ [pty, Pretty.str "."])))
  2038 end*}
  2038 end*}
  2039 
  2039 
  2040 text {*
  2040 text {*
  2041   In Line 3 we define a function that inserts according to spaces 
  2041   In Line 3 we define a function that inserts possible linebreaks in places
  2042   possible linebreaks into an (English). In Lines 4 and 5 we pretty-print
  2042   where a space is. In Lines 4 and 5 we pretty-print the term and its type
  2043   the term and its type using the functions @{ML [index] pretty_term in Syntax} and
  2043   using the functions @{ML [index] pretty_term in Syntax} and @{ML [index]
  2044   @{ML [index] pretty_typ in Syntax}. We also use the function @{ML [index] quote in Pretty}
  2044   pretty_typ in Syntax}. We also use the function @{ML [index] quote in
  2045   in order to enclose the term and type inside quotation marks. In Line 
  2045   Pretty} in order to enclose the term and type inside quotation marks. In
  2046   9 we add a period right after the type without the possibility of a 
  2046   Line 9 we add a period right after the type without the possibility of a
  2047   linebreak, because we do not want that a linebreak occurs there.
  2047   linebreak, because we do not want that a linebreak occurs there.
       
  2048 
  2048 
  2049 
  2049   Now you can write
  2050   Now you can write
  2050 
  2051 
  2051   @{ML_response_fake [display,gray]
  2052   @{ML_response_fake [display,gray]
  2052   "tell_type @{context} @{term \"min (Suc 0)\"}"
  2053   "tell_type @{context} @{term \"min (Suc 0)\"}"
  2058   @{ML_response_fake [display,gray]
  2059   @{ML_response_fake [display,gray]
  2059   "tell_type @{context} @{term \"op = (op = (op = (op = (op = op =))))\"}"
  2060   "tell_type @{context} @{term \"op = (op = (op = (op = (op = op =))))\"}"
  2060   "The term \"op = (op = (op = (op = (op = op =))))\" has type 
  2061   "The term \"op = (op = (op = (op = (op = op =))))\" has type 
  2061 \"((((('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> bool) \<Rightarrow> bool) \<Rightarrow> bool) \<Rightarrow> bool) \<Rightarrow> bool\"."}
  2062 \"((((('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> bool) \<Rightarrow> bool) \<Rightarrow> bool) \<Rightarrow> bool) \<Rightarrow> bool\"."}
  2062 
  2063 
       
  2064 
       
  2065   FIXME: TBD below
  2063 *}
  2066 *}
  2064 
  2067 
  2065 ML {* pprint (Pretty.big_list "header"  (map (Pretty.str o string_of_int) (4 upto 10))) *}
  2068 ML {* pprint (Pretty.big_list "header"  (map (Pretty.str o string_of_int) (4 upto 10))) *}
  2066 
  2069 
  2067 text {*
  2070 text {*