src/Tools/isac/ProgLang/ListC.thy
author Walther Neuper <walther.neuper@jku.at>
Sun, 22 Sep 2019 14:47:35 +0200
changeset 59630 d345b109672f
parent 59620 src/Tools/isac/CalcElements/ListC.thy@086e4d9967a3
child 59801 17d807bf28fb
permissions -rw-r--r--
lucin: shift ListC.thy into ProgLang
     1 (* Title:  functions on lists for Programs
     2    Author: Walther Neuper 0108
     3    (c) due to copyright terms
     4 *)
     5 
     6 theory ListC imports "~~/src/Tools/isac/CalcElements/CalcElements"
     7 
     8 begin
     9 
    10 text \<open>Abstract:
    11   There are examples in the Knowledge, which demonstrate list processing, e.g. InsSort.thy.
    12 
    13   Lists in example calculations must have a different type for list processing in Isac programs.
    14   Presently the lists in examples have a new type xlist with the unfamiliar list
    15   notation "{||a, b, c||}". One could expect math-authors to use such unfamiliar notation 
    16   and expose students to familiar notation in calculations, and not vice versa as done presently.
    17 
    18   Thus this theory resides in ProgLang so far.
    19 \<close>
    20 
    21 subsection \<open>General constants\<close>
    22 consts
    23   EmptyList :: "bool list" 
    24   UniversalList :: "bool list" 
    25 
    26 ML \<open>
    27 \<close> ML \<open>
    28 val UniversalList = (Thm.term_of o the o (TermC.parse @{theory})) "UniversalList";
    29 val EmptyList = (Thm.term_of o the o (TermC.parse @{theory}))  "[]::bool list";     
    30 \<close> ML \<open>
    31 \<close>
    32 
    33 subsection \<open>Type 'xlist' for Lucas-Interpretation | for InsSort.thy\<close>
    34 (* cp fom ~~/src/HOL/List.thy
    35    TODO: ask for shorter deliminters in xlist *)
    36 datatype  'a xlist =
    37     XNil  ("{|| ||}")
    38   | XCons (xhd: 'a)  (xtl: "'a xlist")  (infixr "@#" 65)
    39 
    40 syntax
    41    \<comment> \<open>list Enumeration\<close>
    42   "_xlist" :: "args => 'a xlist"    ("{|| (_) ||}")
    43 
    44 translations
    45   "{||x, xs||}" == "x@#{||xs||}"
    46   "{||x||}" == "x@#{|| ||}"
    47 
    48 term "{|| ||}"
    49 term "{||1,2,3||}"
    50 
    51 subsection \<open>Functions for 'xlist'\<close>
    52 (* TODO: 
    53 (1) revise, if definition of identifiers like LENGTH_NIL are still required.
    54 (2) switch the role of 'xlist' and 'list' in the functions below, in particular for
    55   'foldr', 'foldr_Nil', 'foldr_Cons' and 'xfoldr', 'xfoldr_Nil', 'xfoldr_Cons'.
    56   For transition phase just outcomment InsSort.thy and inssort.sml.
    57 *)
    58 
    59 primrec xfoldr :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a xlist \<Rightarrow> 'b \<Rightarrow> 'b" where
    60 xfoldr_Nil:  "xfoldr f {|| ||} = id" |
    61 xfoldr_Cons: "xfoldr f (x @# xs) = f x \<circ> xfoldr f xs"
    62 
    63 primrec LENGTH   :: "'a list => real"
    64 where
    65 LENGTH_NIL:	"LENGTH [] = 0" |
    66 LENGTH_CONS: "LENGTH (x#xs) = 1 + LENGTH xs"
    67 
    68 subsection \<open>Functions for 'list'\<close>
    69 
    70 consts NTH ::  "[real, 'a list] => 'a"
    71 axiomatization where
    72 NTH_NIL: "NTH 1 (x # xs) = x" and
    73 NTH_CONS: (*NO primrec, fun ..*)"1 < n ==> NTH n (x # xs) = NTH (n + -1) xs"
    74 
    75 (* redefine together with identifiers (still) required for KEStore ..*)
    76 axiomatization where
    77 hd_thm:	"hd (x # xs) = x"
    78 
    79 axiomatization where
    80 tl_Nil:	"tl []  = []" and
    81 tl_Cons: "tl (x # xs) = xs"
    82 
    83 axiomatization where
    84 LAST:	"last (x # xs) = (if xs = [] then x else last xs)"
    85 
    86 axiomatization where
    87 butlast_Nil: "butlast []    = []" and
    88 butlast_Cons:	"butlast (x # xs) = (if xs = [] then [] else x # butlast xs)"
    89 
    90 axiomatization where
    91 map_Nil:"map f [] = []" and
    92 map_Cons:	"map f (x # xs) = f x  #  map f xs"
    93 
    94 axiomatization where
    95 rev_Nil: "rev [] = []" and
    96 rev_Cons:	"rev (x # xs) = rev xs @ [x]"
    97 
    98 axiomatization where
    99 filter_Nil:	"filter P [] = []" and
   100 filter_Cons: "filter P (x # xs) = (if P x then x # filter P xs else filter P xs)" 
   101 
   102 axiomatization where
   103 concat_Nil:	"concat [] = []" and
   104 concat_Cons: "concat (x # xs) = x @ concat xs"
   105 
   106 axiomatization where
   107 takeWhile_Nil: "takeWhile P []     = []" and
   108 takeWhile_Cons: "takeWhile P (x # xs) = (if P x then x # takeWhile P xs else [])"
   109 
   110 axiomatization where
   111 dropWhile_Nil: "dropWhile P [] = []" and
   112 dropWhile_Cons: "dropWhile P (x # xs) = (if P x then dropWhile P xs else x#xs)"
   113 
   114 axiomatization where
   115 zip_Nil: "zip xs [] = []" and
   116 zip_Cons:	"zip xs (y # ys) = (case xs of [] => [] | z # zs => (z,y) # zip zs ys)"
   117 
   118 axiomatization where
   119 distinct_Nil:	"distinct []     = True" and
   120 distinct_Cons:	"distinct (x # xs) = (x ~: set xs & distinct xs)"
   121 
   122 axiomatization where
   123 remdups_Nil:	"remdups [] = []" and
   124 remdups_Cons:	"remdups (x#xs) = (if x : set xs then remdups xs else x # remdups xs)" 
   125 
   126 consts lastI :: "'a list \<Rightarrow> 'a"
   127 axiomatization where
   128 last_thmI: "lastI (x#xs) = (if xs = [] then x else lastI xs)"
   129 
   130 subsection \<open>rule set for evaluating Porg_Expr, will be extended in several thys\<close>
   131 ML \<open>
   132 \<close> ML \<open>
   133 val list_rls = 
   134   Rule.Rls {id = "list_rls", preconds = [], rew_ord = ("dummy_ord", Rule.dummy_ord), 
   135     erls = Rule.Erls, srls = Rule.Erls, calc = [], errpatts = [],
   136     rules = [Rule.Thm ("refl", TermC.num_str @{thm refl}),  (*'a<>b -> FALSE' by fun eval_equal*)
   137        Rule.Thm ("o_apply", TermC.num_str @{thm o_apply}),
   138 
   139        Rule.Thm ("NTH_CONS",TermC.num_str @{thm NTH_CONS}),(*erls for cond. in Atools.ML*)
   140        Rule.Thm ("NTH_NIL",TermC.num_str @{thm NTH_NIL}),
   141        Rule.Thm ("append_Cons",TermC.num_str @{thm append_Cons}),
   142        Rule.Thm ("append_Nil",TermC.num_str @{thm append_Nil}),
   143 (*       Thm ("butlast_Cons",num_str @{thm butlast_Cons}),
   144        Thm ("butlast_Nil",num_str @{thm butlast_Nil}),*)
   145        Rule.Thm ("concat_Cons",TermC.num_str @{thm concat_Cons}),
   146        Rule.Thm ("concat_Nil",TermC.num_str @{thm concat_Nil}),
   147 (*       Rule.Thm ("del_base",num_str @{thm del_base}),
   148        Rule.Thm ("del_rec",num_str @{thm del_rec}), *)
   149 
   150        Rule.Thm ("distinct_Cons",TermC.num_str @{thm distinct_Cons}),
   151        Rule.Thm ("distinct_Nil",TermC.num_str @{thm distinct_Nil}),
   152        Rule.Thm ("dropWhile_Cons",TermC.num_str @{thm dropWhile_Cons}),
   153        Rule.Thm ("dropWhile_Nil",TermC.num_str @{thm dropWhile_Nil}),
   154        Rule.Thm ("filter_Cons",TermC.num_str @{thm filter_Cons}),
   155        Rule.Thm ("filter_Nil",TermC.num_str @{thm filter_Nil}),
   156        Rule.Thm ("foldr_Cons",TermC.num_str @{thm foldr_Cons}),
   157        Rule.Thm ("foldr_Nil",TermC.num_str @{thm foldr_Nil}),
   158        Rule.Thm ("hd_thm",TermC.num_str @{thm hd_thm}),
   159        Rule.Thm ("LAST",TermC.num_str @{thm LAST}),
   160        Rule.Thm ("LENGTH_CONS",TermC.num_str @{thm LENGTH_CONS}),
   161        Rule.Thm ("LENGTH_NIL",TermC.num_str @{thm LENGTH_NIL}),
   162 (*       Rule.Thm ("list_diff_def",num_str @{thm list_diff_def}),*)
   163        Rule.Thm ("map_Cons",TermC.num_str @{thm map_Cons}),
   164        Rule.Thm ("map_Nil",TermC.num_str @{thm map_Cons}),
   165 (*       Rule.Thm ("mem_Cons",TermC.num_str @{thm mem_Cons}),
   166        Rule.Thm ("mem_Nil",TermC.num_str @{thm mem_Nil}), *)
   167 (*       Rule.Thm ("null_Cons",TermC.num_str @{thm null_Cons}),
   168        Rule.Thm ("null_Nil",TermC.num_str @{thm null_Nil}),*)
   169        Rule.Thm ("remdups_Cons",TermC.num_str @{thm remdups_Cons}),
   170        Rule.Thm ("remdups_Nil",TermC.num_str @{thm remdups_Nil}),
   171        Rule.Thm ("rev_Cons",TermC.num_str @{thm rev_Cons}),
   172        Rule.Thm ("rev_Nil",TermC.num_str @{thm rev_Nil}),
   173        Rule.Thm ("take_Nil",TermC.num_str @{thm take_Nil}),
   174        Rule.Thm ("take_Cons",TermC.num_str @{thm take_Cons}),
   175        Rule.Thm ("tl_Cons",TermC.num_str @{thm tl_Cons}),
   176        Rule.Thm ("tl_Nil",TermC.num_str @{thm tl_Nil}),
   177        Rule.Thm ("zip_Cons",TermC.num_str @{thm zip_Cons}),
   178        Rule.Thm ("zip_Nil",TermC.num_str @{thm zip_Nil})],
   179     scr = Rule.EmptyScr}: Rule.rls;
   180 \<close>
   181 setup \<open>KEStore_Elems.add_rlss [("list_rls", (Context.theory_name @{theory}, list_rls))]\<close>
   182 
   183 end