src/Tools/Compute_Oracle/am_sml.ML
changeset 32952 aeb1e44fbc19
parent 32740 9dd0a2f83429
child 32960 69916a850301
equal deleted inserted replaced
32951:83392f9d303f 32952:aeb1e44fbc19
   204 	     in
   204 	     in
   205 		 if a <= len then 
   205 		 if a <= len then 
   206 		     let
   206 		     let
   207 			 val strict_a = (case toplevel_arity_of c of SOME sa => sa | NONE => a)
   207 			 val strict_a = (case toplevel_arity_of c of SOME sa => sa | NONE => a)
   208 			 val _ = if strict_a > a then raise Compile "strict" else ()
   208 			 val _ = if strict_a > a then raise Compile "strict" else ()
   209 			 val s = module_prefix^"c"^(str c)^(concat (map (fn t => " "^(protect_blank (print_term d t))) (List.take (args, strict_a))))
   209 			 val s = module_prefix^"c"^(str c)^(implode (map (fn t => " "^(protect_blank (print_term d t))) (List.take (args, strict_a))))
   210 			 val s = s^(concat (map (fn t => " (fn () => "^print_term d t^")") (List.drop (List.take (args, a), strict_a))))
   210 			 val s = s^(implode (map (fn t => " (fn () => "^print_term d t^")") (List.drop (List.take (args, a), strict_a))))
   211 		     in
   211 		     in
   212 			 print_apps d s (List.drop (args, a))
   212 			 print_apps d s (List.drop (args, a))
   213 		     end
   213 		     end
   214 		 else 
   214 		 else 
   215 		     let
   215 		     let
   271 	val c = (case p of PConst (c, _) => c | _ => raise Match)
   271 	val c = (case p of PConst (c, _) => c | _ => raise Match)
   272 	val (n, pattern) = print_pattern true 0 p
   272 	val (n, pattern) = print_pattern true 0 p
   273 	val lazy_vars = the (arity_of c) - the (toplevel_arity_of c)
   273 	val lazy_vars = the (arity_of c) - the (toplevel_arity_of c)
   274 	fun print_tm tm = print_term NONE arity_of toplevel_arity_of n lazy_vars tm
   274 	fun print_tm tm = print_term NONE arity_of toplevel_arity_of n lazy_vars tm
   275         fun print_guard (Guard (a,b)) = "term_eq ("^(print_tm a)^") ("^(print_tm b)^")"
   275         fun print_guard (Guard (a,b)) = "term_eq ("^(print_tm a)^") ("^(print_tm b)^")"
   276 	val else_branch = "c"^(str c)^"_"^(str (gnum+1))^(concat (map (fn i => " a"^(str i)) (section (the (arity_of c)))))
   276 	val else_branch = "c"^(str c)^"_"^(str (gnum+1))^(implode (map (fn i => " a"^(str i)) (section (the (arity_of c)))))
   277 	fun print_guards t [] = print_tm t
   277 	fun print_guards t [] = print_tm t
   278 	  | print_guards t (g::gs) = "if ("^(print_guard g)^")"^(concat (map (fn g => " andalso ("^(print_guard g)^")") gs))^" then ("^(print_tm t)^") else "^else_branch
   278 	  | print_guards t (g::gs) = "if ("^(print_guard g)^")"^(implode (map (fn g => " andalso ("^(print_guard g)^")") gs))^" then ("^(print_tm t)^") else "^else_branch
   279     in
   279     in
   280 	(if null guards then gnum else gnum+1, pattern^" = "^(print_guards t guards))
   280 	(if null guards then gnum else gnum+1, pattern^" = "^(print_guards t guards))
   281     end
   281     end
   282 
   282 
   283 fun group_rules rules =
   283 fun group_rules rules =
   305 	val (arity, toplevel_arity, rules) = adjust_rules rules
   305 	val (arity, toplevel_arity, rules) = adjust_rules rules
   306 	val rules = group_rules rules
   306 	val rules = group_rules rules
   307 	val constants = Inttab.keys arity
   307 	val constants = Inttab.keys arity
   308 	fun arity_of c = Inttab.lookup arity c
   308 	fun arity_of c = Inttab.lookup arity c
   309 	fun toplevel_arity_of c = Inttab.lookup toplevel_arity c
   309 	fun toplevel_arity_of c = Inttab.lookup toplevel_arity c
   310 	fun rep_str s n = concat (rep n s)
   310 	fun rep_str s n = implode (rep n s)
   311 	fun indexed s n = s^(str n)
   311 	fun indexed s n = s^(str n)
   312         fun string_of_tuple [] = ""
   312         fun string_of_tuple [] = ""
   313 	  | string_of_tuple (x::xs) = "("^x^(concat (map (fn s => ", "^s) xs))^")"
   313 	  | string_of_tuple (x::xs) = "("^x^(implode (map (fn s => ", "^s) xs))^")"
   314         fun string_of_args [] = ""
   314         fun string_of_args [] = ""
   315 	  | string_of_args (x::xs) = x^(concat (map (fn s => " "^s) xs))
   315 	  | string_of_args (x::xs) = x^(implode (map (fn s => " "^s) xs))
   316 	fun default_case gnum c = 
   316 	fun default_case gnum c = 
   317 	    let
   317 	    let
   318 		val leftargs = concat (map (indexed " x") (section (the (arity_of c))))
   318 		val leftargs = implode (map (indexed " x") (section (the (arity_of c))))
   319 		val rightargs = section (the (arity_of c))
   319 		val rightargs = section (the (arity_of c))
   320 		val strict_args = (case toplevel_arity_of c of NONE => the (arity_of c) | SOME sa => sa)
   320 		val strict_args = (case toplevel_arity_of c of NONE => the (arity_of c) | SOME sa => sa)
   321 		val xs = map (fn n => if n < strict_args then "x"^(str n) else "x"^(str n)^"()") rightargs
   321 		val xs = map (fn n => if n < strict_args then "x"^(str n) else "x"^(str n)^"()") rightargs
   322 		val right = (indexed "C" c)^" "^(string_of_tuple xs)
   322 		val right = (indexed "C" c)^" "^(string_of_tuple xs)
   323 		val message = "(\"unresolved lazy call: " ^ string_of_int c ^ "\")"
   323 		val message = "(\"unresolved lazy call: " ^ string_of_int c ^ "\")"
   372 	fun mk_constr_type_args n = if n > 0 then " of Term "^(rep_str " * Term" (n-1)) else ""
   372 	fun mk_constr_type_args n = if n > 0 then " of Term "^(rep_str " * Term" (n-1)) else ""
   373 	val _ = writelist [                   
   373 	val _ = writelist [                   
   374 		"structure "^name^" = struct",
   374 		"structure "^name^" = struct",
   375 		"",
   375 		"",
   376 		"datatype Term = Const of int | App of Term * Term | Abs of (Term -> Term)",
   376 		"datatype Term = Const of int | App of Term * Term | Abs of (Term -> Term)",
   377 		"         "^(concat (map (fn c => " | C"^(str c)^(mk_constr_type_args (the (arity_of c)))) constants)),
   377 		"         "^(implode (map (fn c => " | C"^(str c)^(mk_constr_type_args (the (arity_of c)))) constants)),
   378 		""]
   378 		""]
   379 	fun make_constr c argprefix = "(C"^(str c)^" "^(string_of_tuple (map (fn i => argprefix^(str i)) (section (the (arity_of c)))))^")"
   379 	fun make_constr c argprefix = "(C"^(str c)^" "^(string_of_tuple (map (fn i => argprefix^(str i)) (section (the (arity_of c)))))^")"
   380 	fun make_term_eq c = "  | term_eq "^(make_constr c "a")^" "^(make_constr c "b")^" = "^
   380 	fun make_term_eq c = "  | term_eq "^(make_constr c "a")^" "^(make_constr c "b")^" = "^
   381                              (case the (arity_of c) of 
   381                              (case the (arity_of c) of 
   382 				  0 => "true"
   382 				  0 => "true"
   383 				| n => 
   383 				| n => 
   384 				  let 
   384 				  let 
   385 				      val eqs = map (fn i => "term_eq a"^(str i)^" b"^(str i)) (section n)
   385 				      val eqs = map (fn i => "term_eq a"^(str i)^" b"^(str i)) (section n)
   386 				      val (eq, eqs) = (List.hd eqs, map (fn s => " andalso "^s) (List.tl eqs))
   386 				      val (eq, eqs) = (List.hd eqs, map (fn s => " andalso "^s) (List.tl eqs))
   387 				  in
   387 				  in
   388 				      eq^(concat eqs)
   388 				      eq^(implode eqs)
   389 				  end)
   389 				  end)
   390 	val _ = writelist [
   390 	val _ = writelist [
   391 		"fun term_eq (Const c1) (Const c2) = (c1 = c2)",
   391 		"fun term_eq (Const c1) (Const c2) = (c1 = c2)",
   392 		"  | term_eq (App (a1,a2)) (App (b1,b2)) = term_eq a1 b1 andalso term_eq a2 b2"]
   392 		"  | term_eq (App (a1,a2)) (App (b1,b2)) = term_eq a1 b1 andalso term_eq a2 b2"]
   393 	val _ = writelist (map make_term_eq constants)		
   393 	val _ = writelist (map make_term_eq constants)		
   419 						in 
   419 						in 
   420 						    if gnum' = gnum then 
   420 						    if gnum' = gnum then 
   421 							(gnum, r::l)::rs
   421 							(gnum, r::l)::rs
   422 						    else
   422 						    else
   423 							let
   423 							let
   424 							    val args = concat (map (fn i => " a"^(str i)) (section (the (arity_of c))))
   424 							    val args = implode (map (fn i => " a"^(str i)) (section (the (arity_of c))))
   425 							    fun gnumc g = if g > 0 then "c"^(str c)^"_"^(str g)^args else "c"^(str c)^args
   425 							    fun gnumc g = if g > 0 then "c"^(str c)^"_"^(str g)^args else "c"^(str c)^args
   426 							    val s = gnumc (gnum) ^ " = " ^ gnumc (gnum') 
   426 							    val s = gnumc (gnum) ^ " = " ^ gnumc (gnum') 
   427 							in
   427 							in
   428 							    (gnum', [])::(gnum, s::r::l)::rs
   428 							    (gnum', [])::(gnum, s::r::l)::rs
   429 							end
   429 							end
   442 	    let
   442 	    let
   443 		val args = map (indexed "a") (section (the (arity_of c)))
   443 		val args = map (indexed "a") (section (the (arity_of c)))
   444 		val leftargs = 
   444 		val leftargs = 
   445 		    case args of
   445 		    case args of
   446 			[] => ""
   446 			[] => ""
   447 		      | (x::xs) => "("^x^(concat (map (fn s => ", "^s) xs))^")"
   447 		      | (x::xs) => "("^x^(implode (map (fn s => ", "^s) xs))^")"
   448 		val args = map (indexed "convert a") (section (the (arity_of c)))
   448 		val args = map (indexed "convert a") (section (the (arity_of c)))
   449 		val right = fold (fn x => fn s => "AM_SML.App ("^s^", "^x^")") args ("AM_SML.Const "^(str c))
   449 		val right = fold (fn x => fn s => "AM_SML.App ("^s^", "^x^")") args ("AM_SML.Const "^(str c))
   450 	    in
   450 	    in
   451 		"  | convert (C"^(str c)^" "^leftargs^") = "^right
   451 		"  | convert (C"^(str c)^" "^leftargs^") = "^right
   452 	    end 		
   452 	    end