src/Tools/Compute_Oracle/am_sml.ML
changeset 25217 3224db6415ae
parent 24654 329f1b4d9d16
child 25220 f22c1fcbc501
equal deleted inserted replaced
25216:eb512c1717ea 25217:3224db6415ae
    31 (*fun list_nth (l,n) = (writeln (makestring ("list_nth", (length l,n))); List.nth (l,n))*)
    31 (*fun list_nth (l,n) = (writeln (makestring ("list_nth", (length l,n))); List.nth (l,n))*)
    32 
    32 
    33 val compiled_rewriter = ref (NONE:(term -> term)Option.option)
    33 val compiled_rewriter = ref (NONE:(term -> term)Option.option)
    34 
    34 
    35 fun set_compiled_rewriter r = (compiled_rewriter := SOME r)
    35 fun set_compiled_rewriter r = (compiled_rewriter := SOME r)
    36 
       
    37 fun importable (Var _) = false
       
    38   | importable (Const _) = true			   
       
    39   | importable (App (a, b)) = importable a andalso importable b
       
    40   | importable (Abs _) = false
       
    41 
       
    42 (*Returns true iff at most 0 .. (free-1) occur unbound. therefore
       
    43   check_freevars 0 t iff t is closed*)
       
    44 fun check_freevars free (Var x) = x < free
       
    45   | check_freevars free (Const c) = true
       
    46   | check_freevars free (App (u, v)) = check_freevars free u andalso check_freevars free v
       
    47   | check_freevars free (Abs m) = check_freevars (free+1) m
       
    48 
    36 
    49 fun count_patternvars PVar = 1
    37 fun count_patternvars PVar = 1
    50   | count_patternvars (PConst (_, ps)) =
    38   | count_patternvars (PConst (_, ps)) =
    51       List.foldl (fn (p, count) => (count_patternvars p)+count) 0 ps
    39       List.foldl (fn (p, count) => (count_patternvars p)+count) 0 ps
    52 
    40 
    83   | beta (App (a, b)) = 
    71   | beta (App (a, b)) = 
    84     (case beta a of
    72     (case beta a of
    85 	 Abs m => beta (App (Abs m, b))
    73 	 Abs m => beta (App (Abs m, b))
    86        | a => App (a, beta b))
    74        | a => App (a, beta b))
    87   | beta (Abs m) = Abs (beta m)
    75   | beta (Abs m) = Abs (beta m)
       
    76   | beta (Computed t) = Computed t
    88 and subst x (Const c) t = Const c
    77 and subst x (Const c) t = Const c
    89   | subst x (Var i) t = if i = x then t else Var i
    78   | subst x (Var i) t = if i = x then t else Var i
    90   | subst x (App (a,b)) t = App (subst x a t, subst x b t)
    79   | subst x (App (a,b)) t = App (subst x a t, subst x b t)
    91   | subst x (Abs m) t = Abs (subst (x+1) m (lift 0 t))
    80   | subst x (Abs m) t = Abs (subst (x+1) m (lift 0 t))
    92 and lift level (Const c) = Const c
    81 and lift level (Const c) = Const c
   358 		    in
   347 		    in
   359 			"  | eval bounds ("^left^") = "^right
   348 			"  | eval bounds ("^left^") = "^right
   360 		    end
   349 		    end
   361 	    in
   350 	    in
   362 		map eval_rule (rev (section (arity + 1)))
   351 		map eval_rule (rev (section (arity + 1)))
       
   352 	    end
       
   353 
       
   354 	fun (convert_computed_rules : int -> string list) c = 
       
   355 	    let
       
   356 		val arity = the (arity_of c)
       
   357 		fun eval_rule () = 
       
   358 		    let
       
   359 			val sc = string_of_int c
       
   360 			val left = fold (fn i => fn s => "AbstractMachine.App ("^s^(indexed ", x" i)^")") (section arity) ("AbstractMachine.Const "^sc)
       
   361                         fun arg i = "(convert_computed "^(indexed "x" i)^")" 
       
   362 			val right = "C"^sc^" "^(string_of_tuple (map arg (section arity)))		
       
   363 			val right = if arity > 0 then right else "C"^sc
       
   364 		    in
       
   365 			"  | convert_computed ("^left^") = "^right
       
   366 		    end
       
   367 	    in
       
   368 		[eval_rule ()]
   363 	    end
   369 	    end
   364         
   370         
   365 	fun mk_constr_type_args n = if n > 0 then " of Term "^(rep_str " * Term" (n-1)) else ""
   371 	fun mk_constr_type_args n = if n > 0 then " of Term "^(rep_str " * Term" (n-1)) else ""
   366 	val _ = writelist [                   
   372 	val _ = writelist [                   
   367 		"structure "^name^" = struct",
   373 		"structure "^name^" = struct",
   442 		val right = fold (fn x => fn s => "AM_SML.App ("^s^", "^x^")") args ("AM_SML.Const "^(str c))
   448 		val right = fold (fn x => fn s => "AM_SML.App ("^s^", "^x^")") args ("AM_SML.Const "^(str c))
   443 	    in
   449 	    in
   444 		"  | convert (C"^(str c)^" "^leftargs^") = "^right
   450 		"  | convert (C"^(str c)^" "^leftargs^") = "^right
   445 	    end 		
   451 	    end 		
   446 	val _ = writelist (map make_convert constants)
   452 	val _ = writelist (map make_convert constants)
       
   453         val _ = writelist [
       
   454                 "",
       
   455                 "fun convert_computed (AbstractMachine.Abs b) = raise AM_SML.Run \"no abstraction in convert_computed allowed\"",
       
   456                 "  | convert_computed (AbstractMachine.Var i) = raise AM_SML.Run \"no bound variables in convert_computed allowed\""]
       
   457         val _ = map (writelist o convert_computed_rules) constants
       
   458         val _ = writelist [
       
   459                 "  | convert_computed (AbstractMachine.Const c) = Const c",
       
   460                 "  | convert_computed (AbstractMachine.App (a, b)) = App (convert_computed a, convert_computed b)",
       
   461                 "  | convert_computed (AbstractMachine.Computed a) = raise AM_SML.Run \"no nesting in convert_computed allowed\""] 
   447 	val _ = writelist [
   462 	val _ = writelist [
   448 		"",
   463 		"",
   449 		"fun eval bounds (AbstractMachine.Abs m) = Abs (fn b => eval (b::bounds) m)",
   464 		"fun eval bounds (AbstractMachine.Abs m) = Abs (fn b => eval (b::bounds) m)",
   450 		"  | eval bounds (AbstractMachine.Var i) = AM_SML.list_nth (bounds, i)"]
   465 		"  | eval bounds (AbstractMachine.Var i) = AM_SML.list_nth (bounds, i)"]
   451 	val _ = map (writelist o eval_rules) constants
   466 	val _ = map (writelist o eval_rules) constants
   452 	val _ = writelist [
   467 	val _ = writelist [
   453                 "  | eval bounds (AbstractMachine.App (a, b)) = app (eval bounds a) (eval bounds b)",
   468                 "  | eval bounds (AbstractMachine.App (a, b)) = app (eval bounds a) (eval bounds b)",
   454                 "  | eval bounds (AbstractMachine.Const c) = Const c"]                
   469                 "  | eval bounds (AbstractMachine.Const c) = Const c",
       
   470                 "  | eval bounds (AbstractMachine.Computed t) = convert_computed t"]                
   455 	val _ = writelist [		
   471 	val _ = writelist [		
   456 		"",
   472 		"",
   457 		"fun export term = AM_SML.save_result (\""^code^"\", convert term)",
   473 		"fun export term = AM_SML.save_result (\""^code^"\", convert term)",
   458 		"",
   474 		"",
   459                 "val _ = AM_SML.set_compiled_rewriter (fn t => (convert (eval [] t)))",
   475                 "val _ = AM_SML.set_compiled_rewriter (fn t => (convert (eval [] t)))",
   481     let
   497     let
   482 	val guid = get_guid ()
   498 	val guid = get_guid ()
   483 	val code = Real.toString (random ())
   499 	val code = Real.toString (random ())
   484 	val module = "AMSML_"^guid
   500 	val module = "AMSML_"^guid
   485 	val (arity, toplevel_arity, inlinetab, source) = sml_prog module code eqs
   501 	val (arity, toplevel_arity, inlinetab, source) = sml_prog module code eqs
   486 (*	val _ = writeTextFile "Gencode.ML" source*)
   502 	val _ = writeTextFile "Gencode.ML" source
   487 	val _ = compiled_rewriter := NONE
   503 	val _ = compiled_rewriter := NONE
   488 	val _ = use_source source
   504 	val _ = use_source source
   489     in
   505     in
   490 	case !compiled_rewriter of 
   506 	case !compiled_rewriter of 
   491 	    NONE => raise Compile "broken link to compiled function"
   507 	    NONE => raise Compile "broken link to compiled function"
   519 	val _ = if check_freevars 0 t then () else raise Run ("can only compute closed terms")
   535 	val _ = if check_freevars 0 t then () else raise Run ("can only compute closed terms")
   520 	fun inline (Const c) = (case Inttab.lookup inlinetab c of NONE => Const c | SOME t => t)
   536 	fun inline (Const c) = (case Inttab.lookup inlinetab c of NONE => Const c | SOME t => t)
   521 	  | inline (Var i) = Var i
   537 	  | inline (Var i) = Var i
   522 	  | inline (App (a, b)) = App (inline a, inline b)
   538 	  | inline (App (a, b)) = App (inline a, inline b)
   523 	  | inline (Abs m) = Abs (inline m)
   539 	  | inline (Abs m) = Abs (inline m)
       
   540 	  | inline (Computed t) = Computed t
   524     in
   541     in
   525 	compiled_fun (beta (inline t))
   542 	compiled_fun (beta (inline t))
   526     end	
   543     end	
   527 
   544 
   528 fun discard p = ()
   545 fun discard p = ()