datatype.ML
author nipkow
Wed, 08 Feb 1995 14:06:37 +0100
changeset 211 9b403e123c1b
parent 186 6be2f3e03786
child 215 5f9d7ed4ea0c
permissions -rw-r--r--
Norbert's update allowing nested calls in primrec.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
175
3b1e8c22a44e moved parser stuff to thy_syntax.ML;
wenzelm
parents: 147
diff changeset
     1
(* Title:       HOL/datatype.ML
3b1e8c22a44e moved parser stuff to thy_syntax.ML;
wenzelm
parents: 147
diff changeset
     2
   ID:          $Id$
3b1e8c22a44e moved parser stuff to thy_syntax.ML;
wenzelm
parents: 147
diff changeset
     3
   Author:      Max Breitling, Carsten Clasohm, Tobias Nipkow, Norbert Voelker
211
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
     4
   Copyright 1995 TU Muenchen
129
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
     5
*)
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
     6
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
     7
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
     8
(*used for constructor parameters*)
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
     9
datatype dt_type = dtVar of string |
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    10
  dtTyp of dt_type list * string |
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    11
  dtRek of dt_type list * string;
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    12
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    13
local 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    14
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    15
val mysort = sort;
186
6be2f3e03786 replaced ["term"] by termS;
wenzelm
parents: 175
diff changeset
    16
open ThyParse HOLogic;
129
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    17
exception Impossible;
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    18
exception RecError of string;
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    19
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    20
val is_dtRek = (fn dtRek _ => true  |  _  => false);
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    21
fun opt_parens s = if s = "" then "" else enclose "(" ")" s; 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    22
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    23
(* ----------------------------------------------------------------------- *)
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    24
(* Derivation of the primrec combinator application from the equations     *)
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    25
211
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
    26
(* substitute fname(ls,xk,rs) by yk(ls,rs) in t for (xk,yk) in pairs  *) 
129
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    27
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    28
fun subst_apps (_,_) [] t = t
211
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
    29
  | subst_apps (fname,rpos) pairs t =
129
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    30
    let 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    31
    fun subst (Abs(a,T,t)) = Abs(a,T,subst t)
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    32
      | subst (funct $ body) = 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    33
	let val (f,b) = strip_comb (funct$body)
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    34
	in 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    35
	  if is_Const f andalso fst(dest_Const f) = fname 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    36
	    then 
211
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
    37
	      let val (ls,rest) = (take(rpos,b), drop(rpos,b));
129
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    38
		val (xk,rs) = (hd rest,tl rest)
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    39
		  handle LIST _ => raise RecError "not enough arguments \
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    40
		   \ in recursive application on rhs"
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    41
              in 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    42
		(case assoc (pairs,xk) of 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    43
		   None => raise RecError 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    44
		     ("illegal occurence of " ^ fname ^ " on rhs")
211
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
    45
		 | Some(U) => list_comb(U,map subst (ls @ rs)))
129
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    46
	      end
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    47
	  else list_comb(f, map subst b)
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    48
	end
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    49
      | subst(t) = t
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    50
    in subst t end;
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    51
  
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    52
(* abstract rhs *)
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    53
211
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
    54
fun abst_rec (fname,rpos,tc,ls,cargs,rs,rhs) =       
129
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    55
  let val rargs = (map fst o 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    56
		   (filter (fn (a,T) => is_dtRek T))) (cargs ~~ tc);
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    57
      val subs = map (fn (s,T) => (s,dummyT))
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    58
	           (rev(rename_wrt_term rhs rargs));
211
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
    59
      val subst_rhs = subst_apps (fname,rpos)
129
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    60
	                (map Free rargs ~~ map Free subs) rhs;
211
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
    61
  in 
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
    62
      list_abs_free (cargs @ subs @ ls @ rs, subst_rhs) 
129
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    63
  end;
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    64
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    65
(* parsing the prim rec equations *)
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    66
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    67
fun dest_eq ( Const("Trueprop",_) $ (Const ("op =",_) $ lhs $ rhs))
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    68
                 = (lhs, rhs)
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    69
   | dest_eq _ = raise RecError "not a proper equation"; 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    70
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    71
fun dest_rec eq = 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    72
  let val (lhs,rhs) = dest_eq eq; 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    73
    val (name,args) = strip_comb lhs; 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    74
    val (ls',rest)  = take_prefix is_Free args; 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    75
    val (middle,rs') = take_suffix is_Free rest;
211
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
    76
    val rpos = length ls';
129
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    77
    val (c,cargs') = strip_comb (hd middle)
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    78
      handle LIST "hd" => raise RecError "constructor missing";
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    79
    val (ls,cargs,rs) = (map dest_Free ls', map dest_Free cargs'
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    80
			 , map dest_Free rs')
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    81
      handle TERM ("dest_Free",_) => 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    82
	  raise RecError "constructor has illegal argument in pattern";
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    83
  in 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    84
    if length middle > 1 then 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    85
      raise RecError "more than one non-variable in pattern"
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    86
    else if not(null(findrep (map fst (ls @ rs @ cargs)))) then 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    87
      raise RecError "repeated variable name in pattern" 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    88
	 else (fst(dest_Const name) handle TERM _ => 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    89
	       raise RecError "function is not declared as constant in theory"
211
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
    90
		 ,rpos,ls,fst( dest_Const c),cargs,rs,rhs)
129
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    91
  end; 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    92
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    93
(* check function specified for all constructors and sort function terms *)
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    94
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    95
fun check_and_sort (n,its) = 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    96
  if length its = n 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    97
    then map snd (mysort (fn ((i : int,_),(j,_)) => i<j) its)
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    98
  else raise error "Primrec definition error:\n\
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
    99
   \Please give an equation for every constructor";
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   100
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   101
(* translate rec equations into function arguments suitable for rec comb *)
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   102
(* theory parameter needed for printing error messages                   *) 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   103
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   104
fun trans_recs _ _ [] = error("No primrec equations.")
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   105
  | trans_recs thy cs' (eq1::eqs) = 
211
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   106
    let val (name1,rpos1,ls1,_,_,_,_) = dest_rec eq1
129
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   107
      handle RecError s =>
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   108
	error("Primrec definition error: " ^ s ^ ":\n" 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   109
	      ^ "   " ^ Sign.string_of_term (sign_of thy) eq1);
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   110
      val tcs = map (fn (_,c,T,_) => (c,T)) cs';  
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   111
      val cs = map fst tcs;
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   112
      fun trans_recs' _ [] = []
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   113
        | trans_recs' cis (eq::eqs) = 
211
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   114
	  let val (name,rpos,ls,c,cargs,rs,rhs) = dest_rec eq; 
129
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   115
	    val tc = assoc(tcs,c);
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   116
	    val i = (1 + find (c,cs))  handle LIST "find" => 0; 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   117
	  in
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   118
	  if name <> name1 then 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   119
	    raise RecError "function names inconsistent"
211
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   120
	  else if rpos <> rpos1 then 
129
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   121
	    raise RecError "position of rec. argument inconsistent"
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   122
	  else if i = 0 then 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   123
	    raise RecError "illegal argument in pattern" 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   124
	  else if i mem cis then
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   125
	    raise RecError "constructor already occured as pattern "
211
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   126
	       else (i,abst_rec (name,rpos,the tc,ls,cargs,rs,rhs))
129
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   127
		     :: trans_recs' (i::cis) eqs 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   128
	  end
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   129
	  handle RecError s =>
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   130
	        error("Primrec definition error\n" ^ s ^ "\n" 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   131
		      ^ "   " ^ Sign.string_of_term (sign_of thy) eq);
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   132
    in (  name1, ls1
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   133
	, check_and_sort (length cs, trans_recs' [] (eq1::eqs)))
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   134
    end ;
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   135
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   136
in
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   137
  fun add_datatype (typevars, tname, cons_list') thy = 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   138
    let (*search for free type variables and convert recursive *)
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   139
      fun analyse_types (cons, typlist, syn) =
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   140
	let fun analyse(t as dtVar v) =
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   141
	  if t mem typevars then t
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   142
	  else error ("Free type variable " ^ v ^ " on rhs.")
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   143
	      | analyse(dtTyp(typl,s)) =
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   144
		if tname <> s then dtTyp(analyses typl, s)
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   145
		else if typevars = typl then dtRek(typl, s)
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   146
                     else error (s ^ " used in different ways")
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   147
	      | analyse(dtRek _) = raise Impossible
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   148
	    and analyses ts = map analyse ts;
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   149
	in (cons, Syntax.const_name cons syn, analyses typlist, syn) 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   150
	end;
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   151
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   152
     (*test if all elements are recursive, i.e. if the type is empty*)
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   153
      
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   154
      fun non_empty (cs : ('a * 'b * dt_type list * 'c) list) = 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   155
	not(forall (exists is_dtRek o #3) cs) orelse
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   156
	error("Empty datatype not allowed!");
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   157
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   158
      val cons_list = map analyse_types cons_list';
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   159
      val dummy = non_empty cons_list;
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   160
      val num_of_cons = length cons_list;
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   161
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   162
     (* Auxiliary functions to construct argument and equation lists *)
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   163
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   164
     (*generate 'var_n, ..., var_m'*)
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   165
      fun Args(var, delim, n, m) = 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   166
	space_implode delim (map (fn n => var^string_of_int(n)) (n upto m));
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   167
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   168
     (*generate 'name_1', ..., 'name_n'*)
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   169
      fun C_exp(name, n, var) =
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   170
        if n > 0 then name ^ parens(Args(var, ",", 1, n)) else name;
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   171
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   172
     (*generate 'x_n = y_n, ..., x_m = y_m'*)
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   173
      fun Arg_eql(n,m) = 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   174
        if n=m then "x" ^ string_of_int(n) ^ "=y" ^ string_of_int(n) 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   175
        else "x" ^ string_of_int(n) ^ "=y" ^ string_of_int(n) ^ " & " ^ 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   176
	  Arg_eql(n+1, m);
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   177
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   178
     (*Pretty printers for type lists;
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   179
       pp_typlist1: parentheses, pp_typlist2: brackets*)
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   180
      fun pp_typ (dtVar s) = s
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   181
        | pp_typ (dtTyp (typvars, id)) =
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   182
	  if null typvars then id else (pp_typlist1 typvars) ^ id
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   183
        | pp_typ (dtRek (typvars, id)) = (pp_typlist1 typvars) ^ id
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   184
      and
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   185
	pp_typlist' ts = commas (map pp_typ ts)
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   186
      and
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   187
	pp_typlist1 ts = if null ts then "" else parens (pp_typlist' ts);
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   188
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   189
      fun pp_typlist2 ts = if null ts then "" else brackets (pp_typlist' ts);
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   190
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   191
     (* Generate syntax translation for case rules *)
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   192
      fun calc_xrules c_nr y_nr ((_, name, typlist, _) :: cs) = 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   193
	let val arity = length typlist;
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   194
	  val body  = "z" ^ string_of_int(c_nr);
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   195
	  val args1 = if arity=0 then ""
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   196
		      else parens (Args ("y", ",", y_nr, y_nr+arity-1));
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   197
	  val args2 = if arity=0 then ""
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   198
		      else "% " ^ Args ("y", " ", y_nr, y_nr+arity-1) 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   199
			^ ". ";
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   200
	  val (rest1,rest2) = 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   201
	    if null cs then ("","")
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   202
	    else let val (h1, h2) = calc_xrules (c_nr+1) (y_nr+arity) cs
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   203
	    in (" | " ^ h1, ", " ^ h2) end;
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   204
	in (name ^ args1 ^ " => " ^ body ^ rest1, args2 ^ body ^ rest2) end
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   205
        | calc_xrules _ _ [] = raise Impossible;
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   206
      
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   207
      val xrules =
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   208
	let val (first_part, scnd_part) = calc_xrules 1 1 cons_list
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   209
	in  [("logic", "case x of " ^ first_part) <->
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   210
	     ("logic", tname ^ "_case(" ^ scnd_part ^ ", x)" )]
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   211
	end;
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   212
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   213
     (*type declarations for constructors*)
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   214
      fun const_type (id, _, typlist, syn) =
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   215
	(id,  
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   216
	 (if null typlist then "" else pp_typlist2 typlist ^ " => ") ^
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   217
	    pp_typlist1 typevars ^ tname, syn);
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   218
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   219
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   220
      fun assumpt (dtRek _ :: ts, v :: vs ,found) =
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   221
	let val h = if found then ";P(" ^ v ^ ")" else "[| P(" ^ v ^ ")"
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   222
	in h ^ (assumpt (ts, vs, true)) end
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   223
        | assumpt (t :: ts, v :: vs, found) = assumpt (ts, vs, found)
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   224
      | assumpt ([], [], found) = if found then "|] ==>" else ""
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   225
        | assumpt _ = raise Impossible;
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   226
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   227
     (*insert type with suggested name 'varname' into table*)
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   228
      fun insert typ varname ((tri as (t, s, n)) :: xs) = 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   229
	if typ = t then (t, s, n+1) :: xs
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   230
	else tri :: (if varname = s then insert typ (varname ^ "'") xs
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   231
		     else insert typ varname xs)
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   232
        | insert typ varname [] = [(typ, varname, 1)];
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   233
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   234
      fun typid(dtRek(_,id)) = id
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   235
        | typid(dtVar s) = implode (tl (explode s))
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   236
        | typid(dtTyp(_,id)) = id;
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   237
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   238
      val insert_types = foldl (fn (tab,typ) => insert typ (typid typ) tab);
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   239
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   240
      fun update(dtRek _, s, v :: vs, (dtRek _) :: ts) = s :: vs
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   241
        | update(t, s, v :: vs, t1 :: ts) = 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   242
	  if t=t1 then s :: vs else v :: (update (t, s, vs, ts))
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   243
        | update _ = raise Impossible;
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   244
      
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   245
      fun update_n (dtRek r1, s, v :: vs, (dtRek r2) :: ts, n) =
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   246
	if r1 = r2 then (s ^ string_of_int n) :: 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   247
	  (update_n (dtRek r1, s, vs, ts, n+1))
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   248
	else v :: (update_n (dtRek r1, s, vs, ts, n))
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   249
        | update_n (t, s, v :: vs, t1 :: ts, n) = 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   250
	  if t = t1 then (s ^ string_of_int n) :: 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   251
	    (update_n (t, s, vs, ts, n+1))
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   252
	  else v :: (update_n (t, s, vs, ts, n))
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   253
        | update_n (_,_,[],[],_) = []
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   254
        | update_n _ = raise Impossible;
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   255
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   256
     (*insert type variables into table*)
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   257
      fun convert typs =
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   258
        let fun conv(vars, (t, s, n)) =
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   259
	  if n=1 then update (t, s, vars, typs)
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   260
	  else update_n (t, s, vars, typs, 1)
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   261
        in foldl conv 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   262
	end;
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   263
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   264
      fun empty_list n = replicate n "";
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   265
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   266
      fun t_inducting ((_, name, typl, _) :: cs) =
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   267
	let val tab = insert_types([],typl);
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   268
	  val arity = length typl;
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   269
	  val var_list = convert typl (empty_list arity,tab); 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   270
	  val h = if arity = 0 then " P(" ^ name ^ ")"
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   271
		  else " !!" ^ (space_implode " " var_list) ^ "." ^
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   272
		    (assumpt (typl, var_list, false)) ^ "P(" ^ 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   273
		    name ^ "(" ^ (commas var_list) ^ "))";
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   274
	  val rest = t_inducting cs;
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   275
	in if rest = "" then h else h ^ "; " ^ rest end
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   276
        | t_inducting [] = "";
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   277
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   278
      fun t_induct cl typ_name =
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   279
        "[|" ^ t_inducting cl ^ "|] ==> P(" ^ typ_name ^ ")";
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   280
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   281
      fun gen_typlist typevar f ((_, _, ts, _) :: cs) =
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   282
	let val h = if (length ts) > 0
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   283
		      then pp_typlist2(f ts) ^ "=>"
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   284
		    else ""
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   285
	in h ^ typevar ^  "," ^ (gen_typlist typevar f cs) end
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   286
        | gen_typlist _ _ [] = "";
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   287
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   288
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   289
(* -------------------------------------------------------------------- *)
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   290
(* The case constant and rules 	        				*)
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   291
 		
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   292
      val t_case = tname ^ "_case";
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   293
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   294
      fun case_rule n (id, name, ts, _) =
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   295
	let val args = opt_parens(Args("x", ",", 1, length ts))
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   296
	in (t_case ^ "_" ^ id,
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   297
	    t_case ^ "(" ^ Args("f", ",", 1, num_of_cons)
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   298
	    ^ "," ^ name ^ args 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   299
	    ^ ") = f"  ^ string_of_int(n) ^ args)
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   300
	end
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   301
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   302
      fun case_rules n (c :: cs) = case_rule n c :: case_rules(n+1) cs
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   303
        | case_rules _ [] = [];
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   304
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   305
      val datatype_arity = length typevars;
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   306
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   307
      val types = [(tname, datatype_arity, NoSyn)];
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   308
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   309
      val arities = 
186
6be2f3e03786 replaced ["term"] by termS;
wenzelm
parents: 175
diff changeset
   310
        let val term_list = replicate datatype_arity termS;
6be2f3e03786 replaced ["term"] by termS;
wenzelm
parents: 175
diff changeset
   311
        in [(tname, term_list, termS)] 
129
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   312
	end;
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   313
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   314
      val datatype_name = pp_typlist1 typevars ^ tname;
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   315
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   316
      val new_tvar_name = variant (map (fn dtVar s => s) typevars) "'z";
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   317
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   318
      val case_const =
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   319
	(t_case,
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   320
	 "[" ^ gen_typlist new_tvar_name I cons_list 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   321
	 ^  pp_typlist1 typevars ^ tname ^ "] =>" ^ new_tvar_name,
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   322
	 NoSyn);
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   323
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   324
      val rules_case = case_rules 1 cons_list;
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   325
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   326
(* -------------------------------------------------------------------- *)
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   327
(* The prim-rec combinator						*) 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   328
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   329
      val t_rec = tname ^ "_rec"
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   330
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   331
(* adding type variables for dtRek types to end of list of dt_types      *)   
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   332
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   333
      fun add_reks ts = 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   334
	ts @ map (fn _ => dtVar new_tvar_name) (filter is_dtRek ts); 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   335
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   336
(* positions of the dtRek types in a list of dt_types, starting from 1  *)
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   337
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   338
      fun rek_pos ts = 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   339
	map snd (filter (is_dtRek o fst) (ts ~~ (1 upto length ts)))
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   340
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   341
      fun rec_rule n (id,name,ts,_) = 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   342
	let val args = Args("x",",",1,length ts)
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   343
	  val fargs = Args("f",",",1,num_of_cons)
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   344
	  fun rarg i = "," ^ t_rec ^ parens(fargs ^ "," ^ "x" ^ 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   345
					    string_of_int(i)) 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   346
	  val rargs = implode (map rarg (rek_pos ts)) 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   347
	in     
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   348
	  ( t_rec ^ "_" ^ id
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   349
	   , t_rec ^ parens(fargs ^  "," ^ name ^ (opt_parens args)) ^ " = f"
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   350
	   ^ string_of_int(n) ^ opt_parens (args ^ rargs)) 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   351
	end
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   352
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   353
      fun rec_rules n (c::cs) = rec_rule n c :: rec_rules (n+1) cs 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   354
	| rec_rules _ [] = [];
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   355
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   356
      val rec_const =
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   357
	(t_rec,
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   358
	 "[" ^ (gen_typlist new_tvar_name add_reks cons_list) 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   359
	 ^ (pp_typlist1 typevars) ^ tname ^ "] =>" ^ new_tvar_name,
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   360
	 NoSyn);
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   361
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   362
      val rules_rec = rec_rules 1 cons_list
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   363
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   364
(* -------------------------------------------------------------------- *)
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   365
      val consts = 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   366
	map const_type cons_list
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   367
	@ (if num_of_cons < dtK then []
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   368
	   else [(tname ^ "_ord", datatype_name ^ "=>nat", NoSyn)])
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   369
	@ [case_const,rec_const];
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   370
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   371
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   372
      fun Ci_ing ((id, name, typlist, _) :: cs) =
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   373
	let val arity = length typlist;
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   374
	in if arity = 0 then Ci_ing cs
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   375
	   else ("inject_" ^ id,
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   376
		 "(" ^ C_exp(name,arity,"x") ^ "=" ^ C_exp(name,arity,"y") 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   377
		 ^ ") = (" ^ Arg_eql (1, arity) ^ ")") :: (Ci_ing cs)
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   378
	end
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   379
	| Ci_ing [] = [];
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   380
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   381
      fun Ci_negOne (id1, name1, tl1, _) (id2, name2, tl2, _) =
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   382
	let val ax = C_exp(name1, length tl1, "x") ^ "~=" ^
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   383
	  C_exp(name2, length tl2, "y")
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   384
	in (id1 ^ "_not_" ^ id2, ax) 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   385
	end;
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   386
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   387
      fun Ci_neg1 [] = []
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   388
	| Ci_neg1 (c1::cs) = (map (Ci_negOne c1) cs) @ Ci_neg1 cs;
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   389
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   390
      fun suc_expr n = 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   391
	if n=0 then "0" else "Suc(" ^ suc_expr(n-1) ^ ")";
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   392
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   393
      fun Ci_neg2() =
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   394
	let val ord_t = tname ^ "_ord";
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   395
	  val cis = cons_list ~~ (0 upto (num_of_cons - 1))
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   396
	  fun Ci_neg2equals ((id, name, typlist, _), n) =
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   397
	    let val ax = ord_t ^ "(" ^ (C_exp(name, length typlist, "x")) 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   398
	      ^ ") = " ^ (suc_expr n)
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   399
	    in (ord_t ^ "_" ^ id, ax) end
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   400
	in (ord_t ^ "_distinct", ord_t^"(x) ~= "^ord_t^"(y) ==> x ~= y") ::
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   401
	  (map Ci_neg2equals cis)
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   402
	end;
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   403
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   404
      val rules_distinct = if num_of_cons < dtK then Ci_neg1 cons_list
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   405
			   else Ci_neg2();
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   406
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   407
      val rules_inject = Ci_ing cons_list;
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   408
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   409
      val rule_induct = (tname ^ "_induct", t_induct cons_list tname);
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   410
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   411
      val rules = rule_induct ::
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   412
	(rules_inject @ rules_distinct @ rules_case @ rules_rec);
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   413
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   414
      fun add_primrec eqns thy =
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   415
	let val rec_comb = Const(t_rec,dummyT)
143
3226f25f88e7 improved error reporting for primrec
nipkow
parents: 142
diff changeset
   416
	  val teqns = map (fn neq => snd(read_axm (sign_of thy) neq)) eqns
129
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   417
	  val (fname,ls,fns) = trans_recs thy cons_list teqns
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   418
	  val rhs = 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   419
	    list_abs_free
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   420
	    (ls @ [(tname,dummyT)]
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   421
	     ,list_comb(rec_comb
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   422
			, fns @ map Bound (0 ::(length ls downto 1))));
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   423
          val sg = sign_of thy;
147
8ff6c8c95870 Small simplification in add_datatype.
nipkow
parents: 146
diff changeset
   424
          val defpair =  mk_defpair (Const(fname,dummyT),rhs)
8ff6c8c95870 Small simplification in add_datatype.
nipkow
parents: 146
diff changeset
   425
	  val defpairT as (_, _ $ Const(_,T) $ _ ) = inferT_axm sg defpair;
129
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   426
	  val varT = Type.varifyT T;
186
6be2f3e03786 replaced ["term"] by termS;
wenzelm
parents: 175
diff changeset
   427
          val ftyp = the (Sign.const_type sg fname);
129
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   428
	in
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   429
	  if Type.typ_instance (#tsig(Sign.rep_sg sg), ftyp, varT)
147
8ff6c8c95870 Small simplification in add_datatype.
nipkow
parents: 146
diff changeset
   430
	  then add_defs_i [defpairT] thy
129
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   431
	  else error("Primrec definition error: \ntype of " ^ fname 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   432
		     ^ " is not instance of type deduced from equations")
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   433
	end;
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   434
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   435
    in 
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   436
      (thy
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   437
      |> add_types types
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   438
      |> add_arities arities
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   439
      |> add_consts consts
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   440
      |> add_trrules xrules
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   441
      |> add_axioms rules,add_primrec)
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   442
    end
0bba840aa07c New version of datatype.ML with primrec (Norbert).
nipkow
parents:
diff changeset
   443
end
211
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   444
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   445
(*
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   446
Informal description of functions used in datatype.ML for the Isabelle/HOL
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   447
implementation of prim. rec. function definitions. (N. Voelker, Feb. 1995) 
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   448
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   449
* subst_apps (fname,rpos) pairs t:
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   450
   substitute the term 
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   451
       fname(ls,xk,rs) 
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   452
   by 
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   453
      yk(ls,rs) 
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   454
   in t for (xk,yk) in pairs, where rpos = length ls. 
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   455
   Applied with : 
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   456
     fname = function name 
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   457
     rpos = position of recursive argument 
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   458
     pairs = list of pairs (xk,yk), where 
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   459
          xk are the rec. arguments of the constructor in the pattern,
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   460
          yk is a variable with name derived from xk 
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   461
     t = rhs of equation 
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   462
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   463
* abst_rec (fname,rpos,tc,ls,cargs,rs,rhs)
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   464
  - filter recursive arguments from constructor arguments cargs,
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   465
  - perform substitutions on rhs, 
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   466
  - derive list subs of new variable names yk for use in subst_apps, 
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   467
  - abstract rhs with respect to cargs, subs, ls and rs. 
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   468
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   469
* dest_eq t 
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   470
  destruct a term denoting an equation into lhs and rhs. 
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   471
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   472
* dest_req eq 
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   473
  destruct an equation of the form 
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   474
      name (vl1..vlrpos, Ci(vi1..vin), vr1..vrn) = rhs
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   475
  into 
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   476
  - function name  (name) 
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   477
  - position of the first non-variable parameter  (rpos)
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   478
  - the list of first rpos parameters (ls = [vl1..vlrpos]) 
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   479
  - the constructor (fst( dest_Const c) = Ci)
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   480
  - the arguments of the constructor (cargs = [vi1..vin])
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   481
  - the rest of the variables in the pattern (rs = [vr1..vrn])
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   482
  - the right hand side of the equation (rhs).  
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   483
 
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   484
* check_and_sort (n,its)
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   485
  check that  n = length its holds, and sort elements of its by 
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   486
  first component. 
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   487
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   488
* trans_recs thy cs' (eq1::eqs)
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   489
  destruct eq1 into name1, rpos1, ls1, etc.. 
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   490
  get constructor list with and without type (tcs resp. cs) from cs',  
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   491
  for every equation:  
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   492
    destruct it into (name,rpos,ls,c,cargs,rs,rhs)
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   493
    get typed constructor tc from c and tcs 
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   494
    determine the index i of the constructor 
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   495
    check function name and position of rec. argument by comparison
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   496
    with first equation 
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   497
    check for repeated variable names in pattern
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   498
    derive function term f_i which is used as argument of the rec. combinator
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   499
    sort the terms f_i according to i and return them together
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   500
      with the function name and the parameter of the definition (ls). 
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   501
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   502
* Application:
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   503
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   504
  The rec. combinator is applied to the function terms resulting from
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   505
  trans_rec. This results in a function which takes the recursive arg. 
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   506
  as first parameter and then the arguments corresponding to ls. The
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   507
  order of parameters is corrected by setting the rhs equal to 
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   508
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   509
  list_abs_free
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   510
	    (ls @ [(tname,dummyT)]
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   511
	     ,list_comb(rec_comb
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   512
			, fns @ map Bound (0 ::(length ls downto 1))));
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   513
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   514
  Note the de-Bruijn indices counting the number of lambdas between the
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   515
  variable and its binding. 
9b403e123c1b Norbert's update allowing nested calls in primrec.
nipkow
parents: 186
diff changeset
   516
*)