Ruby haskell extension
Ruby haskell extension
Ruby haskell extension
Ruby haskell extension
Ruby haskell extension
Ruby haskell extension
�
Ruby haskell extension
foreign import ccall ”math.h sin”
  sin :: CDouble -> CDouble




foreign export ccall ”fact”
  fact :: CInt -> CInt
�
== factorial.hs ==
Module Factorial where
import Foreign.C.Types
foreign export ccall ”factorial”
  factorial :: CInt -> Cint
factorial 0 = 1
factorial x = x * factorial(x-1)
Ruby haskell extension
== main.c ==
#include <stdio.h>
#include “factorial_stub.h”
int main(int argc, char* argv[])
{
  hs_init(&argc, &argv);
  printf(“%dn”, fact(5));
  hs_exit();
  return 0;
}
$ ghc -shared -dynamic
...                  (   )
#include <ruby.h>
#include "factorial_stub.h"
static VALUE rb_factorial(VALUE self, VALUE val)
{
  return UINT2NUM(factorial(NUM2UINT(val)));
}
void Init_factorial(void)
{
  hs_init(NULL, NULL);//        Haskell          ..
    rb_define_module_function(rb_mMath, "factorial",
      RUBY_METHOD_FUNC(rb_factorial), 1);
}
(gem install         )

$ irb -r factorial/factorial
irb(main):001:0> Math.factorial 4
=> 24
foreign export ccall ”factorial”
factorial :: CInt -> CInt
CInt   -229   +229-1

  9               362,880
 10              3,628,800
 15            1.30767E+12
Ruby haskell extension
Ruby haskell extension
Ruby haskell extension
$ cd `ghc --print-libdir`




     �
Ruby haskell extension
Ruby haskell extension
require 'rbconfig'
TARGET = 'factorial'
C_SRC = %w{ main.c }
HS_SRC = %w{ factorial.hs }
HS_LIBS = %w{ base integer-gmp ghc-prim }
CONF = RbConfig::MAKEFILE_CONFIG
VALS = %w{prefix includedir RUBY_BASE_NAME ruby_version
rubyhdrdir arch MKDIR_P INSTALL INSTALL_PROGRAM}.map{|k|
          "#{k}=#{CONF[k]}"
        }.join("n")
GHC_LIBDIR=`ghc --print-libdir`.strip
FLAGS = "FLAGS=-I$(rubyhdrdir) -I$(rubyhdrdir)/$(arch) -
shared -dynamic -no-hs-main -lHSrts -lffin" 
       + "FLAGS+=-no-auto-link-packages -I#{GHC_LIBDIR}/
include -L#{GHC_LIBDIR}n" 
       + HS_LIBS.map{|l|
          'FLAGS+=' + `ghc-pkg describe #{l}|sed -ne 's/
library-dirs: /-L/p;s/hs-libraries: /-l/p;'`.gsub(/n/, ' ')
         }.join("n") 
       + "nFLAGS+=-lgmpn"
File.open("Makefile", "w") {|f|
  f.puts <<__MAKEFILE__
TARGET=#{TARGET}.#{CONF['DLEXT']}
HS_SRC=#{HS_SRC.join(' ')}
C_SRC=#{C_SRC.join(' ')}
#{VALS}
#{FLAGS}
STUB=$(HS_SRC:.hs=_stub.o)

all : $(TARGET)

$(STUB) : $(HS_SRC)
   ghc $<

$(TARGET) : $(C_SRC) $(STUB) $(HS_SRC:.hs=.o)
   ghc -o $@ $^ $(FLAGS)

install:
   $(MKDIR_P) ../lib/#{TARGET}/
   $(INSTALL_PROGRAM) $(TARGET) ../lib/#{TARGET}/

clean:
   rm -f $(TARGET) $(C_SRC:.c=.o) $(HS_SRC:.hs=.hi) $
(HS_SRC:.hs=.o) $(HS_SRC:.hs=_stub.)*
__MAKEFILE__
}
Ruby haskell extension
Ruby haskell extension
Ruby haskell extension
�




�
�
Ruby haskell extension
Ruby haskell extension
Ruby haskell extension
Ruby haskell extension

More Related Content

PDF
20151224-games
PDF
2016 gunma.web games-and-asm.js
DOCX
Pratik Bakane C++
DOCX
DOCX
Basic Programs of C++
DOCX
Pratik Bakane C++
DOCX
Include
20151224-games
2016 gunma.web games-and-asm.js
Pratik Bakane C++
Basic Programs of C++
Pratik Bakane C++
Include

What's hot (20)

DOCX
Pratik Bakane C++
DOCX
Pratik Bakane C++
DOCX
Pratik Bakane C++
DOCX
Include
PDF
C++ programs
TXT
Bankers Algo Implementation
PPTX
Oops in c++
DOCX
One dimensional operation of Array in C- language
DOCX
A Shiny Example-- R
PPT
Cpp tutorial
PPTX
Oprerator overloading
DOCX
Modificacion del programa
DOCX
PROGRAM FOR INSERTION SORTING IN ARRAY
PDF
Bcsl 033 data and file structures lab s5-2
PDF
Introduction to cron queue
DOCX
Document
DOCX
cosc 281 hw2
Pratik Bakane C++
Pratik Bakane C++
Pratik Bakane C++
Include
C++ programs
Bankers Algo Implementation
Oops in c++
One dimensional operation of Array in C- language
A Shiny Example-- R
Cpp tutorial
Oprerator overloading
Modificacion del programa
PROGRAM FOR INSERTION SORTING IN ARRAY
Bcsl 033 data and file structures lab s5-2
Introduction to cron queue
Document
cosc 281 hw2

Viewers also liked (11)

PDF
Frank-afcproxy
KEY
Amazon aws 20110611
PDF
Castoro sequense
KEY
Apache mod authまわりとか
PDF
Ruby2.0 - refinements - 鳥取Ruby会 第11回
ODP
とっとるびー(2回目)発表資料
KEY
Castoro / RubyKaigi2010
KEY
Apache2.3 探訪
PDF
Tottoruby 20130119
PPT
PPTX
Web Server - Internet Applications
Frank-afcproxy
Amazon aws 20110611
Castoro sequense
Apache mod authまわりとか
Ruby2.0 - refinements - 鳥取Ruby会 第11回
とっとるびー(2回目)発表資料
Castoro / RubyKaigi2010
Apache2.3 探訪
Tottoruby 20130119
Web Server - Internet Applications

Similar to Ruby haskell extension (20)

PDF
C programs Set 2
DOCX
Bijender (1)
PDF
C Programming lab
PDF
IR Journal (itscholar.codegency.co.in).pdf
PDF
Chainer-Compiler 動かしてみた
PDF
NVIDIA cuda programming, open source and AI
DOCX
C Programming
PDF
Basicsof c make and git for a hello qt application
DOC
C basics
DOC
'C' language notes (a.p)
PPT
CppTutorial.ppt
PDF
Final DAA_prints.pdf
PDF
All I know about rsc.io/c2go
PDF
JVM code reading -- C2
PDF
C++ manual Report Full
PPTX
ISCA Final Presentaiton - Compilations
DOCX
R (Shiny Package) - Server Side Code for Decision Support System
DOCX
Billing in a supermarket c++
PDF
C lab programs
C programs Set 2
Bijender (1)
C Programming lab
IR Journal (itscholar.codegency.co.in).pdf
Chainer-Compiler 動かしてみた
NVIDIA cuda programming, open source and AI
C Programming
Basicsof c make and git for a hello qt application
C basics
'C' language notes (a.p)
CppTutorial.ppt
Final DAA_prints.pdf
All I know about rsc.io/c2go
JVM code reading -- C2
C++ manual Report Full
ISCA Final Presentaiton - Compilations
R (Shiny Package) - Server Side Code for Decision Support System
Billing in a supermarket c++
C lab programs

Recently uploaded (20)

PDF
zbrain.ai-Scope Key Metrics Configuration and Best Practices.pdf
PPTX
AI-driven Assurance Across Your End-to-end Network With ThousandEyes
PPTX
Presentation - Principles of Instructional Design.pptx
PDF
Launch a Bumble-Style App with AI Features in 2025.pdf
PDF
CEH Module 2 Footprinting CEH V13, concepts
PDF
The-Future-of-Automotive-Quality-is-Here-AI-Driven-Engineering.pdf
PDF
Transform-Your-Supply-Chain-with-AI-Driven-Quality-Engineering.pdf
PDF
Advancing precision in air quality forecasting through machine learning integ...
PDF
A hybrid framework for wild animal classification using fine-tuned DenseNet12...
PDF
Transform-Your-Factory-with-AI-Driven-Quality-Engineering.pdf
PDF
EIS-Webinar-Regulated-Industries-2025-08.pdf
PDF
Build Real-Time ML Apps with Python, Feast & NoSQL
PDF
AI.gov: A Trojan Horse in the Age of Artificial Intelligence
PDF
Examining Bias in AI Generated News Content.pdf
PDF
Connector Corner: Transform Unstructured Documents with Agentic Automation
PDF
SaaS reusability assessment using machine learning techniques
PPTX
Blending method and technology for hydrogen.pptx
PDF
IT-ITes Industry bjjbnkmkhkhknbmhkhmjhjkhj
PPTX
How to use fields_get method in Odoo 18
PDF
Introduction to MCP and A2A Protocols: Enabling Agent Communication
zbrain.ai-Scope Key Metrics Configuration and Best Practices.pdf
AI-driven Assurance Across Your End-to-end Network With ThousandEyes
Presentation - Principles of Instructional Design.pptx
Launch a Bumble-Style App with AI Features in 2025.pdf
CEH Module 2 Footprinting CEH V13, concepts
The-Future-of-Automotive-Quality-is-Here-AI-Driven-Engineering.pdf
Transform-Your-Supply-Chain-with-AI-Driven-Quality-Engineering.pdf
Advancing precision in air quality forecasting through machine learning integ...
A hybrid framework for wild animal classification using fine-tuned DenseNet12...
Transform-Your-Factory-with-AI-Driven-Quality-Engineering.pdf
EIS-Webinar-Regulated-Industries-2025-08.pdf
Build Real-Time ML Apps with Python, Feast & NoSQL
AI.gov: A Trojan Horse in the Age of Artificial Intelligence
Examining Bias in AI Generated News Content.pdf
Connector Corner: Transform Unstructured Documents with Agentic Automation
SaaS reusability assessment using machine learning techniques
Blending method and technology for hydrogen.pptx
IT-ITes Industry bjjbnkmkhkhknbmhkhmjhjkhj
How to use fields_get method in Odoo 18
Introduction to MCP and A2A Protocols: Enabling Agent Communication

Ruby haskell extension

  • 7.
  • 9. foreign import ccall ”math.h sin” sin :: CDouble -> CDouble foreign export ccall ”fact” fact :: CInt -> CInt
  • 10.
  • 11. == factorial.hs == Module Factorial where import Foreign.C.Types foreign export ccall ”factorial” factorial :: CInt -> Cint factorial 0 = 1 factorial x = x * factorial(x-1)
  • 13. == main.c == #include <stdio.h> #include “factorial_stub.h” int main(int argc, char* argv[]) { hs_init(&argc, &argv); printf(“%dn”, fact(5)); hs_exit(); return 0; }
  • 14. $ ghc -shared -dynamic ... ( )
  • 15. #include <ruby.h> #include "factorial_stub.h" static VALUE rb_factorial(VALUE self, VALUE val) { return UINT2NUM(factorial(NUM2UINT(val))); } void Init_factorial(void) { hs_init(NULL, NULL);// Haskell .. rb_define_module_function(rb_mMath, "factorial", RUBY_METHOD_FUNC(rb_factorial), 1); }
  • 16. (gem install ) $ irb -r factorial/factorial irb(main):001:0> Math.factorial 4 => 24
  • 17. foreign export ccall ”factorial” factorial :: CInt -> CInt CInt -229 +229-1 9 362,880 10 3,628,800 15 1.30767E+12
  • 21. $ cd `ghc --print-libdir` �
  • 24. require 'rbconfig' TARGET = 'factorial' C_SRC = %w{ main.c } HS_SRC = %w{ factorial.hs } HS_LIBS = %w{ base integer-gmp ghc-prim } CONF = RbConfig::MAKEFILE_CONFIG VALS = %w{prefix includedir RUBY_BASE_NAME ruby_version rubyhdrdir arch MKDIR_P INSTALL INSTALL_PROGRAM}.map{|k| "#{k}=#{CONF[k]}" }.join("n") GHC_LIBDIR=`ghc --print-libdir`.strip FLAGS = "FLAGS=-I$(rubyhdrdir) -I$(rubyhdrdir)/$(arch) - shared -dynamic -no-hs-main -lHSrts -lffin" + "FLAGS+=-no-auto-link-packages -I#{GHC_LIBDIR}/ include -L#{GHC_LIBDIR}n" + HS_LIBS.map{|l| 'FLAGS+=' + `ghc-pkg describe #{l}|sed -ne 's/ library-dirs: /-L/p;s/hs-libraries: /-l/p;'`.gsub(/n/, ' ') }.join("n") + "nFLAGS+=-lgmpn"
  • 25. File.open("Makefile", "w") {|f| f.puts <<__MAKEFILE__ TARGET=#{TARGET}.#{CONF['DLEXT']} HS_SRC=#{HS_SRC.join(' ')} C_SRC=#{C_SRC.join(' ')} #{VALS} #{FLAGS} STUB=$(HS_SRC:.hs=_stub.o) all : $(TARGET) $(STUB) : $(HS_SRC) ghc $< $(TARGET) : $(C_SRC) $(STUB) $(HS_SRC:.hs=.o) ghc -o $@ $^ $(FLAGS) install: $(MKDIR_P) ../lib/#{TARGET}/ $(INSTALL_PROGRAM) $(TARGET) ../lib/#{TARGET}/ clean: rm -f $(TARGET) $(C_SRC:.c=.o) $(HS_SRC:.hs=.hi) $ (HS_SRC:.hs=.o) $(HS_SRC:.hs=_stub.)* __MAKEFILE__ }
  • 30.

Editor's Notes