src/HOL/Matrix/codegen_prep.ML
author obua
Fri, 03 Sep 2004 17:10:36 +0200
changeset 15178 5f621aa35c25
child 15531 08c8dad8e399
permissions -rw-r--r--
Matrix theory, linear programming
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15178
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
     1
structure codegen_prep = 
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
     2
struct
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
     3
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
     4
exception Prepare_thms of string;
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
     5
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
     6
fun is_meta_eq th =
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
     7
    let
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
     8
	fun check ((Const ("==", _)) $ _ $ _) = true
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
     9
	  | check _ = false
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    10
    in
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    11
	check (concl_of th)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    12
    end
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    13
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    14
fun printty ty = 
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    15
    let
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    16
	fun immerse s [] = []
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    17
	  | immerse s [x] = [x]
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    18
	  | immerse s (x::xs) = x::s::(immerse s xs)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    19
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    20
	fun py t = 
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    21
	    let
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    22
		val (name, args) =  dest_Type t
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    23
		val args' = map printty args
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    24
	    in
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    25
		concat (immerse "_" (name::args'))
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    26
	    end
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    27
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    28
	val (args, res) = strip_type ty
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    29
	val tystrs = map py (args@[res])
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    30
			  
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    31
	val s = "'"^(concat (immerse "_" tystrs))^"'"
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    32
    in
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    33
	s
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    34
    end
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    35
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    36
fun head_name th = 
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    37
    let val s = 
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    38
	    let
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    39
		val h = fst (strip_comb (hd (snd (strip_comb (concl_of th)))))
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    40
		val n = fst (dest_Const h)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    41
		val ty = snd (dest_Const h)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    42
		val hn = fst (dest_Const h)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    43
	    in
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    44
		hn^"_"^(printty ty) handle _ => (writeln ("warning: polymorphic "^hn); hn)	
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    45
	    end
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    46
    in
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    47
	s
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    48
    end
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    49
	    
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    50
val mangle_id  = 
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    51
    let
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    52
	fun tr #"=" = "_eq_"
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    53
	  | tr #"." = "_"
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    54
	  | tr #" " = "_"
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    55
	  | tr #"<" = "_le_"
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    56
	  | tr #">" = "_ge_"
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    57
	  | tr #"(" = "_bro_"
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    58
	  | tr #")" = "_brc_"
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    59
	  | tr #"+" = "_plus_"
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    60
	  | tr #"*" = "_mult_"
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    61
	  | tr #"-" = "_minus_"
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    62
	  | tr #"|" = "_or_"
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    63
	  | tr #"&" = "_and_"
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    64
	  | tr x = Char.toString x
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    65
	fun m s = "simp_"^(String.translate tr s)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    66
    in
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    67
	m
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    68
    end
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    69
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    70
fun group f [] = []
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    71
  | group f (x::xs) = 
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    72
    let
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    73
	val g = group f xs
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    74
	val key = f x
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    75
    in
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    76
	case assoc (g, key) of
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    77
	    None => (key, [x])::g
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    78
	  | Some l => overwrite (g, (key, x::l))
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    79
    end
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    80
    
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    81
fun prepare_thms ths = 
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    82
    let
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    83
	val ths = (filter is_meta_eq ths) @ (map (standard o mk_meta_eq) (filter (not o is_meta_eq) ths))		      
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    84
	val _ = if forall Thm.no_prems ths then () else raise (Prepare_thms "premisse found")
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    85
	val thmgroups = group head_name ths
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    86
	val test_clashgroups = group (fn (a,b) => mangle_id a) thmgroups
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    87
	val _ = if (length thmgroups <> length test_clashgroups) then raise (Prepare_thms "clash after name mangling") else ()	
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    88
		
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    89
	fun prep (name, ths) = 
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    90
	    let
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    91
		val m = mangle_id name
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    92
			
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    93
	    in
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    94
		(m, true, ths)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    95
	    end
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    96
	    
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    97
	val thmgroups = map prep thmgroups
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    98
    in
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    99
	(thmgroups)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   100
    end
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   101
    
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   102
fun writestr filename s = 
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   103
    let 
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   104
	val f = TextIO.openOut filename
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   105
	val _ = TextIO.output(f, s)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   106
	val _ = TextIO.closeOut f
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   107
    in 
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   108
	() 
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   109
    end
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   110
end