src/HOL/Matrix/Compute_Oracle/am_ghc.ML
changeset 41952 c7297638599b
parent 41491 a2ad5b824051
child 41953 994d088fbfbc
equal deleted inserted replaced
41951:117eb7aeddf0 41952:c7297638599b
     1 (*  Title:      Tools/Compute_Oracle/am_ghc.ML
     1 (*  Title:      Tools/Compute_Oracle/am_ghc.ML
     2     Author:     Steven Obua
     2     Author:     Steven Obua
     3 *)
     3 *)
     4 
     4 
     5 structure AM_GHC : ABSTRACT_MACHINE = struct
     5 structure AM_GHC : ABSTRACT_MACHINE =
       
     6 struct
     6 
     7 
     7 open AbstractMachine;
     8 open AbstractMachine;
     8 
     9 
     9 type program = string * string * (int Inttab.table)
    10 type program = string * string * (int Inttab.table)
    10 
    11 
   212 fun tmp_file s = Path.implode (Path.expand (File.tmp_path (Path.make [s])));
   213 fun tmp_file s = Path.implode (Path.expand (File.tmp_path (Path.make [s])));
   213 fun wrap s = "\""^s^"\""
   214 fun wrap s = "\""^s^"\""
   214 
   215 
   215 fun writeTextFile name s = File.write (Path.explode name) s
   216 fun writeTextFile name s = File.write (Path.explode name) s
   216     
   217     
   217 val ghc = Unsynchronized.ref (case getenv "GHC_PATH" of "" => "ghc" | s => s)
       
   218 
       
   219 fun fileExists name = ((OS.FileSys.fileSize name; true) handle OS.SysErr _ => false)
   218 fun fileExists name = ((OS.FileSys.fileSize name; true) handle OS.SysErr _ => false)
   220 
   219 
   221 fun compile cache_patterns const_arity eqs = 
   220 fun compile cache_patterns const_arity eqs = 
   222     let
   221     let
   223         val _ = if exists (fn (a,b,c) => not (null a)) eqs then raise Compile ("cannot deal with guards") else ()
   222         val _ = if exists (fn (a,b,c) => not (null a)) eqs then raise Compile ("cannot deal with guards") else ()
   226         val module = "AMGHC_Prog_"^guid
   225         val module = "AMGHC_Prog_"^guid
   227         val (arity, source) = haskell_prog module eqs
   226         val (arity, source) = haskell_prog module eqs
   228         val module_file = tmp_file (module^".hs")
   227         val module_file = tmp_file (module^".hs")
   229         val object_file = tmp_file (module^".o")
   228         val object_file = tmp_file (module^".o")
   230         val _ = writeTextFile module_file source
   229         val _ = writeTextFile module_file source
   231         val _ = bash ((!ghc)^" -c "^module_file)
   230         val _ = bash ("\"$ISABELLE_GHC\" -c " ^ module_file)
   232         val _ = if not (fileExists object_file) then raise Compile ("Failure compiling haskell code (GHC_PATH = '"^(!ghc)^"')") else ()
   231         val _ =
       
   232           if not (fileExists object_file) then
       
   233             raise Compile ("Failure compiling haskell code (ISABELLE_GHC='" ^ getenv "ISABELLE_GHC" ^ "')")
       
   234           else ()
   233     in
   235     in
   234         (guid, module_file, arity)      
   236         (guid, module_file, arity)      
   235     end
   237     end
   236 
   238 
   237 fun readResultFile name = File.read (Path.explode name) 
   239 fun readResultFile name = File.read (Path.explode name) 
   307         val result_file = tmp_file (module^"_Result_"^callguid^".txt")
   309         val result_file = tmp_file (module^"_Result_"^callguid^".txt")
   308         val call_file = tmp_file (call^".hs")
   310         val call_file = tmp_file (call^".hs")
   309         val term = print_term arity_of 0 t
   311         val term = print_term arity_of 0 t
   310         val call_source = "module "^call^" where\n\nimport "^module^"\n\ncall = "^module^".calc \""^result_file^"\" ("^term^")"
   312         val call_source = "module "^call^" where\n\nimport "^module^"\n\ncall = "^module^".calc \""^result_file^"\" ("^term^")"
   311         val _ = writeTextFile call_file call_source
   313         val _ = writeTextFile call_file call_source
   312         val _ = bash ((!ghc)^" -e \""^call^".call\" "^module_file^" "^call_file)
   314         val _ = bash ("\"$ISABELLE_GHC\" -e \""^call^".call\" "^module_file^" "^call_file)
   313         val result = readResultFile result_file handle IO.Io _ => raise Run ("Failure running haskell compiler (GHC_PATH = '"^(!ghc)^"')")
   315         val result = readResultFile result_file handle IO.Io _ =>
       
   316           raise Run ("Failure running haskell compiler (ISABELLE_GHC='" ^ getenv "ISABELLE_GHC" ^ "')")
   314         val t' = parse_result arity_of result
   317         val t' = parse_result arity_of result
   315         val _ = OS.FileSys.remove call_file
   318         val _ = OS.FileSys.remove call_file
   316         val _ = OS.FileSys.remove result_file
   319         val _ = OS.FileSys.remove result_file
   317     in
   320     in
   318         t'
   321         t'